|
1 | 1 | #include <algorithm> |
2 | 2 | #include <cstddef> |
3 | | -#include <vector> |
| 3 | +#include <ranges> |
| 4 | +#include <string> |
| 5 | +#include <string_view> |
| 6 | +#include <type_traits> |
4 | 7 | #include <utility> |
| 8 | +#include <vector> |
5 | 9 |
|
6 | 10 | #include <ystdlib/container/Array.hpp> |
7 | 11 |
|
8 | 12 | #include <catch2/catch_test_macros.hpp> |
9 | 13 |
|
| 14 | +namespace { |
| 15 | +constexpr size_t cBufferSize{1024}; |
| 16 | + |
| 17 | +class NoDefault { |
| 18 | +public: |
| 19 | + NoDefault(size_t n) : m_n{n} {} |
| 20 | + |
| 21 | + [[nodiscard]] auto get() const -> size_t { return m_n; } |
| 22 | + |
| 23 | +private: |
| 24 | + size_t m_n; |
| 25 | +}; |
| 26 | + |
| 27 | +class VarietyConstructors { |
| 28 | +public: |
| 29 | + static constexpr size_t cDefault = 3190; |
| 30 | + |
| 31 | + VarietyConstructors() = default; |
| 32 | + |
| 33 | + VarietyConstructors(size_t n) : m_n{n} {} |
| 34 | + |
| 35 | + VarietyConstructors(std::string const& s) : m_n{s.size()} {} |
| 36 | + |
| 37 | + VarietyConstructors(NoDefault const& nd) : m_n{nd.get()} {} |
| 38 | + |
| 39 | + [[nodiscard]] auto get() const -> size_t { return m_n; } |
| 40 | + |
| 41 | +private: |
| 42 | + size_t m_n{cDefault}; |
| 43 | +}; |
| 44 | + |
| 45 | +void test_function_argument_list_initialization( |
| 46 | + ystdlib::container::Array<VarietyConstructors> const& arr, |
| 47 | + std::vector<size_t> const& data |
| 48 | +) { |
| 49 | + REQUIRE(std::ranges::equal( |
| 50 | + arr | std::views::transform([](auto const& obj) { return obj.get(); }), |
| 51 | + data |
| 52 | + )); |
| 53 | +} |
| 54 | +} // namespace |
| 55 | + |
10 | 56 | namespace ystdlib::container::test { |
11 | 57 | TEST_CASE("test_empty_array", "[container][Array]") { |
12 | | - Array<int> arr{0}; |
| 58 | + Array<int> arr(0); |
13 | 59 | REQUIRE(arr.empty()); |
14 | 60 | // NOLINTNEXTLINE(readability-container-size-empty) |
15 | 61 | REQUIRE((0 == arr.size())); |
16 | 62 | REQUIRE((arr.begin() == arr.end())); |
17 | 63 | } |
18 | 64 |
|
19 | 65 | // NOLINTNEXTLINE(readability-function-cognitive-complexity) |
20 | | -TEST_CASE("test_array_fundamental", "[container][Array]") { |
21 | | - constexpr size_t cBufferSize{1024}; |
22 | | - |
| 66 | +TEST_CASE("test_array_basic", "[container][Array]") { |
23 | 67 | std::vector<int> vec; |
24 | 68 | for (int i{0}; i < cBufferSize; ++i) { |
25 | 69 | vec.push_back(i); |
26 | 70 | } |
27 | 71 |
|
28 | | - Array<int> arr{cBufferSize}; |
| 72 | + Array<int> arr(cBufferSize); |
29 | 73 | auto const& arr_const_ref = arr; |
30 | | - std::ranges::for_each(arr_const_ref, [](int i) -> void { REQUIRE((0 == i)); }); |
31 | | - |
32 | 74 | std::ranges::copy(vec, arr.begin()); |
| 75 | + |
33 | 76 | REQUIRE(std::ranges::equal(vec, arr)); |
34 | 77 | REQUIRE(std::ranges::equal(vec, arr_const_ref)); |
| 78 | + |
35 | 79 | REQUIRE_THROWS(arr.at(cBufferSize)); |
36 | 80 | REQUIRE_THROWS(arr_const_ref.at(cBufferSize)); |
37 | 81 |
|
38 | 82 | auto const arr_moved{std::move(arr)}; |
39 | 83 | REQUIRE(std::ranges::equal(vec, arr_moved)); |
40 | 84 | REQUIRE_THROWS(arr_moved.at(cBufferSize)); |
41 | 85 | } |
| 86 | + |
| 87 | +// NOLINTNEXTLINE(readability-function-cognitive-complexity) |
| 88 | +TEST_CASE("test_array_default_initialization", "[container][Array]") { |
| 89 | + Array<int> int_arr(cBufferSize); |
| 90 | + auto const& int_arr_const_ref = int_arr; |
| 91 | + |
| 92 | + REQUIRE(std::is_fundamental_v<int>); |
| 93 | + std::ranges::for_each(int_arr, [](auto const& i) -> void { REQUIRE((0 == i)); }); |
| 94 | + std::ranges::for_each(int_arr_const_ref, [](auto const& i) -> void { REQUIRE((0 == i)); }); |
| 95 | + |
| 96 | + Array<std::nullptr_t> ptr_arr(cBufferSize); |
| 97 | + auto const& ptr_arr_const_ref = ptr_arr; |
| 98 | + |
| 99 | + REQUIRE(std::is_fundamental_v<std::nullptr_t>); |
| 100 | + std::ranges::for_each(ptr_arr, [](auto const& p) -> void { REQUIRE((nullptr == p)); }); |
| 101 | + std::ranges::for_each(ptr_arr_const_ref, [](auto const& p) -> void { |
| 102 | + REQUIRE((nullptr == p)); |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +TEST_CASE("test_array_list_initialization_basic", "[container][Array]") { |
| 107 | + std::vector<std::string> const |
| 108 | + vec{"yscope", "clp", "ystdlib", "ystdlib::container::Array", "default_initializable"}; |
| 109 | + Array<std::string> const |
| 110 | + arr0{"yscope", "clp", "ystdlib", "ystdlib::container::Array", "default_initializable"}; |
| 111 | + Array<std::string> const arr1 |
| 112 | + = {"yscope", "clp", "ystdlib", "ystdlib::container::Array", "default_initializable"}; |
| 113 | + REQUIRE(std::ranges::equal(vec, arr0)); |
| 114 | + REQUIRE(std::ranges::equal(vec, arr1)); |
| 115 | +} |
| 116 | + |
| 117 | +TEST_CASE("test_array_list_initialization_variety_constructors", "[container][Array]") { |
| 118 | + constexpr size_t cTestNum0{344}; |
| 119 | + constexpr size_t cTestNum1{454}; |
| 120 | + constexpr std::string_view cTestStr{"yscope"}; |
| 121 | + |
| 122 | + Array<VarietyConstructors> const arr |
| 123 | + = {VarietyConstructors{}, cTestNum0, std::string{cTestStr}, NoDefault{cTestNum1}}; |
| 124 | + std::vector<size_t> const data |
| 125 | + = {VarietyConstructors::cDefault, cTestNum0, cTestStr.size(), cTestNum1}; |
| 126 | + REQUIRE(std::ranges::equal( |
| 127 | + arr | std::views::transform([](auto const& obj) { return obj.get(); }), |
| 128 | + data |
| 129 | + )); |
| 130 | + test_function_argument_list_initialization( |
| 131 | + {VarietyConstructors{}, cTestNum0, std::string{cTestStr}, NoDefault{cTestNum1}}, |
| 132 | + data |
| 133 | + ); |
| 134 | +} |
42 | 135 | } // namespace ystdlib::container::test |
0 commit comments