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