MultipleChoiceSelection_test.cc
Go to the documentation of this file.
1 /**
2  * @file test/Utilities/MultipleChoiceSelection_test.cc
3  * @brief Unit test for `intervals_fhicl.h` header
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date November 27, 2019
6  * @see lardataalg/Utilities/intervals_fhicl.h
7  *
8  */
9 
10 // Boost libraries
11 #define BOOST_TEST_MODULE ( MultipleChoiceSelection_test )
12 #include <boost/test/unit_test.hpp>
13 
14 // LArSoft libraries
16 
17 // C/C++ standard libraries
18 #include <iostream>
19 #include <string>
20 
21 
22 // -----------------------------------------------------------------------------
24 
25  enum class Color { white, gray, black, blue };
26 
27  using OptionSelector_t = util::MultipleChoiceSelection<Color>;
28 
29  OptionSelector_t options {
30  { Color::black, "black" },
31  { Color::gray, "gray", "grey" }
32  };
33 
34  BOOST_TEST(options.size() == 2U);
35 
36  BOOST_CHECK_THROW(
37  options.addAlias(Color::white, "blanche"), // Color::white not yet added
38  OptionSelector_t::UnknownOptionError
39  );
40 
41  auto const& opWhite0 = options.addOption(Color::white, "white");
42  auto const& opWhite0again = options.addAlias(Color::white, "blanche");
43  BOOST_TEST(&opWhite0again == &opWhite0);
44  BOOST_TEST(options.size() == 3U);
45 
46  std::cout << "Options:\n" << options.optionListDump(" * ") << std::endl;
47 
48  BOOST_TEST( options.hasOption(Color::white));
49  BOOST_TEST( options.hasOption(Color::gray));
50  BOOST_TEST(!options.hasOption(Color::blue));
51  BOOST_TEST( options.hasOption("white"));
52  BOOST_TEST( options.hasOption("blanche"));
53  BOOST_TEST( options.hasOption("wHite"));
54  BOOST_TEST(!options.hasOption("blue"));
55 
56  //
57  // white
58  //
59  BOOST_TEST(options.hasOption(opWhite0));
60 
61  auto const opWhite1 = options.parse("white");
62  static_assert(std::is_same_v
63  <std::decay_t<decltype(opWhite1)>, OptionSelector_t::Option_t>
64  );
65  BOOST_TEST((opWhite1 == Color::white));
66  BOOST_TEST(opWhite1 == "white");
67  BOOST_TEST(opWhite1 == opWhite0);
68 
69  auto const opWhite2 = options.parse("blanche");
70  static_assert(std::is_same_v
71  <std::decay_t<decltype(opWhite2)>, OptionSelector_t::Option_t>
72  );
73  BOOST_TEST((opWhite2 == Color::white));
74  BOOST_TEST(opWhite2 == "white");
75  BOOST_TEST(opWhite2 == "blanche");
76  BOOST_TEST(opWhite2 == "Blanche");
77  BOOST_TEST(opWhite2 == opWhite0);
78 
79  auto const opWhite3 = options.get("white");
80  static_assert(std::is_same_v
81  <std::decay_t<decltype(opWhite1)>, OptionSelector_t::Option_t>
82  );
83  BOOST_TEST(opWhite3 == "white");
84 
85  //
86  // gray
87  //
88  auto const opGray0 = options.get("gray");
89  static_assert(std::is_same_v
90  <std::decay_t<decltype(opWhite1)>, OptionSelector_t::Option_t>
91  );
92  BOOST_TEST(opWhite0 == "white");
93 
94  auto const opGray1 = options.parse("gray");
95  static_assert(std::is_same_v
96  <std::decay_t<decltype(opWhite1)>, OptionSelector_t::Option_t>
97  );
98  BOOST_TEST((opGray1 == Color::gray));
99  BOOST_TEST(opGray1 == "gray");
100  BOOST_TEST(opGray1 == "grey");
101  BOOST_TEST(opGray1 == opGray0);
102 
103  Color color = opGray1;
104  BOOST_TEST((color == Color::gray));
105 
106  //
107  // blue
108  //
109  BOOST_CHECK_THROW(options.get("blue"), OptionSelector_t::UnknownOptionError);
110  BOOST_CHECK_THROW
111  (options.parse("blue"), OptionSelector_t::UnknownOptionError);
112 
113 } // MultipleChoiceSelection_testcase()
114 
115 
116 // -----------------------------------------------------------------------------
117 // BEGIN Test cases -----------------------------------------------------------
118 // -----------------------------------------------------------------------------
119 BOOST_AUTO_TEST_CASE(MultipleChoiceSelection_testcase) {
120 
122 
123 } // BOOST_AUTO_TEST_CASE(MultipleChoiceSelection_testcase)
124 
125 // -----------------------------------------------------------------------------
126 // END Test cases -------------------------------------------------------------
127 // -----------------------------------------------------------------------------
Helper to select an string option among a set of allowed choices.
Byte blue
Definition: image.cpp:32
Definition: image.cpp:28
BOOST_AUTO_TEST_CASE(MultipleChoiceSelection_testcase)
std::size_t color(std::string const &procname)
void MultipleChoiceSelection_test()
Helper to select one among multiple choices via strings.
QTextStream & endl(QTextStream &s)