Skip to content

Commit 56aa63f

Browse files
test(vector): add assertions and fix minor issues in test suite
- Add <cassert> includes and comprehensive assert() checks to all vector container tests, verifying size, capacity, element values, and state after operations (push_back, pop_back, erase, insert, assign, swap, clear, shrink_to_fit, etc.) - Fix typo in back.cc: "The first character" -> "The last character" - Fix missing trailing newlines in multiple files - Enable previously disabled append_range.cc test (remove #if 0) - Add state assertions to shared_ptr index tests for insert_index and erase_index operations
1 parent 66a34da commit 56aa63f

27 files changed

Lines changed: 218 additions & 56 deletions

tests/0026.container/0001.vector/append_range.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ using namespace fast_io::mnp;
88

99
int main()
1010
{
11-
#if 0
1211
auto head = fast_io::vector<int>{1, 2, 3, 4};
1312
auto const tail = std::list{-5, -6, -7};
1413
head.append_range(tail);
1514
assert(std::ranges::equal(head, fast_io::vector<int>{1, 2, 3, 4, -5, -6, -7}));
16-
#endif
17-
}
15+
}

tests/0026.container/0001.vector/assign.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <string>
1+
#include <cassert>
2+
#include <string>
23
#include <fast_io.h>
34
#include <fast_io_dsal/vector.h>
45
using namespace fast_io::io;
@@ -18,6 +19,11 @@ int main()
1819
};
1920

2021
characters.assign(5, 'a');
22+
assert(characters.size() == 5);
23+
for (char c : characters)
24+
{
25+
assert(c == 'a');
26+
}
2127
print_vector();
2228

2329
#if 0
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <fast_io.h>
1+
#include <cassert>
2+
#include <fast_io.h>
23
#include <fast_io_dsal/vector.h>
34
using namespace fast_io::io;
45
using namespace fast_io::mnp;
@@ -9,6 +10,7 @@ int main()
910

1011
if (!letters.empty())
1112
{
12-
print("The first character is '", chvw(letters.back()), "'.\n");
13+
print("The last character is '", chvw(letters.back()), "'.\n");
1314
}
14-
}
15+
assert(letters.back() == 'f');
16+
}

tests/0026.container/0001.vector/begin.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <algorithm>
1+
#include <cassert>
2+
#include <algorithm>
23
#include <numeric>
34
#include <string>
45
#include <fast_io.h>
@@ -17,17 +18,20 @@ int main()
1718
print("\n");
1819

1920
// Sums all integers in the vector nums (if any), printing only the result.
20-
println("Sum of nums: ",
21-
std::accumulate(nums.begin(), nums.end(), 0));
21+
auto const sum = std::accumulate(nums.begin(), nums.end(), 0);
22+
println("Sum of nums: ", sum);
23+
assert(sum == 31);
2224

2325
// Prints the first fruit in the vector fruits, checking if there is any.
2426
if (!fruits.empty())
2527
{
2628
println("First fruit: ", *fruits.begin());
29+
assert(*fruits.begin() == "orange");
2730
}
2831

2932
if (empty.begin() == empty.end())
3033
{
3134
print("vector 'empty' is indeed empty.\n");
3235
}
33-
}
36+
assert(empty.begin() == empty.end());
37+
}

tests/0026.container/0001.vector/capacity.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <fast_io.h>
1+
#include <cassert>
2+
#include <fast_io.h>
23
#include <fast_io_dsal/vector.h>
34
using namespace fast_io::io;
45
using namespace fast_io::mnp;
@@ -10,18 +11,22 @@ int main()
1011

1112
auto cap = v.capacity();
1213
println("Initial size: ", v.size(), ", capacity: ", cap);
14+
assert(v.size() == 0);
15+
assert(v.capacity() == 0);
1316

14-
print("\nDemonstrate the capacity's growth policy."
17+
print("\nDemonstrate the capacity'\''s growth policy."
1518
"\nSize: Capacity: Ratio:\n");
1619
while (sz-- > 0)
1720
{
1821
v.push_back(sz);
1922
if (cap != v.capacity())
2023
{
2124
println(left(v.size(), 7), left(v.capacity(), 11), left(float(v.capacity()) / static_cast<float>(cap), 10));
25+
assert(v.capacity() > cap);
2226
cap = v.capacity();
2327
}
2428
}
2529

2630
println("\nFinal size: ", v.size(), ", capacity: ", v.capacity());
31+
assert(v.size() == 100);
2732
}

tests/0026.container/0001.vector/clear.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <string_view>
1+
#include <cassert>
2+
#include <string_view>
23
#include <fast_io.h>
34
#include <fast_io_dsal/vector.h>
45
using namespace fast_io::io;
@@ -13,6 +14,10 @@ int main()
1314
{
1415
fast_io::vector<int> container{1, 2, 3};
1516
print_info("Before clear: ", container);
17+
assert(!container.empty());
18+
assert(container.size() == 3);
1619
container.clear();
1720
print_info("After clear: ", container);
18-
}
21+
assert(container.empty());
22+
assert(container.size() == 0);
23+
}

tests/0026.container/0001.vector/data.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <cstddef>
1+
#include <cassert>
2+
#include <cstddef>
23
#include <span>
34
#include <fast_io.h>
45
#include <fast_io_dsal/vector.h>
@@ -29,4 +30,9 @@ int main()
2930

3031
// std::span (C++20) is a safer alternative to separated pointer/size.
3132
span_func({container.data(), container.size()});
32-
}
33+
34+
assert(container.data()[0] == 1);
35+
assert(container.data()[1] == 2);
36+
assert(container.data()[2] == 3);
37+
assert(container.data()[3] == 4);
38+
}

tests/0026.container/0001.vector/emplace.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <algorithm>
1+
#include <cassert>
2+
#include <algorithm>
23
#include <ranges>
34
#include <string>
45
#include <fast_io.h>
@@ -62,4 +63,8 @@ int main()
6263
container.emplace(container.end(), std::move(three));
6364

6465
println("content:\n ", rgvw(container | std::views::transform([](auto const &a) { return a.s; }), " "));
66+
assert(container.size() == 3);
67+
assert(container[0].s == "one");
68+
assert(container[1].s == "two");
69+
assert(container[2].s == "three");
6570
}

tests/0026.container/0001.vector/emplace_index.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#include <algorithm>
2-
#include <ranges>
3-
#include <string>
1+
#include <cassert>
42
#include <fast_io.h>
53
#include <fast_io_dsal/vector.h>
64
using namespace fast_io::io;
@@ -13,6 +11,13 @@ int main()
1311
vec.emplace_index(0,6);
1412
vec.emplace_index(0,8);
1513
vec.erase_index(0);
14+
// After push_back(4): [4]
15+
// After emplace_index(0,6): [6,4]
16+
// After emplace_index(0,8): [8,6,4]
17+
// After erase_index(0): [6,4]
18+
assert(vec.size() == 2);
19+
assert(vec[0] == 6);
20+
assert(vec[1] == 4);
1621
for(auto const & e : vec)
1722
{
1823
::fast_io::io::println(e);
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <fast_io.h>
1+
#include <cassert>
2+
#include <fast_io.h>
23
#include <fast_io_dsal/vector.h>
34
using namespace fast_io::io;
45
using namespace fast_io::mnp;
@@ -7,7 +8,9 @@ int main()
78
{
89
fast_io::vector<int> numbers;
910
println("Initially, numbers.empty(): ", boolalpha(numbers.empty()));
11+
assert(numbers.empty());
1012

1113
numbers.push_back(42);
1214
println("After adding elements, numbers.empty(): ", boolalpha(numbers.empty()));
13-
}
15+
assert(!numbers.empty());
16+
}

0 commit comments

Comments
 (0)