added data augmentation tests for clear and insert

This commit is contained in:
Patrick 2024-07-29 16:32:28 +02:00
parent c1f06f9c6d
commit 0350b33087
2 changed files with 228 additions and 0 deletions

View File

@ -16,3 +16,8 @@ add_executable(test_const_vector_data_access const_vector_data_access.test.cpp
test_params.h)
target_link_libraries(test_const_vector_data_access const_container test_common)
add_test(NAME "const_vector data access" COMMAND test_const_vector_data_access)
add_executable(test_const_vector_data_augmentation const_vector_data_augmentation.test.cpp
test_params.h)
target_link_libraries(test_const_vector_data_augmentation const_container test_common)
add_test(NAME "const_vector data augmentation" COMMAND test_const_vector_data_augmentation)

View File

@ -0,0 +1,223 @@
#include <const_vector.hpp>
#include <test.hpp>
#include <vector>
#include <format>
#include "test_params.h"
#include <format>
#include <ranges>
#include <sstream>
template<std::ranges::range R, typename CharT>
struct std::formatter<R, CharT> {
template<typename ParseContext>
constexpr ParseContext::iterator parse(ParseContext& ctx) {
auto it = ctx.begin();
while (it != ctx.end() && *it != '}')
++it;
return it;
}
template<typename FmtContext>
constexpr FmtContext::iterator format(const R& range, FmtContext& ctx) const {
std::ostringstream out;
out << '[';
auto it = std::ranges::begin(range);
for (; it != std::next(std::ranges::begin(range), std::ranges::size(range) - 1); ++it) {
out << std::format("{}, ", *it);
}
out << std::format("{}]", *it);
return std::ranges::copy(std::move(out).str(), ctx.out()).out;
}
};
template<typename CharT>
struct std::formatter<TestObj, CharT> {
template<typename ParseContext>
constexpr ParseContext::iterator parse(ParseContext &ctx)
{
auto it = ctx.begin();
while (it != ctx.end() && *it != '}')
++it;
return it;
}
template<typename FmtContext>
constexpr FmtContext::iterator format(const TestObj &obj, FmtContext &ctx) const
{
std::ostringstream out;
out << '(' << obj.x << ", '" << obj.c << "', \"" << obj.s << "\")";
return std::ranges::copy(std::move(out).str(), ctx.out()).out;
}
};
constexpr test_suite tests = define_tests("Data Augmentation")
("const_vector::clear()", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N, typename ctx>() constexpr {
cc::const_vector v(get_test_param<ctx, "arr">());
v.clear();
ASSERT(v.empty(), ctx);
return TEST_PASS();
}), 2, int, char, const char *, TestObj);
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::insert(const_iter pos, const T& value)", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N, typename ctx>() constexpr {
cc::const_vector<T, c_arr_len(get_test_param<ctx, "arr">()) + 3> v(get_test_param<ctx, "arr">());
auto val_it = v.insert(std::next(v.begin()), get_test_param<ctx, "value">());
v.insert(val_it, *v.begin());
v.insert(v.end(), get_test_param<ctx, "value">());
ASSERT_THROWS((v.insert(v.end(), get_test_param<ctx, "value">())), std::length_error, ctx);
std::vector<T> comparable;
std::ranges::copy(get_test_param<ctx, "arr">(), std::back_inserter(comparable));
auto comparable_it = comparable.insert(std::next(comparable.begin()), get_test_param<ctx, "value">());
comparable.insert(comparable_it, *comparable.begin());
comparable.insert(comparable.end(), get_test_param<ctx, "value">());
ASSERT(ranges_equal(v, comparable), ctx);
return TEST_PASS();
}), 2, int, char, const char *, TestObj);
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::insert(const_iter pos, T&& value)", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N, typename ctx>() constexpr {
auto value1 = make_copy(get_test_param<ctx, "value">());
auto value2 = make_copy(get_test_param<ctx, "arr">()[0]);
auto value3 = make_copy(get_test_param<ctx, "value">());
auto value4 = make_copy(get_test_param<ctx, "value">());
auto value1_comp = make_copy(get_test_param<ctx, "value">());
auto value2_comp = make_copy(get_test_param<ctx, "arr">()[0]);
auto value3_comp = make_copy(get_test_param<ctx, "value">());
cc::const_vector<T, c_arr_len(get_test_param<ctx, "arr">()) + 3> v(get_test_param<ctx, "arr">());
auto val_it = v.insert(std::next(v.begin()), force_move(value1));
v.insert(val_it, force_move(value2));
v.insert(v.end(), force_move(value3));
ASSERT_THROWS((v.insert(v.end(), force_move(value4))), std::length_error, ctx);
ASSERT_MSG((value4 == get_test_param<ctx, "value">()), "Value has been changed by failed move insert", ctx);
std::vector<T> comparable;
std::ranges::copy(get_test_param<ctx, "arr">(), std::back_inserter(comparable));
auto comparable_it = comparable.insert(std::next(comparable.begin()), force_move(value1_comp));
comparable.insert(comparable_it, force_move(value2_comp));
comparable.insert(comparable.end(), force_move(value3_comp));
ASSERT(ranges_equal(v, comparable), ctx);
return TEST_PASS();
}), 2, int, char, const char *, TestObj);
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::insert(const_iter pos, size_type count, const T &value)", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N, typename ctx>() constexpr {
cc::const_vector<T, c_arr_len(get_test_param<ctx, "arr">()) + 9> v(get_test_param<ctx, "arr">());
auto it = v.insert(std::next(v.begin()), 3, get_test_param<ctx, "value">());
v.insert(it, 3, get_test_param<ctx, "arr">()[0]);
v.insert(v.end(), 3, get_test_param<ctx, "value">());
ASSERT_THROWS((v.insert(v.end(), 1, get_test_param<ctx, "value">())), std::length_error, ctx);
ASSERT_NOTHROW((v.insert(v.end(), 0, get_test_param<ctx, "value">())), std::length_error, ctx);
std::vector<T> comparable;
std::ranges::copy(get_test_param<ctx, "arr">(), std::back_inserter(comparable));
auto cit = comparable.insert(std::next(comparable.begin()), 3, get_test_param<ctx, "value">());
comparable.insert(cit, 3, get_test_param<ctx, "arr">()[0]);
comparable.insert(comparable.end(), 3, get_test_param<ctx, "value">());
ASSERT(ranges_equal(v, comparable), ctx);
return TEST_PASS();
}), 2, int, char, const char *, TestObj);
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::insert(const_iter pos, InputIt first, InputIt last)", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N, typename ctx>() constexpr {
std::vector<T> src;
std::ranges::copy(get_test_param<ctx, "arr">(), std::back_inserter(src));
cc::const_vector<T, c_arr_len(get_test_param<ctx, "arr">()) * 3> v(get_test_param<ctx, "arr">());
auto it = v.insert(std::next(v.begin()), std::begin(get_test_param<ctx, "arr">()), std::end(get_test_param<ctx, "arr">()));
v.insert(it, src.begin(), std::next(src.begin() + src.size() / 2));
v.insert(v.end(), std::next(src.begin() + src.size() / 2), src.end());
ASSERT_THROWS((v.insert(v.end(), src.begin(), src.end())), std::length_error, ctx);
ASSERT_NOTHROW((v.insert(v.end(), src.begin(), src.begin())), std::length_error, ctx);
ASSERT(std::ranges::equal(src, get_test_param<ctx, "arr">()));
std::vector<T> comparable;
std::ranges::copy(get_test_param<ctx, "arr">(), std::back_inserter(comparable));
auto cit = comparable.insert(std::next(comparable.begin()), std::begin(get_test_param<ctx, "arr">()), std::end(get_test_param<ctx, "arr">()));
comparable.insert(cit, src.begin(), std::next(src.begin() + src.size() / 2));
comparable.insert(comparable.end(), std::next(src.begin() + src.size() / 2), src.end());
ASSERT(ranges_equal(v, comparable), ctx);
return TEST_PASS();
}), 2, int, char, const char *, TestObj);
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL)
("const_vector::insert(const_iter pos, std::initializer_list<T> values)", []() constexpr {
REPEAT_FOR_TYPES_N(([]<typename T, std::size_t N, typename ctx>() constexpr {
cc::const_vector<T, get_test_param<ctx, "ilist">().size() * 4> v(get_test_param<ctx, "ilist">());
auto it = v.insert(std::next(v.begin()), get_test_param<ctx, "ilist">());
v.insert(it, get_test_param<ctx, "ilist">());
v.insert(v.end(), get_test_param<ctx, "ilist">());
ASSERT_THROWS((v.insert(v.end(), get_test_param<ctx, "ilist">())), std::length_error, ctx);
std::vector<T> comparable;
std::ranges::copy(get_test_param<ctx, "arr">(), std::back_inserter(comparable));
auto cit = comparable.insert(std::next(comparable.begin()), get_test_param<ctx, "ilist">());
comparable.insert(cit, get_test_param<ctx, "ilist">());
comparable.insert(comparable.end(), get_test_param<ctx, "ilist">());
ASSERT(ranges_equal(v, comparable), ctx);
return TEST_PASS();
}), 2, int, char, const char *, TestObj);
return TEST_PASS();
}, EvalFlag::RUNTIME_CONSTEVAL);
int main() {
return tests.run();
}