You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2012/07/10 17:54:36 UTC
[4/7] git commit: TS-1314: Add Vec::append(src, count)
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/fa61f831
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/fa61f831
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/fa61f831
Branch: refs/heads/master
Commit: fa61f831a5f17133ed8663fce9cb46f2f0956f2b
Parents: 02506c0
Author: James Peach <jp...@apache.org>
Authored: Thu Jun 28 22:13:23 2012 -0700
Committer: James Peach <jp...@apache.org>
Committed: Tue Jul 10 08:48:11 2012 -0700
----------------------------------------------------------------------
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/fa61f831/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/fa61f831/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");
}