Repository navigation
Expand file tree
/
Copy pathtestBitstreamStrided.c
More file actions
95 lines (75 loc) · 2.1 KB
/
Copy pathtestBitstreamStrided.c
File metadata and controls
95 lines (75 loc) · 2.1 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#define BIT_STREAM_STRIDED
#include "include/bitstream.h"
#include "src/inline/bitstream.c"
// 4 words per block
#define BLOCK_SIZE 4
// 16 blocks between consecutive stream-touched blocks
#define DELTA 16
#define STREAM_BUFFER_LEN 3
#define STREAM_STRIDED_LEN (STREAM_BUFFER_LEN * BLOCK_SIZE * DELTA)
struct setupVars {
void* buffer;
bitstream* b;
};
static int
setup(void **state)
{
struct setupVars *s = malloc(sizeof(struct setupVars));
assert_non_null(s);
s->buffer = calloc(STREAM_STRIDED_LEN, sizeof(word));
assert_non_null(s->buffer);
s->b = stream_open(s->buffer, STREAM_STRIDED_LEN * sizeof(word));
assert_non_null(s->b);
assert_true(stream_set_stride(s->b, BLOCK_SIZE, DELTA));
*state = s;
return 0;
}
static int
teardown(void **state)
{
struct setupVars *s = *state;
free(s->buffer);
free(s->b);
free(s);
return 0;
}
static void
given_Strided_when_ReadWordCompletesBlock_expect_PtrAdvancedByStrideLen(void **state)
{
bitstream* s = ((struct setupVars *)*state)->b;
word* prevPtr = s->ptr;
int i;
for (i = 0; i < BLOCK_SIZE - 1; i++) {
stream_read_word(s);
assert_ptr_equal(s->ptr, prevPtr + 1);
prevPtr = s->ptr;
}
stream_read_word(s);
assert_ptr_equal(s->ptr, (prevPtr + 1) + DELTA * BLOCK_SIZE);
}
static void
given_Strided_when_WriteWordCompletesBlock_expect_PtrAdvancedByStrideLen(void **state)
{
bitstream* s = ((struct setupVars *)*state)->b;
word* prevPtr = s->ptr;
int i;
for (i = 0; i < BLOCK_SIZE - 1; i++) {
stream_write_word(s, 0);
assert_ptr_equal(s->ptr, prevPtr + 1);
prevPtr = s->ptr;
}
stream_write_word(s, 0);
assert_ptr_equal(s->ptr, (prevPtr + 1) + DELTA * BLOCK_SIZE);
}
int main()
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(given_Strided_when_WriteWordCompletesBlock_expect_PtrAdvancedByStrideLen, setup, teardown),
cmocka_unit_test_setup_teardown(given_Strided_when_ReadWordCompletesBlock_expect_PtrAdvancedByStrideLen, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}