Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d87990

Browse files
julianoesdagar
authored andcommitted
lockstep_scheduler: convert test to gtest
1 parent 1624054 commit 8d87990

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

platforms/posix/src/lockstep_scheduler/test/src/lockstep_scheduler_test.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <lockstep_scheduler/lockstep_scheduler.h>
2-
#include <cassert>
2+
#include <gtest/gtest.h>
33
#include <thread>
44
#include <atomic>
55
#include <random>
@@ -48,7 +48,7 @@ void test_absolute_time()
4848
{
4949
LockstepScheduler ls;
5050
ls.set_absolute_time(some_time_us);
51-
assert(ls.get_absolute_time() == some_time_us);
51+
EXPECT_EQ(ls.get_absolute_time(), some_time_us);
5252
}
5353

5454
void test_condition_timing_out()
@@ -70,10 +70,10 @@ void test_condition_timing_out()
7070
// Use a thread to wait for condition while we already have the lock.
7171
// This ensures the synchronization happens in the right order.
7272
TestThread thread([&ls, &cond, &lock, &should_have_timed_out]() {
73-
assert(ls.cond_timedwait(&cond, &lock, some_time_us + 1000) == ETIMEDOUT);
74-
assert(should_have_timed_out);
73+
EXPECT_EQ(ls.cond_timedwait(&cond, &lock, some_time_us + 1000), ETIMEDOUT);
74+
EXPECT_TRUE(should_have_timed_out);
7575
// It should be re-locked afterwards, so we should be able to unlock it.
76-
assert(pthread_mutex_unlock(&lock) == 0);
76+
EXPECT_EQ(pthread_mutex_unlock(&lock), 0);
7777
});
7878

7979
ls.set_absolute_time(some_time_us + 500);
@@ -106,9 +106,9 @@ void test_locked_semaphore_getting_unlocked()
106106
TestThread thread([&ls, &cond, &lock]() {
107107

108108
ls.set_absolute_time(some_time_us + 500);
109-
assert(ls.cond_timedwait(&cond, &lock, some_time_us + 1000) == 0);
109+
EXPECT_EQ(ls.cond_timedwait(&cond, &lock, some_time_us + 1000), 0);
110110
// It should be re-locked afterwards, so we should be able to unlock it.
111-
assert(pthread_mutex_unlock(&lock) == 0);
111+
EXPECT_EQ(pthread_mutex_unlock(&lock), 0);
112112
});
113113

114114
pthread_mutex_lock(&lock);
@@ -135,7 +135,7 @@ class TestCase
135135

136136
~TestCase()
137137
{
138-
assert(_is_done);
138+
EXPECT_TRUE(_is_done);
139139
pthread_mutex_destroy(&_lock);
140140
pthread_cond_destroy(&_cond);
141141
}
@@ -168,13 +168,13 @@ class TestCase
168168
_is_done = true;
169169
// We can be sure that this triggers.
170170
_thread->join(_ls);
171-
assert(_result == 0);
171+
EXPECT_EQ(_result, 0);
172172
}
173173

174174
else if (timeout_reached) {
175175
_is_done = true;
176176
_thread->join(_ls);
177-
assert(_result == ETIMEDOUT);
177+
EXPECT_EQ(_result, ETIMEDOUT);
178178
}
179179
}
180180
private:
@@ -302,21 +302,19 @@ void test_usleep()
302302

303303
step = Step::BeforeUsleep;
304304

305-
assert(ls.usleep_until(some_time_us + 1000) == 0);
306-
assert(step == Step::UsleepTriggered);
305+
EXPECT_EQ(ls.usleep_until(some_time_us + 1000), 0);
306+
EXPECT_EQ(step, Step::UsleepTriggered);
307307
thread.join(ls);
308308
}
309309

310-
int main(int /*argc*/, char ** /*argv*/)
310+
TEST(LockstepScheduler, All)
311311
{
312-
for (unsigned iteration = 1; iteration <= 10000; ++iteration) {
313-
std::cout << "Test iteration: " << iteration << "\n";
312+
for (unsigned iteration = 1; iteration <= 100; ++iteration) {
313+
//std::cout << "Test iteration: " << iteration << "\n";
314314
test_absolute_time();
315315
test_condition_timing_out();
316316
test_locked_semaphore_getting_unlocked();
317317
test_usleep();
318318
test_multiple_semaphores_waiting();
319319
}
320-
321-
return 0;
322320
}

0 commit comments

Comments
 (0)