From 81be0d5670233d8a87ddd6ccd1795ad128dfe5e7 Mon Sep 17 00:00:00 2001 From: Adam Shapiro Date: Thu, 29 Apr 2021 09:02:51 -0400 Subject: [PATCH] Added vector and array unit tests. --- tests/CMakeLists.txt | 4 +- tests/Makefile | 6 ++- tests/test_array.cpp | 70 ++++++++++++++++++++++++++ tests/test_vector.cpp | 112 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 tests/test_array.cpp create mode 100644 tests/test_vector.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5e64734..8dc6d06 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,6 +10,7 @@ target_sources(frozen.tests PRIVATE ${CMAKE_CURRENT_LIST_DIR}/bench.hpp ${CMAKE_CURRENT_LIST_DIR}/catch.hpp ${CMAKE_CURRENT_LIST_DIR}/test_algorithms.cpp + ${CMAKE_CURRENT_LIST_DIR}/test_array.cpp ${CMAKE_CURRENT_LIST_DIR}/test_main.cpp ${CMAKE_CURRENT_LIST_DIR}/test_map.cpp ${CMAKE_CURRENT_LIST_DIR}/test_rand.cpp @@ -19,7 +20,8 @@ target_sources(frozen.tests PRIVATE ${CMAKE_CURRENT_LIST_DIR}/test_unordered_map.cpp ${CMAKE_CURRENT_LIST_DIR}/test_unordered_map_str.cpp ${CMAKE_CURRENT_LIST_DIR}/test_unordered_set.cpp - ${CMAKE_CURRENT_LIST_DIR}/test_unordered_str_set.cpp) + ${CMAKE_CURRENT_LIST_DIR}/test_unordered_str_set.cpp + ${CMAKE_CURRENT_LIST_DIR}/test_vector.cpp) string(CONCAT generator # msvc gives invalid integral overflow warning for unsigned type diff --git a/tests/Makefile b/tests/Makefile index b4f75be..94660e3 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,4 +1,4 @@ -SRCS=test_main.cpp test_rand.cpp test_set.cpp test_map.cpp test_unordered_set.cpp test_str_set.cpp test_unordered_str_set.cpp test_unordered_map.cpp test_unordered_map_str.cpp test_str.cpp test_algorithms.cpp +SRCS=test_main.cpp test_rand.cpp test_set.cpp test_map.cpp test_unordered_set.cpp test_str_set.cpp test_unordered_str_set.cpp test_unordered_map.cpp test_unordered_map_str.cpp test_str.cpp test_algorithms.cpp test_array.cpp test_vector.cpp TARGET=test_main CXXFLAGS=-O3 -Wall -std=c++14 -march=native -Wextra -W -Werror -Wshadow -fPIC @@ -25,6 +25,8 @@ test_rand.o: test_rand.cpp \ test_algorithms.o: test_algorithms.cpp \ ../include/frozen/bits/algorithms.h \ catch.hpp +test_array.o: test_array.cpp ../include/frozen/array.h \ + ../include/frozen/bits/basic_types.h catch.hpp test_map.o: test_map.cpp ../include/frozen/map.h \ ../include/frozen/bits/algorithms.h catch.hpp test_set.o: test_set.cpp ../include/frozen/set.h \ @@ -62,3 +64,5 @@ test_str.o: test_str.cpp \ ../include/frozen/bits/basic_types.h ../include/frozen/bits/elsa.h \ ../include/frozen/string.h ../include/frozen/algorithm.h \ catch.hpp +test_vector.o: test_vector.cpp ../include/frozen/vector.h \ + ../include/frozen/bits/basic_types.h catch.hpp diff --git a/tests/test_array.cpp b/tests/test_array.cpp new file mode 100644 index 0000000..b643467 --- /dev/null +++ b/tests/test_array.cpp @@ -0,0 +1,70 @@ +#include + +#include "bench.hpp" +#include "catch.hpp" + +/* +TEST_CASE("empty frozen array", "[array]") { + constexpr frozen::array ze_array{}; + + constexpr auto empty = ze_array.empty(); + REQUIRE(empty); + + constexpr auto size = ze_array.size(); + REQUIRE(size == 0); + + constexpr auto max_size = ze_array.max_size(); + REQUIRE(max_size == 0); + + constexpr auto capacity = ze_array.capacity(); + REQUIRE(capacity == 0); + + auto constexpr begin = ze_array.begin(), end = ze_array.end(); + REQUIRE(begin == end); + + auto constexpr cbegin = ze_array.cbegin(), cend = ze_array.cend(); + REQUIRE(cbegin == cend); + + std::for_each(ze_array.begin(), ze_array.end(), [](int) {}); + REQUIRE(std::distance(ze_array.rbegin(), ze_array.rend()) == 0); + REQUIRE(std::count(ze_array.crbegin(), ze_array.crend(), 3) == 0); +} +*/ + +TEST_CASE("singleton frozen array", "[array]") { + constexpr frozen::array ze_array{1}; + + constexpr auto empty = ze_array.empty(); + REQUIRE(!empty); + + constexpr auto size = ze_array.size(); + REQUIRE(size == 1); + + constexpr auto max_size = ze_array.max_size(); + REQUIRE(max_size == 1); + + const auto value_op = ze_array[0]; + REQUIRE(value_op == 1); + + const auto value_at = ze_array.at(0); + REQUIRE(value_at == 1); + + auto const begin = ze_array.begin(), end = ze_array.end(); + REQUIRE((begin + 1) == end); + + auto const cbegin = ze_array.cbegin(), cend = ze_array.cend(); + REQUIRE(cbegin == (cend - 1)); + + std::for_each(ze_array.begin(), ze_array.end(), [](int) {}); + REQUIRE(std::distance(ze_array.rbegin(), ze_array.rend()) == 1); + REQUIRE(std::count(ze_array.crbegin(), ze_array.crend(), 3) == 0); + REQUIRE(std::count(ze_array.crbegin(), ze_array.crend(), 1) == 1); +} + +TEST_CASE("array from array", "[array]") { + constexpr short data[4]{1, 2, 3, 4}; + constexpr frozen::array ze_array(data); + + constexpr auto value = ze_array[2]; + REQUIRE(value == 3); +} diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp new file mode 100644 index 0000000..34a881b --- /dev/null +++ b/tests/test_vector.cpp @@ -0,0 +1,112 @@ +#include + +#include "bench.hpp" +#include "catch.hpp" + +/* +TEST_CASE("empty frozen vector", "[vector]") { + constexpr frozen::vector ze_vector{}; + + constexpr auto empty = ze_vector.empty(); + REQUIRE(empty); + + constexpr auto size = ze_vector.size(); + REQUIRE(size == 0); + + constexpr auto max_size = ze_vector.max_size(); + REQUIRE(max_size == 0); + + constexpr auto capacity = ze_vector.capacity(); + REQUIRE(capacity == 0); + + auto constexpr begin = ze_vector.begin(), end = ze_vector.end(); + REQUIRE(begin == end); + + auto constexpr cbegin = ze_vector.cbegin(), cend = ze_vector.cend(); + REQUIRE(cbegin == cend); + + std::for_each(ze_vector.begin(), ze_vector.end(), [](int) {}); + REQUIRE(std::distance(ze_vector.rbegin(), ze_vector.rend()) == 0); + REQUIRE(std::count(ze_vector.crbegin(), ze_vector.crend(), 3) == 0); +} +*/ + +TEST_CASE("singleton frozen vector", "[vector]") { + constexpr frozen::vector ze_vector{1}; + + constexpr auto empty = ze_vector.empty(); + REQUIRE(!empty); + + constexpr auto size = ze_vector.size(); + REQUIRE(size == 1); + + constexpr auto max_size = ze_vector.max_size(); + REQUIRE(max_size == 1); + + constexpr auto capacity = ze_vector.capacity(); + REQUIRE(capacity == 1); + + const auto value_op = ze_vector[0]; + REQUIRE(value_op == 1); + + const auto value_at = ze_vector.at(0); + REQUIRE(value_at == 1); + + auto const begin = ze_vector.begin(), end = ze_vector.end(); + REQUIRE((begin + 1) == end); + + auto const cbegin = ze_vector.cbegin(), cend = ze_vector.cend(); + REQUIRE(cbegin == (cend - 1)); + + std::for_each(ze_vector.begin(), ze_vector.end(), [](int) {}); + REQUIRE(std::distance(ze_vector.rbegin(), ze_vector.rend()) == 1); + REQUIRE(std::count(ze_vector.crbegin(), ze_vector.crend(), 3) == 0); + REQUIRE(std::count(ze_vector.crbegin(), ze_vector.crend(), 1) == 1); +} + +TEST_CASE("non-full frozen vector", "[vector]") { + constexpr frozen::vector ze_vector{1}; + + constexpr auto empty = ze_vector.empty(); + REQUIRE(!empty); + + constexpr auto size = ze_vector.size(); + REQUIRE(size == 1); + + constexpr auto max_size = ze_vector.max_size(); + REQUIRE(max_size == 4); + + constexpr auto capacity = ze_vector.capacity(); + REQUIRE(capacity == 4); + + const auto value_op = ze_vector[0]; + REQUIRE(value_op == 1); + + const auto value_at = ze_vector.at(0); + REQUIRE(value_at == 1); + + auto const begin = ze_vector.begin(), end = ze_vector.end(); + REQUIRE((begin + 1) == end); + + auto const cbegin = ze_vector.cbegin(), cend = ze_vector.cend(); + REQUIRE(cbegin == (cend - 1)); + + std::for_each(ze_vector.begin(), ze_vector.end(), [](int) {}); + REQUIRE(std::distance(ze_vector.rbegin(), ze_vector.rend()) == 1); + REQUIRE(std::count(ze_vector.crbegin(), ze_vector.crend(), 3) == 0); + REQUIRE(std::count(ze_vector.crbegin(), ze_vector.crend(), 1) == 1); +} + +TEST_CASE("vector from array", "[vector]") { + constexpr short data1[4]{1, 2, 3, 4}; + constexpr frozen::vector ze_vector1(data1); + + constexpr auto value1 = ze_vector1[2]; + REQUIRE(value1 == 3); + + constexpr short data2[3]{1, 2, 3}; + constexpr frozen::vector ze_vector2(data2); + + constexpr auto value2 = ze_vector2[2]; + REQUIRE(value2 == 3); +}