2
0

test-timed-average.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "qemu/osdep.h"
  13. #include "qemu/timed-average.h"
  14. /* This is the clock for QEMU_CLOCK_VIRTUAL */
  15. static int64_t my_clock_value;
  16. int64_t cpu_get_clock(void)
  17. {
  18. return my_clock_value;
  19. }
  20. static void account(TimedAverage *ta)
  21. {
  22. timed_average_account(ta, 1);
  23. timed_average_account(ta, 5);
  24. timed_average_account(ta, 2);
  25. timed_average_account(ta, 4);
  26. timed_average_account(ta, 3);
  27. }
  28. static void test_average(void)
  29. {
  30. TimedAverage ta;
  31. uint64_t result;
  32. int i;
  33. /* we will compute some average on a period of 1 second */
  34. timed_average_init(&ta, QEMU_CLOCK_VIRTUAL, NANOSECONDS_PER_SECOND);
  35. result = timed_average_min(&ta);
  36. g_assert(result == 0);
  37. result = timed_average_avg(&ta);
  38. g_assert(result == 0);
  39. result = timed_average_max(&ta);
  40. g_assert(result == 0);
  41. for (i = 0; i < 100; i++) {
  42. account(&ta);
  43. result = timed_average_min(&ta);
  44. g_assert(result == 1);
  45. result = timed_average_avg(&ta);
  46. g_assert(result == 3);
  47. result = timed_average_max(&ta);
  48. g_assert(result == 5);
  49. my_clock_value += NANOSECONDS_PER_SECOND / 10;
  50. }
  51. my_clock_value += NANOSECONDS_PER_SECOND * 100;
  52. result = timed_average_min(&ta);
  53. g_assert(result == 0);
  54. result = timed_average_avg(&ta);
  55. g_assert(result == 0);
  56. result = timed_average_max(&ta);
  57. g_assert(result == 0);
  58. for (i = 0; i < 100; i++) {
  59. account(&ta);
  60. result = timed_average_min(&ta);
  61. g_assert(result == 1);
  62. result = timed_average_avg(&ta);
  63. g_assert(result == 3);
  64. result = timed_average_max(&ta);
  65. g_assert(result == 5);
  66. my_clock_value += NANOSECONDS_PER_SECOND / 10;
  67. }
  68. }
  69. int main(int argc, char **argv)
  70. {
  71. /* tests in the same order as the header function declarations */
  72. g_test_init(&argc, &argv, NULL);
  73. g_test_add_func("/timed-average/average", test_average);
  74. return g_test_run();
  75. }