source: mainline/uspace/lib/cpp/src/internal/test/vector.cpp@ 6c089a9

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

cpp: fixed included headers

  • 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 test_construction_and_assignment();
40 test_insert();
41 test_erase();
42 return true;
43 }
44
45 const char* vector_test::name()
46 {
47 return "vector";
48 }
49
50 void vector_test::test_construction_and_assignment()
51 {
52 auto check1 = {1, 2, 3, 4};
53 auto check2 = {4, 3, 2, 1};
54 auto check3 = {5, 5, 5, 5};
55
56 std::vector<int> vec1{};
57 vec1.push_back(1);
58 vec1.push_back(2);
59 vec1.push_back(3);
60 vec1.push_back(4);
61 test_eq(
62 "default constructor + push_back",
63 vec1.begin(), vec1.end(),
64 check1.begin(), check1.end()
65 );
66
67 std::vector<int> vec2{4, 3, 2, 1};
68 test_eq(
69 "initializer list constructor",
70 vec2.begin(), vec2.end(),
71 check2.begin(), check2.end()
72 );
73
74 std::vector<int> vec3(11);
75 test_eq("capacity constructor", 11ul, vec3.capacity());
76
77 std::vector<int> vec4(4ul, 5);
78 test_eq(
79 "replication constructor",
80 vec4.begin(), vec4.end(),
81 check3.begin(), check3.end()
82 );
83
84 // TODO: iterator constructor when implemented
85
86 std::vector<int> vec6{vec4};
87 test_eq(
88 "copy constructor",
89 vec6.begin(), vec6.end(),
90 vec4.begin(), vec4.end()
91 );
92
93 std::vector<int> vec7{std::move(vec6)};
94 test_eq(
95 "move constructor equality",
96 vec7.begin(), vec7.end(),
97 vec4.begin(), vec4.end()
98 );
99 test_eq(
100 "move constructor source empty",
101 vec6.size(), 0ul
102 );
103
104 std::vector<int> vec8{check1};
105 test_eq(
106 "explicit initializer list constructor",
107 vec8.begin(), vec8.end(),
108 check1.begin(), check1.end()
109 );
110
111 std::vector<int> vec9{};
112 vec9 = vec8;
113 test_eq(
114 "copy assignment",
115 vec9.begin(), vec9.end(),
116 vec8.begin(), vec8.end()
117 );
118
119 // TODO: move assignment when implemented
120 }
121
122 void vector_test::test_insert()
123 {
124 auto check1 = {1, 2, 3, 99, 4, 5};
125 auto check2 = {1, 2, 3, 99, 99, 99, 99, 99, 4, 5};
126 auto check3 = {1, 2, 3, 1, 2, 3, 99, 4, 5, 4, 5};
127
128 std::vector<int> vec1{1, 2, 3, 4, 5};
129 auto it = vec1.insert(vec1.begin() + 3, 99);
130 test_eq(
131 "single element insert",
132 vec1.begin(), vec1.end(),
133 check1.begin(), check1.end()
134 );
135 test_eq(
136 "iterator returned from insert",
137 *it, 99
138 );
139
140 std::vector<int> vec2{1, 2, 3, 4, 5};
141 vec2.insert(vec2.begin() + 3, 5ul, 99);
142 test_eq(
143 "multiple element insert",
144 vec2.begin(), vec2.end(),
145 check2.begin(), check2.end()
146 );
147
148 std::vector<int> vec3{1, 2, 3, 4, 5};
149 vec3.insert(vec3.begin() + 3, vec2.begin() + 3, vec2.begin() + 8);
150 test_eq(
151 "iterator insert",
152 vec3.begin(), vec3.end(),
153 check2.begin(), check2.end()
154 );
155
156 std::vector<int> vec4{1, 2, 3, 4, 5};
157 vec4.insert(vec4.begin() + 3, check1);
158 test_eq(
159 "initializer list insert",
160 vec4.begin(), vec4.end(),
161 check3.begin(), check3.end()
162 );
163
164 std::vector<int> vec5{1, 2, 3, 4, 5};
165 vec5.insert(vec5.begin() + 3, {1, 2, 3, 99, 4, 5});
166 test_eq(
167 "implicit initializer list insert",
168 vec5.begin(), vec5.end(),
169 check3.begin(), check3.end()
170 );
171
172 std::vector<int> vec6{};
173 vec6.insert(vec6.begin(), check3);
174 test_eq(
175 "insert to empty vector",
176 vec6.begin(), vec6.end(),
177 check3.begin(), check3.end()
178 );
179 }
180
181 void vector_test::test_erase()
182 {
183 auto check1 = {1, 2, 3, 5};
184 auto check2 = {1, 5};
185 auto check3 = {1, 3, 5};
186
187 std::vector<int> vec1{1, 2, 3, 4, 5};
188 vec1.erase(vec1.begin() + 3);
189 test_eq(
190 "single element erase",
191 vec1.begin(), vec1.end(),
192 check1.begin(), check1.end()
193 );
194
195 std::vector<int> vec2{1, 2, 3, 4, 5};
196 vec2.erase(vec2.begin() + 1, vec2.begin() + 4);
197 test_eq(
198 "range erase",
199 vec2.begin(), vec2.end(),
200 check2.begin(), check2.end()
201 );
202
203 std::vector<int> vec3{1, 2, 3, 4, 5};
204 for (auto it = vec3.begin(); it != vec3.end();)
205 {
206 if (*it % 2 == 0)
207 it = vec3.erase(it);
208 else
209 ++it;
210 }
211 test_eq(
212 "erase all even numbers",
213 vec3.begin(), vec3.end(),
214 check3.begin(), check3.end()
215 );
216 }
217}
Note: See TracBrowser for help on using the repository browser.