diff --git a/unit_tests/Makefile b/unit_tests/Makefile index 891506ab61..5996d2b83e 100644 --- a/unit_tests/Makefile +++ b/unit_tests/Makefile @@ -32,7 +32,7 @@ endif # C++ specific options here (added to USE_OPT). ifeq ($(USE_CPPOPT),) - USE_CPPOPT = -std=c++17 -fno-rtti -fpermissive -fno-exceptions -fno-use-cxa-atexit + USE_CPPOPT = -std=c++17 -fno-rtti -fpermissive -fexceptions -fno-use-cxa-atexit endif # Enable this if you want the linker to remove unused code and data diff --git a/unit_tests/main.cpp b/unit_tests/main.cpp index 4107e1f717..47c357b53e 100644 --- a/unit_tests/main.cpp +++ b/unit_tests/main.cpp @@ -50,7 +50,7 @@ GTEST_API_ int main(int argc, char **argv) { // printTriggerDebug = true; // resizeMap(); - printf("Success 20190103\r\n"); + printf("Success 20190117\r\n"); printAllTriggers(); // printConvertedTable(); testing::InitGoogleTest(&argc, argv); diff --git a/unit_tests/test.mk b/unit_tests/test.mk index c97e73a8ab..541a00e3f7 100644 --- a/unit_tests/test.mk +++ b/unit_tests/test.mk @@ -7,6 +7,7 @@ TEST_SRC_CPP = unit_test_framework.cpp \ test_basic_math/test_efilib.cpp \ tests/test_util.cpp \ tests/test_ion.cpp \ + tests/test_on_demand_parameters.cpp \ tests/test_hip9011.cpp \ tests/test_cj125.cpp \ tests/test_cam_vtt_input.cpp \ diff --git a/unit_tests/tests/test_on_demand_parameters.cpp b/unit_tests/tests/test_on_demand_parameters.cpp new file mode 100644 index 0000000000..2ee347c2e9 --- /dev/null +++ b/unit_tests/tests/test_on_demand_parameters.cpp @@ -0,0 +1,63 @@ +/* + * @file test_on_demand_parameters.cpp + * + * Created on: Jan 16, 2019 + * @author Andrey Belomutskiy, (c) 2012-2018 + */ + +#include +#include "unit_test_framework.h" +using namespace std; + +class TestParameters { +public: + unordered_map values; + TestParameters* put(string key, float value); + float get(string key) const; +}; + +TestParameters* TestParameters::put(string key, float value) { + values[key] = value; + return this; +} + +float TestParameters::get(string key) const { + // WAT? 'contains' method only defined in C++20?! + std::unordered_map::const_iterator got = values.find (key); + if (got == values.end()) + throw "No value for this key: " + key; + return values[key]; +} + +#undef METHOD_SIGNATURE +#define METHOD_SIGNATURE TestParameters *parameterValues + +#define GET_VALUE(x) parameterValues->get(#x) + +static float methodWeWantToTestWhichUsesKey1AndKey2(METHOD_SIGNATURE) { + return GET_VALUE(key1) + GET_VALUE(key2); +} + +static float methodWeWantToTestWhichUsesKey3(METHOD_SIGNATURE) { + return GET_VALUE(key3); +} + +TEST(util, readValues) { + TestParameters* v = (new TestParameters())->put("key2", 20)->put("key1", 10); + + ASSERT_FLOAT_EQ(30, methodWeWantToTestWhichUsesKey1AndKey2(v)); +} + +TEST(util, checkForMissingParameterHandling) { + TestParameters* v = (new TestParameters())->put("key2", 2); + try { + methodWeWantToTestWhichUsesKey3(v); + FAIL() << "Expected 'missing key3' exception"; + } catch(string message) { + // exception about missing value is expected + ASSERT_TRUE(message.find("No value for this key") >= 0); + ASSERT_TRUE(message.find("key3") >= 0); + } +} + +