• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Test of the space optimized adaptor of the circular buffer.
2 
3 // Copyright (c) 2003-2008 Jan Gaspar
4 
5 // Use, modification, and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 #include "test.hpp"
10 
11 #define CB_CONTAINER circular_buffer_space_optimized
12 
13 #include "common.ipp"
14 
15 typedef circular_buffer_space_optimized<MyInteger> cb_space_optimized;
16 typedef cb_space_optimized::capacity_type capacity_ctrl;
17 
18 // min_capacity test (it is useful to use a debug tool)
min_capacity_test()19 void min_capacity_test() {
20 
21     vector<int> v;
22     v.push_back(1);
23     v.push_back(2);
24     v.push_back(3);
25     v.push_back(4);
26     v.push_back(5);
27 
28     cb_space_optimized cb1(capacity_ctrl(10, 10));
29     cb_space_optimized cb2(capacity_ctrl(10, 5), 1);
30     cb_space_optimized cb3(capacity_ctrl(20, 10), v.begin(), v.end());
31 
32     BOOST_TEST(cb1.size() == 0);
33     BOOST_TEST(cb1.capacity().capacity() == 10);
34     BOOST_TEST(cb1.capacity().min_capacity() == 10);
35     BOOST_TEST(cb2[0] == 1);
36     BOOST_TEST(cb2.size() == 10);
37     BOOST_TEST(cb2.capacity() == 10);
38     BOOST_TEST(cb2.capacity().min_capacity() == 5);
39     BOOST_TEST(cb3[0] == 1);
40     BOOST_TEST(cb3.size() == 5);
41     BOOST_TEST(cb3.capacity() == 20);
42     BOOST_TEST(cb3.capacity().min_capacity() == 10);
43     BOOST_TEST(cb1.capacity().min_capacity() <= cb1.internal_capacity());
44     BOOST_TEST(cb2.capacity().min_capacity() <= cb2.internal_capacity());
45     BOOST_TEST(cb3.capacity().min_capacity() <= cb3.internal_capacity());
46 
47     cb2.erase(cb2.begin() + 2, cb2.end());
48 
49     BOOST_TEST(cb2.size() == 2);
50     BOOST_TEST(cb2.capacity().min_capacity() <= cb2.internal_capacity());
51 
52     cb2.clear();
53     cb3.clear();
54 
55     BOOST_TEST(cb2.empty());
56     BOOST_TEST(cb3.empty());
57     BOOST_TEST(cb2.capacity().min_capacity() <= cb2.internal_capacity());
58     BOOST_TEST(cb3.capacity().min_capacity() <= cb3.internal_capacity());
59 }
60 
capacity_control_test()61 void capacity_control_test() {
62 
63     circular_buffer_space_optimized<int>::capacity_type c1 = 10;
64     circular_buffer_space_optimized<int>::capacity_type c2 =
65     circular_buffer_space_optimized<int>::capacity_type(20, 5);
66     circular_buffer_space_optimized<int>::capacity_type c3 = c2;
67 
68     BOOST_TEST(c1.capacity() == 10);
69     BOOST_TEST(c1.min_capacity() == 0);
70     BOOST_TEST(c2.capacity() == 20);
71     BOOST_TEST(c2.min_capacity() == 5);
72     BOOST_TEST(c3.capacity() == 20);
73     BOOST_TEST(c3.min_capacity() == 5);
74 
75     c1 = c2;
76 
77     BOOST_TEST(c1.capacity() == 20);
78     BOOST_TEST(c1.min_capacity() == 5);
79 }
80 
specific_constructors_test()81 void specific_constructors_test() {
82 
83     cb_space_optimized cb1;
84     BOOST_TEST(cb1.capacity() == 0);
85     BOOST_TEST(cb1.capacity().min_capacity() == 0);
86     BOOST_TEST(cb1.internal_capacity() == 0);
87     BOOST_TEST(cb1.size() == 0);
88 
89     cb1.push_back(1);
90     cb1.push_back(2);
91     cb1.push_back(3);
92 
93     BOOST_TEST(cb1.size() == 0);
94     BOOST_TEST(cb1.capacity() == 0);
95 
96     vector<int> v;
97     v.push_back(1);
98     v.push_back(2);
99     v.push_back(3);
100 
101     cb_space_optimized cb2(v.begin(), v.end());
102 
103     BOOST_TEST(cb2.capacity() == 3);
104     BOOST_TEST(cb2.capacity().min_capacity() == 0);
105     BOOST_TEST(cb2.size() == 3);
106 }
107 
shrink_to_fit_test()108 void shrink_to_fit_test() {
109 
110     cb_space_optimized cb(1000);
111     cb.push_back(1);
112     cb.push_back(2);
113     cb.push_back(3);
114 
115     BOOST_TEST(cb.size() == 3);
116     BOOST_TEST(cb.capacity() == 1000);
117 
118     size_t internal_capacity = cb.internal_capacity();
119     cb_space_optimized(cb).swap(cb);
120 
121     BOOST_TEST(cb.size() == 3);
122     BOOST_TEST(cb.capacity() == 1000);
123     BOOST_TEST(internal_capacity >= cb.internal_capacity());
124 }
125 
iterator_invalidation_test()126 void iterator_invalidation_test() {
127 
128 #if BOOST_CB_ENABLE_DEBUG
129 
130     cb_space_optimized cb1(10, 1);
131     cb1.push_back(2);
132     cb1.push_back(3);
133     cb1.push_back(4);
134     cb_space_optimized::iterator it1 = cb1.end();
135     cb_space_optimized::const_iterator it2 = cb1.begin();
136     cb_space_optimized::iterator it3 = cb1.begin() + 6;
137 
138     cb1.set_capacity(10);
139     BOOST_TEST(it1.is_valid(&cb1));
140     BOOST_TEST(!it2.is_valid(&cb1));
141     BOOST_TEST(!it3.is_valid(&cb1));
142 
143     it1 = cb1.end();
144     it2 = cb1.begin();
145     it3 = cb1.begin() + 6;
146     cb1.rset_capacity(10);
147     BOOST_TEST(it1.is_valid(&cb1));
148     BOOST_TEST(!it2.is_valid(&cb1));
149     BOOST_TEST(!it3.is_valid(&cb1));
150 
151     it1 = cb1.end();
152     it2 = cb1.begin();
153     it3 = cb1.begin() + 6;
154     cb1.resize(10);
155     BOOST_TEST(it1.is_valid(&cb1));
156     BOOST_TEST(!it2.is_valid(&cb1));
157     BOOST_TEST(!it3.is_valid(&cb1));
158 
159     it1 = cb1.end();
160     it2 = cb1.begin();
161     it3 = cb1.begin() + 6;
162     cb1.rresize(10);
163     BOOST_TEST(it1.is_valid(&cb1));
164     BOOST_TEST(!it2.is_valid(&cb1));
165     BOOST_TEST(!it3.is_valid(&cb1));
166 
167     {
168         cb_space_optimized cb2(10, 1);
169         cb2.push_back(2);
170         cb2.push_back(3);
171         cb2.push_back(4);
172         it1 = cb2.end();
173         it2 = cb2.begin();
174         it3 = cb2.begin() + 6;
175     }
176     BOOST_TEST(!it1.is_valid(&cb1));
177     BOOST_TEST(!it2.is_valid(&cb1));
178     BOOST_TEST(!it3.is_valid(&cb1));
179 
180 #endif // #if BOOST_CB_ENABLE_DEBUG
181 }
182 
183 // test main
main()184 int main()
185 {
186     run_common_tests();
187     min_capacity_test();
188     capacity_control_test();
189     specific_constructors_test();
190     shrink_to_fit_test();
191     iterator_invalidation_test();
192     return boost::report_errors();
193 }
194