mirror of
https://github.com/boostorg/fusion.git
synced 2025-07-20 15:52:13 +02:00
Added more map tests
[SVN r82712]
This commit is contained in:
@ -89,6 +89,9 @@ project
|
|||||||
[ run sequence/make_list.cpp : : : : ]
|
[ run sequence/make_list.cpp : : : : ]
|
||||||
[ run sequence/make_vector.cpp : : : : ]
|
[ run sequence/make_vector.cpp : : : : ]
|
||||||
[ run sequence/map.cpp : : : : ]
|
[ run sequence/map.cpp : : : : ]
|
||||||
|
[ run sequence/map_compare.cpp : : : : ]
|
||||||
|
[ run sequence/map_construction.cpp : : : : ]
|
||||||
|
[ run sequence/map_copy.cpp : : : : ]
|
||||||
[ run sequence/map_tie.cpp : : : : ]
|
[ run sequence/map_tie.cpp : : : : ]
|
||||||
[ run sequence/nview.cpp : : : : ]
|
[ run sequence/nview.cpp : : : : ]
|
||||||
[ run sequence/reverse_view.cpp : : : : ]
|
[ run sequence/reverse_view.cpp : : : : ]
|
||||||
|
68
test/sequence/map_comparison.cpp
Normal file
68
test/sequence/map_comparison.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*=============================================================================
|
||||||
|
Copyright (c) 1999-2003 Jaakko Jarvi
|
||||||
|
Copyright (c) 2001-2011 Joel de Guzman
|
||||||
|
Copyright (c) 2006 Dan Marsden
|
||||||
|
|
||||||
|
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)
|
||||||
|
==============================================================================*/
|
||||||
|
#include <boost/fusion/container/map/map.hpp>
|
||||||
|
#include <boost/detail/lightweight_test.hpp>
|
||||||
|
#include <boost/fusion/sequence/comparison.hpp>
|
||||||
|
|
||||||
|
struct key1 {};
|
||||||
|
struct key2 {};
|
||||||
|
struct key3 {};
|
||||||
|
|
||||||
|
void
|
||||||
|
equality_test()
|
||||||
|
{
|
||||||
|
using namespace boost::fusion;
|
||||||
|
|
||||||
|
map<pair<key1, int>, pair<key2, char> > v1(5, 'a');
|
||||||
|
map<pair<key1, int>, pair<key2, char> > v2(5, 'a');
|
||||||
|
BOOST_TEST(v1 == v2);
|
||||||
|
|
||||||
|
map<pair<key1, int>, pair<key2, char> > v3(5, 'b');
|
||||||
|
map<pair<key1, int>, pair<key2, char> > t4(2, 'a');
|
||||||
|
BOOST_TEST(v1 != v3);
|
||||||
|
BOOST_TEST(v1 != t4);
|
||||||
|
BOOST_TEST(!(v1 != v2));
|
||||||
|
|
||||||
|
map<pair<key1, int>, pair<key2, char>, pair<key2, bool> > v5(5, 'a', true);
|
||||||
|
BOOST_TEST(v1 != v5);
|
||||||
|
BOOST_TEST(!(v1 == v5));
|
||||||
|
BOOST_TEST(v5 != v1);
|
||||||
|
BOOST_TEST(!(v5 == v1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ordering_test()
|
||||||
|
{
|
||||||
|
using namespace boost::fusion;
|
||||||
|
|
||||||
|
map<pair<key1, int>, pair<key2, float> > v1(4, 3.3f);
|
||||||
|
map<pair<key1, short>, pair<key2, float> > v2(5, 3.3f);
|
||||||
|
map<pair<key1, long>, pair<key2, double> > v3(5, 4.4);
|
||||||
|
BOOST_TEST(v1 < v2);
|
||||||
|
BOOST_TEST(v1 <= v2);
|
||||||
|
BOOST_TEST(v2 > v1);
|
||||||
|
BOOST_TEST(v2 >= v1);
|
||||||
|
BOOST_TEST(v2 < v3);
|
||||||
|
BOOST_TEST(v2 <= v3);
|
||||||
|
BOOST_TEST(v3 > v2);
|
||||||
|
BOOST_TEST(v3 >= v2);
|
||||||
|
|
||||||
|
#if defined(FUSION_TEST_FAIL)
|
||||||
|
map<int, char, bool> v5(5, 'a', true);
|
||||||
|
v1 >= v5;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
equality_test();
|
||||||
|
ordering_test();
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
131
test/sequence/map_construction.cpp
Normal file
131
test/sequence/map_construction.cpp
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
/*=============================================================================
|
||||||
|
Copyright (c) 1999-2003 Jaakko Jarvi
|
||||||
|
Copyright (c) 2001-2011 Joel de Guzman
|
||||||
|
Copyright (c) 2006 Dan Marsden
|
||||||
|
|
||||||
|
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)
|
||||||
|
==============================================================================*/
|
||||||
|
#include <boost/fusion/container/map/map.hpp>
|
||||||
|
#include <boost/detail/lightweight_test.hpp>
|
||||||
|
#include <boost/fusion/sequence/intrinsic/at.hpp>
|
||||||
|
|
||||||
|
struct key1 {};
|
||||||
|
struct key2 {};
|
||||||
|
struct key3 {};
|
||||||
|
|
||||||
|
namespace test_detail
|
||||||
|
{
|
||||||
|
// something to prevent warnings for unused variables
|
||||||
|
template<class T> void dummy(const T&) {}
|
||||||
|
|
||||||
|
// no public default constructor
|
||||||
|
class foo
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit foo(int v) : val(v) {}
|
||||||
|
|
||||||
|
bool operator==(const foo& other) const
|
||||||
|
{
|
||||||
|
return val == other.val;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
foo() {}
|
||||||
|
int val;
|
||||||
|
};
|
||||||
|
|
||||||
|
// another class without a public default constructor
|
||||||
|
class no_def_constructor
|
||||||
|
{
|
||||||
|
no_def_constructor() {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
no_def_constructor(std::string) {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void
|
||||||
|
test()
|
||||||
|
{
|
||||||
|
using namespace boost::fusion;
|
||||||
|
using namespace test_detail;
|
||||||
|
|
||||||
|
nil empty;
|
||||||
|
|
||||||
|
map<> empty0;
|
||||||
|
|
||||||
|
#ifndef NO_CONSTRUCT_FROM_NIL
|
||||||
|
map<> empty1(empty);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
map<pair<key1, int> > t1;
|
||||||
|
BOOST_TEST(at_c<0>(t1).second == int());
|
||||||
|
|
||||||
|
map<pair<key1, float> > t2(5.5f);
|
||||||
|
BOOST_TEST(at_c<0>(t2).second > 5.4f && at_c<0>(t2).second < 5.6f);
|
||||||
|
|
||||||
|
map<pair<key1, foo> > t3(foo(12));
|
||||||
|
BOOST_TEST(at_c<0>(t3).second == foo(12));
|
||||||
|
|
||||||
|
map<pair<key1, double> > t4(t2);
|
||||||
|
BOOST_TEST(at_c<0>(t4).second > 5.4 && at_c<0>(t4).second < 5.6);
|
||||||
|
|
||||||
|
map<pair<key1, int>, pair<key2, float> > t5;
|
||||||
|
BOOST_TEST(at_c<0>(t5).second == int());
|
||||||
|
BOOST_TEST(at_c<1>(t5).second == float());
|
||||||
|
|
||||||
|
map<pair<key1, int>, pair<key2, float> > t6(12, 5.5f);
|
||||||
|
BOOST_TEST(at_c<0>(t6).second == 12);
|
||||||
|
BOOST_TEST(at_c<1>(t6).second > 5.4f && at_c<1>(t6).second < 5.6f);
|
||||||
|
|
||||||
|
map<pair<key1, int>, pair<key2, float> > t7(t6);
|
||||||
|
BOOST_TEST(at_c<0>(t7).second == 12);
|
||||||
|
BOOST_TEST(at_c<1>(t7).second > 5.4f && at_c<1>(t7).second < 5.6f);
|
||||||
|
|
||||||
|
map<pair<key1, long>, pair<key2, double> > t8(t6);
|
||||||
|
BOOST_TEST(at_c<0>(t8).second == 12);
|
||||||
|
BOOST_TEST(at_c<1>(t8).second > 5.4f && at_c<1>(t8).second < 5.6f);
|
||||||
|
|
||||||
|
dummy
|
||||||
|
(
|
||||||
|
map<
|
||||||
|
pair<key1, no_def_constructor>,
|
||||||
|
pair<key2, no_def_constructor>,
|
||||||
|
pair<key3, no_def_constructor> >
|
||||||
|
(
|
||||||
|
pair<key1, no_def_constructor>(std::string("Jaba")), // ok, since the default
|
||||||
|
pair<key2, no_def_constructor>(std::string("Daba")), // constructor is not used
|
||||||
|
pair<key3, no_def_constructor>(std::string("Doo"))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
dummy(map<pair<key1, int>, pair<key2, double> >());
|
||||||
|
dummy(map<pair<key1, int>, pair<key2, double> >(1,3.14));
|
||||||
|
|
||||||
|
#if defined(FUSION_TEST_FAIL)
|
||||||
|
dummy(map<pair<key1, double&> >()); // should fail, no defaults for references
|
||||||
|
dummy(map<pair<key1, const double&> >()); // likewise
|
||||||
|
#endif
|
||||||
|
|
||||||
|
{
|
||||||
|
double dd = 5;
|
||||||
|
dummy(map<pair<key1, double&> >(pair<key1, double&>(dd))); // ok
|
||||||
|
dummy(map<pair<key1, const double&> >(pair<key1, const double&>(dd+3.14))); // ok, but dangerous
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(FUSION_TEST_FAIL)
|
||||||
|
dummy(map<pair<key1, double&> >(dd+3.14)); // should fail,
|
||||||
|
// temporary to non-const reference
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
test();
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
90
test/sequence/map_copy.cpp
Normal file
90
test/sequence/map_copy.cpp
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/*=============================================================================
|
||||||
|
Copyright (c) 1999-2003 Jaakko Jarvi
|
||||||
|
Copyright (c) 2001-2011 Joel de Guzman
|
||||||
|
Copyright (c) 2006 Dan Marsden
|
||||||
|
|
||||||
|
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)
|
||||||
|
==============================================================================*/
|
||||||
|
#include <boost/fusion/container/map/map.hpp>
|
||||||
|
#include <boost/fusion/container/generation/make_map.hpp>
|
||||||
|
#include <boost/fusion/container/generation/map_tie.hpp>
|
||||||
|
#include <boost/detail/lightweight_test.hpp>
|
||||||
|
#include <boost/fusion/sequence/intrinsic/at.hpp>
|
||||||
|
#include <boost/fusion/mpl.hpp>
|
||||||
|
#include <boost/preprocessor/cat.hpp>
|
||||||
|
#include <boost/mpl/insert_range.hpp>
|
||||||
|
#include <boost/mpl/vector.hpp>
|
||||||
|
#include <boost/mpl/begin.hpp>
|
||||||
|
#include <boost/mpl/equal.hpp>
|
||||||
|
#include <boost/static_assert.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct k1 {};
|
||||||
|
struct k2 {};
|
||||||
|
struct k3 {};
|
||||||
|
struct k4 {};
|
||||||
|
|
||||||
|
namespace test_detail
|
||||||
|
{
|
||||||
|
// classes with different kinds of conversions
|
||||||
|
class AA {};
|
||||||
|
class BB : public AA {};
|
||||||
|
struct CC { CC() {} CC(const BB&) {} };
|
||||||
|
struct DD { operator CC() const { return CC(); }; };
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::fusion::map<
|
||||||
|
boost::fusion::pair<k1, double>,
|
||||||
|
boost::fusion::pair<k2, double>,
|
||||||
|
boost::fusion::pair<k3, double>,
|
||||||
|
boost::fusion::pair<k4, double>
|
||||||
|
>
|
||||||
|
foo(int i)
|
||||||
|
{
|
||||||
|
return boost::fusion::make_map<k1, k2, k3, k4>(i, i+1, i+2, i+3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
test()
|
||||||
|
{
|
||||||
|
using namespace boost::fusion;
|
||||||
|
using namespace test_detail;
|
||||||
|
|
||||||
|
map<pair<k1, int>, pair<k2, char> > t1(4, 'a');
|
||||||
|
map<pair<k1, int>, pair<k2, char> > t2(5, 'b');
|
||||||
|
t2 = t1;
|
||||||
|
BOOST_TEST(at_c<0>(t1).second == at_c<0>(t2).second);
|
||||||
|
BOOST_TEST(at_c<1>(t1).second == at_c<1>(t2).second);
|
||||||
|
|
||||||
|
map<pair<k1, int>, pair<k2, char const*> > t4(4, "a");
|
||||||
|
map<pair<k1, long>, pair<k2, std::string> > t3(2, std::string("a"));
|
||||||
|
t3 = t4;
|
||||||
|
BOOST_TEST((double)at_c<0>(t4).second == at_c<0>(t3).second);
|
||||||
|
BOOST_TEST(at_c<1>(t4).second == at_c<1>(t3).second);
|
||||||
|
|
||||||
|
// testing copy and assignment with implicit conversions
|
||||||
|
// between elements testing tie
|
||||||
|
|
||||||
|
map<pair<k1, char>, pair<k2, BB*>, pair<k3, BB>, pair<k4, DD> > t;
|
||||||
|
map<pair<k1, int>, pair<k2, AA*>, pair<k3, CC>, pair<k4, CC> > a(t);
|
||||||
|
a = t;
|
||||||
|
|
||||||
|
int i; char c; double d;
|
||||||
|
map_tie<k1, k2, k3>(i, c, d) = make_map<k1, k2, k3>(1, 'a', 5.5);
|
||||||
|
|
||||||
|
BOOST_TEST(i==1);
|
||||||
|
BOOST_TEST(c=='a');
|
||||||
|
BOOST_TEST(d>5.4 && d<5.6);
|
||||||
|
|
||||||
|
// returning a map with conversion
|
||||||
|
foo(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
test();
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
||||||
|
|
Reference in New Issue
Block a user