summaryrefslogtreecommitdiff
path: root/cesar/mac/pbproc/test/sacki/src/test_sacki.c
blob: adf56781d32f1c5c48a64a7ede69745444a7a8b2 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_sacki.c
 * \brief   Test SACKI compression/decompression.
 * \ingroup test
 */
#include "common/std.h"

#include "mac/pbproc/inc/sacki_enc.h"
#include "mac/pbproc/inc/sacki_dec.h"

#include "lib/test.h"
#include "lib/rnd.h"
#include <string.h>

#define NB_ITER 1000

struct test_t *debug_test_global;

uint
bits_get (const char *s, u32 *bits)
{
    u32 b = 0;
    uint bb = 0;
    while (*s)
    {
        while (*s == ' ')
            s++;
        dbg_assert (*s == '0' || *s == '1');
        if (*s++ == '1')
            b |= 1u << (bb % 32);
        bb++;
        if (bb % 32 == 0)
        {
            bits[bb / 32 - 1] = b;
            b = 0;
        }
    }
    if (bb % 32)
    {
        bits[bb / 32] = b;
    }
    return bb;
}

void
bits_put (const u32 *bits, uint bitsl, char *s)
{
    uint i;
    for (i = 0; i < bitsl; i++)
    {
        *s++ = bits[i / 32] & (1u << (i % 32)) ? '1' : '0';
    }
    *s++ = '\0';
}

void
bits_compare (test_t t, u32 *bits, uint bitsl, const char *s)
{
    test_within (t);
    uint cmpsize = 2 * MAX (bitsl, strlen (s)) + 1;
    char cmp[cmpsize], *cmpp = cmp;
    uint b = 0;
    bool bone;
    uint error = 0;
    const char *model = s;
    while (*s)
    {
        while (*s == ' ')
        {
            s++;
            *cmpp++ = ' ';
        }
        dbg_assert (*s == '0' || *s == '1');
        if (b == bitsl)
        {
            *cmpp++ = '!';
            *cmpp++ = '<';
            error++;
            break;
        }
        else
        {
            bone = bits[b / 32] & (1u << (b % 32));
            b++;
            if ((bone && *s == '0') || (!bone && *s == '1'))
            {
                *cmpp++ = '!';
                error++;
            }
            *cmpp++ = bone ? '1' : '0';
            s++;
        }
    }
    if (b != bitsl)
    {
        *cmpp++ = '!';
        *cmpp++ = '>';
        error++;
        while (b < bitsl)
        {
            bone = bits[b / 32] & (1u << (b % 32));
            b++;
            *cmpp++ = bone ? '1' : '0';
        }
    }
    *cmpp++ = '\0';
    test_verbose_print ("%s %s %s", cmp, error == 0 ? "==" : "!=", model);
    test_fail_unless (error == 0, "mismatch");
}

void
bits_rnd (lib_rnd_t *rnd, char *s, uint min, uint max, u32 ratio_nok)
{
    uint nb = (max == min ? 0 : lib_rnd_uniform (rnd, max - min)) + min;
    for (; nb; nb--)
    {
        *s++ = lib_rnd_flip_coin (rnd, ratio_nok) ? '1' : '0';
    }
    *s = '\0';
}

void
pbproc_sacki_enc_debug (u32 code, uint codel, uint eat)
{
    if (!debug_test_global)
        return;
    test_within (debug_test_global);
    char code_s[32 + 1];
    bits_put (&code, codel, code_s);
    test_verbose_print ("%d => %s", eat, code_s);
}

void
sacki_enc_test_case (test_t t, const char *name, const char *bmp_s,
                     const char *si_s)
{
    u32 bmp[8];
    uint bmpl;
    debug_test_global = t;
    test_case_begin (t, name);
    bmpl = bits_get (bmp_s, bmp);
    test_begin (t, "encoding")
    {
        pbproc_sacki_enc_t se;
        pbproc_sacki_enc_init (&se, 72);
        test_verbose_print ("%s =>", bmp_s);
        pbproc_sacki_enc_process (&se, bmp, bmpl, true);
        bits_compare (t, se.si, se.sis, si_s);
    } test_end;
    test_begin (t, "encoding 2 step")
    {
        uint i;
        for (i = 1; i < bmpl; i++)
        {
            pbproc_sacki_enc_t se;
            pbproc_sacki_enc_init (&se, 72);
            test_verbose_print ("%s (%d-%d) =>", bmp_s, i, bmpl);
            pbproc_sacki_enc_process (&se, bmp, i, false);
            test_verbose_print ("second step");
            pbproc_sacki_enc_process (&se, bmp, bmpl, true);
            bits_compare (t, se.si, se.sis, si_s);
        }
    } test_end;
    test_begin (t, "encoding with offset")
    {
        static const char *pad = "101101101000100011011000100011111000111110"
            "101001001101011111011100011100";
        char sip_s[72 + 1 + strlen (si_s) + 1];
        uint i;
        for (i = 0; i < 72; i++)
        {
            pbproc_sacki_enc_t se;
            pbproc_sacki_enc_init (&se, 72 - i);
            strncpy (sip_s, pad, i);
            sip_s[i] = '\0';
            se.sis = bits_get (sip_s, se.si);
            sip_s[i] = ' ';
            uint j, k;
            for (j = 0, k = 0; j < 72 - i; k++)
            {
                sip_s[i + 1 + k] = si_s[k];
                if (si_s[k] == '0' || si_s[k] == '1')
                    j++;
            }
            sip_s[i + 1 + k] = '\0';
            test_verbose_print ("%s (+%d) =>", bmp_s, i);
            pbproc_sacki_enc_process (&se, bmp, bmpl, true);
            bits_compare (t, se.si, se.sis, sip_s);
        }
    } test_end;
    debug_test_global = NULL;
}

void
sacki_enc_test_suite (test_t t)
{
    test_suite_begin (t, "sacki enc");
    sacki_enc_test_case (
        t, "first",
        "111011000110100010001000011101100011010001000100001",
        "11111111101110111100100101111001111111110111011110010010111100110");
    sacki_enc_test_case (
        t, "long",
        "0000 0000 001 0001 0000 101 0000 0000 0000 0000 0000 11 0000 11 0000"
        " 0000 0000 0000 0000 001 0000 0000 0000 0000 11 11 100 0000 0000 010"
        " 0000 0000 0000 0000 0000 0000 0",
        "0 0 101 1110 0 111101 0 0 0 0 0 11111 0 11111 0 0 0 0 0 101 0 0 0 0"
        " 11111 11111 110 0 0 100 0 0 0 0 0 0 0");
    sacki_enc_test_case (
        t, "too long",
        "0000 0000 001 0001 0000 101 0000 0000 0000 0000 0000 11 0000 11 0000"
        " 0000 0000 0000 0000 001 0000 0000 0000 0000 11 11 100 0000 0000 010"
        " 0000 0000 0000 0000 0000 0000 0000  011 11 0001",
        "0 0 101 1110 0 111101 0 0 0 0 0 11111 0 11111 0 0 0 0 0 101 0 0 0 0"
        " 11111 11111 110 0 0 100 0 0 0 0 0 0 0 111");
}

void
pbproc_sacki_dec_debug (uint eat, uint prod, uint nok0, uint nok1, uint nok2)
{
    if (!debug_test_global)
        return;
    test_within (debug_test_global);
    test_verbose_print ("%d => %d, %d-%d-%d", eat, prod, nok0, nok1, nok2);
}

uint last_nok;

void
test_sacki_dec_nok_cb (void *user, uint first, uint nb)
{
    dbg_assert (user);
    dbg_assert (nb);
    u32 *bmp = user;
    uint i;
    for (i = first; i < first + nb; i++)
    {
        bmp[i / 32] |= 1u << (i % 32);
    }
    last_nok = first;
}

void
sacki_dec_test_case (test_t t, const char *name, const char *si_s, uint sil,
                     const char *bmp_s, uint pbl)
{
    u32 si[3];
    u32 bmp[8];
    uint sir;
    debug_test_global = t;
    test_case_begin (t, name);
    si[0] = 0; si[1] = 0; si[2] = 0;
    bits_get (si_s, si);
    test_begin (t, "decoding")
    {
        test_verbose_print ("%s => %d", si_s, pbl);
        memset (bmp, 0, sizeof (bmp));
        sir = pbproc_sacki_dec_process (si, 72, pbl, bmp,
                                        test_sacki_dec_nok_cb);
        bits_compare (t, bmp, pbl, bmp_s);
        test_fail_unless (sil == 72 - sir, "sir mismatch: %d != 72 - %d", sil,
                          sir);
    } test_end;
    debug_test_global = NULL;
}

void
sacki_dec_burst_test_case (test_t t, const char *name, const char *si_s,
                           uint sil,
                           const char *bmp0_s, uint pb0l,
                           const char *bmp1_s, uint pb1l,
                           const char *bmp2_s, uint pb2l,
                           const char *bmp3_s, uint pb3l)
{
    u32 si[3];
    u32 bmp[8];
    debug_test_global = t;
    test_case_begin (t, name);
    si[0] = 0; si[1] = 0; si[2] = 0;
    bits_get (si_s, si);
    const char *bmp_s[4] = { bmp0_s, bmp1_s, bmp2_s, bmp3_s };
    uint pbl[4] = { pb0l, pb1l, pb2l, pb3l };
    test_begin (t, "decoding")
    {
        test_verbose_print ("%s => %d, %d, %d, %d", si_s, pb0l, pb1l, pb2l,
                            pb3l);
        uint i;
        uint sir = 72;
        for (i = 0; i < COUNT (bmp_s); i++)
        {
            memset (bmp, 0, sizeof (bmp));
            sir = pbproc_sacki_dec_process (si, sir, pbl[i], bmp,
                                            test_sacki_dec_nok_cb);
            bits_compare (t, bmp, pbl[i], bmp_s[i]);
        }
        test_fail_unless (sil == 72 - sir, "sir mismatch: %d != 72 - %d", sil,
                          sir);
    } test_end;
    debug_test_global = NULL;
}

void
sacki_dec_test_suite (test_t t)
{
    test_suite_begin (t, "sacki dec");
    sacki_dec_test_case (
        t, "first",
        "11111111101110111100100101111001111111110111011110010010111100110",
        65, "111011000110100010001000011101100011010001000100001", 51);
    sacki_dec_test_case (
        t, "long",
        "0 0 101 1110 0 111101 0 0 0 0 0 11111 0 11111 0 0 0 0 0 101 0 0 0 0"
        " 11111 11111 110 0 0 100 0 0 0 0 0 0 0", 69,
        "0000 0000 001 0001 0000 101 0000 0000 0000 0000 0000 11 0000 11 0000"
        " 0000 0000 0000 0000 001 0000 0000 0000 0000 11 11 100 0000 0000 010"
        " 0000 0000 0000 0000 0000 0000 0", 132);
    sacki_dec_test_case (
        t, "too long",
        "0 0 101 1110 0 111101 0 0 0 0 0 11111 0 11111 0 0 0 0 0 101 0 0 0 0"
        " 11111 11111 110 0 0 100 0 0 0 0 0 0 0 111", 69,
        "0000 0000 001 0001 0000 101 0000 0000 0000 0000 0000 11 0000 11 0000"
        " 0000 0000 0000 0000 001 0000 0000 0000 0000 11 11 100 0000 0000 010"
        " 0000 0000 0000 0000 0000 0000 0000  111 11 1111", 144);
    sacki_dec_burst_test_case (
        t, "burst",
        "0 111101 100 101 11111 1110"
        "  0 11111 111101 110 0 0"
        "  101 111100 110 0 0 0 11111 111101"
        " 0 0 110", 70,
        "0000 101 010 001 11 0001", 19,
        "0000 11 101 100 0000 0000", 20,
        "001 011 100 0000 0000 0000 11 101", 26,
        "0000 0000 100", 11);
}

void
sacki_encdec_test_suite (test_t t)
{
    test_suite_begin (t, "sacki encdec");
    test_case_begin (t, "random");
    lib_rnd_t rnd[1];
    lib_rnd_init (rnd, 1234);
    static const struct {
        uint r;
        const char *name;
    } ratios[] = {
        { LIB_RND_RATIO (0.0), "zero" },
        { LIB_RND_RATIO (0.05), "rare" },
        { LIB_RND_RATIO (0.2), "few" },
        { LIB_RND_RATIO (0.5), "mean" },
        { LIB_RND_RATIO (0.7), "many" },
        { LIB_RND_RATIO (0.99999), "one" },
    };
    uint j;
    for (j = 0; j < COUNT (ratios); j++)
    {
        test_begin (t, ratios[j].name)
        {
            char bmp_s[257];
            u32 bmp[8];
            uint bmpl;
            uint i;
            for (i = 0; i < NB_ITER; i++)
            {
                /* Random pattern. */
                bits_rnd (rnd, bmp_s, 1, 256, ratios[j].r);
                bmpl = bits_get (bmp_s, bmp);
                /* Encode. */
                pbproc_sacki_enc_t se;
                pbproc_sacki_enc_init (&se, 72);
                pbproc_sacki_enc_process (&se, bmp, bmpl, true);
                char si_s[73];
                bits_put (se.si, se.sis, si_s);
                test_verbose_print ("%s => %s", bmp_s, si_s);
                /* Decode. */
                last_nok = bmpl;
                memset (bmp, 0, sizeof (bmp));
                pbproc_sacki_dec_process (se.si, 72, bmpl, bmp,
                                          test_sacki_dec_nok_cb);
                if (se.sis == 72)
                {
                    /* SACKI too small. */
                    bmpl = last_nok;
                    bmp_s[bmpl] = '\0';
                    test_verbose_print ("reduced to %d", bmpl);
                }
                /* Compare. */
                bits_compare (t, bmp, bmpl, bmp_s);
            }
        } test_end;
    }
}

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