trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iga...@apache.org
Subject [16/21] git commit: TS-1314: Add Vec::append(src, count)
Date Fri, 21 Sep 2012 10:04:40 GMT
TS-1314: Add Vec::append(src, count)


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/0cc0f316
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/0cc0f316
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/0cc0f316

Branch: refs/heads/3.2.x
Commit: 0cc0f316abf5153919ead9f958739dbea6cfe47f
Parents: d566c5e
Author: James Peach <jpeach@apache.org>
Authored: Thu Jun 28 22:13:23 2012 -0700
Committer: Igor Galić <i.galic@brainsware.org>
Committed: Fri Sep 21 08:49:04 2012 +0200

----------------------------------------------------------------------
 lib/ts/Vec.h       |   11 +++++++++++
 lib/ts/test_Vec.cc |   36 ++++++++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0cc0f316/lib/ts/Vec.h
----------------------------------------------------------------------
diff --git a/lib/ts/Vec.h b/lib/ts/Vec.h
index 879fe17..e87c533 100644
--- a/lib/ts/Vec.h
+++ b/lib/ts/Vec.h
@@ -93,6 +93,7 @@ class Vec {
   void copy(const Vec<C,A,S> &v);
   void fill(int n);
   void append(const Vec<C> &v);
+  template <typename CountType> void append(const C * src, CountType count);
   void prepend(const Vec<C> &v);
   void remove_index(int index);
   void remove(C a) { int i = index(a); if (i>=0) remove_index(i); }
@@ -349,6 +350,16 @@ Vec<C,A,S>::append(const Vec<C> &vv)  {
       add(*c);
 }
 
+template <class C, class A, int S>
+template <typename CountType>
+inline void
+Vec<C,A,S>::append(const C * src, CountType count)  {
+  reserve(length() + count);
+  for (CountType c = 0; c < count; ++c) {
+    add(src[c]);
+  }
+}
+
 template <class C, class A, int S> inline void
 Vec<C,A,S>::prepend(const Vec<C> &vv)  {
   if (vv.n) {

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/0cc0f316/lib/ts/test_Vec.cc
----------------------------------------------------------------------
diff --git a/lib/ts/test_Vec.cc b/lib/ts/test_Vec.cc
index 0b54643..92810b6 100644
--- a/lib/ts/test_Vec.cc
+++ b/lib/ts/test_Vec.cc
@@ -27,10 +27,36 @@
 #include <ink_assert.h>
 #include "Vec.h"
 
-int main(int argc, char **argv) {
+static void test_append()
+{
+  static const char value[] = "this is a string";
+  static const size_t len = sizeof(value) - 1;
+
+  Vec<char> str;
+
+  str.append(value, 0);
+  ink_assert(str.length() == 0);
+
+  str.append(value, len);
+  ink_assert(memcmp(&str[0], value, len) == 0);
+  ink_assert(str.length() == len);
+
+  str.clear();
+  ink_assert(str.length() == 0);
+
+  for (unsigned i = 0; i < 1000; ++i) {
+    str.append(value, len);
+    ink_assert(memcmp(&str[i * len], value, len) == 0);
+  }
+
+  ink_assert(str.length() == 1000 * len);
+}
+
+static void test_basic()
+{
   Vec<void *> v, vv, vvv;
   int tt = 99 * 50, t = 0;
-  
+
   for (int i = 0; i < 100; i++)
     v.add((void*)(intptr_t)i);
   for (int i = 0; i < 100; i++)
@@ -93,5 +119,11 @@ int main(int argc, char **argv) {
   uf.unify(1,2);
   ink_assert(uf.find(0) == uf.find(3));
   ink_assert(uf.find(1) == uf.find(3));
+}
+
+int main(int argc, char **argv)
+{
+  test_append();
+  test_basic();
   printf("test_Vec PASSED\n");
 }


Mime
View raw message