summaryrefslogtreecommitdiff
path: root/fw/midi-dials/Drivers/CMSIS/DSP/DSP_Lib_TestSuite/Common/inc/templates/test_templates.h
blob: 700bbe11ad305380379b3fb18003092673e8487d (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
#ifndef _TEST_TEMPLATES_H_
#define _TEST_TEMPLATES_H_

/*--------------------------------------------------------------------------------*/
/* Includes */
/*--------------------------------------------------------------------------------*/
#include "template.h"
#include <string.h>             /* memcmp() */
#include <inttypes.h>           /* PRIu32 */
#include "math_helper.h"        /* arm_snr_f32() */

/*--------------------------------------------------------------------------------*/
/* Function Aliases for use in Templates. */
/*--------------------------------------------------------------------------------*/
#define ref_q31_t_to_float ref_q31_to_float
#define ref_q15_t_to_float ref_q15_to_float
#define ref_q7_t_to_float  ref_q7_to_float
#define ref_float_to_q31_t ref_float_to_q31
#define ref_float_to_q15_t ref_float_to_q15
#define ref_float_to_q7_t  ref_float_to_q7
#define ref_float32_t_to_float ref_copy_f32
#define ref_float_to_float32_t ref_copy_f32


/*--------------------------------------------------------------------------------*/
/* Macros and Defines */
/*--------------------------------------------------------------------------------*/

/**
 *  Call the function-under-test.
 */
#define TEST_CALL_FUT(fut, fut_args)                    \
    JTEST_COUNT_CYCLES(TEMPLATE_CALL_FN(fut, fut_args))

/**
 *  Call the reference-function.
 */
#define TEST_CALL_REF(ref, ref_args)            \
    TEMPLATE_CALL_FN(ref, ref_args)

/**
 *  Call the function-under-test and the reference-function.
 */
#define TEST_CALL_FUT_AND_REF(fut, fut_args, ref, ref_args) \
    do {                                                    \
        TEST_CALL_FUT(fut, fut_args);                       \
        TEST_CALL_REF(ref, ref_args);                       \
    } while (0)

/**
 *  This macro eats a variable number of arguments and evaluates to a null
 *  statement.
 */
#define TEST_NULL_STATEMENT(...) (void) "TEST_NULL_STATEMENT"

/**
 *  A function name, Usable in any template where a fut or ref name is accepted,
 *  that evaluates to a #TEST_NULL_STATEMENT().
 */
#define TEST_NULL_FN TEST_NULL_STATEMENT

/**
 *  Assert that buffers A and B are byte-equivalent for a number of bytes.
 */
#define TEST_ASSERT_BUFFERS_EQUAL(buf_a, buf_b, bytes)  \
    do                                                  \
    {                                                   \
        if (memcmp(buf_a, buf_b, bytes) != 0)           \
        {                                               \
            return JTEST_TEST_FAILED;                   \
        }                                               \
    } while (0)

/**
 *  Assert that the two entities are equal.
 */
#define TEST_ASSERT_EQUAL(a, b)                         \
    do                                                  \
    {                                                   \
        if ((a) != (b))                                 \
        {                                               \
            return JTEST_TEST_FAILED;                   \
        }                                               \
    } while (0)

/**
 *  Convert elements to from src_type to float.
 */
#define TEST_CONVERT_TO_FLOAT(src_ptr, dst_ptr, block_size, src_type)   \
    do                                                                  \
    {                                                                   \
        ref_##src_type##_to_float(                                      \
            src_ptr,                                                    \
            dst_ptr,                                                    \
            block_size);                                                \
        } while (0)                                                      \

/**
 *  Convert elements to from float to dst_type .
 */
#define TEST_CONVERT_FLOAT_TO(src_ptr, dst_ptr, block_size, dst_type)   \
    do                                                                  \
    {                                                                   \
        ref_float_to_##dst_type(                                        \
            src_ptr,                                                    \
            dst_ptr,                                                    \
            block_size);                                                \
    } while (0)                                                          \

/**
 *  Assert that the SNR between a reference and test sample is above a given
 *  threshold.
 */
#define TEST_ASSERT_SNR(ref_ptr, tst_ptr, block_size, threshold)    \
    do                                                              \
    {                                                               \
        float32_t snr = arm_snr_f32(ref_ptr, tst_ptr, block_size);  \
        if ( snr <= threshold)                                       \
        {                                                           \
            JTEST_DUMP_STRF("SNR: %f\n", snr);                      \
            return JTEST_TEST_FAILED;                               \
        }                                                           \
    } while (0)                                                      \

/**
 *  Assert that the SNR between a reference and test sample is above a given
 *  threshold.  Special case for float64_t
 */
#define TEST_ASSERT_DBL_SNR(ref_ptr, tst_ptr, block_size, threshold)    \
    do                                                              \
    {                                                               \
        float64_t snr = arm_snr_f64(ref_ptr, tst_ptr, block_size);  \
        if ( snr <= threshold)                                       \
        {                                                           \
            JTEST_DUMP_STRF("SNR: %f\n", snr);                      \
            return JTEST_TEST_FAILED;                               \
        }                                                           \
    } while (0)                                                      \

/**
 *  Compare test and reference elements by converting to float and
 *  calculating an SNR.
 *
 *  This macro is a merger of the #TEST_CONVERT_TO_FLOAT() and
 *  #TEST_ASSERT_SNR() macros.
 */
#define TEST_CONVERT_AND_ASSERT_SNR(ref_dst_ptr, ref_src_ptr,   \
                                    tst_dst_ptr, tst_src_ptr,   \
                                    block_size,                 \
                                    tst_src_type,               \
                                    threshold)                  \
        do                                                      \
        {                                                       \
            TEST_CONVERT_TO_FLOAT(ref_src_ptr,                  \
                                  ref_dst_ptr,                  \
                                  block_size,                   \
                                  tst_src_type);                \
            TEST_CONVERT_TO_FLOAT(tst_src_ptr,                  \
                                  tst_dst_ptr,                  \
                                  block_size,                   \
                                  tst_src_type);                \
            TEST_ASSERT_SNR(ref_dst_ptr,                        \
                            tst_dst_ptr,                        \
                            block_size,                         \
                            threshold);                         \
        } while (0)

/**
 *  Execute statements only if the combination of block size, function type
 *  specifier, and input ARR_DESC_t are valid.
 *
 *  @example An ARR_DESC_t that contains 64 bytes cant service a 32 element
 *  block size if they are extracted in float32_t increments.
 *
 *  8 * 32 = 256 > 64.
 */
#define TEST_DO_VALID_BLOCKSIZE(block_size, fn_type_spec,   \
                                input_arr_desc, body)       \
    do                                                      \
    {                                                       \
        if (block_size * sizeof(fn_type_spec) <=             \
           ARR_DESC_BYTES(input_arr_desc))                  \
        {                                                   \
            JTEST_DUMP_STRF("Block Size: %"PRIu32"\n", block_size); \
            body;                                           \
        }                                                   \
    } while (0)                                              \

/**
 *  Template for tests that rely on one input buffer and a blocksize parameter.
 *
 *  The buffer is an #ARR_DESC_t.  It is iterated over and it's values are
 *  passed to the function under test and reference functions through their
 *  appropriate argument interfaces.  The argument interfaces this template to
 *  execute structurally similar functions.
 *
 */
#define TEST_TEMPLATE_BUF1_BLK(arr_desc_inputs,                         \
                              arr_desc_block_sizes,                     \
                              input_type, output_type,                  \
                              fut, fut_arg_interface,                   \
                              ref, ref_arg_interface,                   \
                              compare_interface)                        \
    do                                                                  \
    {                                                                   \
        TEMPLATE_DO_ARR_DESC(                                           \
            input_idx, ARR_DESC_t *, input_ptr, arr_desc_inputs         \
            ,                                                           \
            TEMPLATE_DO_ARR_DESC(                                       \
                block_size_idx, uint32_t, block_size, arr_desc_block_sizes \
                ,                                                       \
                void *   input_data_ptr = input_ptr->data_ptr;          \
                                                                        \
                TEST_DO_VALID_BLOCKSIZE(                                \
                    block_size, input_type, input_ptr                   \
                    ,                                                   \
                    TEST_CALL_FUT_AND_REF(                              \
                        fut, fut_arg_interface(                         \
                            input_data_ptr, block_size),                \
                        ref, ref_arg_interface(                         \
                            input_data_ptr, block_size));               \
                                                                        \
                    compare_interface(block_size, output_type))));      \
                                                                        \
        return JTEST_TEST_PASSED;                                       \
                                                                        \
    } while (0)

/**
 *  Template for tests that rely on an input buffer and an element.
 *
 *  An element can is any thing which doesn't walk and talk like a
 *  sequence. Examples include numbers, and structures.
 */
#define TEST_TEMPLATE_BUF1_ELT1(arr_desc_inputs,                        \
                                arr_desc_elts,                          \
                                input_type, elt_type, output_type,      \
                                fut, fut_arg_interface,                 \
                                ref, ref_arg_interface,                 \
                                compare_interface)                      \
        do                                                              \
        {                                                               \
            TEMPLATE_DO_ARR_DESC(                                       \
                input_idx, ARR_DESC_t *, input_ptr, arr_desc_inputs     \
                ,                                                       \
                TEMPLATE_DO_ARR_DESC(                                   \
                    elt_idx, elt_type, elt, arr_desc_elts               \
                    ,                                                   \
                    void * input_data_ptr = input_ptr->data_ptr;        \
                    TEST_CALL_FUT_AND_REF(                              \
                        fut, fut_arg_interface(input_data_ptr, elt),    \
                        ref, ref_arg_interface(input_data_ptr, elt));   \
                                                                        \
                    compare_interface(output_type)));                   \
            return JTEST_TEST_PASSED;                                   \
        } while (0)

/**
 *  Template for tests that rely on an input buffer, an element, and a blocksize
 *  parameter.
 */
#define TEST_TEMPLATE_BUF1_ELT1_BLK(arr_desc_inputs,                \
                                    arr_desc_elts,                  \
                                    arr_desc_block_sizes,           \
                                    input_type, elt_type, output_type,  \
                                    fut, fut_arg_interface,         \
                                    ref, ref_arg_interface,         \
                                    compare_interface);             \
    do                                                              \
    {                                                               \
        TEMPLATE_DO_ARR_DESC(                                       \
            inut_idx, ARR_DESC_t *, input_ptr, arr_desc_inputs      \
            ,                                                       \
            TEMPLATE_DO_ARR_DESC(                                   \
                block_size_idx, uint32_t, block_size,               \
                arr_desc_block_sizes                                \
                ,                                                   \
                TEMPLATE_DO_ARR_DESC(                               \
                    elt_idx, elt_type, elt, arr_desc_elts           \
                    ,                                               \
                    void * input_data_ptr = input_ptr->data_ptr;    \
                    TEST_DO_VALID_BLOCKSIZE(                        \
                        block_size, input_type, input_ptr,          \
                                              \
                        TEST_CALL_FUT_AND_REF(                      \
                            fut, fut_arg_interface(                 \
                                input_data_ptr, elt, block_size),   \
                            ref, ref_arg_interface(                 \
                                input_data_ptr, elt, block_size));  \
                        compare_interface(block_size, output_type))))); \
        return JTEST_TEST_PASSED;                                   \
    } while (0)

/**
 *  Template for tests that rely on an input buffer, two elements, and a blocksize
 *  parameter.
 */
#define TEST_TEMPLATE_BUF1_ELT2_BLK(arr_desc_inputs,                    \
                                    arr_desc_elt1s,                     \
                                    arr_desc_elt2s,                     \
                                    arr_desc_block_sizes,               \
                                    input_type, elt1_type,              \
                                    elt2_type, output_type,             \
                                    fut, fut_arg_interface,             \
                                    ref, ref_arg_interface,             \
                                    compare_interface)                  \
        do                                                              \
        {                                                               \
            TEMPLATE_DO_ARR_DESC(                                       \
                inut_idx, ARR_DESC_t *, input_ptr, arr_desc_inputs      \
                ,                                                       \
                TEMPLATE_DO_ARR_DESC(                                   \
                    block_size_idx, uint32_t, block_size,               \
                    arr_desc_block_sizes                                \
                    ,                                                   \
                    TEMPLATE_DO_ARR_DESC(                               \
                        elt1_idx, elt1_type, elt1, arr_desc_elt1s       \
                        ,                                               \
                        TEMPLATE_DO_ARR_DESC(                           \
                            elt2_idx, elt2_type, elt2, arr_desc_elt2s   \
                            ,                                           \
                            void * input_data_ptr = input_ptr->data_ptr; \
                            TEST_DO_VALID_BLOCKSIZE(                    \
                                block_size, input_type, input_ptr,      \
                                TEST_CALL_FUT_AND_REF(                  \
                                    fut, fut_arg_interface(             \
                                        input_data_ptr, elt1, elt2, block_size), \
                                    ref, ref_arg_interface(             \
                                        input_data_ptr, elt1, elt2, block_size)); \
                                compare_interface(block_size, output_type)))))); \
            return JTEST_TEST_PASSED;                                   \
        } while (0)

/**
 *  Template for tests that rely on two input buffers and a blocksize parameter.
 *
 *  The two #ARR_DESC_t, input buffers are iterated through in parallel. The
 *  length of the first #ARR_DESC_t determines the length of the iteration.
 */
#define TEST_TEMPLATE_BUF2_BLK(arr_desc_inputs_a,                       \
                              arr_desc_inputs_b,                        \
                              arr_desc_block_sizes,                     \
                              input_type, output_type,                  \
                              fut, fut_arg_interface,                   \
                              ref, ref_arg_interface,                   \
                              compare_interface)                        \
    do                                                                  \
    {                                                                   \
        /* Iterate over two input arrays in parallel.*/                 \
        TEMPLATE_DO_ARR_DESC(                                           \
            input_idx, ARR_DESC_t *, input_ptr, arr_desc_inputs_a       \
            ,                                                           \
            TEMPLATE_DO_ARR_DESC(                                       \
                block_size_idx, uint32_t, block_size, arr_desc_block_sizes, \
                void * input_a_ptr = input_ptr->data_ptr;               \
                void * input_b_ptr = ARR_DESC_ELT(                      \
                    ARR_DESC_t *, input_idx,                            \
                    &(arr_desc_inputs_b))->data_ptr;                    \
                                                                        \
                TEST_DO_VALID_BLOCKSIZE(                                \
                    block_size, input_type, input_ptr                   \
                    ,                                                   \
                    TEST_CALL_FUT_AND_REF(                              \
                        fut, fut_arg_interface(                         \
                            input_a_ptr, input_b_ptr, block_size),      \
                        ref, ref_arg_interface(                         \
                            input_a_ptr, input_b_ptr, block_size));     \
                                                                        \
                    compare_interface(block_size, output_type))));      \
        return JTEST_TEST_PASSED;                                       \
    } while (0)

/**
 *  Test template that uses a single element.
 */
#define TEST_TEMPLATE_ELT1(arr_desc_elts,                       \
                           elt_type, output_type,               \
                           fut, fut_arg_interface,              \
                           ref, ref_arg_interface,              \
                           compare_interface)                   \
        do                                                      \
        {                                                       \
            TEMPLATE_DO_ARR_DESC(                               \
                elt_idx, elt_type, elt, arr_desc_elts           \
                ,                                               \
                TEST_CALL_FUT_AND_REF(                          \
                    fut, fut_arg_interface(                     \
                        elt),                                   \
                    ref, ref_arg_interface(                     \
                        elt));                                  \
                /* Comparison interfaces typically accept */    \
                /* a block_size. Pass a dummy value 1.*/        \
                compare_interface(1, output_type));             \
            return JTEST_TEST_PASSED;                           \
        } while (0)

/**
 *  Test template that iterates over two sets of elements in parallel.
 *
 *  The length of the first set determines the number of iteratsions.
 */
#define TEST_TEMPLATE_ELT2(arr_desc_elts_a,                     \
                           arr_desc_elts_b,                     \
                           elt_a_type, elt_b_type, output_type, \
                           fut, fut_arg_interface,              \
                           ref, ref_arg_interface,              \
                           compare_interface)                   \
        do                                                      \
        {                                                       \
            TEMPLATE_DO_ARR_DESC(                               \
                elt_a_idx, elt_a_type, elt_a, arr_desc_elts_a   \
                ,                                               \
                elt_b_type * elt_b = ARR_DESC_ELT(              \
                    elt_b_type,                                 \
                    elt_a_idx,                                  \
                    arr_desc_elts_b);                           \
                                                                \
                TEST_CALL_FUT_AND_REF(                          \
                    fut, fut_arg_interface(                     \
                        elt_a, elt_b),                          \
                    ref, ref_arg_interface(                     \
                        elt_a, elt_b));                         \
                /* Comparison interfaces typically accept */    \
                /* a block_size. Pass a dummy value 1.*/        \
                compare_interface(1, output_type));             \
            return JTEST_TEST_PASSED;                           \
        } while (0)

/**
 *  Test template that uses an element and a block size.
 */
#define TEST_TEMPLATE_ELT1_BLK(arr_desc_elts,                       \
                               arr_desc_block_sizes,                \
                               elt_type, output_type,               \
                               fut, fut_arg_interface,              \
                               ref, ref_arg_interface,              \
                               compare_interface)                   \
        do                                                          \
        {                                                           \
            TEMPLATE_DO_ARR_DESC(                                   \
                block_size_idx, uint32_t, block_size,               \
                arr_desc_block_sizes                                \
                ,                                                   \
                TEMPLATE_DO_ARR_DESC(                               \
                    elt_idx, elt_type, elt, arr_desc_elts           \
                    ,                                               \
                    JTEST_DUMP_STRF("Block Size: %d\n",             \
                         (int)block_size);                          \
                    TEST_CALL_FUT_AND_REF(                          \
                        fut, fut_arg_interface(                     \
                            elt, block_size),                       \
                        ref, ref_arg_interface(                     \
                            elt, block_size));                      \
                    compare_interface(block_size, output_type)));   \
            return JTEST_TEST_PASSED;                               \
        } while (0)

#endif /* _TEST_TEMPLATES_H_ */