summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/bitloading/test/src/test_intervals.c
blob: de72ce7eda638ce772bee736fb1387dbb6442302 (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/bitloading/test/src/test_intervals.c
 * \brief   Test CE RX BL intervals module.
 * \ingroup test
 */

#include "common/std.h"
#include "lib/test.h"
#include "lib/utils.h"
#include "mac/common/interval.h"
#include "mac/common/timings.h"
#include "bsu/aclf/aclf.h"
#include "ce/rx/bitloading/intervals.h"
#include "ce/rx/bitloading/inc/intervals.h"
#include "ce_rx_bl_fsm_defs.h"
#include "mac/common/timings.h"
#include "ce/rx/bitloading/fsm/fsm.h"

/**
 * Test suite for intervals init and uninit.
 */
void
test_suite_ce_rx_bl_intervals_init (test_t t)
{
    test_case_begin (t, "Intervals init/uninit");

    test_begin (t, "Intervals alloc")
    {
        ce_rx_bitloading_intervals_t *its = NULL;
        its = ce_rx_bl_intervals_alloc (42);
        test_fail_if (its == NULL);
        ce_rx_bl_intervals_free (its);
    } test_end;

    test_begin (t, "Intervals bit-loading init.")
    {
        ce_rx_bitloading_intervals_t *its = ce_rx_bl_intervals_alloc (42);
        int i;
        for (i = 0; i < mac_interval_fsm_count_; i++)
        {
            ce_rx_bitloading_t *b = (ce_rx_bitloading_t *)
                blk_table_get (its->blk_table, i);
            test_fail_unless (b == its->intervals[i]);
            test_fail_unless (its->intervals[i]->fsm ==
                              CE_RX_BL_FSM_STATE_INTERVAL_TRACKING);
            test_fail_unless (its->tmi[i] == 42);
        }
        ce_rx_bl_intervals_free (its);
    } test_end;

    test_begin (t, "Initial FSM state of intervals is correct.")
    {
        test_fail_unless (CE_RX_BL_FSM_STATE_INTERVAL_TRACKING_COPY ==
                          CE_RX_BL_FSM_STATE_INTERVAL_TRACKING);
    } test_end;
}

/**
 * Time transformation from pbproc information to ATU.
 */
void
test_suite_ce_rx_bl_intervals_measure_to_date (test_t t)
{
    test_case_begin (t, "Time transformation from pbproc timing to ATU");
    u16 start = 0, end = 0;
    bool out;
    pbproc_rx_params_t rxp;

    test_begin (t, "Classic value")
    {
        rxp.preamble_ntb = 2000000;
        rxp.beacon_period_start_ntb = 1000000;
        rxp.fl_tck = 1000;
        out = ce_rx_bl_intervals_measure_to_date (&rxp, &start, &end);
        u16 computed_start = MAC_TCK_TO_ATU (
            ((MAC_PREAMBLE_HYBRID_TCK + MAC_FC_10_TCK + MAC_FC_AV_TCK)
             + (MAC_PREAMBLE_TCK + MAC_FC_AV_TCK)) / 2 + 1000000);
        test_fail_unless (computed_start == start);
        test_fail_unless (end == start + MAC_TCK_TO_ATU (rxp.fl_tck));
        test_fail_unless (out == true);
    } test_end;

    test_begin (t, "Overflowed counter value")
    {
        rxp.preamble_ntb = 1;
        rxp.beacon_period_start_ntb = 0xfffff000;
        rxp.fl_tck = 1000;
        out = ce_rx_bl_intervals_measure_to_date (&rxp, &start, &end);
        u16 computed_start = MAC_TCK_TO_ATU (
            ((MAC_PREAMBLE_HYBRID_TCK + MAC_FC_10_TCK + MAC_FC_AV_TCK)
             + (MAC_PREAMBLE_TCK + MAC_FC_AV_TCK)) / 2 + (s32) (1 - 0xfffff000));
        test_fail_unless (computed_start == start);
        test_fail_unless (end == start + MAC_TCK_TO_ATU (rxp.fl_tck));
        test_fail_unless (out == true);
    } test_end;

    test_begin (t, "Invalid value: beacon period start after preamble")
    {
        rxp.preamble_ntb = 1000;
        rxp.beacon_period_start_ntb = 2000;
        rxp.fl_tck = 1000;
        start = 0;
        end = 0;
        out = ce_rx_bl_intervals_measure_to_date (&rxp, &start, &end);
        test_fail_unless (out == false);
        test_fail_unless (start == 0);
        test_fail_unless (end == 0);
    } test_end;
}

/*
 * Test suite for measure to interval computation.
 */
void
test_suite_ce_rx_bl_intervals_measure_to_interval (test_t t)
{
    test_case_begin (t, "Measure to interval");

    test_begin (t, "Global measure")
    {
        tonemaps_t *tms = tonemaps_alloc ();
        int int_nb = 0;
        uint bp = MAC_TCK_TO_ATU (BSU_ACLF_BP_50HZ_TCK);
        int end_offset_atu = 0;
        do
        {
            end_offset_atu = ++int_nb * bp / 24;
            int_nb = mac_interval_append (tms, end_offset_atu, 51);
        } while (int_nb != 24);
        mac_interval_commit_changes (tms);

        int i = ce_rx_bl_intervals_measure_to_interval (tms, bp / 4, bp / 2);
        test_fail_if (i != -1);
        tonemaps_release (tms);
    } test_end;

    test_begin (t, "Global measure no existing interval")
    {
        tonemaps_t *tms = tonemaps_alloc ();
        uint bp = MAC_TCK_TO_ATU (BSU_ACLF_BP_50HZ_TCK);
        mac_interval_append (tms, bp, 51);
        mac_interval_commit_changes (tms);

        /* Only one interval so the measure should be global. */
        int i = ce_rx_bl_intervals_measure_to_interval (tms, 0, bp / 48);
        test_fail_if (i != -1);
        tonemaps_release (tms);
    } test_end;

    test_begin (t, "Measure position shift ok")
    {
        tonemaps_t *tms = tonemaps_alloc ();
        int int_nb = 0;
        uint bp = MAC_TCK_TO_ATU (BSU_ACLF_BP_50HZ_TCK);
        int end_offset_atu = 0;
        do
        {
            end_offset_atu = ++int_nb * bp / 24;
            int_nb = mac_interval_append (tms, end_offset_atu, 51);
        } while (int_nb != 24);
        mac_interval_commit_changes (tms);

        int a;
        for (a = 0; a < 24; a++)
        {
            int i = ce_rx_bl_intervals_measure_to_interval (
                tms,
                a == 0 ? 0 : a * bp / 24
                - mac_interval_measure_shift_tolerance_,
                (a + 1) * bp / 24
                + mac_interval_measure_shift_tolerance_ - 1);
            test_fail_if (i != a);
        }
        tonemaps_release (tms);
    } test_end;

    test_begin (t, "Measure position shift fail start")
    {
        tonemaps_t *tms = tonemaps_alloc ();
        int int_nb = 0;
        uint bp = MAC_TCK_TO_ATU (BSU_ACLF_BP_50HZ_TCK);
        int end_offset_atu = 0;
        do
        {
            end_offset_atu = ++int_nb * bp / 24;
            int_nb = mac_interval_append (tms, end_offset_atu, 51);
        } while (int_nb != 24);
        mac_interval_commit_changes (tms);

        int i = ce_rx_bl_intervals_measure_to_interval (
            tms,
            bp / 24 - mac_interval_measure_shift_tolerance_ - 1,
            2 * bp / 24 + mac_interval_measure_shift_tolerance_ - 1);
        test_fail_if (i != -1);
        tonemaps_release (tms);
    } test_end;

    test_begin (t, "Measure position shift fail end")
    {
        tonemaps_t *tms = tonemaps_alloc ();
        int int_nb = 0;
        uint bp = MAC_TCK_TO_ATU (BSU_ACLF_BP_50HZ_TCK);
        int end_offset_atu = 0;
        do
        {
            end_offset_atu = ++int_nb * bp / 24;
            int_nb = mac_interval_append (tms, end_offset_atu, 51);
        } while (int_nb != 24);
        mac_interval_commit_changes (tms);

        int i = ce_rx_bl_intervals_measure_to_interval (
            tms,
            bp / 24 - mac_interval_measure_shift_tolerance_,
            2 * bp / 24 + mac_interval_measure_shift_tolerance_);
        test_fail_if (i != -1);
        tonemaps_release (tms);
    } test_end;
}

void
test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
    test_t t,
    int sound,
    bool intervals,
    bool mea_interval,
    ce_rx_bl_fsm_event_type_t *event,
    int *fsm_id)
{
        sta_t sta;
        tonemaps_t* tms;
        ce_rx_measure_mbox_t measure;
        ce_rx_bitloading_fsm_event_param_t data;
        int i;
        uint bp;

        ce_rx_bitloading_init (&sta.ce_rx_bt);
        tms = tonemaps_alloc ();
        bp = MAC_TCK_TO_ATU (BSU_ACLF_BP_50HZ_TCK);
        if (intervals)
            for (i = 1; i <= 24; i++)
                mac_interval_append (tms, bp * i / 24, i);
        else
            mac_interval_append (tms, bp, 4);
        mac_interval_commit_changes (tms);
        sta.rx_tonemaps = tms;
        sta.intervals = NULL;
        data.measure = &measure;

        /* Configure measure. */
        measure.rx_params.sound = sound;
        measure.rx_params.sound_complete = 0;
        measure.rx_params.preamble_ntb = 0;
        measure.rx_params.beacon_period_start_ntb = 0;
        /* Measure is global. */
        if (!mea_interval)
            measure.rx_params.fl_tck = 30000000;
        /* Measure is located on an interval */
        else
            measure.rx_params.fl_tck = bp / 25;

        *event = ce_rx_bl_fsm_measure_to_event (&sta, data);
        *fsm_id = measure.fsm_id;

        /* Clean. */
        tonemaps_release (tms);
        ce_rx_bitloading_uninit (&sta.ce_rx_bt);
}

void
test_suite_ce_rx_bl_intervals_measure_to_event (test_t t)
{
    ce_rx_bl_fsm_event_type_t out;
    int fsm_id;
    test_begin (t, "Measure to event conversion, sound, no interval, "
                "big measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            1,
            /* RX contains intervals ? */
            false,
            /* Measure is positioned on an interval ? */
            false,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_sound);
        test_fail_if (fsm_id != -1);
    } test_end;
    test_begin (t, "Measure to event conversion, sound, no interval, "
                "small measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            1,
            /* RX contains intervals ? */
            false,
            /* Measure is positioned on an interval ? */
            true,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_sound);
        test_fail_if (fsm_id != -1);
    } test_end;
    test_begin (t, "Measure to event conversion, sound, intervals, "
                "big measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            1,
            /* RX contains intervals ? */
            true,
            /* Measure is positioned on an interval ? */
            false,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_sound);
        test_fail_if (fsm_id != -1);
    } test_end;
    test_begin (t, "Measure to event conversion, sound, intervals, "
                "small measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            1,
            /* RX contains intervals ? */
            true,
            /* Measure is positioned on an interval ? */
            true,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_interval_sound);
        test_fail_if (fsm_id != 0);
    } test_end;
    test_begin (t, "Measure to event conversion, not sound, no interval, "
                "big measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            0,
            /* RX contains intervals ? */
            false,
            /* Measure is positioned on an interval ? */
            false,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_data);
        test_fail_if (fsm_id != -1);
    } test_end;
    test_begin (t, "Measure to event conversion, not sound, no interval, "
                "small measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            0,
            /* RX contains intervals ? */
            false,
            /* Measure is positioned on an interval ? */
            true,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_data);
        test_fail_if (fsm_id != -1);
    } test_end;
    test_begin (t, "Measure to event conversion, not sound, intervals, "
                "big measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            0,
            /* RX contains intervals ? */
            true,
            /* Measure is positioned on an interval ? */
            false,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_data);
        test_fail_if (fsm_id != -1);
    } test_end;
    test_begin (t, "Measure to event conversion, not sound, intervals, "
                "small measure")
    {
        test_suite_ce_rx_bl_intervals_measure_to_event_master_test (
            t,
            /* Sound ? */
            0,
            /* RX contains intervals ? */
            true,
            /* Measure is positioned on an interval ? */
            true,
            &out, &fsm_id);
        test_fail_if (out != CE_RX_BL_FSM_EVENT_TYPE_interval_data);
        test_fail_if (fsm_id != 0);
    } test_end;
}

/*
 * Test suite for updating TMI of intervals.
 */
void
test_suite_ce_rx_bl_intervals_update_tmi (test_t t)
{
    test_case_begin (t, "Update TMI of intervals.");

    test_begin (t, "simple test")
    {
        int i;
        sta_t sta;
        sta_init (&sta, 42);

        mac_interval_clear (sta.rx_tonemaps);
        mac_interval_append (sta.rx_tonemaps, 1000, 42);
        mac_interval_commit_changes (sta.rx_tonemaps);

        sta.intervals = ce_rx_bl_intervals_alloc (0);
        for (i = 0; i < mac_interval_fsm_count_; i++)
            sta.intervals->tmi[i] = i;

        ce_rx_bl_intervals_update_tmi (&sta);

        tonemap_intervals_t *ints = sta.rx_tonemaps->intervals;
        for (i = 0; i < ints->intervals_nb; i++)
            test_fail_unless
                (i % mac_interval_fsm_count_ == ints->interval[i].tmi);
        ce_rx_bl_intervals_free (sta.intervals);
        sta.intervals = NULL;
        sta_uninit (&sta);
    } test_end;
}

void
test_suite_ce_rx_bl_intervals (test_t t)
{
    /* Intervals init and uninit. */
    test_suite_ce_rx_bl_intervals_init (t);
    /* Time transformation from pbproc information to ATU. */
    test_suite_ce_rx_bl_intervals_measure_to_date (t);
    /* Measure to interval computation. */
    test_suite_ce_rx_bl_intervals_measure_to_interval (t);
    /* Measure to event computation */
    test_suite_ce_rx_bl_intervals_measure_to_event (t);
    /* Updating TMI of intervals. */
    test_suite_ce_rx_bl_intervals_update_tmi (t);
}

int
main (int argc, char **argv)
{
    test_t t;
    test_init (t, argc, argv);

    test_suite_begin (t, "CE:RX:BL:interval");

    /* Test intervals. */
    test_suite_ce_rx_bl_intervals (t);

    /* Memory check. */
    test_case_begin (t, "General");
    test_begin (t, "Memory")
    {
        test_fail_unless (blk_check_memory ());
    } test_end;

    /* Result. */
    test_result (t);
    return (test_nb_failed (t) == 0 ? 0 : 1);
}