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