[infiniband] Use "%d" as format specifier for LIDs
[ipxe.git] / src / tests / profile_test.c
1 /*
2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * Profiling self-tests
29 *
30 */
31
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34
35 #include <string.h>
36 #include <assert.h>
37 #include <ipxe/test.h>
38 #include <ipxe/profile.h>
39
40 /** A profiling test */
41 struct profile_test {
42 /** Sample values */
43 const unsigned long *samples;
44 /** Number of samples */
45 unsigned int count;
46 /** Expected mean sample value */
47 unsigned long mean;
48 /** Expected standard deviation */
49 unsigned long stddev;
50 };
51
52 /** Define inline data */
53 #define DATA(...) { __VA_ARGS__ }
54
55 /** Define a profiling test */
56 #define PROFILE_TEST( name, MEAN, STDDEV, SAMPLES ) \
57 static const unsigned long name ## _samples[] = SAMPLES; \
58 static struct profile_test name = { \
59 .samples = name ## _samples, \
60 .count = ( sizeof ( name ## _samples ) / \
61 sizeof ( name ## _samples [0] ) ), \
62 .mean = MEAN, \
63 .stddev = STDDEV, \
64 }
65
66 /** Empty data set */
67 PROFILE_TEST ( empty, 0, 0, DATA() );
68
69 /** Single-element data set (zero) */
70 PROFILE_TEST ( zero, 0, 0, DATA ( 0 ) );
71
72 /** Single-element data set (non-zero) */
73 PROFILE_TEST ( single, 42, 0, DATA ( 42 ) );
74
75 /** Multiple identical element data set */
76 PROFILE_TEST ( identical, 69, 0, DATA ( 69, 69, 69, 69, 69, 69, 69 ) );
77
78 /** Small element data set */
79 PROFILE_TEST ( small, 5, 2, DATA ( 3, 5, 9, 4, 3, 2, 5, 7 ) );
80
81 /** Random data set */
82 PROFILE_TEST ( random, 70198, 394,
83 DATA ( 69772, 70068, 70769, 69653, 70663, 71078, 70101, 70341,
84 70215, 69600, 70020, 70456, 70421, 69972, 70267, 69999,
85 69972 ) );
86
87 /** Large-valued random data set */
88 PROFILE_TEST ( large, 93533894UL, 25538UL,
89 DATA ( 93510333UL, 93561169UL, 93492361UL, 93528647UL,
90 93557566UL, 93503465UL, 93540126UL, 93549020UL,
91 93502307UL, 93527320UL, 93537152UL, 93540125UL,
92 93550773UL, 93586731UL, 93521312UL ) );
93
94 /**
95 * Report a profiling test result
96 *
97 * @v test Profiling test
98 * @v file Test code file
99 * @v line Test code line
100 */
101 static void profile_okx ( struct profile_test *test, const char *file,
102 unsigned int line ) {
103 struct profiler profiler;
104 unsigned long mean;
105 unsigned long stddev;
106 unsigned int i;
107
108 /* Initialise profiler */
109 memset ( &profiler, 0, sizeof ( profiler ) );
110
111 /* Record sample values */
112 for ( i = 0 ; i < test->count ; i++ )
113 profile_update ( &profiler, test->samples[i] );
114
115 /* Check resulting statistics */
116 mean = profile_mean ( &profiler );
117 stddev = profile_stddev ( &profiler );
118 DBGC ( test, "PROFILE calculated mean %ld stddev %ld\n", mean, stddev );
119 okx ( mean == test->mean, file, line );
120 okx ( stddev == test->stddev, file, line );
121 }
122 #define profile_ok( test ) profile_okx ( test, __FILE__, __LINE__ )
123
124 /**
125 * Perform profiling self-tests
126 *
127 */
128 static void profile_test_exec ( void ) {
129
130 /* Perform profiling tests */
131 profile_ok ( &empty );
132 profile_ok ( &zero );
133 profile_ok ( &single );
134 profile_ok ( &identical );
135 profile_ok ( &small );
136 profile_ok ( &random );
137 profile_ok ( &large );
138 }
139
140 /** Profiling self-test */
141 struct self_test profile_test __self_test = {
142 .name = "profile",
143 .exec = profile_test_exec,
144 };