| 1 | /* |
| 2 | * Timed average computation tests |
| 3 | * |
| 4 | * Copyright Nodalink, EURL. 2014 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Benoît Canet <benoit.canet@nodalink.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "system/cpu-timers.h" |
| 15 | #include "qemu/timed-average.h" |
| 16 | |
| 17 | /* This is the clock for QEMU_CLOCK_VIRTUAL */ |
| 18 | static int64_t my_clock_value; |
| 19 | |
| 20 | int64_t cpu_get_clock(void) |
| 21 | { |
| 22 | return my_clock_value; |
| 23 | } |
| 24 | |
| 25 | static void account(TimedAverage *ta) |
| 26 | { |
| 27 | timed_average_account(ta, 1); |
| 28 | timed_average_account(ta, 5); |
| 29 | timed_average_account(ta, 2); |
| 30 | timed_average_account(ta, 4); |
| 31 | timed_average_account(ta, 3); |
| 32 | } |
| 33 | |
| 34 | static void test_average(void) |
| 35 | { |
| 36 | TimedAverage ta; |
| 37 | uint64_t result; |
| 38 | int i; |
| 39 | |
| 40 | /* we will compute some average on a period of 1 second */ |
| 41 | timed_average_init(&ta, QEMU_CLOCK_VIRTUAL, NANOSECONDS_PER_SECOND); |
| 42 | |
| 43 | result = timed_average_min(&ta); |
| 44 | g_assert(result == 0); |
| 45 | result = timed_average_avg(&ta); |
| 46 | g_assert(result == 0); |
| 47 | result = timed_average_max(&ta); |
| 48 | g_assert(result == 0); |
| 49 | |
| 50 | for (i = 0; i < 100; i++) { |
| 51 | account(&ta); |
| 52 | result = timed_average_min(&ta); |
| 53 | g_assert(result == 1); |
| 54 | result = timed_average_avg(&ta); |
| 55 | g_assert(result == 3); |
| 56 | result = timed_average_max(&ta); |
| 57 | g_assert(result == 5); |
| 58 | my_clock_value += NANOSECONDS_PER_SECOND / 10; |
| 59 | } |
| 60 | |
| 61 | my_clock_value += NANOSECONDS_PER_SECOND * 100; |
| 62 | |
| 63 | result = timed_average_min(&ta); |
| 64 | g_assert(result == 0); |
| 65 | result = timed_average_avg(&ta); |
| 66 | g_assert(result == 0); |
| 67 | result = timed_average_max(&ta); |
| 68 | g_assert(result == 0); |
| 69 | |
| 70 | for (i = 0; i < 100; i++) { |
| 71 | account(&ta); |
| 72 | result = timed_average_min(&ta); |
| 73 | g_assert(result == 1); |
| 74 | result = timed_average_avg(&ta); |
| 75 | g_assert(result == 3); |
| 76 | result = timed_average_max(&ta); |
| 77 | g_assert(result == 5); |
| 78 | my_clock_value += NANOSECONDS_PER_SECOND / 10; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | int main(int argc, char **argv) |
| 83 | { |
| 84 | /* tests in the same order as the header function declarations */ |
| 85 | g_test_init(&argc, &argv, NULL); |
| 86 | g_test_add_func("/timed-average/average", test_average); |
| 87 | return g_test_run(); |
| 88 | } |
| 89 |