Skip to content

Commit

Permalink
Added vector and array unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamshapiro0 committed May 4, 2021
1 parent 6fd3a79 commit e29a155
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 2 deletions.
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 \
Expand Down Expand Up @@ -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
75 changes: 75 additions & 0 deletions tests/test_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <frozen/array.h>

#include "bench.hpp"
#include "catch.hpp"

/*
TEST_CASE("empty frozen array", "[array]") {
constexpr frozen::array<int, 0> 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<short, 1> 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<short, 4> ze_array(data);
constexpr auto value = ze_array[2];
REQUIRE(value == 3);
}

TEST_CASE("array from initializer", "[array]") {
constexpr frozen::array<short, 4> ze_array({1, 2, 3, 4});
constexpr auto value = ze_array[2];
REQUIRE(value == 3);
}
122 changes: 122 additions & 0 deletions tests/test_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <frozen/vector.h>

#include "bench.hpp"
#include "catch.hpp"

/*
TEST_CASE("empty frozen vector", "[vector]") {
constexpr frozen::vector<int, 0> 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<short, 1> 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<short, 4> 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<short, 4> ze_vector1(data1);

constexpr auto value1 = ze_vector1[2];
REQUIRE(value1 == 3);

constexpr short data2[3]{1, 2, 3};
constexpr frozen::vector<short, 4> ze_vector2(data2);

constexpr auto value2 = ze_vector2[2];
REQUIRE(value2 == 3);
}

TEST_CASE("vector from initializer", "[vector]") {
constexpr frozen::vector<short, 4> ze_vector1({1, 2, 3, 4});
constexpr auto value1 = ze_vector1[2];
REQUIRE(value1 == 3);

constexpr frozen::vector<short, 4> ze_vector2({1, 2, 3});
constexpr auto value2 = ze_vector2[2];
REQUIRE(value2 == 3);
}

0 comments on commit e29a155

Please sign in to comment.