aboutsummaryrefslogtreecommitdiff
path: root/pw_tokenizer/generate_decoding_test_data.cc
blob: 34e6dd2321764a7b5a594eb80058cb9c5439e8ad (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// 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 program generates Python test data for decoder_test.py.
//
// To generate the test data, build the target
// pw_tokenizer_generate_decoding_test_data. Execute the binary and move the
// generated files to this directory.

#include <array>
#include <cctype>
#include <cinttypes>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <random>

#include "pw_span/span.h"
#include "pw_tokenizer/internal/decode.h"
#include "pw_tokenizer/tokenize.h"
#include "pw_varint/varint.h"

namespace {

// Defines how to format test cases for the target language.
struct SourceFileFormat {
  const char* extension;
  const char* comment;
  const char* header;
  const char* footer;
  const char* test_case_prefix;
  const char* binary_string_prefix;
  const char* binary_string_suffix;
};

// clang-format off
constexpr const char* kCopyrightLines[] = {
"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.",
};
// clang-format on

// The header includes a %s for the name and a %s for the test case type.
constexpr const char kCcHeader[] = R"(#pragma once

#include <string_view>
#include <tuple>

namespace pw::test::%s {

using namespace std::literals::string_view_literals;

// clang-format off
using TestCase = %s;

inline constexpr TestCase kTestData[] = {
)";

constexpr const char kCcFooter[] = R"(
};

}  // namespace pw::test::%s
)";

constexpr const char kPythonHeader[] = R"("""Generated test data."""

# pylint: disable=line-too-long
# C++ test case type for %s:
#     %s


def TestCase(*args):  # pylint: disable=invalid-name
    return tuple(args)



TEST_DATA = (
)";

constexpr SourceFileFormat kCcFormat{
    ".h", "//", kCcHeader, kCcFooter, "TestCase", "\"", "\"sv"};

constexpr SourceFileFormat kPythonFormat{
    ".py", "#", kPythonHeader, "\n)\n", "", "b'", "'"};

class TestDataFile {
 public:
  TestDataFile(const char* name,
               const SourceFileFormat& format,
               const char* test_case_format)
      : format_(format),
        name_(name),
        test_case_format_(test_case_format),
        path_(std::string(name) + "_test_data" + format_.extension),
        file_(std::fopen(path_.c_str(), "w")) {}

  ~TestDataFile() { std::fclose(file_); }

  const SourceFileFormat& fmt() const { return format_; }
  const std::string& path() const { return path_; }

  // Writes a file with test cases uses the provided function.
  void WriteTestCases(void (*function)(TestDataFile*)) {
    static constexpr const char* kFileBase =
        &__FILE__[std::string_view(__FILE__).find_last_of('/') + 1];

    for (const char* line : kCopyrightLines) {
      printf("%s", fmt().comment);
      if (line[0] == '\0') {
        printf("\n");
      } else {
        printf(" %s\n", line);
      }
    }

    printf("\n%s AUTOGENERATED - DO NOT EDIT\n", fmt().comment);
    printf("%s This file contains test data generated by %s.\n",
           fmt().comment,
           kFileBase);

    printf(fmt().header, name_, test_case_format_);
    function(this);
    printf(fmt().footer, name_);
  }

  // Starts a section of test cases in the file.
  void Section(const char* comment) {
    printf("\n%s %s\n", fmt().comment, comment);
  }

  int printf(const char* format, ...) PW_PRINTF_FORMAT(2, 3) {
    va_list args;
    va_start(args, format);
    const int result = std::vfprintf(file_, format, args);
    va_end(args);
    return result;
  }

 private:
  SourceFileFormat format_;
  const char* name_;
  const char* test_case_format_;
  std::string path_;
  FILE* file_;
};

// Writes a decoding test case to the file.
void TestCase(TestDataFile* file,
              pw::span<const uint8_t> buffer,
              const char* format,
              const char* formatted) {
  file->printf(R"(TestCase("%s", "%s", %s)",
               format,
               formatted,
               file->fmt().binary_string_prefix);

  for (uint8_t byte : buffer) {
    file->printf("\\x%02x", byte);
  }

  file->printf("%s),\n", file->fmt().binary_string_suffix);
}

template <size_t kSize>
void TestCase(TestDataFile* file,
              const char* format,
              const char (&buffer)[kSize],
              const char* formatted) {
  TestCase(file,
           pw::span(reinterpret_cast<const uint8_t*>(buffer), kSize - 1),
           format,
           formatted);
}

// __VA_ARGS__ is expanded twice, so ONLY variables / constants should be used.
#define MAKE_TEST_CASE(format, ...)                                           \
  do {                                                                        \
    std::array<uint8_t, 128> buffer;                                          \
    size_t size = buffer.size();                                              \
    PW_TOKENIZE_TO_BUFFER(buffer.data(), &size, format, ##__VA_ARGS__);       \
                                                                              \
    std::array<char, 128> formatted = {};                                     \
    std::snprintf(formatted.data(), formatted.size(), format, ##__VA_ARGS__); \
    TestCase(file,                                                            \
             pw::span(buffer).first(size).subspan(4), /* skip the token */    \
             format,                                                          \
             formatted.data());                                               \
  } while (0)

// Formats the contents like an error.
#define ERROR_STR PW_TOKENIZER_ARG_DECODING_ERROR

// Generates data to test tokenized string decoding.
void GenerateEncodedStrings(TestDataFile* file) {
  std::mt19937 random(6006411);
  std::uniform_int_distribution<int64_t> big;
  std::uniform_int_distribution<int32_t> medium;
  std::uniform_int_distribution<int32_t> small(' ', '~');
  std::uniform_real_distribution<float> real;

  file->Section("Simple strings");
  TestCase(file, "%s", "\3SFO", "SFO");
  TestCase(file, "%s", "\4KSJC", "KSJC");
  TestCase(file, "%s", "\0", "");

  TestCase(file, "%5s%s", "\2no\3fun", "   nofun");
  TestCase(file, "%5s%s", "\6abcdef\0", "abcdef");
  TestCase(file, "%5s%s", "\0\6abcdef", "     abcdef");

  TestCase(file,
           "%s %-6s%s%s%s",
           "\5Intel\580586\7toaster\1 \4oven",
           "Intel 80586 toaster oven");
  TestCase(file,
           "%s %-6s%s%s%s",
           "\5Apple\x09"
           "automatic\7 pencil\1 \x09sharpener",
           "Apple automatic pencil sharpener");

  file->Section("Zero-length strings");
  TestCase(file, "%s-%s", "\x02so\x00", "so-");
  TestCase(file, "%s-%s", "\x00\04cool", "-cool");
  TestCase(file, "%s%s%3s%s", "\0\0\0\0", "   ");
  TestCase(file, "(%5s)(%2s)(%7s)", "\x80\0\x80", "([...])(  )(  [...])");

  file->Section("Invalid strings");
  TestCase(file, "%s", "\x03hi", ERROR_STR("%s ERROR (hi)"));
  TestCase(file, "%30s", "\x03hi", ERROR_STR("%30s ERROR (hi)"));
  TestCase(file, "%30s", "\x83hi", ERROR_STR("%30s ERROR (hi)"));
  TestCase(file, "%s", "\x85yo!", ERROR_STR("%s ERROR (yo!)"));
  TestCase(file, "%s", "\x01", ERROR_STR("%s ERROR"));
  TestCase(file, "%30s", "\x81", ERROR_STR("%30s ERROR"));

  file->Section("Continue after truncated string");
  TestCase(file, "%s %d %s", "\x82go\4\5lunch", "go[...] 2 lunch");
  TestCase(file, "%6s%s%s", "\x80\x85hello\x05there", " [...]hello[...]there");

  file->Section("Floating point");
  TestCase(file, "%1.1f", "\0\0\0\0", "0.0");
  TestCase(file, "%0.5f", "\xdb\x0f\x49\x40", "3.14159");

  file->Section("Character");  // ZigZag doubles the value of positive integers.
  TestCase(file, "%c", "\x40", " ");          // 0x20
  TestCase(file, "%c", "\x48", "$");          // 0x24
  TestCase(file, "%c", "\x48", "$");          // 0x24
  TestCase(file, "100%c!", "\x4A", "100%!");  // 0x25

  file->Section("Atypical argument types");
  MAKE_TEST_CASE("%ju", static_cast<uintmax_t>(99));
  MAKE_TEST_CASE("%jd", static_cast<intmax_t>(99));
  MAKE_TEST_CASE("%zu", sizeof(uint64_t));
  MAKE_TEST_CASE("%zd", static_cast<ptrdiff_t>(123));
  MAKE_TEST_CASE("%td", static_cast<ptrdiff_t>(99));

  file->Section("Percent character");
  TestCase(file, "%%", "", "%");
  TestCase(file, "%%%%%%%%", "abc", "%%%%");
  TestCase(file, "whoa%%%%wow%%%%!%%", "", "whoa%%wow%%!%");
  TestCase(file, "This is %d%% effective", "\x02", "This is 1% effective");
  TestCase(
      file, "%% is 100%sa%%sign%%%s", "\x01%\x03OK?", "% is 100%a%sign%OK?");

  file->Section("Percent character prints after errors");
  TestCase(file, "%s%%", "\x83-10\0", "-10[...]%");
  TestCase(
      file, "%d%% is a good %%", "", ERROR_STR("%d MISSING") "% is a good %");

  file->Section("Various format strings");
  MAKE_TEST_CASE("!");
  MAKE_TEST_CASE("%s", "%s");
  MAKE_TEST_CASE("%s", "hello");
  MAKE_TEST_CASE("%s%s", "Hello", "old");
  MAKE_TEST_CASE("%s to the%c%s", "hello", ' ', "whirled");
  MAKE_TEST_CASE("hello %s %d %d %d", "rolled", 1, 2, 3);

  TestCase(file, "", "", "");
  TestCase(file, "This has no specifiers", "", "This has no specifiers");
  TestCase(file, "%s_or_%3s", "\x05hello\x02hi", "hello_or_ hi");
  TestCase(file, "%s_or_%3d", "\x05hello\x7f", "hello_or_-64");
  TestCase(file,
           "%s or hi%c pi=%1.2e",
           "\x05hello\x42\xdb\x0f\x49\x40",
           "hello or hi! pi=3.14e+00");
  TestCase(file,
           "Why, %s there. My favorite number is %.2f%c",
           "\x05hello\xdb\x0f\x49\x40\x42",
           "Why, hello there. My favorite number is 3.14!");

  file->Section("Various errors");
  TestCase(file, "%d", "", ERROR_STR("%d MISSING"));

  TestCase(file,
           "ABC%d123%dabc%dABC",
           "",
           "ABC" ERROR_STR("%d MISSING") "123" ERROR_STR(
               "%d SKIPPED") "abc" ERROR_STR("%d SKIPPED") "ABC");

  TestCase(file,
           "%sXY%+ldxy%a",
           "\x83Yo!\x80",
           "Yo![...]XY" ERROR_STR("%+ld ERROR") "xy" ERROR_STR("%a SKIPPED"));

  TestCase(file, "%d", "", ERROR_STR("%d MISSING"));

  TestCase(file,
           "%sXY%+ldxy%a",
           "\x83Yo!\x80",
           "Yo![...]XY" ERROR_STR("%+ld ERROR") "xy" ERROR_STR("%a SKIPPED"));

  TestCase(file,
           "%s%lld%9u",
           "\x81$\x80\x80",
           "$[...]" ERROR_STR("%lld ERROR") ERROR_STR("%9u SKIPPED"));

  file->Section("Alternate form (#)");
  MAKE_TEST_CASE("Hex: %#x", 0xbeef);
  MAKE_TEST_CASE("Hex: %#08X", 0xfeed);

  file->Section("Random integers");
  for (int i = 0; i < 100; ++i) {
    float f = real(random);
    MAKE_TEST_CASE(
        "This is a number: %+08.3e%1.0E%02d%g%G%f%-3f", f, f, i, f, f, f, f);
  }

  for (int i = 0; i < 100; ++i) {
    unsigned long long n1 = big(random);
    int n2 = medium(random);
    char ch = static_cast<char>(small(random));
    if (ch == '"' || ch == '\\') {
      ch = '\t';
    }

    MAKE_TEST_CASE("%s: %llu %d %c", std::to_string(i).c_str(), n1, n2, ch);
  }

  for (int i = 0; i < 100; ++i) {
    const long long n1 = big(random);
    const unsigned n2 = medium(random);
    const char ch = static_cast<char>(small(random));

    MAKE_TEST_CASE(
        "%s: %lld 0x%16u%08X %d", std::to_string(i).c_str(), n1, n2, n2, ch);
  }
}

template <typename T>
void OutputVarintTest(TestDataFile* file, T i) {
  if constexpr (sizeof(T) <= sizeof(int)) {
    file->printf(R"(TestCase("%%d", "%d", "%%u", "%u", %s)",
                 static_cast<int>(i),
                 static_cast<unsigned>(i),
                 file->fmt().binary_string_prefix);
  } else {
    file->printf(R"(TestCase("%%lld", "%lld", "%%llu", "%llu", %s)",
                 static_cast<long long>(i),
                 static_cast<unsigned long long>(i),
                 file->fmt().binary_string_prefix);
  }

  std::array<uint8_t, 10> buffer;
  // All integers are encoded as signed for tokenization.
  size_t size = pw::varint::Encode(i, pw::as_writable_bytes(pw::span(buffer)));

  for (size_t i = 0; i < size; ++i) {
    file->printf("\\x%02x", buffer[i]);
  }

  file->printf("%s),\n", file->fmt().binary_string_suffix);
}

// Generates data to test variable-length integer decoding.
void GenerateVarints(TestDataFile* file) {
  std::mt19937 random(6006411);
  std::uniform_int_distribution<int64_t> signed64;
  std::uniform_int_distribution<int32_t> signed32;
  std::uniform_int_distribution<int16_t> signed16;

  file->Section("Important numbers");
  OutputVarintTest(file, 0);
  OutputVarintTest(file, std::numeric_limits<int16_t>::min());
  OutputVarintTest(file, std::numeric_limits<int16_t>::min() + 1);
  OutputVarintTest(file, std::numeric_limits<int16_t>::max() - 1);
  OutputVarintTest(file, std::numeric_limits<int16_t>::max());
  OutputVarintTest(file, std::numeric_limits<int32_t>::min());
  OutputVarintTest(file, std::numeric_limits<int32_t>::min() + 1);
  OutputVarintTest(file, std::numeric_limits<int32_t>::max() - 1);
  OutputVarintTest(file, std::numeric_limits<int32_t>::max());
  OutputVarintTest(file, std::numeric_limits<int64_t>::min());
  OutputVarintTest(file, std::numeric_limits<int64_t>::min() + 1);
  OutputVarintTest(file, std::numeric_limits<int64_t>::max() - 1);
  OutputVarintTest(file, std::numeric_limits<int64_t>::max());

  file->Section("Random 64-bit ints");
  for (int i = 0; i < 500; ++i) {
    OutputVarintTest(file, signed64(random));
  }
  file->Section("Random 32-bit ints");
  for (int i = 0; i < 100; ++i) {
    OutputVarintTest(file, signed32(random));
  }
  file->Section("Random 16-bit ints");
  for (int i = 0; i < 100; ++i) {
    OutputVarintTest(file, signed16(random));
  }

  file->Section("All 8-bit numbers");
  {
    int i = std::numeric_limits<int8_t>::min();
    while (true) {
      OutputVarintTest(file, i);
      if (i == std::numeric_limits<int8_t>::max()) {
        break;
      }
      // Don't use an inline increment to avoid undefined behavior (overflow).
      i += 1;
    }
  }
}

template <typename Function>
void WriteFile(const char* name,
               const char* test_case_format,
               Function function) {
  for (const SourceFileFormat& file_format : {kCcFormat, kPythonFormat}) {
    TestDataFile file(name, file_format, test_case_format);
    file.WriteTestCases(function);

    std::printf("Wrote %s\n", file.path().c_str());
  }
}

}  // namespace

int main(int, char**) {
  WriteFile("tokenized_string_decoding",
            "std::tuple<const char*, std::string_view, std::string_view>",
            GenerateEncodedStrings);
  WriteFile("varint_decoding",
            "std::tuple<const char*, const char*, const char*, const char*, "
            "std::string_view>",
            GenerateVarints);
  return 0;
}