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
70
71
72
73
74
75
76
77
78
79
80
81
|
#include <criterion/criterion.h>
#include <stddef.h>
#include <limits.h>
#include <string.h>
#include <stdio.h>
extern unsigned *incrementer(unsigned *dest, const unsigned *src, size_t n);
#define ARRLIM 0xF
#define FMT_UINT (CHAR_BIT * sizeof(unsigned) / 3)
#define FMT_ALIGN 0x2
#define FMT_DESCR 0x40
#define FMT_ARR ((FMT_UINT + FMT_ALIGN) * ARRLIM)
#define OUTPFLEN (FMT_DESCR + FMT_UINT * 3)
typedef enum {
ASSERT_PASS,
ASSERT_FAIL
} assertop;
assertop assert_mem_eq(const void *actual, const void *expected, size_t n, size_t size)
{
unsigned long i;
if (!actual || !expected)
return actual != expected ? ASSERT_FAIL : ASSERT_PASS;
for (i = 0ul; n--; i += size)
if (memcmp((char *)actual+i, (char *)expected+i, size))
return ASSERT_FAIL;
return ASSERT_PASS;
}
char *assert_fmt(char *outpf, const unsigned *actual, const unsigned *expected, const unsigned *arr, size_t n)
{
const char *descr[] = { "*Actual*:", "\nExpected:", "\n Array:" };
const unsigned *params[] = { actual, expected, arr };
//const unsigned *r;
size_t pos, m, i;
for (pos = m = 0ul; m < 3ul; ++m) {
pos += sprintf(outpf+pos, "%s ", *(descr+m));
if (!(arr = *(params+m))) {
pos += sprintf(outpf+pos, "NULL");
continue;
}
pos += sprintf(outpf+pos, "[ ");
for (i = 0ul; i < n; ++i)
pos += sprintf(outpf+pos, "%u, ", *(arr+i));
if (i)
pos -= FMT_ALIGN;
pos += sprintf(outpf+pos, " ]");
}
pos += sprintf(outpf+pos, "\n N: %lu\n", n);
return outpf;
}
void assert_data(const unsigned *arr, size_t n, const unsigned *expected)
{
char outpf[OUTPFLEN];
unsigned dest[ARRLIM];
unsigned *actual = incrementer(dest, arr, n);
if (!actual) {
if (expected)
cr_assert_fail("%s", assert_fmt(outpf, actual, expected, arr, n));
else
cr_assert(1);
}
else if (assert_mem_eq(actual, expected, n, sizeof(unsigned)) == ASSERT_FAIL)
cr_assert_fail("%s", assert_fmt(outpf, actual, expected, arr, n));
else if (assert_mem_eq(dest, expected, n, sizeof(unsigned)) == ASSERT_FAIL)
cr_assert_fail("Error: should not allocate memory!\n%s", assert_fmt(outpf, actual, expected, arr, n));
else
cr_assert(1);
}
Test(Sample_Test, should_return_the_incremented_array)
{
assert_data((const unsigned[]){ 1,2,3 }, 3ul, (const unsigned[]){ 2,4,6 });
assert_data((const unsigned[]){ 4,6,7,1,3 }, 5ul, (const unsigned[]){ 5,8,0,5,8 });
assert_data((const unsigned[]){ 3,6,9,8,9 }, 5ul, (const unsigned[]){ 4,8,2,2,4 });
assert_data((const unsigned[]){ 1,2,3,4,5,6,7,8,9,9,9,9,9,8 }, 14ul, (const unsigned[]){ 2,4,6,8,0,2,4,6,8,9,0,1,2,2 });
assert_data((const unsigned[]){ 0 }, 0ul, NULL);
}
|