migration: use save_page_use_compression in flush_compressed_data
[qemu/ar7.git] / tests / test-qdist.c
blob9541ce31ebb95d9226ed8e1f3e4f594001c4e24b
1 /*
2 * Copyright (C) 2016, Emilio G. Cota <cota@braap.org>
4 * License: GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7 #include "qemu/osdep.h"
8 #include "qemu/qdist.h"
10 #include <math.h>
12 struct entry_desc {
13 double x;
14 unsigned long count;
16 /* 0 prints a space, 1-8 prints from qdist_blocks[] */
17 int fill_code;
20 /* See: https://en.wikipedia.org/wiki/Block_Elements */
21 static const gunichar qdist_blocks[] = {
22 0x2581,
23 0x2582,
24 0x2583,
25 0x2584,
26 0x2585,
27 0x2586,
28 0x2587,
29 0x2588
32 #define QDIST_NR_BLOCK_CODES ARRAY_SIZE(qdist_blocks)
34 static char *pr_hist(const struct entry_desc *darr, size_t n)
36 GString *s = g_string_new("");
37 size_t i;
39 for (i = 0; i < n; i++) {
40 int fill = darr[i].fill_code;
42 if (fill) {
43 assert(fill <= QDIST_NR_BLOCK_CODES);
44 g_string_append_unichar(s, qdist_blocks[fill - 1]);
45 } else {
46 g_string_append_c(s, ' ');
49 return g_string_free(s, FALSE);
52 static void
53 histogram_check(const struct qdist *dist, const struct entry_desc *darr,
54 size_t n, size_t n_bins)
56 char *pr = qdist_pr_plain(dist, n_bins);
57 char *str = pr_hist(darr, n);
59 g_assert_cmpstr(pr, ==, str);
60 g_free(pr);
61 g_free(str);
64 static void histogram_check_single_full(const struct qdist *dist, size_t n_bins)
66 struct entry_desc desc = { .fill_code = 8 };
68 histogram_check(dist, &desc, 1, n_bins);
71 static void
72 entries_check(const struct qdist *dist, const struct entry_desc *darr, size_t n)
74 size_t i;
76 for (i = 0; i < n; i++) {
77 struct qdist_entry *e = &dist->entries[i];
79 g_assert_cmpuint(e->count, ==, darr[i].count);
83 static void
84 entries_insert(struct qdist *dist, const struct entry_desc *darr, size_t n)
86 size_t i;
88 for (i = 0; i < n; i++) {
89 qdist_add(dist, darr[i].x, darr[i].count);
93 static void do_test_bin(const struct entry_desc *a, size_t n_a,
94 const struct entry_desc *b, size_t n_b)
96 struct qdist qda;
97 struct qdist qdb;
99 qdist_init(&qda);
101 entries_insert(&qda, a, n_a);
102 qdist_inc(&qda, a[0].x);
103 qdist_add(&qda, a[0].x, -1);
105 g_assert_cmpuint(qdist_unique_entries(&qda), ==, n_a);
106 g_assert_cmpfloat(qdist_xmin(&qda), ==, a[0].x);
107 g_assert_cmpfloat(qdist_xmax(&qda), ==, a[n_a - 1].x);
108 histogram_check(&qda, a, n_a, 0);
109 histogram_check(&qda, a, n_a, n_a);
111 qdist_bin__internal(&qdb, &qda, n_b);
112 g_assert_cmpuint(qdb.n, ==, n_b);
113 entries_check(&qdb, b, n_b);
114 g_assert_cmpuint(qdist_sample_count(&qda), ==, qdist_sample_count(&qdb));
116 * No histogram_check() for $qdb, since we'd rebin it and that is a bug.
117 * Instead, regenerate it from $qda.
119 histogram_check(&qda, b, n_b, n_b);
121 qdist_destroy(&qdb);
122 qdist_destroy(&qda);
125 static void do_test_pr(uint32_t opt)
127 static const struct entry_desc desc[] = {
128 [0] = { 1, 900, 8 },
129 [1] = { 2, 1, 1 },
130 [2] = { 3, 2, 1 }
132 static const char border[] = "|";
133 const char *llabel = NULL;
134 const char *rlabel = NULL;
135 struct qdist dist;
136 GString *s;
137 char *str;
138 char *pr;
139 size_t n;
141 n = ARRAY_SIZE(desc);
142 qdist_init(&dist);
144 entries_insert(&dist, desc, n);
145 histogram_check(&dist, desc, n, 0);
147 s = g_string_new("");
149 if (opt & QDIST_PR_LABELS) {
150 unsigned int lopts = opt & (QDIST_PR_NODECIMAL |
151 QDIST_PR_PERCENT |
152 QDIST_PR_100X |
153 QDIST_PR_NOBINRANGE);
155 if (lopts == 0) {
156 llabel = "[1.0,1.7)";
157 rlabel = "[2.3,3.0]";
158 } else if (lopts == QDIST_PR_NODECIMAL) {
159 llabel = "[1,2)";
160 rlabel = "[2,3]";
161 } else if (lopts == (QDIST_PR_PERCENT | QDIST_PR_NODECIMAL)) {
162 llabel = "[1,2)%";
163 rlabel = "[2,3]%";
164 } else if (lopts == QDIST_PR_100X) {
165 llabel = "[100.0,166.7)";
166 rlabel = "[233.3,300.0]";
167 } else if (lopts == (QDIST_PR_NOBINRANGE | QDIST_PR_NODECIMAL)) {
168 llabel = "1";
169 rlabel = "3";
170 } else {
171 g_assert_cmpstr("BUG", ==, "This is not meant to be exhaustive");
175 if (llabel) {
176 g_string_append(s, llabel);
178 if (opt & QDIST_PR_BORDER) {
179 g_string_append(s, border);
182 str = pr_hist(desc, n);
183 g_string_append(s, str);
184 g_free(str);
186 if (opt & QDIST_PR_BORDER) {
187 g_string_append(s, border);
189 if (rlabel) {
190 g_string_append(s, rlabel);
193 str = g_string_free(s, FALSE);
194 pr = qdist_pr(&dist, n, opt);
195 g_assert_cmpstr(pr, ==, str);
196 g_free(pr);
197 g_free(str);
199 qdist_destroy(&dist);
202 static inline void do_test_pr_label(uint32_t opt)
204 opt |= QDIST_PR_LABELS;
205 do_test_pr(opt);
208 static void test_pr(void)
210 do_test_pr(0);
212 do_test_pr(QDIST_PR_BORDER);
214 /* 100X should be ignored because we're not setting LABELS */
215 do_test_pr(QDIST_PR_100X);
217 do_test_pr_label(0);
218 do_test_pr_label(QDIST_PR_NODECIMAL);
219 do_test_pr_label(QDIST_PR_PERCENT | QDIST_PR_NODECIMAL);
220 do_test_pr_label(QDIST_PR_100X);
221 do_test_pr_label(QDIST_PR_NOBINRANGE | QDIST_PR_NODECIMAL);
224 static void test_bin_shrink(void)
226 static const struct entry_desc a[] = {
227 [0] = { 0.0, 42922, 7 },
228 [1] = { 0.25, 47834, 8 },
229 [2] = { 0.50, 26628, 0 },
230 [3] = { 0.625, 597, 4 },
231 [4] = { 0.75, 10298, 1 },
232 [5] = { 0.875, 22, 2 },
233 [6] = { 1.0, 2771, 1 }
235 static const struct entry_desc b[] = {
236 [0] = { 0.0, 42922, 7 },
237 [1] = { 0.25, 47834, 8 },
238 [2] = { 0.50, 27225, 3 },
239 [3] = { 0.75, 13091, 1 }
242 return do_test_bin(a, ARRAY_SIZE(a), b, ARRAY_SIZE(b));
245 static void test_bin_expand(void)
247 static const struct entry_desc a[] = {
248 [0] = { 0.0, 11713, 5 },
249 [1] = { 0.25, 20294, 0 },
250 [2] = { 0.50, 17266, 8 },
251 [3] = { 0.625, 1506, 0 },
252 [4] = { 0.75, 10355, 6 },
253 [5] = { 0.833, 2, 1 },
254 [6] = { 0.875, 99, 4 },
255 [7] = { 1.0, 4301, 2 }
257 static const struct entry_desc b[] = {
258 [0] = { 0.0, 11713, 5 },
259 [1] = { 0.0, 0, 0 },
260 [2] = { 0.0, 20294, 8 },
261 [3] = { 0.0, 0, 0 },
262 [4] = { 0.0, 0, 0 },
263 [5] = { 0.0, 17266, 6 },
264 [6] = { 0.0, 1506, 1 },
265 [7] = { 0.0, 10355, 4 },
266 [8] = { 0.0, 101, 1 },
267 [9] = { 0.0, 4301, 2 }
270 return do_test_bin(a, ARRAY_SIZE(a), b, ARRAY_SIZE(b));
273 static void test_bin_precision(void)
275 static const struct entry_desc a[] = {
276 [0] = { 0, 213549, 8 },
277 [1] = { 1, 70, 1 },
279 static const struct entry_desc b[] = {
280 [0] = { 0, 213549, 8 },
281 [1] = { 0, 70, 1 },
284 return do_test_bin(a, ARRAY_SIZE(a), b, ARRAY_SIZE(b));
287 static void test_bin_simple(void)
289 static const struct entry_desc a[] = {
290 [0] = { 10, 101, 8 },
291 [1] = { 11, 0, 0 },
292 [2] = { 12, 2, 1 }
294 static const struct entry_desc b[] = {
295 [0] = { 0, 101, 8 },
296 [1] = { 0, 0, 0 },
297 [2] = { 0, 0, 0 },
298 [3] = { 0, 0, 0 },
299 [4] = { 0, 2, 1 }
302 return do_test_bin(a, ARRAY_SIZE(a), b, ARRAY_SIZE(b));
305 static void test_single_full(void)
307 struct qdist dist;
309 qdist_init(&dist);
311 qdist_add(&dist, 3, 102);
312 g_assert_cmpfloat(qdist_avg(&dist), ==, 3);
313 g_assert_cmpfloat(qdist_xmin(&dist), ==, 3);
314 g_assert_cmpfloat(qdist_xmax(&dist), ==, 3);
316 histogram_check_single_full(&dist, 0);
317 histogram_check_single_full(&dist, 1);
318 histogram_check_single_full(&dist, 10);
320 qdist_destroy(&dist);
323 static void test_single_empty(void)
325 struct qdist dist;
326 char *pr;
328 qdist_init(&dist);
330 qdist_add(&dist, 3, 0);
331 g_assert_cmpuint(qdist_sample_count(&dist), ==, 0);
332 g_assert(isnan(qdist_avg(&dist)));
333 g_assert_cmpfloat(qdist_xmin(&dist), ==, 3);
334 g_assert_cmpfloat(qdist_xmax(&dist), ==, 3);
336 pr = qdist_pr_plain(&dist, 0);
337 g_assert_cmpstr(pr, ==, " ");
338 g_free(pr);
340 pr = qdist_pr_plain(&dist, 1);
341 g_assert_cmpstr(pr, ==, " ");
342 g_free(pr);
344 pr = qdist_pr_plain(&dist, 2);
345 g_assert_cmpstr(pr, ==, " ");
346 g_free(pr);
348 qdist_destroy(&dist);
351 static void test_none(void)
353 struct qdist dist;
354 char *pr;
356 qdist_init(&dist);
358 g_assert(isnan(qdist_avg(&dist)));
359 g_assert(isnan(qdist_xmin(&dist)));
360 g_assert(isnan(qdist_xmax(&dist)));
362 pr = qdist_pr_plain(&dist, 0);
363 g_assert_cmpstr(pr, ==, "(empty)");
364 g_free(pr);
366 pr = qdist_pr_plain(&dist, 2);
367 g_assert_cmpstr(pr, ==, "(empty)");
368 g_free(pr);
370 pr = qdist_pr(&dist, 0, QDIST_PR_BORDER);
371 g_assert_cmpstr(pr, ==, "(empty)");
372 g_free(pr);
374 qdist_destroy(&dist);
377 int main(int argc, char *argv[])
379 g_test_init(&argc, &argv, NULL);
380 g_test_add_func("/qdist/none", test_none);
381 g_test_add_func("/qdist/single/empty", test_single_empty);
382 g_test_add_func("/qdist/single/full", test_single_full);
383 g_test_add_func("/qdist/binning/simple", test_bin_simple);
384 g_test_add_func("/qdist/binning/precision", test_bin_precision);
385 g_test_add_func("/qdist/binning/expand", test_bin_expand);
386 g_test_add_func("/qdist/binning/shrink", test_bin_shrink);
387 g_test_add_func("/qdist/pr", test_pr);
388 return g_test_run();