1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-30 00:00:11 +01:00

added missing unit tests for cpuset helpers and box muller transform

This commit is contained in:
Steffen Vogel 2017-03-29 08:10:00 +02:00
parent 6818b58648
commit d9137790a3
3 changed files with 83 additions and 1 deletions

View file

@ -149,6 +149,9 @@ int strftimespec(char *s, size_t max, const char *format, struct timespec *ts)
*/
void cpuset_from_integer(uintmax_t set, cpu_set_t *cset);
/** Convert cpu_set_t to an integer. */
void cpuset_to_integer(cpu_set_t *cset, uintmax_t *set);
/** Parses string with list of CPU ranges.
*
* From: https://github.com/mmalecki/util-linux/blob/master/lib/cpuset.c

View file

@ -131,10 +131,19 @@ char * vstrcatf(char **dest, const char *fmt, va_list ap)
return *dest;
}
void cpuset_to_integer(cpu_set_t *cset, uintmax_t *set)
{
*set = 0;
for (int i = 0; i < MIN(sizeof(*set) * 8, CPU_SETSIZE); i++) {
if (CPU_ISSET(i, cset))
*set |= 1ULL << i;
}
}
void cpuset_from_integer(uintmax_t set, cpu_set_t *cset)
{
CPU_ZERO(cset);
for (int i = 0; i < MIN(sizeof(set), CPU_SETSIZE) * 8; i++) {
for (int i = 0; i < MIN(sizeof(set) * 8, CPU_SETSIZE); i++) {
if (set & (1L << i))
CPU_SET(i, cset);
}
@ -196,6 +205,7 @@ int cpulist_parse(const char *str, cpu_set_t *set, int fail)
if (r == 2)
return 1;
return 0;
}

View file

@ -8,6 +8,75 @@
#include "utils.h"
/* Simple normality test for 1,2,3s intervals */
Test(utils, box_muller)
{
double n;
unsigned sigma[3] = { 0 };
unsigned iter = 1000000;
for (int i = 0; i < iter; i++) {
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);
}
Test(utils, cpuset)
{
int ret;
char str[512];
cpu_set_t cset1;
cpu_set_t cset2;
uintmax_t int1 = 0x1234567890ABCDEFULL;
uintmax_t int2 = 0;
cpuset_from_integer(int1, &cset1);
cpulist_create(str, sizeof(str), &cset1);
ret = cpulist_parse(str, &cset2, 1);
cr_assert_eq(ret, 0);
cr_assert(CPU_EQUAL(&cset1, &cset2));
cpuset_to_integer(&cset2, &int2);
cr_assert_eq(int1, int2);
}
Test(utils, memdup)
{
char orig[1024], *copy;
size_t len;
len = read_random(orig, sizeof(orig));
cr_assert_eq(len, sizeof(orig));
copy = memdup(orig, sizeof(orig));
cr_assert_not_null(copy);
cr_assert_arr_eq(copy, orig, sizeof(orig));
free(copy);
}
Test(utils, is_aligned)
{
/* Positive */