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