1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include "small_vector.h"
#define BOOST_TEST_MODULE svTest
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
BOOST_AUTO_TEST_CASE(LargerThan2) {
SmallVectorInt v;
SmallVectorInt v2;
v.push_back(0);
v.push_back(1);
v.push_back(2);
BOOST_CHECK(v.size() == 3);
BOOST_CHECK(v[2] == 2);
BOOST_CHECK(v[1] == 1);
BOOST_CHECK(v[0] == 0);
v2 = v;
SmallVectorInt copy(v);
BOOST_CHECK(copy.size() == 3);
BOOST_CHECK(copy[0] == 0);
BOOST_CHECK(copy[1] == 1);
BOOST_CHECK(copy[2] == 2);
BOOST_CHECK(copy == v2);
copy[1] = 99;
BOOST_CHECK(copy != v2);
BOOST_CHECK(v2.size() == 3);
BOOST_CHECK(v2[2] == 2);
BOOST_CHECK(v2[1] == 1);
BOOST_CHECK(v2[0] == 0);
v2[0] = -2;
v2[1] = -1;
v2[2] = 0;
BOOST_CHECK(v2[2] == 0);
BOOST_CHECK(v2[1] == -1);
BOOST_CHECK(v2[0] == -2);
SmallVectorInt v3(1,1);
BOOST_CHECK(v3[0] == 1);
v2 = v3;
BOOST_CHECK(v2.size() == 1);
BOOST_CHECK(v2[0] == 1);
SmallVectorInt v4(10, 1);
BOOST_CHECK(v4.size() == 10);
BOOST_CHECK(v4[5] == 1);
BOOST_CHECK(v4[9] == 1);
v4 = v;
BOOST_CHECK(v4.size() == 3);
BOOST_CHECK(v4[2] == 2);
BOOST_CHECK(v4[1] == 1);
BOOST_CHECK(v4[0] == 0);
SmallVectorInt v5(10, 2);
BOOST_CHECK(v5.size() == 10);
BOOST_CHECK(v5[7] == 2);
BOOST_CHECK(v5[0] == 2);
BOOST_CHECK(v.size() == 3);
v = v5;
BOOST_CHECK(v.size() == 10);
BOOST_CHECK(v[2] == 2);
BOOST_CHECK(v[9] == 2);
SmallVectorInt cc;
for (int i = 0; i < 33; ++i)
cc.push_back(i);
for (int i = 0; i < 33; ++i)
BOOST_CHECK(cc[i] == i);
cc.resize(20);
BOOST_CHECK(cc.size() == 20);
for (int i = 0; i < 20; ++i)
BOOST_CHECK(cc[i] == i);
cc[0]=-1;
cc.resize(1, 999);
BOOST_CHECK(cc.size() == 1);
BOOST_CHECK(cc[0] == -1);
cc.resize(99, 99);
for (int i = 1; i < 99; ++i) {
cerr << i << " " << cc[i] << endl;
BOOST_CHECK(cc[i] == 99);
}
cc.clear();
BOOST_CHECK(cc.size() == 0);
}
BOOST_AUTO_TEST_CASE(SwapSV) {
SmallVectorInt v;
SmallVectorInt v2(2, 10);
SmallVectorInt v3(2, 10);
BOOST_CHECK(v2 == v3);
BOOST_CHECK(v != v3);
v.swap(v2);
BOOST_CHECK(v == v3);
SmallVectorInt v4;
BOOST_CHECK(v4 == v2);
}
BOOST_AUTO_TEST_CASE(Small) {
SmallVectorInt v;
SmallVectorInt v1(1,0);
SmallVectorInt v2(2,10);
SmallVectorInt v1a(2,0);
BOOST_CHECK(v1 != v1a);
BOOST_CHECK(v1 == v1);
BOOST_CHECK_EQUAL(v1[0], 0);
BOOST_CHECK_EQUAL(v2[1], 10);
BOOST_CHECK_EQUAL(v2[0], 10);
++v2[1];
--v2[0];
BOOST_CHECK_EQUAL(v2[0], 9);
BOOST_CHECK_EQUAL(v2[1], 11);
SmallVectorInt v3(v2);
BOOST_CHECK(v3[0] == 9);
BOOST_CHECK(v3[1] == 11);
BOOST_CHECK(!v3.empty());
BOOST_CHECK(v3.size() == 2);
v3.clear();
BOOST_CHECK(v3.empty());
BOOST_CHECK(v3.size() == 0);
BOOST_CHECK(v3 != v2);
BOOST_CHECK(v2 != v3);
v3 = v2;
BOOST_CHECK(v3 == v2);
BOOST_CHECK(v2 == v3);
BOOST_CHECK(v3[0] == 9);
BOOST_CHECK(v3[1] == 11);
BOOST_CHECK(!v3.empty());
BOOST_CHECK(v3.size() == 2);
cerr << sizeof(SmallVectorInt) << endl;
cerr << sizeof(vector<int>) << endl;
}
BOOST_AUTO_TEST_CASE(Serialize) {
std::string in;
{
SmallVectorInt v;
v.push_back(0);
v.push_back(1);
v.push_back(-2);
ostringstream os;
boost::archive::text_oarchive oa(os);
oa << v;
in = os.str();
cerr << in;
}
{
istringstream is(in);
boost::archive::text_iarchive ia(is);
SmallVectorInt v;
ia >> v;
BOOST_CHECK_EQUAL(v.size(), 3);
BOOST_CHECK_EQUAL(v[0], 0);
BOOST_CHECK_EQUAL(v[1], 1);
BOOST_CHECK_EQUAL(v[2], -2);
}
}
|