xref: /aosp_15_r20/external/pigweed/pw_tokenizer/tokenize_test_c.c (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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_ToBufferTest_Requires8(void * buffer,size_t * buffer_size)56 void pw_tokenizer_ToBufferTest_Requires8(void* buffer, size_t* buffer_size) {
57   PW_TOKENIZE_TO_BUFFER(buffer, buffer_size, TEST_FORMAT_REQUIRES_8, "hi", -7);
58 }
59