From aba201eb4e1042d9582a740179908746b61052d1 Mon Sep 17 00:00:00 2001 From: Joel de Guzman Date: Mon, 4 Feb 2013 06:20:10 +0000 Subject: [PATCH] Added more map tests [SVN r82712] --- test/Jamfile | 3 + test/sequence/map_comparison.cpp | 68 +++++++++++++++ test/sequence/map_construction.cpp | 131 +++++++++++++++++++++++++++++ test/sequence/map_copy.cpp | 90 ++++++++++++++++++++ 4 files changed, 292 insertions(+) create mode 100644 test/sequence/map_comparison.cpp create mode 100644 test/sequence/map_construction.cpp create mode 100644 test/sequence/map_copy.cpp diff --git a/test/Jamfile b/test/Jamfile index f23b0c2a..5f6380d1 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -89,6 +89,9 @@ project [ run sequence/make_list.cpp : : : : ] [ run sequence/make_vector.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/nview.cpp : : : : ] [ run sequence/reverse_view.cpp : : : : ] diff --git a/test/sequence/map_comparison.cpp b/test/sequence/map_comparison.cpp new file mode 100644 index 00000000..63f39515 --- /dev/null +++ b/test/sequence/map_comparison.cpp @@ -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 +#include +#include + +struct key1 {}; +struct key2 {}; +struct key3 {}; + +void +equality_test() +{ + using namespace boost::fusion; + + map, pair > v1(5, 'a'); + map, pair > v2(5, 'a'); + BOOST_TEST(v1 == v2); + + map, pair > v3(5, 'b'); + map, pair > t4(2, 'a'); + BOOST_TEST(v1 != v3); + BOOST_TEST(v1 != t4); + BOOST_TEST(!(v1 != v2)); + + map, pair, pair > 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 > v1(4, 3.3f); + map, pair > v2(5, 3.3f); + map, pair > 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 v5(5, 'a', true); + v1 >= v5; +#endif +} + +int +main() +{ + equality_test(); + ordering_test(); + return boost::report_errors(); +} diff --git a/test/sequence/map_construction.cpp b/test/sequence/map_construction.cpp new file mode 100644 index 00000000..ee308a13 --- /dev/null +++ b/test/sequence/map_construction.cpp @@ -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 +#include +#include + +struct key1 {}; +struct key2 {}; +struct key3 {}; + +namespace test_detail +{ + // something to prevent warnings for unused variables + template 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 > t1; + BOOST_TEST(at_c<0>(t1).second == int()); + + map > t2(5.5f); + BOOST_TEST(at_c<0>(t2).second > 5.4f && at_c<0>(t2).second < 5.6f); + + map > t3(foo(12)); + BOOST_TEST(at_c<0>(t3).second == foo(12)); + + map > t4(t2); + BOOST_TEST(at_c<0>(t4).second > 5.4 && at_c<0>(t4).second < 5.6); + + map, pair > t5; + BOOST_TEST(at_c<0>(t5).second == int()); + BOOST_TEST(at_c<1>(t5).second == float()); + + map, pair > 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 > 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 > 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, + pair, + pair > + ( + pair(std::string("Jaba")), // ok, since the default + pair(std::string("Daba")), // constructor is not used + pair(std::string("Doo")) + ) + ); + + dummy(map, pair >()); + dummy(map, pair >(1,3.14)); + +#if defined(FUSION_TEST_FAIL) + dummy(map >()); // should fail, no defaults for references + dummy(map >()); // likewise +#endif + + { + double dd = 5; + dummy(map >(pair(dd))); // ok + dummy(map >(pair(dd+3.14))); // ok, but dangerous + } + +#if defined(FUSION_TEST_FAIL) + dummy(map >(dd+3.14)); // should fail, + // temporary to non-const reference +#endif +} + +int +main() +{ + test(); + return boost::report_errors(); +} diff --git a/test/sequence/map_copy.cpp b/test/sequence/map_copy.cpp new file mode 100644 index 00000000..b8218bdf --- /dev/null +++ b/test/sequence/map_copy.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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, + boost::fusion::pair, + boost::fusion::pair, + boost::fusion::pair +> +foo(int i) +{ + return boost::fusion::make_map(i, i+1, i+2, i+3); +} + +void +test() +{ + using namespace boost::fusion; + using namespace test_detail; + + map, pair > t1(4, 'a'); + map, pair > 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 > t4(4, "a"); + map, pair > 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, pair, pair > t; + map, pair, pair, pair > a(t); + a = t; + + int i; char c; double d; + map_tie(i, c, d) = make_map(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(); +} +