From 691686323ef85be8670b4dcbff3f6ca21e4d4f08 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Mon, 13 Jul 2020 12:15:36 +0900 Subject: [PATCH] test : compare values with template function - To remove duplicated source code Change-Id: Ifb7c1df91228a07cecedbc066c6d9414c8052db5 Signed-off-by: Kichan Kwon --- test/init_db/feature.cpp | 53 ++++++++++++++++++------------------------------ test/init_db/feature.h | 7 +++++++ 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/test/init_db/feature.cpp b/test/init_db/feature.cpp index 3a35dd0..b4642f8 100644 --- a/test/init_db/feature.cpp +++ b/test/init_db/feature.cpp @@ -19,6 +19,20 @@ Feature::ModType Feature::getModType(void) return (ModType)(rand() % static_cast(ModType::NUM_TYPE)); } +template +int Feature::compareValue(T value, T expected) +{ + if (value != expected) { + LOG << "Failed to verify value : key(" << key + << "), value(" << value + << "), expected(" << expected + << ")" << ENDL; + return -1; + } + + return SUCCEED; +} + /********** BoolFeature **********/ BoolFeature::BoolFeature(std::string k, std::string v) : Feature(k) @@ -63,15 +77,7 @@ int BoolFeature::verifyValue(void) return ret; } - if (value != output) { - LOG << "Failed to verify value : key(" << key - << "), value(" << (output ? STRBOOL_TRUE : STRBOOL_FALSE) - << "), expected(" << (value ? STRBOOL_TRUE : STRBOOL_FALSE) - << ")" << ENDL; - return -1; - } - - return SUCCEED; + return compareValue(output, value); } /********** IntFeature **********/ @@ -114,14 +120,7 @@ int IntFeature::verifyValue(void) return ret; } - if (value != output) { - LOG << "Failed to verify value : key(" << key - << "), value(" << output - << "), expected(" << value << ")" << ENDL; - return -1; - } - - return SUCCEED; + return compareValue(output, value); } /********** DoubleFeature **********/ @@ -166,15 +165,7 @@ int DoubleFeature::verifyValue(void) return ret; } - if (value != output) { - LOG << std::fixed << std::setprecision(15) - << "Failed to verify value : key(" << key - << "), value(" << output - << "), expected(" << value << ")" << ENDL; - return -1; - } - - return SUCCEED; + return compareValue(output, value); } /********** StringFeature **********/ @@ -214,12 +205,8 @@ int StringFeature::verifyValue(void) return ret; } - if (value.compare(output)) { - LOG << "Failed to verify value : key(" << key - << "), value(" << output - << "), expected(" << value << ")" << ENDL; - return -1; - } + ret = compareValue(std::string(output), value); + delete(output); - return SUCCEED; + return ret; } diff --git a/test/init_db/feature.h b/test/init_db/feature.h index 07489af..980f5db 100644 --- a/test/init_db/feature.h +++ b/test/init_db/feature.h @@ -36,6 +36,13 @@ protected: * @retval Refer to ModType */ ModType getModType(void); + + /** + * @brief Compare two values + * @retval SUCCEED if they are same, otherwise -1 + */ + template + int compareValue(T value, T expected); }; extern std::vectorfeatureList; -- 2.7.4