libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
enum_tags.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2020 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/enum_as_char_array.hpp"
25 
26 using namespace libjmmcg;
27 using namespace enum_tags::mpl;
28 
30  e4=to_tag<'1', '2', '3', '4'>::value,
31  e3=to_tag<'3', '1', '4', '2'>::value,
32  e2=to_tag<'3', '1', '0', '0'>::value,
33  e1=to_tag<'3', '0', '0', '0'>::value
34 };
35 
36 static char const str3000[]="3000";
37 static char const str1234[]="1234";
38 static std::array<char, sizeof(enum_tags_as_strs)> str3142={'3', '1', '4', '2'};
39 
40 BOOST_AUTO_TEST_SUITE(enum_as_char_array)
41 
42 BOOST_AUTO_TEST_CASE(dynamic_string_literal_conversiom) {
43  // Slowest.
44  BOOST_CHECK_EQUAL(str3000, std::string(to_array<enum_tags_as_strs, enum_tags_as_strs::e1>::value));
45  // Slow.
46  BOOST_CHECK(strcmp(str3000, to_array<enum_tags_as_strs, enum_tags_as_strs::e1>::value)==0);
47  const auto t3=enum_tags::convert<enum_tags_as_strs>(str3000);
48  // Better...
49  BOOST_CHECK_EQUAL(static_cast<std::uint64_t>(t3), static_cast<std::uint64_t>(enum_tags_as_strs::e1));
50  // Best!
51  BOOST_CHECK(t3==enum_tags_as_strs::e1);
52  const auto t1234=enum_tags::convert<enum_tags_as_strs>(str1234);
53  BOOST_CHECK(t1234==enum_tags_as_strs::e4);
54  const auto t3142=enum_tags::convert<enum_tags_as_strs>(str3142);
55  // Super-good!
56  BOOST_CHECK(t3142==enum_tags_as_strs::e3);
57 }
58 
59 BOOST_AUTO_TEST_SUITE_END()