1 #include <ccan/tap/tap.h>
3 #include <ccan/alloc/tiny.c>
4 #include <ccan/alloc/bitops.c>
8 /* Test encoding and decoding. */
13 unsigned char array[ARR_SIZE];
19 /* Test encode_length */
20 for (i = 1; i < 0x8000000; i *= 2) {
21 ok1(encode_length(i-1) >= prev);
22 ok1(encode_length(i) >= encode_length(i-1));
23 ok1(encode_length(i+1) >= encode_length(i));
24 prev = encode_length(i);
27 /* Test it against actual encoding return val. */
28 for (i = 1; i < 0x8000000; i *= 2) {
29 ok1(encode_length(i-1) == encode(i - 1 + MIN_BLOCK_SIZE,
30 false, array, ARR_SIZE));
31 ok1(encode_length(i) == encode(i + MIN_BLOCK_SIZE,
32 false, array, ARR_SIZE));
33 ok1(encode_length(i+1) == encode(i + 1 + MIN_BLOCK_SIZE,
34 false, array, ARR_SIZE));
37 /* Test encoder vs. decoder. */
38 for (i = 1; i < 0x8000000; i *= 2) {
39 unsigned long hdrlen, len;
42 hdrlen = encode(i - 1 + MIN_BLOCK_SIZE, false, array, ARR_SIZE);
43 ok1(decode(&len, &free, array) == hdrlen);
44 ok1(len == i - 1 + MIN_BLOCK_SIZE);
47 hdrlen = encode(i + MIN_BLOCK_SIZE, true, array, ARR_SIZE);
48 ok1(decode(&len, &free, array) == hdrlen);
49 ok1(len == i + MIN_BLOCK_SIZE);
52 hdrlen = encode(i + 1 + MIN_BLOCK_SIZE, true, array, ARR_SIZE);
53 ok1(decode(&len, &free, array) == hdrlen);
54 ok1(len == i + 1 + MIN_BLOCK_SIZE);
58 /* Test encoder limit enforcement. */
59 for (i = 1; i < 0x8000000; i *= 2) {
63 /* These should fit. */
64 ok1(encode(i-1 + MIN_BLOCK_SIZE, false, array,
65 encode_length(i-1)) == encode_length(i-1));
66 ok1(encode(i + MIN_BLOCK_SIZE, false, array,
67 encode_length(i)) == encode_length(i));
68 ok1(encode(i+1 + MIN_BLOCK_SIZE, false, array,
69 encode_length(i+1)) == encode_length(i+1));
71 /* These should not: malloc so valgrind finds overruns. */
72 len = encode_length(i-1) - 1;
74 ok1(encode(i-1 + MIN_BLOCK_SIZE, true, arr, len) == 0);
77 len = encode_length(i-1) - 1;
79 ok1(encode(i + MIN_BLOCK_SIZE, false, arr, len) == 0);
82 len = encode_length(i+1) - 1;
84 ok1(encode(i+1 + MIN_BLOCK_SIZE, false, arr, len) == 0);