aboutsummaryrefslogtreecommitdiff
path: root/pw_tokenizer/argument_types_test.cc
blob: 10c3095295c125bfeafcbb36c1ee8622cda2f0a6 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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.

#include "pw_tokenizer/internal/argument_types.h"

#include <cstddef>

#include "gtest/gtest.h"
#include "pw_preprocessor/concat.h"
#include "pw_tokenizer/tokenize.h"
#include "pw_tokenizer_private/argument_types_test.h"

namespace pw::tokenizer {
namespace {

struct FakeType {};

// Check each relevant type mapping.
#define CHECK_TYPE(c_type, enum_type)                     \
  static_assert(_PW_VARARGS_TYPE((c_type)0) == 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(FakeType*,         _PW_TOKENIZER_SELECT_INT_TYPE(void*));

// nullptr
CHECK_TYPE(std::nullptr_t,    _PW_TOKENIZER_SELECT_INT_TYPE(void*));
static_assert(_PW_VARARGS_TYPE(nullptr) ==
              _PW_TOKENIZER_SELECT_INT_TYPE(void*));

// clang-format on

// Define a macro that generates expected values for tests. This works with
// either 4-bit or 6-bit argument counts (for encoding types in a uint32_t or
// uint64_t).
#define PACKED_TYPES(...)                                                 \
  ((PW_CONCAT(0b, __VA_ARGS__, u) << PW_TOKENIZER_TYPE_COUNT_SIZE_BITS) | \
   PW_MACRO_ARG_COUNT(__VA_ARGS__))

// Test this test macro for both uint32_t and uint64_t.
#if PW_TOKENIZER_CFG_ARG_TYPES_SIZE_BYTES == 4

static_assert(PACKED_TYPES(00) == 0b00'0001u);
static_assert(PACKED_TYPES(11) == 0b11'0001u);
static_assert(PACKED_TYPES(01, 10) == 0b0110'0010u);
static_assert(PACKED_TYPES(11, 01, 10) == 0b110110'0011u);
static_assert(PACKED_TYPES(11, 10, 01, 00) == 0b11'10'01'00'0100u);

#elif PW_TOKENIZER_CFG_ARG_TYPES_SIZE_BYTES == 8

static_assert(PACKED_TYPES(00) == 0b00'000001u);
static_assert(PACKED_TYPES(11) == 0b11'000001u);
static_assert(PACKED_TYPES(01, 10) == 0b0110'000010u);
static_assert(PACKED_TYPES(11, 01, 10) == 0b110110'000011u);
static_assert(PACKED_TYPES(11, 10, 01, 00) == 0b11'10'01'00'000100u);

#else

#error "Unsupported value for PW_TOKENIZER_CFG_ARG_TYPES_SIZE_BYTES"

#endif  // PW_TOKENIZER_CFG_ARG_TYPES_SIZE_BYTES

#define SOME_OTHER_MACRO(...) PW_TOKENIZER_ARG_TYPES(__VA_ARGS__)

TEST(ArgumentTypes, Empty) {
  static_assert(PW_TOKENIZER_ARG_TYPES() == 0u);
  static_assert(PW_TOKENIZER_ARG_TYPES(/* nothing here */) == 0u);
  static_assert(SOME_OTHER_MACRO() == 0u);
  static_assert(SOME_OTHER_MACRO(/* nothing here */) == 0u);
}

TEST(ArgumentTypes, Int32) {
  static_assert(PW_TOKENIZER_ARG_TYPES(0) == PACKED_TYPES(00));
  static_assert(PW_TOKENIZER_ARG_TYPES('a') == PACKED_TYPES(00));
  static_assert(PW_TOKENIZER_ARG_TYPES(u'X') == PACKED_TYPES(00));
  static_assert(PW_TOKENIZER_ARG_TYPES(static_cast<uint16_t>(1)) ==
                PACKED_TYPES(00));
}

TEST(ArgumentTypes, Int64) {
  static_assert(PW_TOKENIZER_ARG_TYPES(-123ll) == PACKED_TYPES(01));
  static_assert(PW_TOKENIZER_ARG_TYPES(123ull) == PACKED_TYPES(01));
  static_assert(PW_TOKENIZER_ARG_TYPES(static_cast<uint64_t>(1)) ==
                PACKED_TYPES(01));
}

TEST(ArgumentTypes, Double) {
  static_assert(PW_TOKENIZER_ARG_TYPES(1.0) == PACKED_TYPES(10));
  static_assert(PW_TOKENIZER_ARG_TYPES(5.0f) == PACKED_TYPES(10));
  float number = 9;
  static_assert(PW_TOKENIZER_ARG_TYPES(number) == PACKED_TYPES(10));
}

TEST(ArgumentTypes, String) {
  static_assert(PW_TOKENIZER_ARG_TYPES("string") == PACKED_TYPES(11));
  const char buffer[2] = {'a', 'b'};
  static_assert(PW_TOKENIZER_ARG_TYPES(buffer) == PACKED_TYPES(11));
  char mutable_buffer[8] = {};
  static_assert(PW_TOKENIZER_ARG_TYPES(mutable_buffer) == PACKED_TYPES(11));
  char character = 'a';
  static_assert(PW_TOKENIZER_ARG_TYPES(&character) == PACKED_TYPES(11));
}

TEST(ArgumentTypes, Pointer) {
  static const bool some_data[8] = {};

  // For 32-bit systems, non-string pointers show up as int32.
  static_assert(sizeof(void*) != sizeof(int32_t) ||
                PACKED_TYPES(00) == PW_TOKENIZER_ARG_TYPES(nullptr));
  static_assert(sizeof(void*) != sizeof(int32_t) ||
                PACKED_TYPES(00) ==
                    PW_TOKENIZER_ARG_TYPES(static_cast<void*>(nullptr)));
  static_assert(sizeof(void*) != sizeof(int32_t) ||
                PACKED_TYPES(00) == PW_TOKENIZER_ARG_TYPES(some_data));

  // For 64-bit systems, non-string pointers show up as int64.
  static_assert(sizeof(void*) != sizeof(int64_t) ||
                PACKED_TYPES(01) == PW_TOKENIZER_ARG_TYPES(nullptr));
  static_assert(sizeof(void*) != sizeof(int64_t) ||
                PACKED_TYPES(01) ==
                    PW_TOKENIZER_ARG_TYPES(static_cast<void*>(nullptr)));
  static_assert(sizeof(void*) != sizeof(int64_t) ||
                PACKED_TYPES(01) == PW_TOKENIZER_ARG_TYPES(some_data));
}

TEST(ArgumentTypes, TwoArgs) {
  static_assert(PW_TOKENIZER_ARG_TYPES(-100, 1000) == PACKED_TYPES(00, 00));
  static_assert(PW_TOKENIZER_ARG_TYPES(-100, 10ll) == PACKED_TYPES(01, 00));
  static_assert(PW_TOKENIZER_ARG_TYPES(-100, 1.0f) == PACKED_TYPES(10, 00));
  static_assert(PW_TOKENIZER_ARG_TYPES(-100, "hi") == PACKED_TYPES(11, 00));

  static_assert(PW_TOKENIZER_ARG_TYPES(1ull, 1000) == PACKED_TYPES(00, 01));
  static_assert(PW_TOKENIZER_ARG_TYPES(1ull, 10ll) == PACKED_TYPES(01, 01));
  static_assert(PW_TOKENIZER_ARG_TYPES(1ull, 1.0f) == PACKED_TYPES(10, 01));
  static_assert(PW_TOKENIZER_ARG_TYPES(1ull, "hi") == PACKED_TYPES(11, 01));

  static_assert(PW_TOKENIZER_ARG_TYPES(9.0f, 1000) == PACKED_TYPES(00, 10));
  static_assert(PW_TOKENIZER_ARG_TYPES(9.0f, 10ll) == PACKED_TYPES(01, 10));
  static_assert(PW_TOKENIZER_ARG_TYPES(9.0f, 1.0f) == PACKED_TYPES(10, 10));
  static_assert(PW_TOKENIZER_ARG_TYPES(9.0f, "hi") == PACKED_TYPES(11, 10));

  static_assert(PW_TOKENIZER_ARG_TYPES("!!", 1000) == PACKED_TYPES(00, 11));
  static_assert(PW_TOKENIZER_ARG_TYPES("!!", 10ll) == PACKED_TYPES(01, 11));
  static_assert(PW_TOKENIZER_ARG_TYPES("!!", 1.0f) == PACKED_TYPES(10, 11));
  static_assert(PW_TOKENIZER_ARG_TYPES("!!", "hi") == PACKED_TYPES(11, 11));
}

TEST(ArgumentTypes, MultipleArgs) {
  // clang-format off
  static_assert(PW_TOKENIZER_ARG_TYPES(1) == 1);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2) == 2);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3) == 3);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4) == 4);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5) == 5);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6) == 6);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7) == 7);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8) == 8);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9) == 9);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) == 10);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) == 11);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) == 12);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) == 13);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) == 14);

#if PW_TOKENIZER_CFG_ARG_TYPES_SIZE_BYTES >= 8
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14) == 14);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) == 15);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) == 16);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) == 17);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) == 18);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19) == 19);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) == 20);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21) == 21);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22) == 22);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23) == 23);
  static_assert(PW_TOKENIZER_ARG_TYPES(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24) == 24);
  static_assert(PW_TOKENIZER_ARG_TYPES(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) == 25);
  static_assert(PW_TOKENIZER_ARG_TYPES(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) == 26);
  static_assert(PW_TOKENIZER_ARG_TYPES(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) == 27);
  static_assert(PW_TOKENIZER_ARG_TYPES(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) == 28);
  static_assert(PW_TOKENIZER_ARG_TYPES(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) == 29);
#endif  // PW_TOKENIZER_CFG_ARG_TYPES_SIZE_BYTES
  // clang-format on
}

// The ArgumentTypesFromC test suite tests the arguments types macro in C. The
// pw_Test* functions are defined in argument_types_c_test.c
TEST(ArgumentTypesFromC, NoArgs) {
  EXPECT_EQ(0b0000u, pw_TestTokenizerNoArgs());
}

TEST(ArgumentTypesFromC, OneArg_Int32) {
  EXPECT_EQ(PACKED_TYPES(00), pw_TestTokenizerChar());
  EXPECT_EQ(PACKED_TYPES(00), pw_TestTokenizerUint8());
  EXPECT_EQ(PACKED_TYPES(00), pw_TestTokenizerUint16());
  EXPECT_EQ(PACKED_TYPES(00), pw_TestTokenizerInt32());
}

TEST(ArgumentTypesFromC, OneArg_Int64) {
  EXPECT_EQ(PACKED_TYPES(01), pw_TestTokenizerInt64());
  EXPECT_EQ(PACKED_TYPES(01), pw_TestTokenizerUint64());
}

TEST(ArgumentTypesFromC, OneArg_Float) {
  EXPECT_EQ(PACKED_TYPES(10), pw_TestTokenizerFloat());
  EXPECT_EQ(PACKED_TYPES(10), pw_TestTokenizerDouble());
}

TEST(ArgumentTypesFromC, OneArg_String) {
  EXPECT_EQ(PACKED_TYPES(11), pw_TestTokenizerString());
  EXPECT_EQ(PACKED_TYPES(11), pw_TestTokenizerMutableString());
}

TEST(ArgumentTypesFromC, MultipleArgs) {
  EXPECT_EQ(PACKED_TYPES(10, 00), pw_TestTokenizerIntFloat());
  EXPECT_EQ(PACKED_TYPES(00, 01), pw_TestTokenizerUint64Char());
  EXPECT_EQ(PACKED_TYPES(11, 11), pw_TestTokenizerStringString());
  EXPECT_EQ(PACKED_TYPES(00, 00), pw_TestTokenizerUint16Int());
  EXPECT_EQ(PACKED_TYPES(11, 10), pw_TestTokenizerFloatString());
}

TEST(ArgumentTypesFromC, Pointers) {
  if constexpr (sizeof(void*) == sizeof(int32_t)) {
    EXPECT_EQ(PACKED_TYPES(00), pw_TestTokenizerNull());
    EXPECT_EQ(PACKED_TYPES(00), pw_TestTokenizerPointer());
    EXPECT_EQ(PACKED_TYPES(00, 00), pw_TestTokenizerPointerPointer());
  } else {  // 64-bit system
    EXPECT_EQ(PACKED_TYPES(01), pw_TestTokenizerNull());
    EXPECT_EQ(PACKED_TYPES(01), pw_TestTokenizerPointer());
    EXPECT_EQ(PACKED_TYPES(01, 01), pw_TestTokenizerPointerPointer());
  }
}

}  // namespace
}  // namespace pw::tokenizer