diff --git a/base_c/src/condition_variable.c b/base_c/src/condition_variable.c index 81f8c3b..575bb33 100644 --- a/base_c/src/condition_variable.c +++ b/base_c/src/condition_variable.c @@ -83,8 +83,8 @@ int embb_condition_wait_until(embb_condition_t* condition_var, embb_time_t now; embb_time_now(&now); /* Check if absolute timepoint (in milliseconds) still is in the future */ - if (time->seconds * 1000 + time->nanoseconds / 1000000 - - now.seconds * 1000 - now.nanoseconds / 1000000 > 0) { + if ((time->seconds * 1000 + time->nanoseconds / 1000000) + > (now.seconds * 1000 + now.nanoseconds / 1000000)) { /* Convert to (unsigned type) milliseconds and round up */ DWORD time_diff = (DWORD) ( time->seconds * 1000 + time->nanoseconds / 1000000 diff --git a/base_c/test/condition_var_test.cc b/base_c/test/condition_var_test.cc index f292d3c..ad0fe32 100644 --- a/base_c/test/condition_var_test.cc +++ b/base_c/test/condition_var_test.cc @@ -105,7 +105,7 @@ void ConditionVarTest::TestTimedWaitTimeouts() { embb_time_t time; embb_duration_t duration = EMBB_DURATION_INIT; - // Wait for now tests already passed time point + // Wait for "now" tests already passed time point embb_time_now(&time); embb_mutex_lock(&mutex); int status = embb_condition_wait_until(&cond, &mutex, &time); diff --git a/base_c/test/time_test.cc b/base_c/test/time_test.cc index 94d6d1c..9797350 100644 --- a/base_c/test/time_test.cc +++ b/base_c/test/time_test.cc @@ -36,6 +36,9 @@ namespace test { TimeTest::TimeTest() { CreateUnit("Time in duration").Add(&TimeTest::TestTimeInDuration, this); + CreateUnit("Monotonicity").Add( + &TimeTest::TestMonotonicity, this, + 1, partest::TestSuite::GetDefaultNumIterations() * 10); } void TimeTest::TestTimeInDuration() { @@ -48,6 +51,20 @@ void TimeTest::TestTimeInDuration() { PT_EXPECT_EQ(status, EMBB_SUCCESS); } +void TimeTest::TestMonotonicity() { + embb_time_t first; + embb_time_t second; + int status1 = embb_time_in(&first, embb_duration_zero()); + int status2 = embb_time_in(&second, embb_duration_zero()); + PT_EXPECT_EQ(status1, EMBB_SUCCESS); + PT_EXPECT_EQ(status2, EMBB_SUCCESS); + unsigned long long first_abs = first.seconds * 1000 + + first.nanoseconds / 1000000; + unsigned long long second_abs = second.seconds * 1000 + + second.nanoseconds / 1000000; + PT_EXPECT_GE(second_abs, first_abs); +} + } // namespace test } // namespace base } // namespace embb diff --git a/base_c/test/time_test.h b/base_c/test/time_test.h index 629befc..419d26d 100644 --- a/base_c/test/time_test.h +++ b/base_c/test/time_test.h @@ -42,9 +42,14 @@ class TimeTest : public partest::TestCase { private: /** - * Tests time in duration method. + * Tests time-in-duration method. */ void TestTimeInDuration(); + + /** + * Tests that succeedingly taken times are monotonously increasing. + */ + void TestMonotonicity(); }; } // namespace test