root/examples/unordered_tuple.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. BOOST_AUTO_TEST_SUITE

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/examples/unordered_tuple.cpp 2292 2018-07-29 21:03:08Z jmmcg $
   3 **
   4 ** Copyright (c) 2017 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #include "stdafx.h"
  22 
  23 #define BOOST_TEST_MODULE libjmmcg_tests
  24 #include <boost/test/included/unit_test.hpp>
  25 
  26 #include "core/unordered_tuple.hpp"
  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 ~base()=default;
  41 
  42         virtual int fn(int j) const=0;
  43 };
  44 struct derived1 final : base {
  45         static constexpr base::key_type hash=1;
  46 
  47         const int i_;
  48 
  49         explicit derived1(int i)
  50         : i_(i) {}
  51 
  52         int fn(int j) const override {
  53                 return i_*j;
  54         }
  55 };
  56 constexpr base::key_type derived1::hash;
  57 struct derived2 final : base {
  58         static constexpr base::key_type hash=2;
  59 
  60         const int i_;
  61 
  62         explicit derived2(int i)
  63         : i_(i) {}
  64 
  65         int fn(int j) const override {
  66                 return i_+j;
  67         }
  68 };
  69 constexpr base::key_type derived2::hash;
  70 
  71 template<class T>
  72 struct extract {
  73         static constexpr const typename T::key_type value=T::hash;
  74 };
  75 
  76 BOOST_AUTO_TEST_SUITE(unordered_tuple_tests)
  77 
  78 BOOST_AUTO_TEST_CASE(unordered_tuple) {
  79         using collection_type=jmmcg::unordered_tuple<base::key_type, base, base::hasher, extract, derived1, derived2>;
  80 
  81         collection_type colln(derived1(667), derived2(42));
  82         BOOST_CHECK_EQUAL(colln[derived1::hash].fn(1), 667);
  83         BOOST_CHECK_EQUAL(colln[derived2::hash].fn(13), 55);
  84 }
  85 
  86 BOOST_AUTO_TEST_SUITE_END()

/* [<][>][^][v][top][bottom][index][help] */