xref: /aosp_15_r20/external/pigweed/pw_polyfill/c_test.c (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #define ARRAY_LEN(array) (sizeof(array) / sizeof(*array))
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [static_assert-example]
18*61c4878aSAndroid Build Coastguard Worker 
19*61c4878aSAndroid Build Coastguard Worker #include "pw_polyfill/standard.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_polyfill/static_assert.h"
21*61c4878aSAndroid Build Coastguard Worker 
22*61c4878aSAndroid Build Coastguard Worker extern int array[3];
23*61c4878aSAndroid Build Coastguard Worker 
24*61c4878aSAndroid Build Coastguard Worker static_assert(ARRAY_LEN(array) == 3, "The array must contain 3 elements");
25*61c4878aSAndroid Build Coastguard Worker 
26*61c4878aSAndroid Build Coastguard Worker static_assert(sizeof(array) == 3 * sizeof(int));  // The message is optional.
27*61c4878aSAndroid Build Coastguard Worker 
28*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [static_assert-example]
29*61c4878aSAndroid Build Coastguard Worker 
30*61c4878aSAndroid Build Coastguard Worker #if 0  // failing asserts
31*61c4878aSAndroid Build Coastguard Worker 
32*61c4878aSAndroid Build Coastguard Worker static_assert(0);  // no message
33*61c4878aSAndroid Build Coastguard Worker static_assert(0, "This static assert should FAIL");
34*61c4878aSAndroid Build Coastguard Worker 
35*61c4878aSAndroid Build Coastguard Worker #endif  // 0
36*61c4878aSAndroid Build Coastguard Worker 
37*61c4878aSAndroid Build Coastguard Worker // static_assert should continue to work even if <assert.h> is included, but,
38*61c4878aSAndroid Build Coastguard Worker // depending on the library, the <assert.h> version of static_assert may
39*61c4878aSAndroid Build Coastguard Worker // override pw_polyfill's, so the message argument may be required.
40*61c4878aSAndroid Build Coastguard Worker 
41*61c4878aSAndroid Build Coastguard Worker #include <assert.h>
42*61c4878aSAndroid Build Coastguard Worker 
43*61c4878aSAndroid Build Coastguard Worker static_assert(1, "This static assert should PASS");
44*61c4878aSAndroid Build Coastguard Worker 
45*61c4878aSAndroid Build Coastguard Worker // Tests for pw_polyfill/standard.h
46*61c4878aSAndroid Build Coastguard Worker 
47*61c4878aSAndroid Build Coastguard Worker // Test that the PW_C_STANDARD_IS_SUPPORTED() is true for C89 and C99 since
48*61c4878aSAndroid Build Coastguard Worker // support is required in Pigweed.
49*61c4878aSAndroid Build Coastguard Worker static_assert(PW_C_STANDARD_IS_SUPPORTED(89),
50*61c4878aSAndroid Build Coastguard Worker               "Macro must be true since Pigweed requires C99 or newer");
51*61c4878aSAndroid Build Coastguard Worker static_assert(PW_C_STANDARD_IS_SUPPORTED(99),
52*61c4878aSAndroid Build Coastguard Worker               "Macro must be true since Pigweed requires C99 or newer");
53*61c4878aSAndroid Build Coastguard Worker 
54*61c4878aSAndroid Build Coastguard Worker // Test that PW_C_STANDARD_IS_SUPPORTED() for C11, C17, and C23 is true or false
55*61c4878aSAndroid Build Coastguard Worker // depending on the value of __STDC_VERSION__.
56*61c4878aSAndroid Build Coastguard Worker static_assert(PW_C_STANDARD_IS_SUPPORTED(11) == (__STDC_VERSION__ >= 201112L),
57*61c4878aSAndroid Build Coastguard Worker               "Macro value should match __STDC_VERSION__");
58*61c4878aSAndroid Build Coastguard Worker static_assert(PW_C_STANDARD_IS_SUPPORTED(17) == (__STDC_VERSION__ >= 201710L),
59*61c4878aSAndroid Build Coastguard Worker               "Macro value should match __STDC_VERSION__");
60*61c4878aSAndroid Build Coastguard Worker static_assert(PW_C_STANDARD_IS_SUPPORTED(23) == (__STDC_VERSION__ >= 202311L),
61*61c4878aSAndroid Build Coastguard Worker               "Macro value should match __STDC_VERSION__");
62*61c4878aSAndroid Build Coastguard Worker 
63*61c4878aSAndroid Build Coastguard Worker // Test that PW_CXX_STANDARD_IS_SUPPORTED() is always false since this is a C
64*61c4878aSAndroid Build Coastguard Worker // file.
65*61c4878aSAndroid Build Coastguard Worker static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(98),
66*61c4878aSAndroid Build Coastguard Worker               "PW_CXX_STANDARD_IS_SUPPORTED() must always be false in C");
67*61c4878aSAndroid Build Coastguard Worker static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(11),
68*61c4878aSAndroid Build Coastguard Worker               "PW_CXX_STANDARD_IS_SUPPORTED() must always be false in C");
69*61c4878aSAndroid Build Coastguard Worker static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(14),
70*61c4878aSAndroid Build Coastguard Worker               "PW_CXX_STANDARD_IS_SUPPORTED() must always be false in C");
71*61c4878aSAndroid Build Coastguard Worker static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(17),
72*61c4878aSAndroid Build Coastguard Worker               "PW_CXX_STANDARD_IS_SUPPORTED() must always be false in C");
73*61c4878aSAndroid Build Coastguard Worker static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(20),
74*61c4878aSAndroid Build Coastguard Worker               "PW_CXX_STANDARD_IS_SUPPORTED() must always be false in C");
75*61c4878aSAndroid Build Coastguard Worker static_assert(!PW_CXX_STANDARD_IS_SUPPORTED(23),
76*61c4878aSAndroid Build Coastguard Worker               "PW_CXX_STANDARD_IS_SUPPORTED() must always be false in C");
77