Wyatt Hepler | 80c6ee5 | 2020-01-03 09:54:58 -0800 | [diff] [blame] | 1 | // Copyright 2020 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 | // This C source file tests that the tokenizer argument type encoding works |
| 16 | // correctly in C. These functions are called from the main C++ test file |
| 17 | // argument_types_test.cc. |
Wyatt Hepler | 80c6ee5 | 2020-01-03 09:54:58 -0800 | [diff] [blame] | 18 | #include <assert.h> |
| 19 | #include <stddef.h> |
| 20 | |
Michael Spang | a99220e | 2020-06-11 20:07:16 -0400 | [diff] [blame] | 21 | #include "pw_tokenizer_private/argument_types_test.h" |
| 22 | |
Wyatt Hepler | 80c6ee5 | 2020-01-03 09:54:58 -0800 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | #error "This is a test of C code and must be compiled as C, not C++." |
| 25 | #endif // __cplusplus |
| 26 | |
| 27 | struct DummyType {}; // stand-in type for pointer argument type test |
| 28 | |
| 29 | // Check each relevant type mapping using static_asserts. |
| 30 | #define CHECK_TYPE(c_type, enum_type) \ |
| 31 | static_assert(_PW_VARARGS_TYPE((c_type)1) == enum_type, \ |
| 32 | #c_type " should map to " #enum_type) |
| 33 | |
| 34 | // integral |
| 35 | // clang-format off |
| 36 | CHECK_TYPE(_Bool, PW_TOKENIZER_ARG_TYPE_INT); |
| 37 | CHECK_TYPE(char, PW_TOKENIZER_ARG_TYPE_INT); |
| 38 | CHECK_TYPE(signed char, PW_TOKENIZER_ARG_TYPE_INT); |
| 39 | CHECK_TYPE(unsigned char, PW_TOKENIZER_ARG_TYPE_INT); |
| 40 | CHECK_TYPE(short, PW_TOKENIZER_ARG_TYPE_INT); |
| 41 | CHECK_TYPE(unsigned short, PW_TOKENIZER_ARG_TYPE_INT); |
| 42 | CHECK_TYPE(int, PW_TOKENIZER_ARG_TYPE_INT); |
| 43 | CHECK_TYPE(unsigned int, PW_TOKENIZER_ARG_TYPE_INT); |
| 44 | CHECK_TYPE(long, _PW_TOKENIZER_SELECT_INT_TYPE(long)); |
| 45 | CHECK_TYPE(unsigned long, _PW_TOKENIZER_SELECT_INT_TYPE(unsigned long)); |
| 46 | CHECK_TYPE(long long, PW_TOKENIZER_ARG_TYPE_INT64); |
| 47 | CHECK_TYPE(unsigned long long, PW_TOKENIZER_ARG_TYPE_INT64); |
| 48 | |
| 49 | // floating point |
| 50 | CHECK_TYPE(float, PW_TOKENIZER_ARG_TYPE_DOUBLE); |
| 51 | CHECK_TYPE(double, PW_TOKENIZER_ARG_TYPE_DOUBLE); |
| 52 | CHECK_TYPE(long double, PW_TOKENIZER_ARG_TYPE_DOUBLE); |
| 53 | |
| 54 | // strings |
| 55 | CHECK_TYPE(char*, PW_TOKENIZER_ARG_TYPE_STRING); |
| 56 | CHECK_TYPE(const char*, PW_TOKENIZER_ARG_TYPE_STRING); |
| 57 | |
| 58 | // pointers (which should map to the appropriate sized integer) |
| 59 | CHECK_TYPE(void*, _PW_TOKENIZER_SELECT_INT_TYPE(void*)); |
| 60 | CHECK_TYPE(const void*, _PW_TOKENIZER_SELECT_INT_TYPE(void*)); |
| 61 | CHECK_TYPE(signed char*, _PW_TOKENIZER_SELECT_INT_TYPE(void*)); |
| 62 | CHECK_TYPE(unsigned char*, _PW_TOKENIZER_SELECT_INT_TYPE(void*)); |
| 63 | CHECK_TYPE(int*, _PW_TOKENIZER_SELECT_INT_TYPE(void*)); |
| 64 | CHECK_TYPE(long long*, _PW_TOKENIZER_SELECT_INT_TYPE(void*)); |
| 65 | CHECK_TYPE(struct DummyType*, _PW_TOKENIZER_SELECT_INT_TYPE(void*)); |
| 66 | // clang-format on |
| 67 | |
| 68 | // null |
| 69 | static_assert(_PW_VARARGS_TYPE(NULL) == _PW_TOKENIZER_SELECT_INT_TYPE(void*), |
| 70 | ""); |
| 71 | |
| 72 | static char char_array[16]; |
| 73 | |
| 74 | // Define the test functions that are called by the C++ unit test. |
Wyatt Hepler | bcf0735 | 2021-04-05 14:44:30 -0700 | [diff] [blame] | 75 | #define DEFINE_TEST_FUNCTION(name, ...) \ |
| 76 | pw_tokenizer_ArgTypes pw_TestTokenizer##name(void) { \ |
| 77 | (void)char_array; \ |
| 78 | return PW_TOKENIZER_ARG_TYPES(__VA_ARGS__); \ |
Wyatt Hepler | 80c6ee5 | 2020-01-03 09:54:58 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | DEFINE_TEST_FUNCTION(NoArgs); |
| 82 | |
| 83 | DEFINE_TEST_FUNCTION(Char, 'a'); |
| 84 | DEFINE_TEST_FUNCTION(Uint8, ((uint8_t)23)); |
| 85 | DEFINE_TEST_FUNCTION(Uint16, ((int16_t)100)); |
| 86 | DEFINE_TEST_FUNCTION(Int32, ((int32_t)1)); |
| 87 | DEFINE_TEST_FUNCTION(Int64, ((int64_t)0)); |
| 88 | DEFINE_TEST_FUNCTION(Uint64, ((uint64_t)1)); |
| 89 | DEFINE_TEST_FUNCTION(Float, 1e10f) |
| 90 | DEFINE_TEST_FUNCTION(Double, -2.5e-50); |
| 91 | DEFINE_TEST_FUNCTION(String, "const char*"); |
| 92 | DEFINE_TEST_FUNCTION(MutableString, ((char*)NULL)); |
| 93 | |
| 94 | DEFINE_TEST_FUNCTION(IntFloat, 54321, ((float)0)); |
| 95 | DEFINE_TEST_FUNCTION(Uint64Char, ((uint64_t)0ull), ((unsigned char)'x')); |
| 96 | DEFINE_TEST_FUNCTION(StringString, char_array, ((const char*)NULL)); |
| 97 | DEFINE_TEST_FUNCTION(Uint16Int, ((uint16_t)100), ((int)0)); |
| 98 | DEFINE_TEST_FUNCTION(FloatString, 100.0f, "string"); |
| 99 | |
| 100 | DEFINE_TEST_FUNCTION(Null, NULL); |
| 101 | DEFINE_TEST_FUNCTION(Pointer, ((void*)NULL)); |
| 102 | DEFINE_TEST_FUNCTION(PointerPointer, (int*)char_array, (void*)0); |