1 /*
2 * Copyright (c) 2020 Stephanos Ioannidis <root@stephanos.io>
3 * Copyright (C) 2010-2020 ARM Limited or its affiliates. All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/ztest.h>
9 #include <zephyr/kernel.h>
10 #include <stdlib.h>
11 #include <arm_math.h>
12 #include "../../common/test_common.h"
13
14 #include "f64.pat"
15
16 #define SNR_ERROR_THRESH ((float64_t)300)
17 #define REL_ERROR_THRESH (1.0e-14)
18
ZTEST(statistics_f64,test_arm_entropy_f64)19 ZTEST(statistics_f64, test_arm_entropy_f64)
20 {
21 size_t index;
22 size_t length = in_entropy_dim[0];
23 const float64_t *ref = (float64_t *)ref_entropy;
24 const float64_t *input = (float64_t *)in_entropy;
25 float64_t *output;
26
27 __ASSERT_NO_MSG(ARRAY_SIZE(in_entropy_dim) > length);
28 __ASSERT_NO_MSG(ARRAY_SIZE(ref_entropy) >= length);
29
30 /* Allocate output buffer */
31 output = malloc(length * sizeof(float64_t));
32 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
33
34 /* Run test function */
35 for (index = 0; index < length; index++) {
36 output[index] =
37 arm_entropy_f64(input, in_entropy_dim[index + 1]);
38 input += in_entropy_dim[index + 1];
39 }
40
41 /* Validate output */
42 zassert_true(
43 test_snr_error_f64(length, ref, output, SNR_ERROR_THRESH),
44 ASSERT_MSG_SNR_LIMIT_EXCEED);
45
46 zassert_true(
47 test_near_equal_f64(length, ref, output, REL_ERROR_THRESH),
48 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
49
50 /* Free output buffer */
51 free(output);
52 }
53
ZTEST(statistics_f64,test_arm_kullback_leibler_f64)54 ZTEST(statistics_f64, test_arm_kullback_leibler_f64)
55 {
56 size_t index;
57 size_t length = in_kl_dim[0];
58 const float64_t *ref = (float64_t *)ref_kl;
59 const float64_t *input1 = (float64_t *)in_kl1;
60 const float64_t *input2 = (float64_t *)in_kl2;
61 float64_t *output;
62
63 __ASSERT_NO_MSG(ARRAY_SIZE(in_kl_dim) > length);
64 __ASSERT_NO_MSG(ARRAY_SIZE(ref_kl) >= length);
65
66 /* Allocate output buffer */
67 output = malloc(length * sizeof(float64_t));
68 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
69
70 /* Run test function */
71 for (index = 0; index < length; index++) {
72 output[index] =
73 arm_kullback_leibler_f64(
74 input1, input2, in_kl_dim[index + 1]);
75
76 input1 += in_kl_dim[index + 1];
77 input2 += in_kl_dim[index + 1];
78 }
79
80 /* Validate output */
81 zassert_true(
82 test_snr_error_f64(length, ref, output, SNR_ERROR_THRESH),
83 ASSERT_MSG_SNR_LIMIT_EXCEED);
84
85 zassert_true(
86 test_near_equal_f64(length, ref, output, REL_ERROR_THRESH),
87 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
88
89 /* Free output buffer */
90 free(output);
91 }
92
93 ZTEST_SUITE(statistics_f64, NULL, NULL, NULL, NULL, NULL);
94