aboutsummaryrefslogtreecommitdiff
path: root/pw_tokenizer/argument_types_test_c.c
blob: 2030587b538b7a782ca06e681178e34c429c1fef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// This C source file tests that the tokenizer argument type encoding works
// correctly in C. These functions are called from the main C++ test file
// argument_types_test.cc.
#include <assert.h>
#include <stddef.h>

#include "pw_tokenizer_private/argument_types_test.h"

#ifdef __cplusplus
#error "This is a test of C code and must be compiled as C, not C++."
#endif  // __cplusplus

struct DummyType {};  // stand-in type for pointer argument type test

// Check each relevant type mapping using static_asserts.
#define CHECK_TYPE(c_type, enum_type)                     \
  static_assert(_PW_VARARGS_TYPE((c_type)1) == enum_type, \
                #c_type " should map to " #enum_type)

// integral
// clang-format off
CHECK_TYPE(_Bool,              PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(char,               PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(signed char,        PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(unsigned char,      PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(short,              PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(unsigned short,     PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(int,                PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(unsigned int,       PW_TOKENIZER_ARG_TYPE_INT);
CHECK_TYPE(long,              _PW_TOKENIZER_SELECT_INT_TYPE(long));
CHECK_TYPE(unsigned long,     _PW_TOKENIZER_SELECT_INT_TYPE(unsigned long));
CHECK_TYPE(long long,          PW_TOKENIZER_ARG_TYPE_INT64);
CHECK_TYPE(unsigned long long, PW_TOKENIZER_ARG_TYPE_INT64);

// floating point
CHECK_TYPE(float,              PW_TOKENIZER_ARG_TYPE_DOUBLE);
CHECK_TYPE(double,             PW_TOKENIZER_ARG_TYPE_DOUBLE);
CHECK_TYPE(long double,        PW_TOKENIZER_ARG_TYPE_DOUBLE);

// strings
CHECK_TYPE(char*,              PW_TOKENIZER_ARG_TYPE_STRING);
CHECK_TYPE(const char*,        PW_TOKENIZER_ARG_TYPE_STRING);

// pointers (which should map to the appropriate sized integer)
CHECK_TYPE(void*,             _PW_TOKENIZER_SELECT_INT_TYPE(void*));
CHECK_TYPE(const void*,       _PW_TOKENIZER_SELECT_INT_TYPE(void*));
CHECK_TYPE(signed char*,      _PW_TOKENIZER_SELECT_INT_TYPE(void*));
CHECK_TYPE(unsigned char*,    _PW_TOKENIZER_SELECT_INT_TYPE(void*));
CHECK_TYPE(int*,              _PW_TOKENIZER_SELECT_INT_TYPE(void*));
CHECK_TYPE(long long*,        _PW_TOKENIZER_SELECT_INT_TYPE(void*));
CHECK_TYPE(struct DummyType*, _PW_TOKENIZER_SELECT_INT_TYPE(void*));
// clang-format on

// null
static_assert(_PW_VARARGS_TYPE(NULL) == _PW_TOKENIZER_SELECT_INT_TYPE(void*),
              "");

static char char_array[16];

// Define the test functions that are called by the C++ unit test.
#define DEFINE_TEST_FUNCTION(name, ...)                 \
  _pw_tokenizer_ArgTypes pw_TestTokenizer##name(void) { \
    (void)char_array;                                   \
    return PW_TOKENIZER_ARG_TYPES(__VA_ARGS__);         \
  }

DEFINE_TEST_FUNCTION(NoArgs);

DEFINE_TEST_FUNCTION(Char, 'a');
DEFINE_TEST_FUNCTION(Uint8, ((uint8_t)23));
DEFINE_TEST_FUNCTION(Uint16, ((int16_t)100));
DEFINE_TEST_FUNCTION(Int32, ((int32_t)1));
DEFINE_TEST_FUNCTION(Int64, ((int64_t)0));
DEFINE_TEST_FUNCTION(Uint64, ((uint64_t)1));
DEFINE_TEST_FUNCTION(Float, 1e10f)
DEFINE_TEST_FUNCTION(Double, -2.5e-50);
DEFINE_TEST_FUNCTION(String, "const char*");
DEFINE_TEST_FUNCTION(MutableString, ((char*)NULL));

DEFINE_TEST_FUNCTION(IntFloat, 54321, ((float)0));
DEFINE_TEST_FUNCTION(Uint64Char, ((uint64_t)0ull), ((unsigned char)'x'));
DEFINE_TEST_FUNCTION(StringString, char_array, ((const char*)NULL));
DEFINE_TEST_FUNCTION(Uint16Int, ((uint16_t)100), ((int)0));
DEFINE_TEST_FUNCTION(FloatString, 100.0f, "string");

DEFINE_TEST_FUNCTION(Null, NULL);
DEFINE_TEST_FUNCTION(Pointer, ((void*)NULL));
DEFINE_TEST_FUNCTION(PointerPointer, (int*)char_array, (void*)0);