1 // 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
18 #include "pw_tokenizer/tokenize.h"
19 #include "pw_tokenizer_private/tokenize_test.h"
20
21 #ifdef __cplusplus
22 #error "This is a test of C code and must be compiled as C, not C++."
23 #endif // __cplusplus
24
pw_tokenizer_ToBufferTest_StringShortFloat(void * buffer,size_t * buffer_size)25 void pw_tokenizer_ToBufferTest_StringShortFloat(void* buffer,
26 size_t* buffer_size) {
27 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.
pw_tokenizer_ToBufferTest_SequentialZigZag(void * buffer,size_t * buffer_size)35 void pw_tokenizer_ToBufferTest_SequentialZigZag(void* buffer,
36 size_t* buffer_size) {
37 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
pw_tokenizer_ToCallbackTest_SequentialZigZag(void (* callback)(const uint8_t * buffer,size_t size))56 void pw_tokenizer_ToCallbackTest_SequentialZigZag(
57 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
pw_tokenizer_ToBufferTest_Requires8(void * buffer,size_t * buffer_size)76 void pw_tokenizer_ToBufferTest_Requires8(void* buffer, size_t* buffer_size) {
77 PW_TOKENIZE_TO_BUFFER(buffer, buffer_size, TEST_FORMAT_REQUIRES_8, "hi", -7);
78 }
79