| 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 |
|
|---|
| 35 | namespace 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 | std::vector<int> vec10{};
|
|---|
| 124 | vec10 = std::move(vec9);
|
|---|
| 125 | test_eq(
|
|---|
| 126 | "move assignment",
|
|---|
| 127 | vec10.begin(), vec10.end(),
|
|---|
| 128 | vec8.begin(), vec8.end()
|
|---|
| 129 | );
|
|---|
| 130 |
|
|---|
| 131 | test_eq("move assignment origin empty", vec9.size(), 0);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void vector_test::test_insert()
|
|---|
| 135 | {
|
|---|
| 136 | auto check1 = {1, 2, 3, 99, 4, 5};
|
|---|
| 137 | auto check2 = {1, 2, 3, 99, 99, 99, 99, 99, 4, 5};
|
|---|
| 138 | auto check3 = {1, 2, 3, 1, 2, 3, 99, 4, 5, 4, 5};
|
|---|
| 139 |
|
|---|
| 140 | std::vector<int> vec1{1, 2, 3, 4, 5};
|
|---|
| 141 | auto it = vec1.insert(vec1.begin() + 3, 99);
|
|---|
| 142 | test_eq(
|
|---|
| 143 | "single element insert",
|
|---|
| 144 | vec1.begin(), vec1.end(),
|
|---|
| 145 | check1.begin(), check1.end()
|
|---|
| 146 | );
|
|---|
| 147 | test_eq(
|
|---|
| 148 | "iterator returned from insert",
|
|---|
| 149 | *it, 99
|
|---|
| 150 | );
|
|---|
| 151 |
|
|---|
| 152 | std::vector<int> vec2{1, 2, 3, 4, 5};
|
|---|
| 153 | vec2.insert(vec2.begin() + 3, 5ul, 99);
|
|---|
| 154 | test_eq(
|
|---|
| 155 | "multiple element insert",
|
|---|
| 156 | vec2.begin(), vec2.end(),
|
|---|
| 157 | check2.begin(), check2.end()
|
|---|
| 158 | );
|
|---|
| 159 |
|
|---|
| 160 | std::vector<int> vec3{1, 2, 3, 4, 5};
|
|---|
| 161 | vec3.insert(vec3.begin() + 3, vec2.begin() + 3, vec2.begin() + 8);
|
|---|
| 162 | test_eq(
|
|---|
| 163 | "iterator insert",
|
|---|
| 164 | vec3.begin(), vec3.end(),
|
|---|
| 165 | check2.begin(), check2.end()
|
|---|
| 166 | );
|
|---|
| 167 |
|
|---|
| 168 | std::vector<int> vec4{1, 2, 3, 4, 5};
|
|---|
| 169 | vec4.insert(vec4.begin() + 3, check1);
|
|---|
| 170 | test_eq(
|
|---|
| 171 | "initializer list insert",
|
|---|
| 172 | vec4.begin(), vec4.end(),
|
|---|
| 173 | check3.begin(), check3.end()
|
|---|
| 174 | );
|
|---|
| 175 |
|
|---|
| 176 | std::vector<int> vec5{1, 2, 3, 4, 5};
|
|---|
| 177 | vec5.insert(vec5.begin() + 3, {1, 2, 3, 99, 4, 5});
|
|---|
| 178 | test_eq(
|
|---|
| 179 | "implicit initializer list insert",
|
|---|
| 180 | vec5.begin(), vec5.end(),
|
|---|
| 181 | check3.begin(), check3.end()
|
|---|
| 182 | );
|
|---|
| 183 |
|
|---|
| 184 | std::vector<int> vec6{};
|
|---|
| 185 | vec6.insert(vec6.begin(), check3);
|
|---|
| 186 | test_eq(
|
|---|
| 187 | "insert to empty vector",
|
|---|
| 188 | vec6.begin(), vec6.end(),
|
|---|
| 189 | check3.begin(), check3.end()
|
|---|
| 190 | );
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | void vector_test::test_erase()
|
|---|
| 194 | {
|
|---|
| 195 | auto check1 = {1, 2, 3, 5};
|
|---|
| 196 | auto check2 = {1, 5};
|
|---|
| 197 | auto check3 = {1, 3, 5};
|
|---|
| 198 |
|
|---|
| 199 | std::vector<int> vec1{1, 2, 3, 4, 5};
|
|---|
| 200 | vec1.erase(vec1.begin() + 3);
|
|---|
| 201 | test_eq(
|
|---|
| 202 | "single element erase",
|
|---|
| 203 | vec1.begin(), vec1.end(),
|
|---|
| 204 | check1.begin(), check1.end()
|
|---|
| 205 | );
|
|---|
| 206 |
|
|---|
| 207 | std::vector<int> vec2{1, 2, 3, 4, 5};
|
|---|
| 208 | vec2.erase(vec2.begin() + 1, vec2.begin() + 4);
|
|---|
| 209 | test_eq(
|
|---|
| 210 | "range erase",
|
|---|
| 211 | vec2.begin(), vec2.end(),
|
|---|
| 212 | check2.begin(), check2.end()
|
|---|
| 213 | );
|
|---|
| 214 |
|
|---|
| 215 | std::vector<int> vec3{1, 2, 3, 4, 5};
|
|---|
| 216 | for (auto it = vec3.begin(); it != vec3.end();)
|
|---|
| 217 | {
|
|---|
| 218 | if (*it % 2 == 0)
|
|---|
| 219 | it = vec3.erase(it);
|
|---|
| 220 | else
|
|---|
| 221 | ++it;
|
|---|
| 222 | }
|
|---|
| 223 | test_eq(
|
|---|
| 224 | "erase all even numbers",
|
|---|
| 225 | vec3.begin(), vec3.end(),
|
|---|
| 226 | check3.begin(), check3.end()
|
|---|
| 227 | );
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|