From 3a1cdedf018fff3ad0092f8f6c81f34e2367256f Mon Sep 17 00:00:00 2001 From: Seppo Ingalsuo Date: Tue, 9 Jan 2024 19:08:45 +0200 Subject: [PATCH] Test: Cmocka: Add test case for lookup table sine function 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 --- test/cmocka/src/math/trig/CMakeLists.txt | 4 ++ test/cmocka/src/math/trig/lut_sin_16b_fixed.c | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/cmocka/src/math/trig/lut_sin_16b_fixed.c diff --git a/test/cmocka/src/math/trig/CMakeLists.txt b/test/cmocka/src/math/trig/CMakeLists.txt index a818df1cc709..2d39eecd27b0 100644 --- a/test/cmocka/src/math/trig/CMakeLists.txt +++ b/test/cmocka/src/math/trig/CMakeLists.txt @@ -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 +) diff --git a/test/cmocka/src/math/trig/lut_sin_16b_fixed.c b/test/cmocka/src/math/trig/lut_sin_16b_fixed.c new file mode 100644 index 000000000000..b95390022903 --- /dev/null +++ b/test/cmocka/src/math/trig/lut_sin_16b_fixed.c @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2024 Intel Corporation. All rights reserved. +// +// Author: Slawomir Blauciak +// Author: Seppo Ingalsuo + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#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); +}