summaryrefslogtreecommitdiff
path: root/cesar/lib/test/bitstream/src/test_bit.c
blob: 4c3def84f6e3c255f0997562c49572583f61732b (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_bit.c
 * \brief   Bit stream and bit array test.
 * \ingroup test
 */
#include "common/std.h"

#include "lib/bitstream.h"
#include "lib/test.h"

void
bitstream_basic_test_case (test_t t)
{
    test_case_begin (t, "basic");
    /* Read tests */
    test_begin (t, "read")
    {
        bitstream_t bsr;
        u8 bit8 = 0; u16 bit16 = 0; u32 bit32 = 0;
        static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };

        bitstream_init (&bsr, (void*)s, sizeof (s), BITSTREAM_READ);
        test_fail_unless (bitstream_access (&bsr, &bit8, 8)
                          && bit8 == 0x10);
        test_fail_unless (bitstream_available_bits (&bsr) == 88);
        test_fail_unless (bitstream_access (&bsr, &bit16, 16)
                          && bit16 == 0x5432);
        test_fail_unless (bitstream_available_bits (&bsr) == 72);
        test_fail_unless (bitstream_access (&bsr, &bit8, 4)
                          && bit8 == 0x6);
        test_fail_unless (bitstream_available_bits (&bsr) == 68);
        test_fail_unless (bitstream_access (&bsr, &bit16, 12)
                          && bit16 == 0x987);
        test_fail_unless (bitstream_available_bits (&bsr) == 56);
        test_fail_unless (bitstream_access (&bsr, &bit32, 32)
                          && bit32 == 0x78fedcba);
        test_fail_unless (bitstream_available_bits (&bsr) == 24);
        test_fail_unless (bitstream_access (&bsr, &bit32, 24)
                          && bit32 == 0x123456);
        test_fail_unless (bitstream_available_bits (&bsr) == 0);
    } test_end;
    test_begin (t, "read_large")
    {
        u64 bit64 = 0;
        bitstream_t bsr;
        static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
        bitstream_init (&bsr, (void*)s, sizeof (s), BITSTREAM_READ);
        test_fail_unless (bitstream_access (&bsr, &bit64, 48)
                          && bit64 == 0xba9876543210LL);
        test_fail_unless (bitstream_available_bits (&bsr) == 48);
        test_fail_unless (bitstream_access (&bsr, &bit64, 24)
                          && bit64 == 0x78fedcLL);
        test_fail_unless (bitstream_available_bits (&bsr) == 24);
        test_fail_unless (bitstream_access (&bsr, &bit64, 24)
                          && bit64 == 0x123456LL);
        test_fail_unless (bitstream_available_bits (&bsr) == 0);
    } test_end;
    test_begin (t, "direct_read")
    {
        static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
        test_fail_unless (bitstream_direct_read ((void*)s, 0, 8) == 0x10);
        test_fail_unless (bitstream_direct_read ((void*)s, 8, 16) == 0x5432);
        test_fail_unless (bitstream_direct_read ((void*)s, 24, 4) == 0x6);
        test_fail_unless (bitstream_direct_read ((void*)s, 28, 12) == 0x987);
        test_fail_unless (bitstream_direct_read ((void*)s, 40, 32)
                          == 0x78fedcba);
        test_fail_unless (bitstream_direct_read ((void*)s, 72, 24)
                          == 0x123456);

    } test_end;
    test_begin (t, "direct_read_large")
    {
        static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };
        test_fail_unless (bitstream_direct_read_large ((void*)s, 0, 48)
                          == 0xba9876543210LL);
        test_fail_unless (bitstream_direct_read_large ((void*)s, 48, 24)
                          == 0x78fedcLL);
        test_fail_unless (bitstream_direct_read_large ((void*)s, 72, 24)
                          == 0x123456LL);
    } test_end;
    /* Write tests */
    test_begin (t, "write")
    {
        bitstream_t bsw;
        u8 bit8; u16 bit16; u32 bit32;
        u32 s[4] = { [3] = 0xabba5555 };
        bitstream_init (&bsw, (void*)s, sizeof(s) - 4, BITSTREAM_WRITE);
        bit8 = 0x10;
        bitstream_access (&bsw, &bit8, 8);
        test_fail_unless (bitstream_available_bits (&bsw) == 88);
        bit16 = 0x5432;
        bitstream_access (&bsw, &bit16, 16);
        test_fail_unless (bitstream_available_bits (&bsw) == 72);
        bit8 = 0x6;
        bitstream_access (&bsw, &bit8, 4);
        test_fail_unless (bitstream_available_bits (&bsw) == 68);
        bit16 = 0x987;
        bitstream_access (&bsw, &bit16, 12);
        test_fail_unless (bitstream_available_bits (&bsw) == 56);
        bit32 = 0x78fedcba;
        bitstream_access (&bsw, &bit32, 32);
        test_fail_unless (bitstream_available_bits (&bsw) == 24);
        bit32 = 0x123456;
        bitstream_access (&bsw, &bit32, 24);
        test_fail_unless (bitstream_available_bits (&bsw) == 0);
        test_fail_unless (bitstream_finalise (&bsw) == 32);
        test_fail_unless (s[0] == 0x76543210
                          && s[1] == 0xfedcba98
                          && s[2] == 0x12345678
                          && s[3] == 0xabba5555);
    } test_end;
    test_begin (t, "write_large")
    {
        u64 bit64;
        bitstream_t bsw;
        u32 s[4] = { [3] = 0xabba5555 };
        bitstream_init (&bsw, (void*)s, sizeof(s) - 4, BITSTREAM_WRITE);
        bit64 = 0xba9876543210LL;
        bitstream_access (&bsw, &bit64, 48);
        test_fail_unless (bitstream_available_bits (&bsw) == 48);
        bit64 = 0x78fedcLL;
        bitstream_access (&bsw, &bit64, 24);
        test_fail_unless (bitstream_available_bits (&bsw) == 24);
        bit64 = 0x123456LL;
        bitstream_access (&bsw, &bit64, 24);
        test_fail_unless (bitstream_available_bits (&bsw) == 0);
        test_fail_unless (bitstream_finalise (&bsw) == 32);
        test_fail_unless (s[0] == 0x76543210
                          && s[1] == 0xfedcba98
                          && s[2] == 0x12345678
                          && s[3] == 0xabba5555);
    } test_end;
    test_begin (t, "direct_write")
    {
        u32 s[4] = { [3] = 0xabba5555 };
        bitstream_direct_write ((void*)s, 0, 0x10, 8);
        bitstream_direct_write ((void*)s, 8, 0x5432, 16);
        bitstream_direct_write ((void*)s, 24, 0x6, 4);
        bitstream_direct_write ((void*)s, 28, 0x987, 12);
        bitstream_direct_write ((void*)s, 40, 0x78fedcba, 32);
        bitstream_direct_write ((void*)s, 72, 0x123456, 24);
        test_fail_unless (s[0] == 0x76543210
                          && s[1] == 0xfedcba98
                          && s[2] == 0x12345678
                          && s[3] == 0xabba5555);
    } test_end;
    test_begin (t, "direct_write_large")
    {
        u32 s[4] = { [3] = 0xabba5555 };
        bitstream_direct_write_large ((void*)s, 0,  0xba9876543210LL, 48);
        bitstream_direct_write_large ((void*)s, 48, 0x78fedcLL, 24);
        bitstream_direct_write_large ((void*)s, 72, 0x123456LL, 24);
        test_fail_unless (s[0] == 0x76543210
                          && s[1] == 0xfedcba98
                          && s[2] == 0x12345678
                          && s[3] == 0xabba5555);
    } test_end;

    test_begin (t, "read_unaligned")
    {
        u32 bit32 = 0;
        bitstream_t bsr;
        static const u32 s[] = { 0x76543210, 0xfedcba98, 0x12345678 };

        bitstream_init (&bsr, (void*)((u8*)s+1), sizeof (s), BITSTREAM_READ);
        test_fail_unless (bitstream_access (&bsr, &bit32, 24)
                          && bit32 == 0x765432);

        bitstream_finalise (&bsr);
    } test_end;
    test_begin (t, "write_unaligned")
    {
        u16 bit16;
        bitstream_t bsw;
        u32 s[4] = { [0] = 0x55000088, [1] = 0xabba5555 };
        bitstream_init (&bsw, (void*)((u8*)s+1),
                        sizeof(s) - 4, BITSTREAM_WRITE);

        bit16 = 0x6677;
        bitstream_access (&bsw, &bit16, 16);

        bit16 = 0x6655;
        bitstream_access (&bsw, &bit16, 16);

        test_fail_unless (bitstream_finalise (&bsw) == 8);
        test_fail_unless (s[0] == 0x55667788
                          && s[1] == 0xabba5566);
    } test_end;
    test_begin (t, "memcpy")
    {
        u8 data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00 };
        u8 copy[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff };
        bitstream_memcpy (copy, data, sizeof(data) - 1);
        test_fail_unless (data[6] == copy[6] && copy[7] == 0xff);
    } test_end;

    /* TODO: -- add a random number based test here --. */
}

void
bitstream_test_suite (test_t t)
{
    test_suite_begin (t, "bitstream");
    bitstream_basic_test_case (t);
}

int
main (int argc, char **argv)
{
    test_t t;
    test_init (t, argc, argv);
    bitstream_test_suite (t);
    test_result (t);
    return test_nb_failed (t) == 0 ? 0 : 1;
}