qapi: Replace ad hoc "since" documentation by member documentation
[qemu/armbru.git] / tests / unit / test-timed-average.c
blob82c92500df8d397a99973ef3094619830ccd36d4
1 /*
2 * Timed average computation tests
4 * Copyright Nodalink, EURL. 2014
6 * Authors:
7 * BenoƮt Canet <benoit.canet@nodalink.com>
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.
13 #include "qemu/osdep.h"
14 #include "sysemu/cpu-timers.h"
15 #include "qemu/timed-average.h"
17 /* This is the clock for QEMU_CLOCK_VIRTUAL */
18 static int64_t my_clock_value;
20 int64_t cpu_get_clock(void)
22 return my_clock_value;
25 static void account(TimedAverage *ta)
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);
34 static void test_average(void)
36 TimedAverage ta;
37 uint64_t result;
38 int i;
40 /* we will compute some average on a period of 1 second */
41 timed_average_init(&ta, QEMU_CLOCK_VIRTUAL, NANOSECONDS_PER_SECOND);
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);
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;
61 my_clock_value += NANOSECONDS_PER_SECOND * 100;
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);
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;
82 int main(int argc, char **argv)
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();