MOS Source Code
Loading...
Searching...
No Matches
test.c
Go to the documentation of this file.
1#include <assert.h>
2#include <pb_decode.h>
3#include "unittests.h"
4#include "test.pb.h"
5#include <stdio.h>
6
7const uint8_t input_data[] = {
8 0x08, 0x01,
9};
10
11const size_t input_len = sizeof(input_data);
12
13bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
14{
15 size_t cursor = (size_t)(uintptr_t)stream->state;
16
17 if ((cursor + count) > input_len)
18 {
19 stream->bytes_left = 0;
20 return false;
21 }
22
23 memcpy(buf, &input_data[cursor], count);
24 cursor += count;
25
26 stream->state = (void*)(uintptr_t)cursor;
27
28 return true;
29}
30
31int main()
32{
33 int status = 0;
34
35 /* test buffer stream */
36 {
37 TestMessage msg = TestMessage_init_zero;
38 pb_istream_t stream = pb_istream_from_buffer(input_data, input_len);
39 TEST(pb_decode(&stream, TestMessage_fields, &msg));
40 TEST(msg.foo == 0x1);
41 TEST(stream.errmsg == NULL);
42 }
43
44 /* test callback stream */
45 {
46 TestMessage msg = TestMessage_init_zero;
47 pb_istream_t stream = {&stream_callback, 0, SIZE_MAX};
48 TEST(pb_decode(&stream, TestMessage_fields, &msg));
49 TEST(msg.foo == 0x1);
50 TEST(stream.errmsg == NULL);
51 }
52
53 return status;
54}
bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
Definition test.c:8
bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
const size_t input_len
Definition test.c:11
const uint8_t input_data[]
Definition test.c:7
int main()
Definition test.c:31
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1210
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:142
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
uint32_t size_t
Definition pb_syshdr.h:42
unsigned char uint8_t
Definition pb_syshdr.h:20
#define TEST(x)
Definition test.h:3