2 //=============================================================================
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>
10 //=============================================================================
14 #include "test_config.h"
16 #include "ace/Vector_T.h"
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"));
36 for (i
= 0; i
< TOP
; ++i
)
39 ACE_TEST_ASSERT (vector
.size () == TOP
);
41 ACE_TEXT ("Size: %d\n"),
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
);
50 size_t iter_count
= 0;
53 if (iter
.next (p_item
) == 0)
54 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Fail to get value on iter pass %d\n"),
56 if (*p_item
!= iter_count
)
57 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Iter pass %d got %d\n"),
58 iter_count
, *p_item
));
62 if (iter_count
!= TOP
)
63 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Iterated %d elements; expected %d\n"),
66 for (i
= 0; i
< (TOP
- LEFT
); ++i
)
69 ACE_TEST_ASSERT (vector
.size () == LEFT
);
71 ACE_TEXT ("Size: %d\n"),
74 for (i
= 0; i
< LEFT
; ++i
)
76 ACE_TEST_ASSERT (vector
[i
] == i
);
78 ACE_TEXT ("vector[%d]:%d\n"),
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.
92 ACE_TEST_ASSERT (vector
[i
] == i
);
96 ACE_TEST_ASSERT (vector
[i
] == 0);
99 ACE_TEXT ("vector[%d]:%d\n"),
104 ACE_TEST_ASSERT (vector
.size () == 0);
105 ACE_DEBUG ((LM_DEBUG
,
106 ACE_TEXT ("Size: %d\n"),
109 // test resize (shrink and enlarge with buffer realloc)
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,
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
);
147 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Inequality test failed!\n")));
149 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Equality test failed!\n")));
153 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("v1's size should be 3\n")));
157 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("v2's size should be 3\n")));
159 // Assert that vector standard iterators work
162 const size_t size
= 200;
163 const size_t offset
= 5;
165 ACE_TEST_ASSERT (vector
.begin () == vector
.end ());
168 for (i
= 0; i
< size
; i
++)
170 vector
.push_back (i
+ offset
);
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());
181 for (VECTOR::iterator it
= vector
.begin (); it
!= vector
.end (); ++it
)
183 ACE_TEST_ASSERT (*it
== i
-- + offset
);