blob: 849357d24ebe46c99b53c0b3b03f333dfec3b6c3 [file] [log] [blame]
Wyatt Hepler80c6ee52020-01-03 09:54:58 -08001// 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 Hepler80c6ee52020-01-03 09:54:58 -080018#include <assert.h>
19#include <stddef.h>
20
Michael Spanga99220e2020-06-11 20:07:16 -040021#include "pw_tokenizer_private/argument_types_test.h"
22
Wyatt Hepler80c6ee52020-01-03 09:54:58 -080023#ifdef __cplusplus
24#error "This is a test of C code and must be compiled as C, not C++."
25#endif // __cplusplus
26
27struct 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
36CHECK_TYPE(_Bool, PW_TOKENIZER_ARG_TYPE_INT);
37CHECK_TYPE(char, PW_TOKENIZER_ARG_TYPE_INT);
38CHECK_TYPE(signed char, PW_TOKENIZER_ARG_TYPE_INT);
39CHECK_TYPE(unsigned char, PW_TOKENIZER_ARG_TYPE_INT);
40CHECK_TYPE(short, PW_TOKENIZER_ARG_TYPE_INT);
41CHECK_TYPE(unsigned short, PW_TOKENIZER_ARG_TYPE_INT);
42CHECK_TYPE(int, PW_TOKENIZER_ARG_TYPE_INT);
43CHECK_TYPE(unsigned int, PW_TOKENIZER_ARG_TYPE_INT);
44CHECK_TYPE(long, _PW_TOKENIZER_SELECT_INT_TYPE(long));
45CHECK_TYPE(unsigned long, _PW_TOKENIZER_SELECT_INT_TYPE(unsigned long));
46CHECK_TYPE(long long, PW_TOKENIZER_ARG_TYPE_INT64);
47CHECK_TYPE(unsigned long long, PW_TOKENIZER_ARG_TYPE_INT64);
48
49// floating point
50CHECK_TYPE(float, PW_TOKENIZER_ARG_TYPE_DOUBLE);
51CHECK_TYPE(double, PW_TOKENIZER_ARG_TYPE_DOUBLE);
52CHECK_TYPE(long double, PW_TOKENIZER_ARG_TYPE_DOUBLE);
53
54// strings
55CHECK_TYPE(char*, PW_TOKENIZER_ARG_TYPE_STRING);
56CHECK_TYPE(const char*, PW_TOKENIZER_ARG_TYPE_STRING);
57
58// pointers (which should map to the appropriate sized integer)
59CHECK_TYPE(void*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
60CHECK_TYPE(const void*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
61CHECK_TYPE(signed char*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
62CHECK_TYPE(unsigned char*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
63CHECK_TYPE(int*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
64CHECK_TYPE(long long*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
65CHECK_TYPE(struct DummyType*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
66// clang-format on
67
68// null
69static_assert(_PW_VARARGS_TYPE(NULL) == _PW_TOKENIZER_SELECT_INT_TYPE(void*),
70 "");
71
72static char char_array[16];
73
74// Define the test functions that are called by the C++ unit test.
Wyatt Heplerbcf07352021-04-05 14:44:30 -070075#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 Hepler80c6ee52020-01-03 09:54:58 -080079 }
80
81DEFINE_TEST_FUNCTION(NoArgs);
82
83DEFINE_TEST_FUNCTION(Char, 'a');
84DEFINE_TEST_FUNCTION(Uint8, ((uint8_t)23));
85DEFINE_TEST_FUNCTION(Uint16, ((int16_t)100));
86DEFINE_TEST_FUNCTION(Int32, ((int32_t)1));
87DEFINE_TEST_FUNCTION(Int64, ((int64_t)0));
88DEFINE_TEST_FUNCTION(Uint64, ((uint64_t)1));
89DEFINE_TEST_FUNCTION(Float, 1e10f)
90DEFINE_TEST_FUNCTION(Double, -2.5e-50);
91DEFINE_TEST_FUNCTION(String, "const char*");
92DEFINE_TEST_FUNCTION(MutableString, ((char*)NULL));
93
94DEFINE_TEST_FUNCTION(IntFloat, 54321, ((float)0));
95DEFINE_TEST_FUNCTION(Uint64Char, ((uint64_t)0ull), ((unsigned char)'x'));
96DEFINE_TEST_FUNCTION(StringString, char_array, ((const char*)NULL));
97DEFINE_TEST_FUNCTION(Uint16Int, ((uint16_t)100), ((int)0));
98DEFINE_TEST_FUNCTION(FloatString, 100.0f, "string");
99
100DEFINE_TEST_FUNCTION(Null, NULL);
101DEFINE_TEST_FUNCTION(Pointer, ((void*)NULL));
102DEFINE_TEST_FUNCTION(PointerPointer, (int*)char_array, (void*)0);