| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Vojtech Horky
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <pcut/pcut.h>
|
|---|
| 8 | #include <fibril.h>
|
|---|
| 9 | #include <perf.h>
|
|---|
| 10 |
|
|---|
| 11 | PCUT_INIT;
|
|---|
| 12 |
|
|---|
| 13 | PCUT_TEST_SUITE(perf);
|
|---|
| 14 |
|
|---|
| 15 | /* Checks that initialization zeroes out all entries. */
|
|---|
| 16 | PCUT_TEST(zero_diff)
|
|---|
| 17 | {
|
|---|
| 18 | stopwatch_t sw;
|
|---|
| 19 | stopwatch_init(&sw);
|
|---|
| 20 | PCUT_ASSERT_INT_EQUALS(0, (int) stopwatch_get_nanos(&sw));
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /* Checks that initialization zeroes out all entries. */
|
|---|
| 24 | PCUT_TEST(zero_diff_static)
|
|---|
| 25 | {
|
|---|
| 26 | stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
|
|---|
| 27 | PCUT_ASSERT_INT_EQUALS(0, (int) stopwatch_get_nanos(&sw));
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /* Checks that measuring 1s sleep does not give completely invalid results. */
|
|---|
| 31 | PCUT_TEST(stopwatch_smokes)
|
|---|
| 32 | {
|
|---|
| 33 | stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
|
|---|
| 34 | stopwatch_start(&sw);
|
|---|
| 35 | fibril_sleep(1);
|
|---|
| 36 | stopwatch_stop(&sw);
|
|---|
| 37 | nsec_t diff_nanos = stopwatch_get_nanos(&sw);
|
|---|
| 38 | PCUT_ASSERT_TRUE(diff_nanos > MSEC2NSEC(500));
|
|---|
| 39 | PCUT_ASSERT_TRUE(diff_nanos < SEC2NSEC(5));
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /* Checks that setting time works for small values. */
|
|---|
| 43 | PCUT_TEST(stopwatch_emulation_works_small)
|
|---|
| 44 | {
|
|---|
| 45 | stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
|
|---|
| 46 | stopwatch_set_nanos(&sw, 42);
|
|---|
| 47 | PCUT_ASSERT_INT_EQUALS(42, (int) stopwatch_get_nanos(&sw));
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /* Checks that setting time works for big values too. */
|
|---|
| 51 | PCUT_TEST(stopwatch_emulation_works_big)
|
|---|
| 52 | {
|
|---|
| 53 | stopwatch_t sw = STOPWATCH_INITIALIZE_STATIC;
|
|---|
| 54 | stopwatch_set_nanos(&sw, 4200000000021);
|
|---|
| 55 | PCUT_ASSERT_EQUALS(4200000000021, (long long) stopwatch_get_nanos(&sw));
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | PCUT_EXPORT(perf);
|
|---|