libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
constexpr_map.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2019 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 "frozen/unordered_map.h"
25 
26 using mapping_t=frozen::unordered_map<std::int16_t, char, 5>;
27 static constexpr mapping_t mapping={
28  {1, '1'},
29  {12, '1'},
30  {42, '2'},
31  {69, '3'},
32  {667, '4'}
33 };
34 
35 inline constexpr char
36 convert(std::int16_t v) noexcept(true) {
37  const auto iter=mapping.find(v);
38  if (iter!=mapping.end()) {
39  return iter->second;
40  } else {
41  return '\0';
42  }
43 }
44 
45 BOOST_AUTO_TEST_SUITE(constexp_map)
46 
47 BOOST_AUTO_TEST_CASE(lookup) {
48  constexpr char a=convert(12);
49  BOOST_CHECK_EQUAL(a, '1');
50 }
51 
52 BOOST_AUTO_TEST_SUITE_END()