1 /*
2  * Arm SCP/MCP Software
3  * Copyright (c) 2015-2024, 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_test.h>
11 
12 static void test_fwk_macros_array_size(void);
13 static void test_fwk_macros_align_next(void);
14 static void test_fwk_macros_align_previous(void);
15 static void test_fwk_macros_power_of_two_check(void);
16 static void test_fwk_macros_align_check(void);
17 
18 static const struct fwk_test_case_desc test_case_table[] = {
19     FWK_TEST_CASE(test_fwk_macros_array_size),
20     FWK_TEST_CASE(test_fwk_macros_align_next),
21     FWK_TEST_CASE(test_fwk_macros_align_previous),
22     FWK_TEST_CASE(test_fwk_macros_power_of_two_check),
23     FWK_TEST_CASE(test_fwk_macros_align_check),
24 };
25 
26 struct fwk_test_suite_desc test_suite = {
27     .name = "fwk_macros",
28     .test_case_count = FWK_ARRAY_SIZE(test_case_table),
29     .test_case_table = test_case_table,
30 };
31 
test_fwk_macros_array_size(void)32 static void test_fwk_macros_array_size(void)
33 {
34     unsigned int element_no;
35     unsigned int array_uint[5];
36     char array_char[3];
37 
38     element_no = FWK_ARRAY_SIZE(array_uint);
39     assert(element_no == 5);
40 
41     element_no = FWK_ARRAY_SIZE(array_char);
42     assert(element_no == 3);
43 }
44 
test_fwk_macros_align_next(void)45 static void test_fwk_macros_align_next(void)
46 {
47     unsigned int value;
48     unsigned int interval;
49     unsigned int result;
50 
51     value = 0;
52     interval = 32;
53 
54     /* Precedence test */
55     result = FWK_ALIGN_NEXT(value | 1, interval + 32);
56     assert(result == 64);
57 
58     value = 0;
59     interval = 32;
60     result = FWK_ALIGN_NEXT(value, interval);
61     assert(result == 0);
62 
63     value = 8;
64     interval = 8;
65     result = FWK_ALIGN_NEXT(value, interval);
66     assert(result == 8);
67 
68     value = 9;
69     interval = 8;
70     result = FWK_ALIGN_NEXT(value, interval);
71     assert(result == 16);
72 }
73 
test_fwk_macros_align_previous(void)74 static void test_fwk_macros_align_previous(void)
75 {
76     unsigned int value;
77     unsigned int interval;
78     unsigned int result;
79 
80     value = 65;
81     interval = 32;
82 
83     /* Precedence test */
84     result = FWK_ALIGN_PREVIOUS(value & 1, interval + 32);
85     assert(result == 0);
86 
87     value = 0;
88     interval = 32;
89     result = FWK_ALIGN_PREVIOUS(value, interval);
90     assert(result == 0);
91 
92     value = 8;
93     interval = 8;
94     result = FWK_ALIGN_PREVIOUS(value, interval);
95     assert(result == 8);
96 
97     value = 9;
98     interval = 8;
99     result = FWK_ALIGN_PREVIOUS(value, interval);
100     assert(result == 8);
101 }
102 
test_fwk_macros_power_of_two_check(void)103 static void test_fwk_macros_power_of_two_check(void)
104 {
105     bool result;
106     unsigned int value;
107     struct power_of_two_test_data {
108         unsigned long long value;
109         bool verdict;
110     } test_data[] = {
111         { 0x00000, false }, { 0x00017, false },   { 0x0006C, false },
112         { 0x0004D, false }, { 0x00017, false },   { 0x002aC, false },
113         { 0x102aC, false }, { 0x00001, true },    { 0x00100, true },
114         { 0x00004, true },  { 0x00080, true },    { 0x100000, true },
115         { 0x400000, true }, { 0x80000000, true }, { 0xFFFFFFFF, false },
116     };
117     unsigned int test_count = sizeof(test_data) / sizeof(test_data[0]);
118 
119     /* Precedence tests */
120     value = 3;
121     result = FWK_IS_VALUE_POWER_OF_TWO(value & 1);
122     assert(result == true);
123 
124     value = 3;
125     result = FWK_IS_VALUE_POWER_OF_TWO(value & 2);
126     assert(result == true);
127 
128     value = 5;
129     result = FWK_IS_VALUE_POWER_OF_TWO(value & 1);
130     assert(result == true);
131 
132     value = 5;
133     result = FWK_IS_VALUE_POWER_OF_TWO(value & 4);
134     assert(result == true);
135 
136     for (unsigned int i = 0; i < test_count; ++i) {
137         result = FWK_IS_VALUE_POWER_OF_TWO(test_data[i].value);
138         assert(result == test_data[i].verdict);
139     }
140 }
141 
test_fwk_macros_align_check(void)142 static void test_fwk_macros_align_check(void)
143 {
144     bool result;
145     struct alignment_test_data {
146         unsigned int value;
147         unsigned int alignment;
148         bool verdict;
149     } test_data[] = {
150         { 0x0000, 0, false },     { 0x0000, 4, true },
151         { 0x0000, 32, true },     { 0x4000, 4, true },
152         { 0x4000, 8, true },      { 0x2004, 8, false },
153         { 0x1000, 32, true },     { 0x4020, 32, true },
154         { 0x4000, 15, false },    { 0x4000, 17, false },
155         { 0x3008, 16, false },    { 0x9000, 64, true },
156         { 0x9040, 64, true },     { 0x9048, 64, false },
157         { 0x9040, 67, false },    { 0xFFFFFFFF, 0, false },
158         { 0xFFFFFFFF, 4, false }, { 0xFFFFFFFF, 32, false },
159     };
160     unsigned int test_count = sizeof(test_data) / sizeof(test_data[0]);
161     struct alignment_test_data test;
162     /* Precedence tests */
163     test.value = 3;
164     test.alignment = 4;
165     result = FWK_IS_ALIGNED(test.value & 1, test.alignment + 1);
166     assert(result == false);
167 
168     test.value = 0x4000;
169     test.alignment = 3;
170     result = FWK_IS_ALIGNED(test.value, test.alignment + 1);
171     assert(result == true);
172 
173     for (unsigned int i = 0; i < test_count; ++i) {
174         result = FWK_IS_ALIGNED(test_data[i].value, test_data[i].alignment);
175         assert(result == test_data[i].verdict);
176     }
177 }
178