1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25 #include "igt_core.h"
26 #include "igt_stats.h"
27
28 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
29
push_fixture_1(igt_stats_t * stats)30 static void push_fixture_1(igt_stats_t *stats)
31 {
32 igt_stats_push(stats, 2);
33 igt_stats_push(stats, 4);
34 igt_stats_push(stats, 6);
35 igt_stats_push(stats, 8);
36 igt_stats_push(stats, 10);
37 }
38
39 /* Make sure we zero igt_stats_t fields at init() time */
test_init_zero(void)40 static void test_init_zero(void)
41 {
42 igt_stats_t stats;
43
44 stats.mean = 1.;
45 igt_stats_init(&stats);
46 igt_assert_eq_double(stats.mean, 0.);
47 }
48
test_init(void)49 static void test_init(void)
50 {
51 igt_stats_t stats;
52
53 igt_stats_init(&stats);
54
55 /*
56 * Make sure we default to representing only a sample of a bigger
57 * population.
58 */
59 igt_assert(igt_stats_is_population(&stats) == false);
60 }
61
test_min_max(void)62 static void test_min_max(void)
63 {
64 igt_stats_t stats;
65
66 igt_stats_init(&stats);
67 push_fixture_1(&stats);
68
69 igt_assert(igt_stats_get_min(&stats) == 2);
70 igt_assert(igt_stats_get_max(&stats) == 10);
71 }
72
test_range(void)73 static void test_range(void)
74 {
75 igt_stats_t stats;
76
77 igt_stats_init(&stats);
78 push_fixture_1(&stats);
79
80 igt_assert(igt_stats_get_range(&stats) == 8);
81 }
82
83 /*
84 * Examples taken from: https://en.wikipedia.org/wiki/Quartile
85 * The values are shifted a bit to test we do indeed start by sorting data
86 * set.
87 */
test_quartiles(void)88 static void test_quartiles(void)
89 {
90 static const uint64_t s1[] =
91 { 47, 49, 6, 7, 15, 36, 39, 40, 41, 42, 43 };
92 static const uint64_t s2[] = { 40, 41, 7, 15, 36, 39 };
93 igt_stats_t stats;
94 double q1, q2, q3;
95
96 /* s1, odd number of data points */
97 igt_stats_init(&stats);
98 igt_stats_push_array(&stats, s1, ARRAY_SIZE(s1));
99
100 igt_stats_get_quartiles(&stats, &q1, &q2, &q3);
101 igt_assert_eq_double(q1, 25.5);
102 igt_assert_eq_double(q2, 40);
103 igt_assert_eq_double(q3, 42.5);
104 igt_assert_eq_double(igt_stats_get_median(&stats), 40);
105 igt_assert_eq_double(igt_stats_get_iqr(&stats), 42.5 - 25.5);
106
107 igt_stats_fini(&stats);
108
109 /* s1, even number of data points */
110 igt_stats_init(&stats);
111 igt_stats_push_array(&stats, s2, ARRAY_SIZE(s2));
112
113 igt_stats_get_quartiles(&stats, &q1, &q2, &q3);
114 igt_assert_eq_double(q1, 15);
115 igt_assert_eq_double(q2, 37.5);
116 igt_assert_eq_double(q3, 40);
117 igt_assert_eq_double(igt_stats_get_median(&stats), 37.5);
118 igt_assert_eq_double(igt_stats_get_iqr(&stats), 40 - 15);
119
120 igt_stats_fini(&stats);
121 }
122
test_invalidate_sorted(void)123 static void test_invalidate_sorted(void)
124 {
125 igt_stats_t stats;
126 static const uint64_t s1_truncated[] =
127 { 47, 49, 6, 7, 15, 36, 39, 40, 41, 42};
128 double median1, median2;
129
130 igt_stats_init(&stats);
131 igt_stats_push_array(&stats, s1_truncated, ARRAY_SIZE(s1_truncated));
132 median1 = igt_stats_get_median(&stats);
133
134 igt_stats_push(&stats, 43);
135 median2 = igt_stats_get_median(&stats);
136
137 igt_assert_eq_double(median2, 40);
138 igt_assert(median1 != median2);
139 }
140
test_mean(void)141 static void test_mean(void)
142 {
143 igt_stats_t stats;
144 double mean;
145
146 igt_stats_init(&stats);
147 push_fixture_1(&stats);
148
149 mean = igt_stats_get_mean(&stats);
150 igt_assert_eq_double(mean, (2 + 4 + 6 + 8 + 10) / 5.);
151
152 igt_stats_fini(&stats);
153 }
154
test_invalidate_mean(void)155 static void test_invalidate_mean(void)
156 {
157 igt_stats_t stats;
158 double mean1, mean2;
159
160 igt_stats_init(&stats);
161 push_fixture_1(&stats);
162
163 mean1 = igt_stats_get_mean(&stats);
164 igt_assert_eq_double(mean1, (2 + 4 + 6 + 8 + 10) / 5.);
165
166 igt_stats_push(&stats, 100);
167
168 mean2 = igt_stats_get_mean(&stats);
169 igt_assert(mean1 != mean2);
170
171 igt_stats_fini(&stats);
172 }
173
174 /*
175 * Taken from the "Basic examples" section of:
176 * https://en.wikipedia.org/wiki/Standard_deviation
177 */
test_std_deviation(void)178 static void test_std_deviation(void)
179 {
180 igt_stats_t stats;
181 double mean, variance, std_deviation;
182
183 igt_stats_init(&stats);
184 igt_stats_set_population(&stats, true);
185
186 igt_stats_push(&stats, 2);
187 igt_stats_push(&stats, 4);
188 igt_stats_push(&stats, 4);
189 igt_stats_push(&stats, 4);
190 igt_stats_push(&stats, 5);
191 igt_stats_push(&stats, 5);
192 igt_stats_push(&stats, 7);
193 igt_stats_push(&stats, 9);
194
195 mean = igt_stats_get_mean(&stats);
196 igt_assert_eq_double(mean, (2 + 3 * 4 + 2 * 5 + 7 + 9) / 8.);
197
198 variance = igt_stats_get_variance(&stats);
199 igt_assert_eq_double(variance, 4);
200
201 std_deviation = igt_stats_get_std_deviation(&stats);
202 igt_assert_eq_double(std_deviation, 2);
203
204 igt_stats_fini(&stats);
205 }
206
test_reallocation(void)207 static void test_reallocation(void)
208 {
209 igt_stats_t stats;
210 unsigned int i;
211
212 igt_stats_init_with_size(&stats, 1);
213
214 for (i = 0; i < 101; i++) {
215 igt_stats_push(&stats, i);
216 /* also triggers ->sorted reallocations */
217 if (i > 10)
218 igt_stats_get_median(&stats);
219 }
220 igt_assert(!stats.is_float);
221
222 igt_assert_eq(stats.n_values, 101);
223 for (i = 0; i < 101; i++)
224 igt_assert_eq(stats.values_u64[i], i);
225 igt_assert_eq_double(igt_stats_get_mean(&stats), 50.0);
226 igt_assert_eq_double(igt_stats_get_median(&stats), 50.0);
227
228 igt_stats_fini(&stats);
229 }
230
231 igt_simple_main
232 {
233 test_init_zero();
234 test_init();
235 test_min_max();
236 test_range();
237 test_quartiles();
238 test_invalidate_sorted();
239 test_mean();
240 test_invalidate_mean();
241 test_std_deviation();
242 test_reallocation();
243 }
244