Sprout/libs/array/test/array.cpp

187 lines
5.7 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#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());
2013-08-16 15:10:42 +00:00
{
auto arr = arr1;
TESTSPR_ASSERT(carr[0] == *arr.data());
}
2011-12-23 11:59:33 +00:00
// c_array
2013-08-16 15:10:42 +00:00
TESTSPR_BOTH_ASSERT(carr[0] == *arr1.c_array());
TESTSPR_BOTH_ASSERT(carr[0] == *arr2.c_array());
2011-12-23 11:59:33 +00:00
{
auto arr = arr1;
TESTSPR_ASSERT(carr[0] == *arr.c_array());
}
// assign
2013-02-04 16:02:59 +00:00
TESTSPR_BOTH_ASSERT(testspr::equal(arr1.assign(-1), sprout::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);
2013-02-04 16:02:59 +00:00
TESTSPR_ASSERT(testspr::equal(arr, sprout::array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
2011-12-23 11:59:33 +00:00
}
// fill
2013-02-04 16:02:59 +00:00
TESTSPR_BOTH_ASSERT(testspr::equal(arr1.fill(-1), sprout::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);
2013-02-04 16:02:59 +00:00
TESTSPR_ASSERT(testspr::equal(arr, sprout::array<int, 10>{{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}));
2011-12-23 11:59:33 +00:00
}
// swap
{
2013-02-04 16:02:59 +00:00
auto arr1 = sprout::array<int, 2>{{1, 2}};
auto arr2 = sprout::array<int, 2>{{-1, -2}};
2011-12-23 11:59:33 +00:00
arr1.swap(arr2);
TESTSPR_ASSERT(arr1[0] == -1);
}
// operator=
{
2013-02-04 16:02:59 +00:00
auto arr1 = sprout::array<int, 2>{{1, 2}};
arr1 = sprout::array<int, 2>{{-1, -2}};
2011-12-23 11:59:33 +00:00
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);
2013-02-04 16:02:59 +00:00
// get
TESTSPR_BOTH_ASSERT(sprout::tuples::get<0>(arr1) == 1);
TESTSPR_BOTH_ASSERT(sprout::tuples::get<1>(arr1) == 2);
{
auto arr4 = arr1;
TESTSPR_ASSERT(sprout::tuples::get<0>(arr4) == 1);
TESTSPR_ASSERT(sprout::tuples::get<1>(arr4) == 2);
}
// tuple_size
TESTSPR_BOTH_ASSERT(sprout::tuples::tuple_size<decltype(arr1)>::value == 10);
// tuple_element
TESTSPR_BOTH_ASSERT((std::is_same<sprout::tuples::tuple_element<0, decltype(arr1)>::type, int const>::value));
TESTSPR_BOTH_ASSERT((std::is_same<sprout::tuples::tuple_element<1, decltype(arr1)>::type, int const>::value));
// is_array
TESTSPR_BOTH_ASSERT(sprout::is_array_class<decltype(arr1)>::value);
TESTSPR_BOTH_ASSERT(!sprout::is_array_class<int>::value);
2013-02-04 16:02:59 +00:00
// sprout::to_hash, sprout::hash
TESTSPR_BOTH_ASSERT(sprout::to_hash(arr1) == sprout::hash<decltype(arr1)>()(arr1));
TESTSPR_BOTH_ASSERT(sprout::to_hash(arr1) != sprout::to_hash(sprout::array<int, 10>{{}}));
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