forked from simplegeo/zbase32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
69 lines (62 loc) · 1.2 KB
/
test.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
#include "base32.h"
#include "zstr.h"
#include "zutil.h"
#undef NDEBUG
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
zstr randz(const size_t len)
{
zstr result = new_z(len);
size_t i;
for (i=0; i<len; i++) {
result.buf[i] = rand() % 256;
}
return result;
}
int test_rands()
{
int i;
czstr z = cz(randz(16));
/*czstr z = cs_as_cz("8b8t");*/
czstr a;
czstr zrecovered;
for (i=0; i<32768; i++) {
a = cz(b2a(z));
/* printf("a.len: %d, buf: %s\n", a.len, a.buf);*/
assert (strlen(cz_as_cs(a)) == divceil(z.len*8, 5));
assert (isgraph(a.buf[0]));
zrecovered = cz(a2b(a));
if (!zeq(z, zrecovered)) {
printf("failed basic recovery test. z: %s, a: %s, zr: %s\n", repr(z).buf, a.buf, repr(zrecovered).buf);
return -1;
}
free((void*)a.buf);
free((void*)zrecovered.buf);
}
return 0;
}
int test_b2alen_doesnt_overflow()
{
if (b2alen(SIZE_T_MAX) != (unsigned long)ceil((double)SIZE_T_MAX/(double)5))
return -1;
else
return 0;
}
int bench_ed()
{
unsigned i;
for (i=0;i<32;i++)
test_rands();
return 0;
}
int main(int argc, char**argv)
{
if (test_b2alen_doesnt_overflow()) {
return -1;
}
return test_rands();
/*return bench_ed();*/
}