Make x.0.10 publicly available
[ACE_TAO.git] / ACE / tests / Vector_Test.cpp
blob50b9a08f0383e0889a1dd8e278a16ce8a97f5a8a
2 //=============================================================================
3 /**
4 * @file Vector_Test.cpp
6 * This is a simple test of the ACE_Vector class and its iterators.
8 * @author Gonzalo A. Diethelm <gonzalo.diethelm@aditiva.com> and Karl-Heinz Wind <wind@itq.de>
9 */
10 //=============================================================================
12 #include <algorithm>
14 #include "test_config.h"
16 #include "ace/Vector_T.h"
18 using DATA = size_t;
19 using VECTOR = ACE_Vector<DATA>;
20 using ITERATOR = ACE_Vector<DATA>::Iterator;
22 const size_t TOP = 100;
23 const size_t LEFT = 10;
24 const size_t RESIZE = 20;
26 const size_t FILLER1 = 1;
27 const size_t FILLER2 = 2;
29 int run_main (int, ACE_TCHAR *[])
31 ACE_START_TEST (ACE_TEXT ("Vector_Test"));
33 VECTOR vector;
34 size_t i;
36 for (i = 0; i < TOP; ++i)
37 vector.push_back (i);
39 ACE_TEST_ASSERT (vector.size () == TOP);
40 ACE_DEBUG ((LM_DEBUG,
41 ACE_TEXT ("Size: %d\n"),
42 vector.size ()));
44 for (i = 0; i < TOP; ++i)
45 ACE_TEST_ASSERT (vector[i] == i);
47 // Test to be sure the iterator gets the correct count and entries.
48 ITERATOR iter (vector);
49 DATA *p_item = 0 ;
50 size_t iter_count = 0;
51 while (!iter.done ())
53 if (iter.next (p_item) == 0)
54 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Fail to get value on iter pass %d\n"),
55 iter_count));
56 if (*p_item != iter_count)
57 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Iter pass %d got %d\n"),
58 iter_count, *p_item));
59 iter_count++;
60 iter.advance();
62 if (iter_count != TOP)
63 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Iterated %d elements; expected %d\n"),
64 iter_count, TOP));
66 for (i = 0; i < (TOP - LEFT); ++i)
67 vector.pop_back ();
69 ACE_TEST_ASSERT (vector.size () == LEFT);
70 ACE_DEBUG ((LM_DEBUG,
71 ACE_TEXT ("Size: %d\n"),
72 vector.size ()));
74 for (i = 0; i < LEFT; ++i)
76 ACE_TEST_ASSERT (vector[i] == i);
77 ACE_DEBUG ((LM_DEBUG,
78 ACE_TEXT ("vector[%d]:%d\n"),
79 i, vector[i]));
82 vector.resize(RESIZE, 0);
83 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("After resize\n")));
85 for (i = 0; i < RESIZE ; ++i)
87 // The original vector of size LEFT must have the same original contents
88 // the new elements should have the value 0 (this value is passed as
89 // second argument of the resize() call.
90 if (i < LEFT)
92 ACE_TEST_ASSERT (vector[i] == i);
94 else
96 ACE_TEST_ASSERT (vector[i] == 0);
98 ACE_DEBUG ((LM_DEBUG,
99 ACE_TEXT ("vector[%d]:%d\n"),
100 i, vector[i]));
103 vector.clear ();
104 ACE_TEST_ASSERT (vector.size () == 0);
105 ACE_DEBUG ((LM_DEBUG,
106 ACE_TEXT ("Size: %d\n"),
107 vector.size ()));
109 // test resize (shrink and enlarge with buffer realloc)
110 VECTOR vector2;
112 // should be around 32
113 size_t boundary = vector2.capacity ();
115 // we fill everything up with 1
116 // 1, 1, 1, 1, 1, 1, 1, 1,
117 // 1, 1, 1, 1, 1, 1, 1, 1,
118 // 1, 1, 1, 1, 1, 1, 1, 1,
119 // 1, 1, 1, 1, 1, 1, 1, 1,
120 for (i = 0; i < boundary; ++i)
121 vector2.push_back (FILLER1);
123 // we throw almost everything away.
124 vector2.resize (1, 0);
126 // we fill up with another pattern
127 // 1, 2, 2, 2, 2, 2, 2, 2,
128 // 2, 2, 2, 2, 2, 2, 2, 2,
129 // 2, 2, 2, 2, 2, 2, 2, 2,
130 // 2, 2, 2, 2, 2, 2, 2, 2,
131 // 2,
132 for (i = 0; i < boundary; ++i)
133 vector2.push_back (FILLER2);
135 // now we check the result
136 ACE_TEST_ASSERT (vector2[0] == FILLER1);
137 for (i = 0; i < boundary; ++i)
138 ACE_TEST_ASSERT (vector2[i+1] == FILLER2);
140 VECTOR v1;
141 VECTOR v2;
142 v1.push_back (1);
143 v2.push_back (1);
144 v1.push_back (2);
145 v2.push_back (2);
146 if (v1 != v2)
147 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Inequality test failed!\n")));
148 if (!(v1 == v2))
149 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Equality test failed!\n")));
151 v1.push_back (3);
152 if (v1.size () != 3)
153 ACE_ERROR ((LM_ERROR, ACE_TEXT ("v1's size should be 3\n")));
155 v1.swap (v2);
156 if (v2.size () != 3)
157 ACE_ERROR ((LM_ERROR, ACE_TEXT ("v2's size should be 3\n")));
159 // Assert that vector standard iterators work
161 VECTOR vector;
162 const size_t size = 200;
163 const size_t offset = 5;
165 ACE_TEST_ASSERT (vector.begin () == vector.end ());
167 size_t i;
168 for (i = 0; i < size; i++)
170 vector.push_back (i + offset);
173 i = 0;
174 for (VECTOR::iterator it = vector.begin (); it != vector.end (); ++it)
176 ACE_TEST_ASSERT (*it == i++ + offset);
179 std::reverse (vector.begin (), vector.end());
180 i = size - 1;
181 for (VECTOR::iterator it = vector.begin (); it != vector.end (); ++it)
183 ACE_TEST_ASSERT (*it == i-- + offset);
187 ACE_END_TEST;
189 return 0;