utils.cpp 5.22 KB
Newer Older
1
2
3
/** Unit tests for utilities
 *
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
Steffen Vogel's avatar
Steffen Vogel committed
4
 * @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * @license GNU General Public License (version 3)
 *
 * VILLAScommon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#include <criterion/criterion.h>

Steffen Vogel's avatar
Steffen Vogel committed
25
#include <villas/colors.hpp>
Steffen Vogel's avatar
Steffen Vogel committed
26
27
28
29
#include <villas/utils.hpp>
#include <villas/version.hpp>
#include <villas/cpuset.hpp>
#include <villas/log.hpp>
30

Steffen Vogel's avatar
Steffen Vogel committed
31
using namespace villas::utils;
Steffen Vogel's avatar
Steffen Vogel committed
32

Steffen Vogel's avatar
Steffen Vogel committed
33
// cppcheck-suppress unknownMacro
34
TestSuite(utils, .description = "Utilities");
Steffen Vogel's avatar
Steffen Vogel committed
35

36
37
38
39
40
41
42
/* Simple normality test for 1,2,3s intervals */
Test(utils, box_muller)
{
	double n;
	unsigned sigma[3] = { 0 };
	unsigned iter = 1000000;

Steffen Vogel's avatar
Steffen Vogel committed
43
	for (unsigned i = 0; i < iter; i++) {
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
		n = box_muller(0, 1);

		if      (n > 2 || n < -2) sigma[2]++;
		else if (n > 1 || n < -1) sigma[1]++;
		else                     sigma[0]++;
	}

#if 0
	printf("%f %f %f\n",
		(double) sigma[2] / iter,
		(double) sigma[1] / iter,
		(double) sigma[0] / iter);
#endif

	/* The random variable generated by the Box Muller transform is
	 * not an ideal normal distributed variable.
	 * The numbers from below are empirically measured. */
	cr_assert_float_eq((double) sigma[2] / iter, 0.045527, 1e-2);
	cr_assert_float_eq((double) sigma[1] / iter, 0.271644, 1e-2);
	cr_assert_float_eq((double) sigma[0] / iter, 0.682829, 1e-2);
}

#ifdef __linux__
Test(utils, cpuset)
{
Steffen Vogel's avatar
Steffen Vogel committed
69
	using villas::utils::CpuSet;
70
71
72

	uintmax_t int1 = 0x1234567890ABCDEFULL;

Steffen Vogel's avatar
Steffen Vogel committed
73
	CpuSet cset1(int1);
74

Steffen Vogel's avatar
Steffen Vogel committed
75
	std::string cset1_str = cset1;
76

Steffen Vogel's avatar
Steffen Vogel committed
77
	CpuSet cset2(cset1_str);
78

Steffen Vogel's avatar
Steffen Vogel committed
79
	cr_assert_eq(cset1, cset2);
80

Steffen Vogel's avatar
Steffen Vogel committed
81
	uintmax_t int2 = cset2;
82
83

	cr_assert_eq(int1, int2);
Steffen Vogel's avatar
Steffen Vogel committed
84
85
86
87
88
89
90

	CpuSet cset3("1-5");
	CpuSet cset4("1,2,3,4,5");

	cr_assert_eq(cset3, cset4);
	cr_assert_eq(cset3.count(), 5);

91
92
	cr_assert(cset3.isSet(3));
	cr_assert_not(cset3.isSet(6));
Steffen Vogel's avatar
Steffen Vogel committed
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

	cr_assert(cset3[3]);
	cr_assert_not(cset3[6]);

	cset4.set(6);
	cr_assert(cset4[6]);

	cset4.clear(6);
	cr_assert_not(cset4[6]);

	cr_assert_str_eq(static_cast<std::string>(cset4).c_str(), "1-5");

	cr_assert_any_throw(CpuSet cset5("0-"));

	CpuSet cset6;
	cr_assert(cset6.empty());
	cr_assert_eq(cset6.count(), 0);

	cr_assert((~cset6).full());
	cr_assert((cset1 | ~cset1).full());
	cr_assert((cset1 ^ cset1).empty());
	cr_assert((cset1 & cset6).empty());

	cset1.zero();
	cr_assert(cset1.empty());
118
119
120
121
122
123
124
125
126
127
128
}
#endif /* __linux__ */

Test(utils, memdup)
{
	char orig[1024], *copy;
	size_t len;

	len = read_random(orig, sizeof(orig));
	cr_assert_eq(len, sizeof(orig));

Steffen Vogel's avatar
Steffen Vogel committed
129
	copy = (char *) memdup(orig, sizeof(orig));
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
	cr_assert_not_null(copy);
	cr_assert_arr_eq(copy, orig, sizeof(orig));

	free(copy);
}

Test(utils, is_aligned)
{
	/* Positive */
	cr_assert(IS_ALIGNED(1, 1));
	cr_assert(IS_ALIGNED(128, 64));

	/* Negative */
	cr_assert(!IS_ALIGNED(55, 16));
	cr_assert(!IS_ALIGNED(55, 55));
	cr_assert(!IS_ALIGNED(1128, 256));
}

Test(utils, ceil)
{
	cr_assert_eq(CEIL(10, 3), 4);
	cr_assert_eq(CEIL(10, 5), 2);
	cr_assert_eq(CEIL(4, 3), 2);
}

Test(utils, is_pow2)
{
	/* Positive */
	cr_assert(IS_POW2(1));
	cr_assert(IS_POW2(2));
	cr_assert(IS_POW2(64));

	/* Negative */
	cr_assert(!IS_POW2(0));
	cr_assert(!IS_POW2(3));
	cr_assert(!IS_POW2(11111));
	cr_assert(!IS_POW2(-1));
}

Test(utils, strf)
{
Steffen Vogel's avatar
Steffen Vogel committed
171
	char *buf = nullptr;
172

Steffen Vogel's avatar
Steffen Vogel committed
173
	buf = strcatf(&buf, "Hallo %s", "Steffen.");
174
175
176
177
178
179
180
181
182
183
	cr_assert_str_eq(buf, "Hallo Steffen.");

	strcatf(&buf, " Its Monday %uth %s %u.", 13, "August", 2018);
	cr_assert_str_eq(buf, "Hallo Steffen. Its Monday 13th August 2018.");

	free(buf);
}

Test(utils, version)
{
Steffen Vogel's avatar
Steffen Vogel committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	using villas::utils::Version;

	Version v1 = Version("1.2");
	Version v2 = Version("1.3");
	Version v3 = Version("55");
	Version v4 = Version("66");
	Version v5 = Version(66);
	Version v6 = Version(1, 2, 5);
	Version v7 = Version("1.2.5");

	cr_assert_lt(v1, v2);
	cr_assert_eq(v1, v1);
	cr_assert_gt(v2, v1);
	cr_assert_lt(v3, v4);
	cr_assert_eq(v4, v5);
	cr_assert_eq(v6, v7);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
}

Test(utils, sha1sum)
{
	int ret;
	FILE *f = tmpfile();

	unsigned char     hash[SHA_DIGEST_LENGTH];
	unsigned char expected[SHA_DIGEST_LENGTH] = { 0x69, 0xdf, 0x29, 0xdf, 0x1f, 0xf2, 0xd2, 0x5d, 0xb8, 0x68, 0x6c, 0x02, 0x8d, 0xdf, 0x40, 0xaf, 0xb3, 0xc1, 0xc9, 0x4d };

	/* Write the first 512 fibonaccia numbers to the file */
	for (int i = 0, a = 0, b = 1, c; i < 512; i++, a = b, b = c) {
		c = a + b;

		fwrite((void *) &c, sizeof(c), 1, f);
	}

	ret = sha1sum(f, hash);

	cr_assert_eq(ret, 0);
	cr_assert_arr_eq(hash, expected, SHA_DIGEST_LENGTH);

	fclose(f);
}

Test(utils, decolor)
{
	char str[] = "This " CLR_RED("is") " a " CLR_BLU("colored") " " CLR_BLD("text!");
	char expect[] = "This is a colored text!";

	decolor(str);

	cr_assert_str_eq(str, expect);
}