Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-04 11:19:43

0001 
0002 // Copyright 2020, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 
0006 #include <JANA/Services/JParameterManager.h>
0007 #include "JANA/Utils/JEventLevel.h"
0008 #include <string>
0009 #include <vector>
0010 #include <cstdio>
0011 #include <fstream>
0012 #include "catch.hpp"
0013 
0014 TEST_CASE("JParameterManager::SetDefaultParameter") {
0015 
0016     JParameterManager jpm;
0017 
0018 
0019    SECTION("Multiple calls to SetDefaultParameter with same defaults succeed") {
0020 
0021         jpm.SetParameter("testing:dummy_var", 22);
0022 
0023         int x = 44;
0024         jpm.SetDefaultParameter("testing:dummy_var", x);
0025         REQUIRE(x == 22);
0026 
0027         int y = 44;
0028         jpm.SetDefaultParameter("testing:dummy_var", y);
0029         REQUIRE(y == 22);
0030     }
0031 
0032 
0033     SECTION("Multiple calls to SetDefaultParameter with same defaults succeed, float") {
0034 
0035         float x = 1.1;
0036         jpm.SetDefaultParameter("small_float", x);
0037         float y = 1.1;
0038         jpm.SetDefaultParameter("small_float", y);
0039         float temp;
0040         jpm.Parse<float>(jpm.Stringify(1.1f), temp);
0041         REQUIRE(jpm.Equals(temp, 1.1f));
0042         jpm.Parse<float>(jpm.Stringify(1.1f), temp);
0043         REQUIRE(!jpm.Equals(temp, 1.10001f));
0044 
0045         float v = 1.1e20f;
0046         jpm.SetDefaultParameter("large_float", v);
0047         float w = 1.1e20f;
0048 
0049         jpm.SetDefaultParameter("large_float", w);
0050         jpm.Parse<float>(jpm.Stringify(1.1e20f), temp);
0051         REQUIRE(jpm.Equals(temp, 1.1e20f));
0052         jpm.Parse<float>(jpm.Stringify(1.1e20f), temp);
0053         REQUIRE(!jpm.Equals(temp, 1.100001e20f));
0054 
0055         double xx = 1.1;
0056         jpm.SetDefaultParameter("small_double", xx);
0057         double yy = 1.1;
0058         jpm.SetDefaultParameter("small_double", yy);
0059         
0060         double tempD;
0061         jpm.Parse<double>(jpm.Stringify(1.1), tempD);
0062         REQUIRE(jpm.Equals(tempD, 1.1));
0063         jpm.Parse<double>(jpm.Stringify(1.1), tempD);
0064         REQUIRE(!jpm.Equals(tempD, 1.100001));
0065 
0066         double vv = 1.1e50;
0067         jpm.SetDefaultParameter("large_double", vv);
0068         double ww = 1.1e50;
0069         jpm.SetDefaultParameter("large_double", ww);
0070 
0071         jpm.Parse<double>(jpm.Stringify(1.1e20), tempD);
0072         REQUIRE(jpm.Equals(tempD, 1.1e20));
0073         REQUIRE(!jpm.Equals(tempD, 1.1000000001e20));
0074     }
0075 
0076 
0077     SECTION("Multiple calls to SetDefaultParameter with different defaults") {
0078 
0079         // If set, the user provided value overrides ALL default values
0080 
0081         jpm.SetParameter("testing:dummy_var", 22);
0082 
0083         int x = 44;
0084         jpm.SetDefaultParameter("testing:dummy_var", x);
0085         REQUIRE(x == 22);
0086 
0087         int y = 77;
0088         jpm.SetDefaultParameter("testing:dummy_var", y);
0089         REQUIRE(x == 22);
0090 
0091 
0092         // If no value set and there are two conflicting defaults, use the _local_ one
0093         int z = 44;
0094         jpm.SetDefaultParameter("testing:dummy_var_2", z);
0095         REQUIRE(z == 44);
0096 
0097         int zz = 77;
0098         jpm.SetDefaultParameter("testing:dummy_var_2", zz);
0099         REQUIRE(zz == 77);
0100     }
0101 
0102     SECTION("Multiple calls to check strings with spaces") {
0103 
0104         // basic string test
0105         std::string x = "MyStringValue";
0106         jpm.SetDefaultParameter("testing:dummy_var", x);
0107         REQUIRE(x == "MyStringValue");
0108 
0109         // string with spaces
0110         std::string y = "My String Value With Spaces";
0111         auto p = jpm.SetDefaultParameter("testing:dummy_var2", y);
0112         REQUIRE(p->GetValue() == "My String Value With Spaces");
0113 
0114         // Stringify returns identical string
0115         REQUIRE( jpm.Stringify("My String Value With Spaces") == "My String Value With Spaces" );
0116 
0117         // Parse returns identical string
0118         std::string z = "My String Value With Spaces";
0119         std::string testString;
0120         jpm.Parse<std::string>(z,testString);
0121         REQUIRE( testString == "My String Value With Spaces" );
0122     }
0123 }
0124 
0125 TEST_CASE("JParameterManager_JEventLevel") {
0126     JParameterManager jpm;
0127     jpm.SetParameter("test_param", "PhysicsEvent");
0128     auto level = jpm.GetParameterValue<JEventLevel>("test_param");
0129     REQUIRE(level == JEventLevel::PhysicsEvent);
0130 }
0131 
0132 
0133 TEST_CASE("JParameterManagerBoolTests") {
0134     JParameterManager jpm;
0135 
0136     SECTION("'0' parses to false") {
0137         jpm.SetParameter("test_param", "0");
0138         bool val = jpm.GetParameterValue<bool>("test_param");
0139         REQUIRE(val == false);
0140     }
0141 
0142     SECTION("'1' parses to true") {
0143         jpm.SetParameter("test_param", "1");
0144         bool val = jpm.GetParameterValue<bool>("test_param");
0145         REQUIRE(val == true);
0146     }
0147 
0148     SECTION("'off' parses to false") {
0149         jpm.SetParameter("test_param", "off");
0150         bool val = jpm.GetParameterValue<bool>("test_param");
0151         REQUIRE(val == false);
0152     }
0153 
0154     SECTION("'on' parses to true") {
0155         jpm.SetParameter("test_param", "on");
0156         bool val = jpm.GetParameterValue<bool>("test_param");
0157         REQUIRE(val == true);
0158     }
0159 
0160     SECTION("'true' parses to true") {
0161         jpm.SetParameter("test_param", "true");
0162         bool val = jpm.GetParameterValue<bool>("test_param");
0163         REQUIRE(val == true);
0164     }
0165 
0166     SECTION("'false' parses to false") {
0167         jpm.SetParameter("test_param", "false");
0168         bool val = jpm.GetParameterValue<bool>("test_param");
0169         REQUIRE(val == false);
0170     }
0171 
0172     SECTION("Parsing anything else as bool throws an exception") {
0173         jpm.SetParameter("test_param", "maybe");
0174         CHECK_THROWS(jpm.GetParameterValue<bool>("test_param"));
0175     }
0176 
0177     SECTION("Stringify still works") {
0178         jpm.SetParameter("test_param", false);
0179         std::string val = jpm.GetParameterValue<std::string>("test_param");
0180         REQUIRE(val == "0");
0181 
0182         jpm.SetParameter("test_param", true);
0183         val = jpm.GetParameterValue<std::string>("test_param");
0184         REQUIRE(val == "1");
0185     }
0186 }
0187 
0188 TEST_CASE("JParameterManager_VectorParams") {
0189     JParameterManager jpm;
0190 
0191     SECTION("Reading a vector of strings") {
0192         jpm.SetParameter("test", "simple,whitespace in middle, also with whitespace padding ");
0193         std::vector<std::string> vals;
0194         jpm.GetParameter<std::vector<std::string>>("test", vals);
0195 
0196         REQUIRE(vals[0] == "simple");
0197         REQUIRE(vals[1] == "whitespace in middle");
0198         REQUIRE(vals[2] == " also with whitespace padding ");
0199     }
0200     SECTION("Writing a vector of strings") {
0201         std::vector<std::string> inputs;
0202         inputs.emplace_back("first");
0203         inputs.emplace_back("second one");
0204         inputs.emplace_back(" third one ");
0205 
0206         jpm.SetDefaultParameter("test", inputs);
0207         std::vector<std::string> outputs;
0208         auto param = jpm.GetParameter("test", outputs);
0209         REQUIRE(param->GetValue() == "first,second one, third one ");
0210         REQUIRE(inputs.size()==3); // an additional test to see that the size of the input vector remains the same: Issue #256
0211     }
0212     SECTION("Reading a vector of ints") {
0213         jpm.SetParameter("test", "1,2, 3 ");
0214         std::vector<int32_t> vals;
0215         jpm.GetParameter("test", vals);
0216 
0217         REQUIRE(vals[0] == 1);
0218         REQUIRE(vals[1] == 2);
0219         REQUIRE(vals[2] == 3);
0220     }
0221     SECTION("Writing a vector of ints") {
0222         std::vector<int32_t> inputs;
0223         inputs.emplace_back(22);
0224         inputs.emplace_back(49);
0225         inputs.emplace_back(42);
0226 
0227         jpm.SetDefaultParameter("test", inputs);
0228         std::vector<std::string> outputs;
0229         auto param = jpm.GetParameter("test", outputs);
0230         REQUIRE(param->GetValue() == "22,49,42");
0231     }
0232     SECTION("Reading a vector of floats") {
0233         jpm.SetParameter("test", "1,2,3");
0234         std::vector<float> vals;
0235         jpm.GetParameter("test", vals);
0236 
0237         REQUIRE(vals[0] == 1.0f);
0238         REQUIRE(vals[1] == 2.0f);
0239         REQUIRE(vals[2] == 3.0f);
0240     }
0241     SECTION("Writing a vector of floats") {
0242         std::vector<float> inputs;
0243         inputs.emplace_back(22.0);
0244         inputs.emplace_back(49.2);
0245         inputs.emplace_back(42.0);
0246 
0247         jpm.SetDefaultParameter("test", inputs);
0248 
0249         std::vector<float> outputs = jpm.GetParameterValue<std::vector<float>>("test");
0250         REQUIRE(outputs == std::vector<float>{22,49.2,42});
0251     }
0252     SECTION("Reading a vector of functions with commas") {
0253         // As of Mon Jan 27, JParameterManager does not allow an escape key to prevent splitting on the next comma)
0254         jpm.SetParameter("test", "phi-fmod(phi\\,5),theta-fmod(theta\\,10),omega-fmod(omega\\,15)"); // Issue #380 (Feature request)
0255         std::vector<std::string> vals;
0256         jpm.GetParameter<std::vector<std::string>>("test", vals);
0257         auto stringification = jpm.Stringify(vals);
0258 
0259         REQUIRE(stringification == "phi-fmod(phi\\,5),theta-fmod(theta\\,10),omega-fmod(omega\\,15)");
0260 
0261         REQUIRE(vals[0] == "phi-fmod(phi,5)");
0262         REQUIRE(vals[1] == "theta-fmod(theta,10)");
0263         REQUIRE(vals[2] == "omega-fmod(omega,15)");
0264     }
0265     SECTION("Writing a vector of functions with commas") {
0266         std::vector<std::string> inputs;
0267         inputs.emplace_back("phi-fmod(phi,5)");
0268         inputs.emplace_back("theta-fmod(theta,10)");
0269         inputs.emplace_back("omega-fmod(omega,15)");
0270         std::vector<std::string> temp1 = inputs;
0271 
0272 
0273         jpm.SetDefaultParameter("test", inputs);
0274         auto stringified = jpm.Stringify(inputs);
0275         std::vector<std::string> mangled_inputs;
0276         jpm.Parse(stringified, mangled_inputs);
0277         REQUIRE(inputs == mangled_inputs);
0278 
0279         std::vector<std::string> outputs;
0280         auto param = jpm.GetParameter("test", outputs);
0281         REQUIRE(param->GetValue() == "phi-fmod(phi\\,5),theta-fmod(theta\\,10),omega-fmod(omega\\,15)");
0282         REQUIRE(inputs.size()==3);
0283 
0284         std::vector<std::string> temp2;
0285         jpm.Parse(jpm.Stringify(temp1), temp2);
0286         REQUIRE(temp2 == outputs);
0287     }
0288     SECTION("ParseVectorOfStrings") {
0289 
0290         auto s1 = "This,is,a,test"; // Should have 4 elements in vector
0291         std::vector<std::string> v1;
0292         jpm.Parse(s1, v1);
0293         REQUIRE(v1.size() == 4);
0294         REQUIRE(v1.at(0) == "This");
0295 
0296         auto s2 = "This\\,is,a\\,test"; // Should have 2 elements in vector
0297         std::vector<std::string> v2;
0298         jpm.Parse(s2, v2);
0299         REQUIRE(v2.size() == 2);
0300         REQUIRE(v2.at(0) == "This,is");
0301         
0302         auto s3 = "This\\,is,a\\\\,test"; // Should have 2 elements in vector
0303         std::vector<std::string> v3;
0304         jpm.Parse(s3, v3);
0305         REQUIRE(v3.size() == 2);
0306         REQUIRE(v3.at(1) == "a\\,test");
0307     }
0308     SECTION("StringifyVectorOfStrings") {
0309 
0310         std::vector<std::string> v1 {"This", "is", "a", "test"};
0311         auto s1 = jpm.Stringify(v1);
0312         REQUIRE(s1 == "This,is,a,test");
0313 
0314         std::vector<std::string> v2 {"This,is", "a,test"};
0315         auto s2 = jpm.Stringify(v2);
0316         REQUIRE(s2 == "This\\,is,a\\,test");
0317         
0318         std::vector<std::string> v3 {"This,is", "a\\,test"};
0319         auto s3 = jpm.Stringify(v3);
0320         REQUIRE(s3 == "This\\,is,a\\\\,test");
0321     }
0322 }
0323 
0324 TEST_CASE("JParameterManager::RegisterParameter") {
0325 
0326     JParameterManager jpm;
0327 
0328     SECTION("Set/Get") {
0329         int x_default = 44;
0330         auto x_actual = jpm.RegisterParameter("testing:dummy_var", x_default);
0331         REQUIRE(x_actual == x_default);
0332     }
0333 
0334     SECTION("Set/Get templated float") {
0335         auto y_actual = jpm.RegisterParameter("testing:dummy_var2", 22.0);
0336         REQUIRE(y_actual == 22.0);
0337     }
0338 
0339     SECTION("Set/Get default") {
0340         jpm.SetParameter("testing:dummy_var", 22);
0341         auto x_actual = jpm.RegisterParameter("testing:dummy_var", 44);  // this should set the default value to 44 while keeping value at 22
0342         auto x_default_str = jpm.FindParameter("testing:dummy_var")->GetDefault();
0343         int x_default;
0344         jpm.Parse<int>(x_default_str,x_default);
0345         REQUIRE(x_actual == 22);
0346         REQUIRE(x_default == 44);
0347     }
0348 
0349 }
0350 
0351 TEST_CASE("JParameterManager_ArrayParams") {
0352     JParameterManager jpm;
0353 
0354     SECTION("Reading a array of strings") {
0355         jpm.SetParameter("test", "simple,whitespace in middle, also with whitespace padding ");
0356         std::array<std::string,3> vals;
0357         jpm.GetParameter<std::array<std::string,3>>("test", vals); 
0358         REQUIRE(vals[0] == "simple");
0359         REQUIRE(vals[1] == "whitespace in middle");
0360         REQUIRE(vals[2] == " also with whitespace padding ");
0361     }
0362     SECTION("Writing an array of strings") {
0363         std::array<std::string,3> inputs = {"first", "second one" , " third one "};
0364         jpm.SetDefaultParameter("test", inputs);
0365         std::array<std::string,3> outputs;
0366         auto param = jpm.GetParameter("test", outputs);
0367         REQUIRE(param->GetValue() == "first,second one, third one ");
0368     }
0369     SECTION("Reading a array of ints") {
0370         jpm.SetParameter("test", "1,2, 3 ");
0371         std::array<int32_t,3> vals;
0372         jpm.GetParameter("test", vals);
0373 
0374         REQUIRE(vals[0] == 1);
0375         REQUIRE(vals[1] == 2);
0376         REQUIRE(vals[2] == 3);
0377     }
0378     SECTION("Writing a array of ints") {
0379         std::array<int32_t,3> inputs = {22,49,42};
0380         jpm.SetDefaultParameter("test", inputs);
0381         std::array<std::string,3> outputs;
0382         auto param = jpm.GetParameter("test", outputs);
0383         REQUIRE(param->GetValue() == "22,49,42");
0384     }
0385     SECTION("Reading a array of floats") {
0386         jpm.SetParameter("test", "1,2,3");
0387         std::array<float,3> vals;
0388         jpm.GetParameter("test", vals);
0389 
0390         REQUIRE(vals[0] == 1.0f);
0391         REQUIRE(vals[1] == 2.0f);
0392         REQUIRE(vals[2] == 3.0f);
0393     }
0394     SECTION("Writing a array of floats") {
0395         std::array<float,3> inputs = {22.0,49.2,42.0};
0396         jpm.SetDefaultParameter("test", inputs);
0397 
0398         std::array<float,3> outputs = jpm.GetParameterValue<std::array<float,3>>("test");
0399         REQUIRE(outputs == std::array<float,3>{22.0, 49.2, 42.0});
0400     }
0401     SECTION("Reading a array of functions with commas") {
0402         // As of Mon Jan 27, JParameterManager does not allow an escape key to prevent splitting on the next comma)
0403         jpm.SetParameter("test", "theta-fmod(phi-fmod(phi\\,5)\\,7),theta-fmod(theta\\,10),omega-fmod(omega\\,15)"); // Issue #380 (Feature request)
0404         std::array<std::string, 3> vals;
0405         jpm.GetParameter("test", vals);
0406 
0407         REQUIRE(vals[0] == "theta-fmod(phi-fmod(phi,5),7)");
0408         REQUIRE(vals[1] == "theta-fmod(theta,10)");
0409         REQUIRE(vals[2] == "omega-fmod(omega,15)");
0410     }
0411     SECTION("Writing a array of functions with commas") {
0412         std::array<std::string, 3> inputs = {
0413             "theta-fmod(phi-fmod(phi,5),7)",
0414             "theta-fmod(theta,10)",
0415             "omega-fmod(omega,15)"
0416         };
0417 
0418         jpm.SetDefaultParameter("test", inputs);
0419         std::array<std::string,3> outputs;
0420 
0421         auto param = jpm.GetParameter("test", outputs);
0422 
0423         REQUIRE(outputs == inputs);
0424         REQUIRE(param->GetValue() == "theta-fmod(phi-fmod(phi\\,5)\\,7),theta-fmod(theta\\,10),omega-fmod(omega\\,15)");
0425     }
0426     SECTION("ParseArrayOfStrings") {
0427 
0428         auto s1 = "This,is,a,test"; // Should have 4 elements in vector
0429         std::array<std::string,4> v1;
0430         jpm.Parse(s1, v1);
0431         REQUIRE(v1.size() == 4);
0432         REQUIRE(v1.at(0) == "This");
0433 
0434         auto s2 = "This\\,is,a\\,test"; // Should have 2 elements in vector
0435         std::array<std::string, 2> v2;
0436         jpm.Parse(s2, v2);
0437         REQUIRE(v2.size() == 2);
0438         REQUIRE(v2.at(0) == "This,is");
0439 
0440         auto s3 = "This\\,is,a\\\\,test"; // Should have 2 elements in vector
0441         std::array<std::string, 2> v3;
0442         jpm.Parse(s3, v3);
0443         REQUIRE(v3.size() == 2);
0444         REQUIRE(v3.at(1) == "a\\,test");
0445     }
0446     SECTION("StringifyArrayOfStrings") {
0447 
0448         std::array<std::string, 4> v1 {"This", "is", "a", "test"};
0449         auto s1 = jpm.Stringify(v1);
0450         REQUIRE(s1 == "This,is,a,test");
0451 
0452         std::array<std::string, 2> v2 {"This,is", "a,test"};
0453         auto s2 = jpm.Stringify(v2);
0454         REQUIRE(s2 == "This\\,is,a\\,test");
0455 
0456         std::array<std::string, 2> v3 {"This,is", "a\\,test"};
0457         auto s3 = jpm.Stringify(v3);
0458         REQUIRE(s3 == "This\\,is,a\\\\,test");
0459     }
0460 }
0461 
0462 TEST_CASE("JParameterManagerFloatingPointRoundTrip") {
0463     JParameterManager jpm;
0464 
0465     SECTION("Integer") {
0466         const std::string testValString = jpm.Stringify(123);
0467         REQUIRE(testValString == "123");
0468         int testValParsed;
0469         jpm.Parse<int>(testValString, testValParsed);
0470         REQUIRE(testValParsed == 123);
0471     }
0472 
0473     SECTION("Double requiring low precision") {
0474         const double testVal = 123;
0475         const std::string testValString = jpm.Stringify(testVal);
0476         REQUIRE(testValString == "123");
0477         double testValParsed;
0478         jpm.Parse<double>(testValString, testValParsed);
0479         REQUIRE(testValParsed == 123.0);
0480     }
0481 
0482     SECTION("Double requiring low precision, with extra zeros") {
0483         const double testVal = 123.0;
0484         const std::string testValString = jpm.Stringify(testVal);
0485         REQUIRE(testValString == "123");
0486         double testValParsed;
0487         jpm.Parse<double>(testValString, testValParsed);
0488         REQUIRE(testValParsed == 123.0);
0489     }
0490 
0491     SECTION("Double requiring high precision") {
0492         const double testVal = 123.0001;
0493         const std::string testValString = jpm.Stringify(testVal);
0494         std::cout << "High-precision double stringified to " << testValString << std::endl;
0495         // REQUIRE(testValString == "123.0001");
0496         double testValParsed;
0497         jpm.Parse<double>(testValString, testValParsed);
0498         REQUIRE(testValParsed == 123.0001);
0499     }
0500 
0501     SECTION("Float requiring high precision") {
0502         const float testVal = 123.0001f;
0503         const std::string testValString = jpm.Stringify(testVal);
0504         std::cout << "High-precision float stringified to " << testValString << std::endl;
0505         // REQUIRE(testValString == "123.0001");
0506         float testValParsed;
0507         jpm.Parse<float>(testValString, testValParsed);
0508         REQUIRE(testValParsed == 123.0001f);
0509     }
0510 
0511     SECTION("Float requiring low precision") {
0512         const float testVal = 123.0f;
0513         const std::string testValString = jpm.Stringify(testVal);
0514         REQUIRE(testValString == "123");
0515         float testValParsed;
0516         jpm.Parse<float>(testValString, testValParsed);
0517         REQUIRE(testValParsed == 123.0f);
0518     }
0519 
0520 }
0521 
0522 
0523 TEST_CASE("JParameterManagerIssue233") {
0524     JParameterManager jpm;
0525     double x = 0.0;
0526     jpm.SetDefaultParameter("x", x, "Description");
0527     // This should NOT print out a warning about losing equality with itself after stringification
0528 
0529     // We reproduce the logic inside SetDefaultParameter here so that CI can catch regressions
0530     std::string x_stringified = JParameterManager::Stringify(x);
0531     double x_roundtrip;
0532     JParameterManager::Parse(x_stringified, x_roundtrip);
0533     REQUIRE(JParameterManager::Equals(x_roundtrip, x));
0534 }
0535 
0536 
0537 TEST_CASE("JParameterManager_Issue217StringsWithWhitespace") {
0538     JParameterManager jpm;
0539     SECTION("Reading a array of strings") {
0540         jpm.SetParameter("test", "(  abs(fmod(tower_1, 24) - fmod(tower_2, 24))  + min(      abs((sector_1 - sector_2) * (2 * 5) + (floor(tower_1 / 24) - floor(tower_2 / 24)) * 5 + fmod(tile_1, 5) - fmod(tile_2, 5)),      (32 * 2 * 5) - abs((sector_1 - sector_2) * (2 * 5) + (floor(tower_1 / 24) - floor(tower_2 / 24)) * 5 + fmod(tile_1, 5) - fmod(tile_2, 5))    )) == 1");
0541         std::string vals;
0542         jpm.GetParameter<std::string>("test", vals); 
0543         REQUIRE(vals == "(  abs(fmod(tower_1, 24) - fmod(tower_2, 24))  + min(      abs((sector_1 - sector_2) * (2 * 5) + (floor(tower_1 / 24) - floor(tower_2 / 24)) * 5 + fmod(tile_1, 5) - fmod(tile_2, 5)),      (32 * 2 * 5) - abs((sector_1 - sector_2) * (2 * 5) + (floor(tower_1 / 24) - floor(tower_2 / 24)) * 5 + fmod(tile_1, 5) - fmod(tile_2, 5))    )) == 1");
0544     }
0545 }
0546 
0547 
0548 enum class Mood {Good, Bad, Mediocre};
0549 TEST_CASE("JParameterManager_CompileTimeErrorForParseAndStringify") {
0550 
0551     int x;
0552     JParameterManager::Parse("22", x);
0553     // Uncomment these to test compile-time error message
0554     //Mood m;
0555     //JParameterManager::Parse("Mediocre", m);
0556     //JParameterManager::Stringify(m);
0557 }
0558 
0559 
0560 TEST_CASE("JParameterManager_Strictness") {
0561     JParameterManager sut;
0562     sut.SetParameter("jana:parameter_strictness", 2);
0563     sut.SetParameter("jana:unused", 22);
0564     bool exception_found = false;
0565     try {
0566         sut.PrintParameters();
0567     }
0568     catch (JException& e) {
0569         exception_found = true;
0570     }
0571     REQUIRE(exception_found == true);
0572 }
0573 
0574 
0575 
0576 TEST_CASE("JParameterManager_ConflictingDefaults") {
0577 
0578     int x1 = 3;
0579     int x2 = 4;
0580     int x3 = 3;
0581     int x4 = 4;
0582 
0583     JParameterManager sut;
0584     sut.SetLogger(JLogger());
0585 
0586     // Simulate a FactorySet containing two JFactories, each of which declares the same parameter with different default values
0587     auto p1 = sut.SetDefaultParameter("my_param_name", x1, "Tests how conflicting defaults are handled");
0588     REQUIRE(p1->HasDefault() == true);
0589     REQUIRE(p1->IsDefault() == true);
0590     REQUIRE(p1->GetDefault() == "3"); // Should be the _latest_ default found
0591     REQUIRE(p1->GetValue() == "3"); // Should be the _latest_ default value
0592     REQUIRE(x1 == 3);
0593     auto p2 = sut.SetDefaultParameter("my_param_name", x2, "Tests how conflicting defaults are handled");
0594     REQUIRE(p2->HasDefault() == true);
0595     REQUIRE(p2->IsDefault() == true);
0596     REQUIRE(p2->GetDefault() == "4"); // Should be the _latest_ default found
0597     REQUIRE(p2->GetValue() == "4"); // Should be the _latest_ default value
0598     REQUIRE(x2 == 4);
0599     
0600     // Simulate a _second_ FactorySet containing fresh instances of the same two JFactories, 
0601     auto p3 = sut.SetDefaultParameter("my_param_name", x3, "Tests how conflicting defaults are handled");
0602     REQUIRE(p3->HasDefault() == true);
0603     REQUIRE(p3->IsDefault() == true);
0604     REQUIRE(p3->GetDefault() == "3"); // Should be the _latest_ default found
0605     REQUIRE(p3->GetValue() == "3"); // Should be the _latest_ default value
0606     REQUIRE(x3 == 3);
0607     auto p4 = sut.SetDefaultParameter("my_param_name", x4, "Tests how conflicting defaults are handled");
0608     REQUIRE(p4->HasDefault() == true);
0609     REQUIRE(p4->IsDefault() == true);
0610     REQUIRE(p4->GetDefault() == "4"); // Should be the _latest_ default value found
0611     REQUIRE(p4->GetValue() == "4"); // Should be the _latest_ default value
0612     REQUIRE(x4 == 4);
0613 
0614     sut.PrintParameters(2,1);
0615 }
0616 
0617 
0618 
0619 
0620 
0621 
0622 
0623 
0624 
0625 
0626 TEST_CASE("JParameterManager_ConfigFileRoundTrip") {
0627 
0628     std::string filename = "JParameterManager_ConfigFileRoundTrip.config";
0629 
0630     SECTION("Values longer than 10000 characters") {
0631         std::string long_val(12000, 'x');
0632         JParameterManager writer;
0633         writer.SetParameter("long_param", long_val);
0634         writer.WriteConfigFile(filename);
0635 
0636         JParameterManager reader;
0637         reader.ReadConfigFile(filename);
0638         REQUIRE(reader.GetParameterValue<std::string>("long_param") == long_val);
0639     }
0640 
0641     SECTION("Empty values and flags") {
0642         JParameterManager writer;
0643         writer.SetParameter("empty_param", std::string(""));
0644         writer.SetParameter("nonempty_param", std::string("abc"));
0645         writer.WriteConfigFile(filename);
0646         {
0647             std::ofstream ofs(filename, std::ios::app);
0648             ofs << "flag_param" << std::endl;
0649         }
0650 
0651         JParameterManager reader;
0652         reader.ReadConfigFile(filename);
0653         REQUIRE(reader.GetParameterValue<std::string>("empty_param") == "");
0654         REQUIRE(reader.GetParameterValue<std::string>("nonempty_param") == "abc");
0655         REQUIRE(reader.GetParameterValue<std::string>("flag_param") == "1");
0656     }
0657 
0658     std::remove(filename.c_str());
0659 }