Files
geometry/test/static_vector.cpp
Adam Wulkiewicz 479c6a3bab Error fixed in static_vector::at().
Added test for static_vector.

[SVN r81958]
2012-12-14 22:35:46 +00:00

108 lines
2.6 KiB
C++

// Boost.Geometry Index
// Unit Test
// Copyright (c) 2011-2012 Adam Wulkiewicz, Lodz, Poland.
// Use, modification and distribution is subject to 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)
#include <boost/test/included/test_exec_monitor.hpp>
#include <boost/test/impl/execution_monitor.ipp>
#include <boost/geometry/extensions/index/static_vector.hpp>
using namespace boost::geometry::index;
class value_ndc
{
public:
value_ndc(int a) : aa(a) {}
bool operator==(value_ndc const& v) const { return aa == v.aa; }
private:
value_ndc(value_ndc const&) {}
value_ndc & operator=(value_ndc const&) { return *this; }
int aa;
};
class value_nd
{
public:
value_nd(int a) : aa(a) {}
bool operator==(value_nd const& v) const { return aa == v.aa; }
private:
int aa;
};
class value_nc
{
public:
value_nc(int a = 0) : aa(a) {}
bool operator==(value_nc const& v) const { return aa == v.aa; }
private:
value_nc(value_nc const&) {}
value_nc & operator=(value_ndc const&) { return *this; }
int aa;
};
template <typename T, size_t N>
void test_ctor_d()
{
static_vector<T, N> s;
BOOST_CHECK(s.size() == 0);
BOOST_CHECK(s.capacity() == N);
BOOST_CHECK_THROW( s.at(0), std::out_of_range );
}
template <typename T, size_t N>
void test_ctor_r(size_t n)
{
static_vector<T, N> s(n);
BOOST_CHECK(s.size() == n);
BOOST_CHECK(s.capacity() == N);
BOOST_CHECK_THROW( s.at(n), std::out_of_range );
if ( 1 < n )
{
s[0] = T(10);
BOOST_CHECK(T(10) == s[0]);
BOOST_CHECK(T(10) == s.at(0));
s.at(1) = T(20);
BOOST_CHECK(T(20) == s[1]);
BOOST_CHECK(T(20) == s.at(1));
}
}
template <typename T, size_t N>
void test_ctor_rv(size_t n, T const& v)
{
static_vector<T, N> s(n, v);
BOOST_CHECK(s.size() == n);
BOOST_CHECK(s.capacity() == N);
BOOST_CHECK_THROW( s.at(n), std::out_of_range );
if ( 1 < n )
{
BOOST_CHECK(v == s[0]);
BOOST_CHECK(v == s.at(0));
BOOST_CHECK(v == s[1]);
BOOST_CHECK(v == s.at(1));
s[0] = T(10);
BOOST_CHECK(T(10) == s[0]);
BOOST_CHECK(T(10) == s.at(0));
s.at(1) = T(20);
BOOST_CHECK(T(20) == s[1]);
BOOST_CHECK(T(20) == s.at(1));
}
}
int test_main(int, char* [])
{
test_ctor_d<int, 10>();
test_ctor_d<value_ndc, 10>();
test_ctor_r<int, 10>(5);
test_ctor_r<value_nc, 10>(5);
test_ctor_rv<int, 10>(5, 0);
test_ctor_rv<value_nd, 10>(5, 0);
return 0;
}