qemu/tests/test-timed-average.c
<<
>>
Prefs
   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 <glib.h>
  14#include <unistd.h>
  15
  16#include "qemu/timed-average.h"
  17
  18/* This is the clock for QEMU_CLOCK_VIRTUAL */
  19static int64_t my_clock_value;
  20
  21int64_t cpu_get_clock(void)
  22{
  23    return my_clock_value;
  24}
  25
  26static void account(TimedAverage *ta)
  27{
  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);
  33}
  34
  35static void test_average(void)
  36{
  37    TimedAverage ta;
  38    uint64_t result;
  39    int i;
  40
  41    /* we will compute some average on a period of 1 second */
  42    timed_average_init(&ta, QEMU_CLOCK_VIRTUAL, NANOSECONDS_PER_SECOND);
  43
  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);
  50
  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;
  60    }
  61
  62    my_clock_value += NANOSECONDS_PER_SECOND * 100;
  63
  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);
  70
  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;
  80    }
  81}
  82
  83int main(int argc, char **argv)
  84{
  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();
  89}
  90
  91