2222#include < vector>
2323
2424// Helper to print info logs in a Python-parsable way
25+
26+ constexpr double kFloatEpsilon = 1e-6 ;
2527const std::string kTargetName {" cpp_test_scenarios::cit::default_values" };
2628using score::mw::per::kvs::KvsValue;
2729
@@ -95,7 +97,7 @@ void DefaultValuesScenario::run(const std::optional<std::string> &input) const {
9597 } else if (std::abs (
9698 std::get<double >(get_default_result.value ().getValue ()) -
9799 std::get<double >(get_value_result.value ().getValue ())) <
98- 1e-6 ) {
100+ kFloatEpsilon ) {
99101 value_is_default = " Ok(true)" ;
100102 } else {
101103 value_is_default = " Ok(false)" ;
@@ -133,7 +135,7 @@ void DefaultValuesScenario::run(const std::optional<std::string> &input) const {
133135 try {
134136 double v = std::get<double >(cur_val->getValue ());
135137 double d = std::get<double >(def_val->getValue ());
136- if (v == d )
138+ if (std::abs (v - d) < kFloatEpsilon )
137139 value_is_default = " Ok(true)" ;
138140 } catch (const std::bad_variant_access &e) {
139141 throw ;
@@ -216,7 +218,8 @@ void RemoveKeyScenario::run(const std::optional<std::string> &input) const {
216218 current_value == " Err(KeyNotFound)" ) {
217219 value_is_default = " Err(KeyNotFound)" ;
218220 } else if (std::abs (std::get<double >(get_default->getValue ()) -
219- std::get<double >(get_value->getValue ())) < 1e-6 ) {
221+ std::get<double >(get_value->getValue ())) <
222+ kFloatEpsilon ) {
220223 value_is_default = " Ok(true)" ;
221224 } else {
222225 value_is_default = " Ok(false)" ;
@@ -234,7 +237,7 @@ void RemoveKeyScenario::run(const std::optional<std::string> &input) const {
234237 get_value->getType () == KvsValue::Type::f64 ) {
235238 double v = std::get<double >(get_value->getValue ());
236239 double d = std::get<double >(get_default->getValue ());
237- if (v == d )
240+ if (std::abs (v - d) < kFloatEpsilon )
238241 value_is_default = " Ok(true)" ;
239242 }
240243 // Format current_value for log
@@ -265,7 +268,7 @@ void RemoveKeyScenario::run(const std::optional<std::string> &input) const {
265268 get_value->getType () == KvsValue::Type::f64 ) {
266269 double v = std::get<double >(get_value->getValue ());
267270 double d = std::get<double >(get_default->getValue ());
268- if (v == d )
271+ if (std::abs (v - d) < kFloatEpsilon )
269272 value_is_default = " Ok(true)" ;
270273 }
271274 }
@@ -303,7 +306,8 @@ void ResetAllKeysScenario::run(const std::optional<std::string> &input) const {
303306 get_value->getType () == KvsValue::Type::f64 ) {
304307 double v = std::get<double >(get_value->getValue ());
305308 double d = std::get<double >(get_default->getValue ());
306- value_is_default = (v == d) ? " Ok(true)" : " Ok(false)" ;
309+ value_is_default =
310+ (std::abs (v - d) < kFloatEpsilon ) ? " Ok(true)" : " Ok(false)" ;
307311 } else if (!get_default) {
308312 value_is_default = " Err(KeyNotFound)" ;
309313 } else {
@@ -350,7 +354,8 @@ void ResetAllKeysScenario::run(const std::optional<std::string> &input) const {
350354 get_value->getType () == KvsValue::Type::f64 ) {
351355 double v = std::get<double >(get_value->getValue ());
352356 double d = std::get<double >(get_default->getValue ());
353- value_is_default = (v == d) ? " Ok(true)" : " Ok(false)" ;
357+ value_is_default =
358+ (std::abs (v - d) < kFloatEpsilon ) ? " Ok(true)" : " Ok(false)" ;
354359 } else if (!get_default) {
355360 value_is_default = " Err(KeyNotFound)" ;
356361 } else {
@@ -395,7 +400,8 @@ void ResetSingleKeyScenario::run(
395400 get_value->getType () == KvsValue::Type::f64 ) {
396401 double v = std::get<double >(get_value->getValue ());
397402 double d = std::get<double >(get_default->getValue ());
398- value_is_default = (v == d) ? " Ok(true)" : " Ok(false)" ;
403+ value_is_default =
404+ (std::abs (v - d) < kFloatEpsilon ) ? " Ok(true)" : " Ok(false)" ;
399405 } else if (!get_default) {
400406 value_is_default = " Err(KeyNotFound)" ;
401407 } else {
@@ -452,7 +458,8 @@ void ResetSingleKeyScenario::run(
452458 get_value->getType () == KvsValue::Type::f64 ) {
453459 double v = std::get<double >(get_value->getValue ());
454460 double d = std::get<double >(get_default->getValue ());
455- value_is_default = (v == d) ? " Ok(true)" : " Ok(false)" ;
461+ value_is_default =
462+ (std::abs (v - d) < kFloatEpsilon ) ? " Ok(true)" : " Ok(false)" ;
456463 std::ostringstream oss;
457464 oss.precision (1 );
458465 oss << std::fixed << v;
0 commit comments