Iguana LATEST
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
TestConfig.h
1// test configuration
2
3#include <cassert>
4#include <iguana/algorithms/Algorithm.h>
5
6inline int TestConfig(int test_num, std::string log_level)
7{
8 if(test_num == 0) {
9 fmt::print(stderr, "ERROR: need a test number\n");
10 return 1;
11 }
12 // first, some sanity checks of `ConfigFileReader`
13 iguana::ConfigFileReader bad_config("bad_config");
14 bad_config.AddDirectory("non_existent_directory");
15 try {
16 bad_config.AddFile("non_existent_file.yaml");
17 }
18 catch(std::exception const& ex) {
19 fmt::print("excpected exception thrown when trying to add non-existent file\n");
20 }
21 bad_config.PrintDirectories();
22
23 // then test configuring an algorithm
24 auto algo = iguana::AlgorithmFactory::Create("example::ExampleAlgorithm");
25 algo->SetLogLevel(log_level);
26 algo->SetConfigDirectory("src/iguana/tests"); // must be relative to build directory
27 algo->SetConfigFile(fmt::format("test_{}.yaml", test_num));
28 algo->Start();
29
30 switch(test_num) {
31
32 case 1: {
33 // test `GetOptionScalar`
34 assert((algo->GetOptionScalar<int>({"scalar_int"}) == 1));
35 assert((algo->GetOptionScalar<double>({"scalar_double"}) == 2.5));
36 assert((algo->GetOptionScalar<std::string>({"scalar_string"}) == "lizard"));
37 // test `GetOptionVector`
38 assert((algo->GetOptionVector<int>({"vector_int"}) == std::vector<int>{1, 2, 3}));
39 assert((algo->GetOptionVector<double>({"vector_double"}) == std::vector<double>{1.5, 2.5}));
40 assert((algo->GetOptionVector<std::string>({"vector_string"}) == std::vector<std::string>{"spider", "bat", "chameleon", "spider"}));
41 // test `GetOptionSet`
42 auto s = algo->GetOptionSet<std::string>({"vector_string"});
43 assert((s.size() == 3));
44 assert((s.find("spider") != s.end()));
45 assert((s.find("bee") == s.end()));
46 // test empty access - expect exceptions, so just catch them and do nothing
47 try {
48 algo->GetOptionScalar<int>({"scalar_empty"});
49 fmt::print(stderr, "ERROR: accessing 'scalar_empty' did not throw exception\n");
50 return 1;
51 }
52 catch(std::exception const& ex) {
53 fmt::print("SUCCESS: accessing 'scalar_empty' threw an expected exception\n");
54 }
55 try {
56 algo->GetOptionVector<int>({"vector_empty"});
57 fmt::print(stderr, "ERROR: accessing 'vector_empty' did not throw exception\n");
58 return 1;
59 }
60 catch(std::exception const& ex) {
61 fmt::print("SUCCESS: accessing 'vector_empty' threw an expected exception\n");
62 }
63 try {
64 algo->GetOptionSet<int>({"vector_empty"});
65 fmt::print(stderr, "ERROR: accessing 'vector_empty' as a `set` did not throw exception\n");
66 return 1;
67 }
68 catch(std::exception const& ex) {
69 fmt::print("SUCCESS: accessing 'vector_empty' as a `set` threw an expected exception\n");
70 }
71 // test access to a key that does not exist
72 try {
73 algo->GetOptionScalar<int>({"non_existent_scalar"});
74 fmt::print(stderr, "ERROR: accessing 'non_existent_scalar' did not throw exception\n");
75 return 1;
76 }
77 catch(std::exception const& ex) {
78 fmt::print("SUCCESS: accessing 'non_existent_scalar' threw an expected exception\n");
79 }
80 try {
81 algo->GetOptionVector<int>({"non_existent_vector"});
82 fmt::print(stderr, "ERROR: accessing 'non_existent_vector' did not throw exception\n");
83 return 1;
84 }
85 catch(std::exception const& ex) {
86 fmt::print("SUCCESS: accessing 'non_existent_vector' threw an expected exception\n");
87 }
88 try {
89 algo->GetOptionSet<int>({"non_existent_vector"});
90 fmt::print(stderr, "ERROR: accessing 'non_existent_vector' as a `set` did not throw exception\n");
91 return 1;
92 }
93 catch(std::exception const& ex) {
94 fmt::print("SUCCESS: accessing 'non_existent_vector' as a `set` threw an expected exception\n");
95 }
96 break;
97 }
98
99 case 2: {
100 assert((algo->GetOptionScalar<double>({"tree1", "scalar1"}) == 1.5));
101 assert((algo->GetOptionScalar<double>({"tree1", "scalar2"}) == 2.5));
102 assert((algo->GetOptionScalar<double>({"tree2", "tree1", "scalar1"}) == 3.5));
103 assert((algo->GetOptionVector<std::string>({"tree2", "tree2", "tree3", "vector1"}) == std::vector<std::string>{"gecko", "snake"}));
104 assert((algo->GetOptionVector<int>({"tree2", "vector2"}) == std::vector<int>{3, -4, 5}));
105 assert((algo->GetOptionVector<std::string>({"vector1"}) == std::vector<std::string>{"bee"}));
106 break;
107 }
108
109 case 3: {
110 // test InRange tree1
111 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 1), "val"}) == 3));
112 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 3), "val"}) == 3));
113 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 5), "val"}) == 3)); // at a border
114 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 6), "val"}) == 4));
115 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 10), "val"}) == 4));
116 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 11), "val"}) == 0)); // default fallback
117 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 10.1), "val"}) == 0)); // wrong type
118 assert((algo->GetOptionScalar<int>({"tree1", algo->GetConfig()->InRange("test_range", 3.7), "val"}) == 3)); // wrong type
119 // test InRange tree2
120 assert((algo->GetOptionScalar<std::string>({"tree2", algo->GetConfig()->InRange("test_range", 1.9), "subtree", "lizard"}) == "iguana"));
121 assert((algo->GetOptionScalar<int>({"tree2", algo->GetConfig()->InRange("test_range", 1.9), "subtree", "number"}) == 7));
122 assert((algo->GetOptionScalar<int>({"tree2", algo->GetConfig()->InRange("test_range", 3.0), "subtree", algo->GetConfig()->InRange("sub_range", 1), "val"}) == 7));
123 assert((algo->GetOptionScalar<int>({"tree2", algo->GetConfig()->InRange("test_range", 3.0), "subtree", algo->GetConfig()->InRange("sub_range", 8), "val"}) == 8));
124 assert((algo->GetOptionScalar<int>({"tree2", algo->GetConfig()->InRange("test_range", 3.5), "subtree", algo->GetConfig()->InRange("sub_range", 11), "val"}) == 1));
125 assert((algo->GetOptionScalar<int>({"tree2", algo->GetConfig()->InRange("test_range", 4.0), "subtree"}) == 10));
126 // test InRange tree3
127 assert((algo->GetOptionScalar<int>({"tree3", algo->GetConfig()->InRange("test_range", 3), "val"}) == 3));
128 try {
129 assert((algo->GetOptionScalar<int>({"tree3", algo->GetConfig()->InRange("test_range", 11), "val"}) == 0));
130 fmt::print(stderr, "ERROR: accessing a missing default value for `InRange` did not throw exception\n");
131 return 1;
132 }
133 catch(std::exception const& ex) {
134 fmt::print("SUCCESS: accessing a missing default value for `InRange` threw expected exception\n");
135 }
136 break;
137 }
138
139 default:
140 fmt::print(stderr, "ERROR: unknown test number '{}'", test_num);
141 return 1;
142 }
143 return 0;
144}
static algo_t Create(std::string const &algo_name) noexcept(false)
Configuration file manager.