libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
unordered_tuple.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2017 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include "core/unordered_tuple.hpp"
25 
26 using namespace libjmmcg;
27 
28 struct base {
29  using key_type=std::int32_t;
30 
31  struct hasher {
32  constexpr key_type operator()(key_type k) const noexcept(true) {
33  return k;
34  }
35  constexpr key_type operator()(key_type k, std::size_t) const noexcept(true) {
36  return k;
37  }
38  };
39 
40  virtual int fn(int j) const=0;
41 
42 protected:
43  virtual ~base()=default;
44 };
45 struct int1 final : base {
46  static inline constexpr base::key_type hash=1;
47 
48  const int i_;
49 
50  explicit int1(int i)
51  : i_(i) {}
52 
53  int fn(int j) const override {
54  return i_*j;
55  }
56 };
57 struct int2 final : base {
58  static inline constexpr base::key_type hash=2;
59 
60  const int i_;
61 
62  explicit int2(int i)
63  : i_(i) {}
64 
65  int fn(int j) const override {
66  return i_+j;
67  }
68 };
69 struct double1 final : base {
70  static inline constexpr base::key_type hash=3;
71 
72  const double i_;
73 
74  explicit double1(double i)
75  : i_(i) {}
76 
77  int fn(int j) const override {
78  return static_cast<int>(i_+j);
79  }
80 };
81 
82 template<class T>
83 struct extract {
84  static constexpr const typename T::key_type value=T::hash;
85 };
86 
87 BOOST_AUTO_TEST_SUITE(unordered_tuple_tests)
88 
89 BOOST_AUTO_TEST_CASE(ctor) {
90  using collection_type=unordered_tuple<base::key_type, base, base::hasher, extract, int1, int2, double1>;
91 
92  BOOST_CHECK_NO_THROW(collection_type colln(int1(667), int2(42), double1(68.999999)));
93 }
94 
95 BOOST_AUTO_TEST_CASE(access_in_order) {
96  using collection_type=unordered_tuple<base::key_type, base, base::hasher, extract, int1, int2, double1>;
97 
98  collection_type colln(int1(667), int2(42), double1(68.999999));
99  BOOST_CHECK_EQUAL(colln[int1::hash].fn(1), 667);
100  BOOST_CHECK_EQUAL(colln[int2::hash].fn(13), 55);
101  BOOST_CHECK_EQUAL(colln[double1::hash].fn(7), 75);
102 }
103 
104 BOOST_AUTO_TEST_CASE(access_reverse_order) {
105  using collection_type=unordered_tuple<base::key_type, base, base::hasher, extract, int1, int2, double1>;
106 
107  collection_type colln(int1(667), int2(42), double1(68.999999));
108  BOOST_CHECK_EQUAL(colln[double1::hash].fn(7), 75);
109  BOOST_CHECK_EQUAL(colln[int2::hash].fn(13), 55);
110  BOOST_CHECK_EQUAL(colln[int1::hash].fn(1), 667);
111 }
112 
113 BOOST_AUTO_TEST_CASE(access_in_then_reverse_order) {
114  using collection_type=unordered_tuple<base::key_type, base, base::hasher, extract, int1, int2, double1>;
115 
116  collection_type colln(int1(667), int2(42), double1(68.999999));
117  BOOST_CHECK_EQUAL(colln[int1::hash].fn(1), 667);
118  BOOST_CHECK_EQUAL(colln[int2::hash].fn(13), 55);
119  BOOST_CHECK_EQUAL(colln[double1::hash].fn(7), 75);
120  BOOST_CHECK_EQUAL(colln[double1::hash].fn(7), 75);
121  BOOST_CHECK_EQUAL(colln[int2::hash].fn(13), 55);
122  BOOST_CHECK_EQUAL(colln[int1::hash].fn(1), 667);
123 }
124 
125 BOOST_AUTO_TEST_CASE(access_reverse_then_in_order) {
126  using collection_type=unordered_tuple<base::key_type, base, base::hasher, extract, int1, int2, double1>;
127 
128  collection_type colln(int1(667), int2(42), double1(68.999999));
129  BOOST_CHECK_EQUAL(colln[double1::hash].fn(7), 75);
130  BOOST_CHECK_EQUAL(colln[int2::hash].fn(13), 55);
131  BOOST_CHECK_EQUAL(colln[int1::hash].fn(1), 667);
132  BOOST_CHECK_EQUAL(colln[int1::hash].fn(1), 667);
133  BOOST_CHECK_EQUAL(colln[int2::hash].fn(13), 55);
134  BOOST_CHECK_EQUAL(colln[double1::hash].fn(7), 75);
135 }
136 
137 BOOST_AUTO_TEST_SUITE_END()