source: mainline/uspace/lib/cpp/src/internal/test/vector.cpp@ 509738fd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 509738fd was 509738fd, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: improved the testing framework

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <algorithm>
30#include <initializer_list>
31#include <internal/test/tests.hpp>
32#include <utility>
33#include <vector>
34
35namespace std::test
36{
37 bool vector_test::run()
38 {
39 start();
40
41 test_construction_and_assignment();
42 test_insert();
43 test_erase();
44
45 return end();
46 }
47
48 const char* vector_test::name()
49 {
50 return "vector";
51 }
52
53 void vector_test::test_construction_and_assignment()
54 {
55 auto check1 = {1, 2, 3, 4};
56 auto check2 = {4, 3, 2, 1};
57 auto check3 = {5, 5, 5, 5};
58
59 std::vector<int> vec1{};
60 vec1.push_back(1);
61 vec1.push_back(2);
62 vec1.push_back(3);
63 vec1.push_back(4);
64 test_eq(
65 "default constructor + push_back",
66 vec1.begin(), vec1.end(),
67 check1.begin(), check1.end()
68 );
69
70 std::vector<int> vec2{4, 3, 2, 1};
71 test_eq(
72 "initializer list constructor",
73 vec2.begin(), vec2.end(),
74 check2.begin(), check2.end()
75 );
76
77 std::vector<int> vec3(11);
78 test_eq("capacity constructor", 11ul, vec3.capacity());
79
80 std::vector<int> vec4(4ul, 5);
81 test_eq(
82 "replication constructor",
83 vec4.begin(), vec4.end(),
84 check3.begin(), check3.end()
85 );
86
87 // TODO: iterator constructor when implemented
88
89 std::vector<int> vec6{vec4};
90 test_eq(
91 "copy constructor",
92 vec6.begin(), vec6.end(),
93 vec4.begin(), vec4.end()
94 );
95
96 std::vector<int> vec7{std::move(vec6)};
97 test_eq(
98 "move constructor equality",
99 vec7.begin(), vec7.end(),
100 vec4.begin(), vec4.end()
101 );
102 test_eq(
103 "move constructor source empty",
104 vec6.size(), 0ul
105 );
106
107 std::vector<int> vec8{check1};
108 test_eq(
109 "explicit initializer list constructor",
110 vec8.begin(), vec8.end(),
111 check1.begin(), check1.end()
112 );
113
114 std::vector<int> vec9{};
115 vec9 = vec8;
116 test_eq(
117 "copy assignment",
118 vec9.begin(), vec9.end(),
119 vec8.begin(), vec8.end()
120 );
121
122 // TODO: move assignment when implemented
123 }
124
125 void vector_test::test_insert()
126 {
127 auto check1 = {1, 2, 3, 99, 4, 5};
128 auto check2 = {1, 2, 3, 99, 99, 99, 99, 99, 4, 5};
129 auto check3 = {1, 2, 3, 1, 2, 3, 99, 4, 5, 4, 5};
130
131 std::vector<int> vec1{1, 2, 3, 4, 5};
132 auto it = vec1.insert(vec1.begin() + 3, 99);
133 test_eq(
134 "single element insert",
135 vec1.begin(), vec1.end(),
136 check1.begin(), check1.end()
137 );
138 test_eq(
139 "iterator returned from insert",
140 *it, 99
141 );
142
143 std::vector<int> vec2{1, 2, 3, 4, 5};
144 vec2.insert(vec2.begin() + 3, 5ul, 99);
145 test_eq(
146 "multiple element insert",
147 vec2.begin(), vec2.end(),
148 check2.begin(), check2.end()
149 );
150
151 std::vector<int> vec3{1, 2, 3, 4, 5};
152 vec3.insert(vec3.begin() + 3, vec2.begin() + 3, vec2.begin() + 8);
153 test_eq(
154 "iterator insert",
155 vec3.begin(), vec3.end(),
156 check2.begin(), check2.end()
157 );
158
159 std::vector<int> vec4{1, 2, 3, 4, 5};
160 vec4.insert(vec4.begin() + 3, check1);
161 test_eq(
162 "initializer list insert",
163 vec4.begin(), vec4.end(),
164 check3.begin(), check3.end()
165 );
166
167 std::vector<int> vec5{1, 2, 3, 4, 5};
168 vec5.insert(vec5.begin() + 3, {1, 2, 3, 99, 4, 5});
169 test_eq(
170 "implicit initializer list insert",
171 vec5.begin(), vec5.end(),
172 check3.begin(), check3.end()
173 );
174
175 std::vector<int> vec6{};
176 vec6.insert(vec6.begin(), check3);
177 test_eq(
178 "insert to empty vector",
179 vec6.begin(), vec6.end(),
180 check3.begin(), check3.end()
181 );
182 }
183
184 void vector_test::test_erase()
185 {
186 auto check1 = {1, 2, 3, 5};
187 auto check2 = {1, 5};
188 auto check3 = {1, 3, 5};
189
190 std::vector<int> vec1{1, 2, 3, 4, 5};
191 vec1.erase(vec1.begin() + 3);
192 test_eq(
193 "single element erase",
194 vec1.begin(), vec1.end(),
195 check1.begin(), check1.end()
196 );
197
198 std::vector<int> vec2{1, 2, 3, 4, 5};
199 vec2.erase(vec2.begin() + 1, vec2.begin() + 4);
200 test_eq(
201 "range erase",
202 vec2.begin(), vec2.end(),
203 check2.begin(), check2.end()
204 );
205
206 std::vector<int> vec3{1, 2, 3, 4, 5};
207 for (auto it = vec3.begin(); it != vec3.end();)
208 {
209 if (*it % 2 == 0)
210 it = vec3.erase(it);
211 else
212 ++it;
213 }
214 test_eq(
215 "erase all even numbers",
216 vec3.begin(), vec3.end(),
217 check3.begin(), check3.end()
218 );
219 }
220}
Note: See TracBrowser for help on using the repository browser.