Skip to content

Commit

Permalink
Test: Cmocka: Add test case for lookup table sine function
Browse files Browse the repository at this point in the history
The test function is based on test function for the cordic
sine function. The error tolerance is adjusted to just pass.

Signed-off-by: Seppo Ingalsuo <[email protected]>
  • Loading branch information
singalsu committed Jan 10, 2024
1 parent 88a15b7 commit 4ed988b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test/cmocka/src/math/trig/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ cmocka_test(acos_16b_fixed
${PROJECT_SOURCE_DIR}/src/math/trig.c
)

cmocka_test(lut_sin_16b_fixed
lut_sin_16b_fixed.c
${PROJECT_SOURCE_DIR}/src/math/lut_trig.c
)
54 changes: 54 additions & 0 deletions test/cmocka/src/math/trig/lut_sin_16b_fixed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2024 Intel Corporation. All rights reserved.
//
// Author: Slawomir Blauciak <[email protected]>
// Author: Seppo Ingalsuo <[email protected]>

#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <math.h>
#include <cmocka.h>

#include <sof/audio/format.h>
#include <sof/math/lut_trig.h>

#include "trig_tables.h"

#define CMP_TOLERANCE 3.1e-5
#define _M_PI 3.14159265358979323846 /* pi */

static void test_math_trig_lut_sin_fixed(void **state)
{
(void)state;

int theta;

for (theta = 0; theta < 360; ++theta) {
double rad = _M_PI / 180.0 * theta;
int32_t rad_q28 = Q_CONVERT_FLOAT(rad, 28);
float r = Q_CONVERT_QTOF(sofm_lut_sin_fixed_16b(rad_q28), 15);
float diff = fabsf(sin_ref_table[theta] - r);

if (diff > CMP_TOLERANCE) {
printf("%s: diff for %d deg = %g\n", __func__,
theta, diff);
}

assert_true(diff <= CMP_TOLERANCE);
}
}

int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_math_trig_lut_sin_fixed)
};

cmocka_set_message_output(CM_OUTPUT_TAP);

return cmocka_run_group_tests(tests, NULL, NULL);
}

0 comments on commit 4ed988b

Please sign in to comment.