-
Notifications
You must be signed in to change notification settings - Fork 44
/
test_helpers.c
152 lines (134 loc) · 3.79 KB
/
test_helpers.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/** \file test_helpers.c
*
* \brief Common helper functions for unit tests.
*
* If TEST is not defined, this file will appear as an empty translation
* unit to the compiler. Thus this file should not be compiled in non-test
* builds.
*
* This file is licensed as described by the file LICENCE.
*/
#ifdef TEST
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "test_helpers.h"
/** Number of test cases which succeeded. */
static int succeeded;
/** Number of test cases which failed. */
static int failed;
/** Time when unit tests were started. */
static time_t start_time;
/** Skip whitespace in an open file, starting from the current position within
* the file and ending such that the file position points to the first
* non-whitespace character found.
* \param f The file to skip whitespace in.
*/
void skipWhiteSpace(FILE *f)
{
int one_char;
do
{
one_char = fgetc(f);
} while (((one_char == ' ') || (one_char == '\t') || (one_char == '\n') || (one_char == '\r'))
&& !feof(f));
ungetc(one_char, f);
}
/** Skip the contents of a line in an open file, starting from the current
* position within the file and ending such that the file position points to
* the first character of the next line.
* \param f The file to skip a line in.
*/
void skipLine(FILE *f)
{
int one_char;
do
{
one_char = fgetc(f);
} while ((one_char != '\n') && !feof(f));
}
/** Display a multi-precision integer of arbitrary size as a hex string.
* \param number The byte array containing the integer.
* \param size The size, in number of bytes, of the byte array.
* \param is_big_endian This should be true if the integer is stored in
* big-endian format and should be false if the number
* is stored in little-endian format.
*/
void bigPrintVariableSize(const uint8_t *number, const unsigned int size, const bool is_big_endian)
{
unsigned int i;
if (is_big_endian)
{
for (i = 0; i < size; i++)
{
printf("%02x", number[i]);
}
}
else
{
for (i = (uint8_t)(size - 1); i < size; i--)
{
printf("%02x", number[i]);
}
}
}
/** Display a 128 bit big-endian multi-precision integer as a hex string.
* \param buffer 16 byte array containing the number to display.
*/
void printBigEndian16(const uint8_t *buffer)
{
bigPrintVariableSize(buffer, 16, true);
}
/** Display a 256 bit little-endian multi-precision integer as a hex string.
* \param buffer 32 byte array containing the number to display.
*/
void printLittleEndian32(const BigNum256 buffer)
{
bigPrintVariableSize(buffer, 32, false);
}
/** Fill array with pseudo-random testing data.
* \param out Byte array to fill.
* \param len Number of bytes to write.
*/
void fillWithRandom(uint8_t *out, unsigned int len)
{
unsigned int i;
for (i = 0; i < len; i++)
{
out[i] = (uint8_t)rand();
}
}
/** Call this whenever a test case succeeds. */
void reportSuccess(void)
{
succeeded++;
}
/** Call this whenever a test case fails. */
void reportFailure(void)
{
failed++;
}
/** This must be called before running any unit tests.
* \param source_file_name The name of the file being unit tested. The use
* of the __FILE__ macro is probably a good idea.
*/
void initTests(const char *source_file_name)
{
succeeded = 0;
failed = 0;
srand(42); // make sure tests which rely on rand() are deterministic
printf("Running unit tests for file: %s\n", source_file_name);
time(&start_time);
}
/** This must be called after running all unit tests for a file. It will
* report test statistics.
*/
void finishTests(void)
{
time_t finish_time;
time(&finish_time);
printf("Tests required about %g seconds\n", difftime(finish_time, start_time));
printf("Tests which succeeded: %d\n", succeeded);
printf("Tests which failed: %d\n", failed);
}
#endif // #ifdef TEST