1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
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.
21 #include "pw_tokenizer_private/argument_types_test.h"
24 #error "This is a test of C code and must be compiled as C, not C++."
27 struct DummyType {}; // stand-in type for pointer argument type test
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)
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);
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);
55 CHECK_TYPE(char*, PW_TOKENIZER_ARG_TYPE_STRING);
56 CHECK_TYPE(const char*, PW_TOKENIZER_ARG_TYPE_STRING);
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*));
69 static_assert(_PW_VARARGS_TYPE(NULL) == _PW_TOKENIZER_SELECT_INT_TYPE(void*),
72 static char char_array[16];
74 // Define the test functions that are called by the C++ unit test.
75 #define DEFINE_TEST_FUNCTION(name, ...) \
76 _pw_tokenizer_ArgTypes pw_TestTokenizer##name(void) { \
78 return PW_TOKENIZER_ARG_TYPES(__VA_ARGS__); \
81 DEFINE_TEST_FUNCTION(NoArgs);
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));
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");
100 DEFINE_TEST_FUNCTION(Null, NULL);
101 DEFINE_TEST_FUNCTION(Pointer, ((void*)NULL));
102 DEFINE_TEST_FUNCTION(PointerPointer, (int*)char_array, (void*)0);