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