MOS Source Code
Loading...
Searching...
No Matches
zero_value.c
Go to the documentation of this file.
1#include <unittests.h>
2#include <pb_encode.h>
3#include <pb_decode.h>
4#include <string.h>
5#include "zero_value.pb.h"
6
7int main()
8{
9 int status = 0;
10
11 COMMENT("Test extension fields with zero values");
12 {
13 uint8_t buffer[256] = {0};
14 pb_ostream_t ostream;
15 int32_t value = 0;
16 Extendable source = {0};
17
18 pb_extension_t source_ext = {0};
19 source_ext.type = &opt_int32;
20 source_ext.dest = &value;
21 source.extensions = &source_ext;
22
23 ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
24 TEST(pb_encode(&ostream, Extendable_fields, &source));
25
26 TEST(ostream.bytes_written == 2);
27 TEST(memcmp(buffer, "\x58\x00", 2) == 0);
28 }
29
30 /* Note: There never was a bug here, but this check is included
31 * in the regression test because the logic is closely related.
32 */
33 COMMENT("Test pointer fields with zero values");
34 {
35 uint8_t buffer[256] = {0};
36 pb_ostream_t ostream;
37 int32_t value = 0;
38 PointerMessage source = {0};
39
40 source.opt_int32 = &value;
41
42 ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
43 TEST(pb_encode(&ostream, PointerMessage_fields, &source));
44
45 TEST(ostream.bytes_written == 2);
46 TEST(memcmp(buffer, "\x58\x00", 2) == 0);
47 }
48
49 return status;
50}
51
MOSAPI int memcmp(const void *s1, const void *s2, size_t n)
Definition mos_string.c:142
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
signed int int32_t
Definition pb_syshdr.h:23
unsigned char uint8_t
Definition pb_syshdr.h:20
#define TEST(x)
Definition test.h:3
static char buffer[2048]
Definition test_printf.c:7
#define COMMENT(x)
Definition unittests.h:17
int main()
Definition zero_value.c:7