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
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
extern inline void libj2_ju_lrot_to_j2u(uintmax_t a, unsigned b, struct libj2_j2u *res);
/* TODO Add man page */
#else
static void
set(struct libj2_j2u *a, const char *pattern, unsigned shift)
{
unsigned i, j = (LIBJ2_J2U_BIT - (shift % LIBJ2_J2U_BIT)) % LIBJ2_J2U_BIT;
a->high = 0;
a->low = 0;
for (i = 0; i < LIBJ2_JU_BIT; i++, j++)
if (pattern[j % LIBJ2_J2U_BIT] == '1')
a->low |= (uintmax_t)1 << i;
for (i = 0; i < LIBJ2_JU_BIT; i++, j++)
if (pattern[j % LIBJ2_J2U_BIT] == '1')
a->high |= (uintmax_t)1 << i;
}
int
main(void)
{
struct libj2_j2u a, r, expected;
char pattern[LIBJ2_J2U_BIT + 1U];
unsigned i, j, k;
srand((unsigned)time(NULL));
for (i = 0; i < 128U; i++) {
for (j = 0; j < LIBJ2_JU_BIT; j++)
pattern[j] = '0' + (rand() < rand());
memset(&pattern[LIBJ2_JU_BIT], '0', LIBJ2_JU_BIT);
pattern[LIBJ2_J2U_BIT] = '\0';
set(&a, pattern, 0);
EXPECT(a.high == 0);
for (j = 0; j <= 2U * LIBJ2_J2U_BIT; j++) {
set(&expected, pattern, j);
r = (struct libj2_j2u){111, 222};
libj2_ju_lrot_to_j2u(a.low, j, &r);
EXPECT(libj2_j2u_eq_j2u(&r, &expected));
k = 2U * LIBJ2_J2U_BIT - j;
r = (struct libj2_j2u){111, 222};
libj2_ju_rrot_to_j2u(a.low, k, &r);
EXPECT(libj2_j2u_eq_j2u(&r, &expected));
}
}
return 0;
}
#endif
|