Luna/tests/libluna/TestVector.cpp

91 lines
1.7 KiB
C++
Raw Normal View History

2023-04-23 19:52:28 +00:00
#include <luna/Vector.h>
#include <test.h>
TestResult test_vector_empty_capacity()
{
Vector<char> v;
validate(v.capacity() == 0);
test_success;
}
TestResult test_vector_reserve_should_raise_capacity()
{
Vector<char> v;
validate(v.capacity() == 0);
TRY(v.try_reserve(8));
validate(v.capacity() == 8);
test_success;
}
TestResult test_vector_append_should_raise_capacity()
{
Vector<char> v;
TRY(v.try_append('c'));
validate(v.capacity() > 0);
validate(v.size() == 1);
test_success;
}
TestResult test_vector_pop_should_remove_last_element()
{
Vector<char> v;
TRY(v.try_append('c'));
validate(v.capacity() > 0);
validate(v.size() == 1);
auto value = v.try_pop();
validate(value.has_value());
validate(value.value() == 'c');
validate(v.size() == 0);
test_success;
}
TestResult test_empty_vector_pop_should_not_remove_anything()
{
Vector<char> v;
validate(v.size() == 0);
auto value = v.try_pop();
validate(!value.has_value());
validate(v.size() == 0);
test_success;
}
TestResult test_vector_clear_should_free_memory()
{
Vector<char> v;
TRY(v.try_append('a'));
TRY(v.try_append('b'));
validate(v.size() == 2);
v.clear();
validate(v.size() == 0);
validate(v.capacity() == 0);
test_success;
}
Result<void> test_main()
{
test_prelude;
run_test(test_vector_empty_capacity);
run_test(test_vector_reserve_should_raise_capacity);
run_test(test_vector_append_should_raise_capacity);
run_test(test_vector_pop_should_remove_last_element);
run_test(test_empty_vector_pop_should_not_remove_anything);
run_test(test_vector_clear_should_free_memory);
return {};
}