1 /*
2 * Arm SCP/MCP Software
3 * Copyright (c) 2017-2021, Arm Limited and Contributors. All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #include <fwk_assert.h>
9 #include <fwk_macros.h>
10 #include <fwk_math.h>
11 #include <fwk_test.h>
12
13 #include <limits.h>
14 #include <stdint.h>
15
test_fwk_math_pow2_uc(void)16 static void test_fwk_math_pow2_uc(void)
17 {
18 unsigned char exp = (sizeof(exp) * CHAR_BIT) - 1;
19
20 assert((fwk_math_pow2(exp) - 1) == SCHAR_MAX);
21 }
22
test_fwk_math_pow2_us(void)23 static void test_fwk_math_pow2_us(void)
24 {
25 unsigned short exp = (sizeof(exp) * CHAR_BIT) - 1;
26
27 assert((fwk_math_pow2(exp) - 1) == SHRT_MAX);
28 }
29
test_fwk_math_pow2_ui(void)30 static void test_fwk_math_pow2_ui(void)
31 {
32 unsigned int exp = (sizeof(exp) * CHAR_BIT) - 1;
33
34 assert((fwk_math_pow2(exp) - 1) == INT_MAX);
35 }
36
test_fwk_math_pow2_ul(void)37 static void test_fwk_math_pow2_ul(void)
38 {
39 unsigned long exp = (sizeof(exp) * CHAR_BIT) - 1;
40
41 assert((fwk_math_pow2(exp) - 1) == LONG_MAX);
42 }
43
test_fwk_math_pow2_ull(void)44 static void test_fwk_math_pow2_ull(void)
45 {
46 unsigned long long exp = (sizeof(exp) * CHAR_BIT) - 1;
47
48 assert((fwk_math_pow2(exp) - 1) == LLONG_MAX);
49 }
50
test_fwk_math_clz_ui(void)51 static void test_fwk_math_clz_ui(void)
52 {
53 unsigned int num = ~0u >> 5;
54
55 assert(fwk_math_clz(num) == 5);
56 }
57
test_fwk_math_clz_ul(void)58 static void test_fwk_math_clz_ul(void)
59 {
60 unsigned long num = ~0ul >> 5;
61
62 assert(fwk_math_clz(num) == 5);
63 }
64
test_fwk_math_clz_ull(void)65 static void test_fwk_math_clz_ull(void)
66 {
67 unsigned long long num = ~0ull >> 5;
68
69 assert(fwk_math_clz(num) == 5);
70 }
71
test_fwk_math_log2_ui(void)72 static void test_fwk_math_log2_ui(void)
73 {
74 unsigned int num = UINT_MAX;
75 unsigned int expected = (sizeof(num) * CHAR_BIT) - 1;
76
77 assert(fwk_math_log2(num) == expected);
78 }
79
test_fwk_math_log2_ul(void)80 static void test_fwk_math_log2_ul(void)
81 {
82 unsigned long num = ULONG_MAX;
83 unsigned long expected = (sizeof(num) * CHAR_BIT) - 1;
84
85 assert(fwk_math_log2(num) == expected);
86 }
87
test_fwk_math_log2_ull(void)88 static void test_fwk_math_log2_ull(void)
89 {
90 unsigned long long num = ULLONG_MAX;
91 unsigned long long expected = (sizeof(num) * CHAR_BIT) - 1;
92
93 assert(fwk_math_log2(num) == expected);
94 }
95
96 static const struct fwk_test_case_desc test_case_table[] = {
97 FWK_TEST_CASE(test_fwk_math_pow2_uc),
98 FWK_TEST_CASE(test_fwk_math_pow2_us),
99 FWK_TEST_CASE(test_fwk_math_pow2_ui),
100 FWK_TEST_CASE(test_fwk_math_pow2_ul),
101 FWK_TEST_CASE(test_fwk_math_pow2_ull),
102 FWK_TEST_CASE(test_fwk_math_clz_ui),
103 FWK_TEST_CASE(test_fwk_math_clz_ul),
104 FWK_TEST_CASE(test_fwk_math_clz_ull),
105 FWK_TEST_CASE(test_fwk_math_log2_ui),
106 FWK_TEST_CASE(test_fwk_math_log2_ul),
107 FWK_TEST_CASE(test_fwk_math_log2_ull),
108 };
109
110 struct fwk_test_suite_desc test_suite = {
111 .name = "fwk_math",
112 .test_case_count = FWK_ARRAY_SIZE(test_case_table),
113 .test_case_table = test_case_table,
114 };
115