File: stats.c

package info (click to toggle)
rauc 1.15-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,336 kB
  • sloc: ansic: 36,989; python: 3,354; sh: 1,391; xml: 53; makefile: 41
file content (74 lines) | stat: -rw-r--r-- 1,735 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <locale.h>
#include <glib.h>

#include "stats.h"

static void test_basic(void)
{
	g_autoptr(RaucStats) stats = NULL;

	stats = r_stats_new("test");
	g_assert_nonnull(stats);
	g_assert_cmpstr(stats->label, ==, "test");

	r_stats_add(stats, 1.0);
	r_stats_add(stats, 2.0);

	g_assert_cmpuint(stats->count, ==, 2);
	g_assert_cmpfloat(r_stats_get_avg(stats), ==, 1.5);
	g_assert_cmpfloat(r_stats_get_recent_avg(stats), ==, 1.5);

	for (guint i = 0; i < 128; i++) {
		r_stats_add(stats, i);
	}

	g_assert_cmpuint(stats->count, ==, 130);

	/*
	 * Don't use an exact compare on non-trivial floating-point
	 * calculations.
	 */
	g_assert_cmpfloat_with_epsilon(r_stats_get_avg(stats), 62.54615384615385, 1e-10);
	g_assert_cmpfloat_with_epsilon(r_stats_get_recent_avg(stats), 95.5, 1e-10);
	g_assert_cmpfloat_with_epsilon(stats->sum, 8131.0, 1e-10);
	g_assert_cmpfloat(stats->min, ==, 0.0);
	g_assert_cmpfloat(stats->max, ==, 127.0);
}

static void test_queue(void)
{
	g_autoptr(RaucStats) stats = NULL;

	r_test_stats_start();
	test_basic();
	stats = r_stats_new("test 2");
	g_clear_pointer(&stats, r_stats_free);
	r_test_stats_stop();

	stats = r_test_stats_next();
	g_assert_nonnull(stats);
	g_assert_cmpstr(stats->label, ==, "test");
	g_clear_pointer(&stats, r_stats_free);

	stats = r_test_stats_next();
	g_assert_nonnull(stats);
	g_assert_cmpstr(stats->label, ==, "test 2");
	g_clear_pointer(&stats, r_stats_free);

	r_test_stats_start();
	r_test_stats_stop();
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL, "C");

	g_assert(g_setenv("GIO_USE_VFS", "local", TRUE));

	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/stats/basic", test_basic);
	g_test_add_func("/stats/queue", test_queue);

	return g_test_run();
}