diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 03480e1d62c7fee5f4e78a2e7ea2f04a96674033..b6c12e7013b044676b26ab686252612db3817470 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -4,6 +4,7 @@ set(EXECUTABLE_OUTPUT_PATH ${PASTIS_BINARY_DIR}) add_executable (unit_tests test_main.cpp + test_Array.cpp test_ItemType.cpp test_PastisAssert.cpp test_RevisionInfo.cpp diff --git a/tests/test_Array.cpp b/tests/test_Array.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cd02146c8775dca7a061672b8efed1fbdcad236 --- /dev/null +++ b/tests/test_Array.cpp @@ -0,0 +1,87 @@ +#include <catch.hpp> + +#include <PastisAssert.hpp> +#include <Array.hpp> +#include <Types.hpp> + +// Instantiate to ensure full coverage is performed +template class Array<int>; + +TEST_CASE("Array", "[utils]") { + + Array<int> a(10); + REQUIRE(a.size() == 10); + + for (size_t i=0; i<a.size(); ++i) { + a[i] = 2*i; + } + + REQUIRE(((a[0] == 0) and (a[1] == 2) and + (a[2] == 4) and (a[3] == 6) and + (a[4] == 8) and (a[5] ==10) and + (a[6] ==12) and (a[7] ==14) and + (a[8] ==16) and (a[9] ==18))); + + SECTION("checking for copies") { + Array<const int> b{a}; + + REQUIRE(((b[0] == 0) and (b[1] == 2) and + (b[2] == 4) and (b[3] == 6) and + (b[4] == 8) and (b[5] ==10) and + (b[6] ==12) and (b[7] ==14) and + (b[8] ==16) and (b[9] ==18))); + + Array<int> c{a}; + + REQUIRE(((c[0] == 0) and (c[1] == 2) and + (c[2] == 4) and (c[3] == 6) and + (c[4] == 8) and (c[5] ==10) and + (c[6] ==12) and (c[7] ==14) and + (c[8] ==16) and (c[9] ==18))); + + + Array<int> d = std::move(c); + + REQUIRE(((d[0] == 0) and (d[1] == 2) and + (d[2] == 4) and (d[3] == 6) and + (d[4] == 8) and (d[5] ==10) and + (d[6] ==12) and (d[7] ==14) and + (d[8] ==16) and (d[9] ==18))); + + } + + SECTION("checking for affectations") { + Array<const int> b; + b = a; + + REQUIRE(((b[0] == 0) and (b[1] == 2) and + (b[2] == 4) and (b[3] == 6) and + (b[4] == 8) and (b[5] ==10) and + (b[6] ==12) and (b[7] ==14) and + (b[8] ==16) and (b[9] ==18))); + + + Array<int> c; + c = a; + + REQUIRE(((c[0] == 0) and (c[1] == 2) and + (c[2] == 4) and (c[3] == 6) and + (c[4] == 8) and (c[5] ==10) and + (c[6] ==12) and (c[7] ==14) and + (c[8] ==16) and (c[9] ==18))); + + Array<int> d; + d = std::move(c); + + REQUIRE(((d[0] == 0) and (d[1] == 2) and + (d[2] == 4) and (d[3] == 6) and + (d[4] == 8) and (d[5] ==10) and + (d[6] ==12) and (d[7] ==14) and + (d[8] ==16) and (d[9] ==18))); + } +#ifndef NDEBUG + SECTION("checking for bounds violation") { + REQUIRE_THROWS_AS(a[10], AssertError); + } +#endif // NDEBUG +}