blob: bf8877a68dd095bd082c866d8ccc0bb0ed1ddd7b [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 function tests the C implementation of tokenization API. These functions
16// are called from the main C++ test file.
17
Wyatt Hepler80c6ee52020-01-03 09:54:58 -080018#include "pw_tokenizer/tokenize.h"
Michael Spanga99220e2020-06-11 20:07:16 -040019#include "pw_tokenizer_private/tokenize_test.h"
Wyatt Hepler80c6ee52020-01-03 09:54:58 -080020
21#ifdef __cplusplus
22#error "This is a test of C code and must be compiled as C, not C++."
23#endif // __cplusplus
24
Wyatt Hepler7a5e4d62020-08-31 08:39:16 -070025void pw_tokenizer_ToBufferTest_StringShortFloat(void* buffer,
26 size_t* buffer_size) {
Wyatt Hepler80c6ee52020-01-03 09:54:58 -080027 char str[] = "1";
28 PW_TOKENIZE_TO_BUFFER(
29 buffer, buffer_size, TEST_FORMAT_STRING_SHORT_FLOAT, str, (short)-2, 3.0);
30}
31
32// This test invokes the tokenization API with a variety of types. To simplify
33// validating the encoded data, numbers that are sequential when zig-zag encoded
34// are used as arguments.
Wyatt Hepler7a5e4d62020-08-31 08:39:16 -070035void pw_tokenizer_ToBufferTest_SequentialZigZag(void* buffer,
36 size_t* buffer_size) {
Wyatt Hepler80c6ee52020-01-03 09:54:58 -080037 PW_TOKENIZE_TO_BUFFER(buffer,
38 buffer_size,
39 TEST_FORMAT_SEQUENTIAL_ZIG_ZAG,
40 0u,
41 -1,
42 1u,
43 (unsigned)-2,
44 (unsigned short)2u,
45 (signed char)-3,
46 3,
47 -4l,
48 4ul,
49 -5ll,
50 5ull,
51 (signed char)-6,
52 (char)6,
53 (signed char)-7);
54}
55
Wyatt Hepler7a5e4d62020-08-31 08:39:16 -070056void pw_tokenizer_ToCallbackTest_SequentialZigZag(
Wyatt Hepler80c6ee52020-01-03 09:54:58 -080057 void (*callback)(const uint8_t* buffer, size_t size)) {
58 PW_TOKENIZE_TO_CALLBACK(callback,
59 TEST_FORMAT_SEQUENTIAL_ZIG_ZAG,
60 0u,
61 -1,
62 1u,
63 (unsigned)-2,
64 (unsigned short)2u,
65 (signed char)-3,
66 3,
67 -4l,
68 4ul,
69 -5ll,
70 5ull,
71 (signed char)-6,
72 (char)6,
73 (signed char)-7);
74}
75
Wyatt Hepler7a5e4d62020-08-31 08:39:16 -070076void pw_tokenizer_ToBufferTest_Requires8(void* buffer, size_t* buffer_size) {
Wyatt Hepler80c6ee52020-01-03 09:54:58 -080077 PW_TOKENIZE_TO_BUFFER(buffer, buffer_size, TEST_FORMAT_REQUIRES_8, "hi", -7);
78}