Sprout/libs/array/test/array.cpp

150 lines
4.1 KiB
C++
Raw Normal View History

#ifndef SPROUT_LIBS_ARRAY_TEST_ARRAY_CPP
#define SPROUT_LIBS_ARRAY_TEST_ARRAY_CPP
2011-12-23 11:59:33 +00:00
#include <sprout/array.hpp>
#include <sprout/container.hpp>
2011-12-23 11:59:33 +00:00
#include <testspr/tools.hpp>
namespace testspr {
static void array_test() {
using namespace sprout;
{
SPROUT_STATIC_CONSTEXPR int carr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
SPROUT_STATIC_CONSTEXPR auto arr1 = sprout::to_array(carr);
SPROUT_STATIC_CONSTEXPR auto arr2 = sprout::make_array<int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
SPROUT_STATIC_CONSTEXPR auto arr3 = sprout::make_common_array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
TESTSPR_BOTH_ASSERT(testspr::equal(carr, arr1));
TESTSPR_BOTH_ASSERT(testspr::equal(carr, arr2));
TESTSPR_BOTH_ASSERT(testspr::equal(carr, arr3));
2011-12-23 11:59:33 +00:00
// begin
TESTSPR_BOTH_ASSERT(carr[0] == *arr1.begin());
TESTSPR_BOTH_ASSERT(carr[0] == *arr2.begin());
2011-12-23 11:59:33 +00:00
// cbegin
TESTSPR_BOTH_ASSERT(carr[0] == *arr1.cbegin());
TESTSPR_BOTH_ASSERT(carr[0] == *arr2.cbegin());
2011-12-23 11:59:33 +00:00
// end
TESTSPR_BOTH_ASSERT(carr[9] == *(arr1.end() - 1));
TESTSPR_BOTH_ASSERT(carr[9] == *(arr2.end() - 1));
2011-12-23 11:59:33 +00:00
// cend
TESTSPR_BOTH_ASSERT(carr[9] == *(arr1.cend() - 1));
TESTSPR_BOTH_ASSERT(carr[9] == *(arr2.cend() - 1));
2011-12-23 11:59:33 +00:00
// rbegin
TESTSPR_BOTH_ASSERT(carr[9] == *arr1.rbegin());
TESTSPR_BOTH_ASSERT(carr[9] == *arr2.rbegin());
2011-12-23 11:59:33 +00:00
// crbegin
TESTSPR_BOTH_ASSERT(carr[9] == *arr1.crbegin());
TESTSPR_BOTH_ASSERT(carr[9] == *arr2.crbegin());
2011-12-23 11:59:33 +00:00
// rend
TESTSPR_BOTH_ASSERT(carr[0] == *(arr1.rend() - 1));
TESTSPR_BOTH_ASSERT(carr[0] == *(arr2.rend() - 1));
2011-12-23 11:59:33 +00:00
// crend
TESTSPR_BOTH_ASSERT(carr[0] == *(arr1.crend() - 1));
TESTSPR_BOTH_ASSERT(carr[0] == *(arr2.crend() - 1));
2011-12-23 11:59:33 +00:00
// size
TESTSPR_BOTH_ASSERT(arr1.size() == 10);
TESTSPR_BOTH_ASSERT(arr2.size() == 10);
2011-12-23 11:59:33 +00:00
// empty
TESTSPR_BOTH_ASSERT(!arr1.empty());
TESTSPR_BOTH_ASSERT(!arr2.empty());
TESTSPR_BOTH_ASSERT((array<int, 0>{{}}.empty()));
2011-12-23 11:59:33 +00:00
// max_size
TESTSPR_BOTH_ASSERT(arr1.max_size() == 10);
TESTSPR_BOTH_ASSERT(arr2.max_size() == 10);
2011-12-23 11:59:33 +00:00
// operator[]
TESTSPR_BOTH_ASSERT(carr[0] == arr1[0]);
TESTSPR_BOTH_ASSERT(carr[0] == arr2[0]);
2011-12-23 11:59:33 +00:00
// at
TESTSPR_BOTH_ASSERT(carr[0] == arr1.at(0));
TESTSPR_BOTH_ASSERT(carr[0] == arr2.at(0));
2011-12-23 11:59:33 +00:00
// front
TESTSPR_BOTH_ASSERT(carr[0] == arr1.front());
TESTSPR_BOTH_ASSERT(carr[0] == arr2.front());
2011-12-23 11:59:33 +00:00
// back
TESTSPR_BOTH_ASSERT(carr[9] == arr1.back());
TESTSPR_BOTH_ASSERT(carr[9] == arr2.back());
2011-12-23 11:59:33 +00:00
// data
TESTSPR_BOTH_ASSERT(carr[0] == *arr1.data());
TESTSPR_BOTH_ASSERT(carr[0] == *arr2.data());
2011-12-23 11:59:33 +00:00
// c_array
{
auto arr = arr1;
TESTSPR_ASSERT(carr[0] == *arr.c_array());
}
// assign
TESTSPR_BOTH_ASSERT(testspr::equal(arr1.assign(-1), array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
2011-12-23 11:59:33 +00:00
{
auto arr = arr1;
arr.assign(-1);
TESTSPR_ASSERT(testspr::equal(arr, array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
2011-12-23 11:59:33 +00:00
}
// fill
TESTSPR_BOTH_ASSERT(testspr::equal(arr1.fill(-1), array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
2011-12-23 11:59:33 +00:00
{
auto arr = arr1;
arr.fill(-1);
TESTSPR_ASSERT(testspr::equal(arr, array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
2011-12-23 11:59:33 +00:00
}
// swap
{
auto arr1 = array<int, 2>{{1, 2}};
auto arr2 = array<int, 2>{{-1, -2}};
arr1.swap(arr2);
TESTSPR_ASSERT(arr1[0] == -1);
}
// operator=
{
auto arr1 = array<int, 2>{{1, 2}};
arr1 = array<int, 2>{{-1, -2}};
TESTSPR_ASSERT(arr1[0] == -1);
}
// operator==
TESTSPR_BOTH_ASSERT(arr1 == arr2);
2011-12-23 11:59:33 +00:00
// operator!=
TESTSPR_BOTH_ASSERT(!(arr1 != arr2));
2011-12-23 11:59:33 +00:00
// operator<
TESTSPR_BOTH_ASSERT(!(arr1 < arr2));
2011-12-23 11:59:33 +00:00
// operator>
TESTSPR_BOTH_ASSERT(!(arr1 > arr2));
2011-12-23 11:59:33 +00:00
// operator<=
TESTSPR_BOTH_ASSERT(arr1 <= arr2);
2011-12-23 11:59:33 +00:00
// operator>=
TESTSPR_BOTH_ASSERT(arr1 >= arr2);
2011-12-23 11:59:33 +00:00
}
}
} // namespace testspr
#ifndef TESTSPR_CPP_INCLUDE
# define TESTSPR_TEST_FUNCTION testspr::array_test
# include <testspr/include_main.hpp>
#endif
2011-12-23 11:59:33 +00:00
#endif // #ifndef SPROUT_LIBS_ARRAY_TEST_ARRAY_CPP