qapi: Simplify QAPISchemaObjectTypeVariants.check()
[qemu/ar7.git] / tests / test-timed-average.c
bloba049799b80422e5d568b3ed5c7f0990481d7fa13
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 <glib.h>
14 #include <unistd.h>
16 #include "qemu/timed-average.h"
18 /* This is the clock for QEMU_CLOCK_VIRTUAL */
19 static int64_t my_clock_value;
21 int64_t cpu_get_clock(void)
23 return my_clock_value;
26 static void account(TimedAverage *ta)
28 timed_average_account(ta, 1);
29 timed_average_account(ta, 5);
30 timed_average_account(ta, 2);
31 timed_average_account(ta, 4);
32 timed_average_account(ta, 3);
35 static void test_average(void)
37 TimedAverage ta;
38 uint64_t result;
39 int i;
41 /* we will compute some average on a period of 1 second */
42 timed_average_init(&ta, QEMU_CLOCK_VIRTUAL, NANOSECONDS_PER_SECOND);
44 result = timed_average_min(&ta);
45 g_assert(result == 0);
46 result = timed_average_avg(&ta);
47 g_assert(result == 0);
48 result = timed_average_max(&ta);
49 g_assert(result == 0);
51 for (i = 0; i < 100; i++) {
52 account(&ta);
53 result = timed_average_min(&ta);
54 g_assert(result == 1);
55 result = timed_average_avg(&ta);
56 g_assert(result == 3);
57 result = timed_average_max(&ta);
58 g_assert(result == 5);
59 my_clock_value += NANOSECONDS_PER_SECOND / 10;
62 my_clock_value += NANOSECONDS_PER_SECOND * 100;
64 result = timed_average_min(&ta);
65 g_assert(result == 0);
66 result = timed_average_avg(&ta);
67 g_assert(result == 0);
68 result = timed_average_max(&ta);
69 g_assert(result == 0);
71 for (i = 0; i < 100; i++) {
72 account(&ta);
73 result = timed_average_min(&ta);
74 g_assert(result == 1);
75 result = timed_average_avg(&ta);
76 g_assert(result == 3);
77 result = timed_average_max(&ta);
78 g_assert(result == 5);
79 my_clock_value += NANOSECONDS_PER_SECOND / 10;
83 int main(int argc, char **argv)
85 /* tests in the same order as the header function declarations */
86 g_test_init(&argc, &argv, NULL);
87 g_test_add_func("/timed-average/average", test_average);
88 return g_test_run();