MOS Source Code
Loading...
Searching...
No Matches
inline_unittests.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3#include <pb_decode.h>
4#include <pb_encode.h>
5#include "unittests.h"
6#include "inline.pb.h"
7
8int main()
9{
10 int status = 0;
11 int i = 0;
12 COMMENT("Test inline byte fields");
13
14 {
15 Message1 msg1 = Message1_init_zero;
16 TEST(sizeof(msg1.data) == 32);
17 }
18
19 {
20 Message1 msg1 = Message1_init_zero;
21 pb_byte_t msg1_buffer[Message1_size];
22 pb_ostream_t ostream = pb_ostream_from_buffer(msg1_buffer, Message1_size);
23 Message1 msg1_deserialized = Message1_init_zero;
24 pb_istream_t istream = pb_istream_from_buffer(msg1_buffer, Message1_size);
25
26 for (i = 0; i < 32; i++) {
27 msg1.data[i] = i;
28 }
29
30 TEST(pb_encode(&ostream, Message1_fields, &msg1));
31 TEST(ostream.bytes_written == Message1_size);
32
33 TEST(pb_decode(&istream, Message1_fields, &msg1_deserialized));
34
35 TEST(istream.bytes_left == 0);
36 TEST(memcmp(&msg1_deserialized, &msg1, sizeof(msg1)) == 0);
37 }
38
39 {
40 Message2 msg2 = {true, {0}};
41 Message2 msg2_no_data = {false, {1}};
42 pb_byte_t msg2_buffer[Message2_size];
43 pb_ostream_t ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
44 Message2 msg2_deserialized = Message2_init_zero;
45 pb_istream_t istream = pb_istream_from_buffer(msg2_buffer, Message2_size);
46
47 for (i = 0; i < 64; i++) {
48 msg2.data[i] = i;
49 }
50
51 TEST(pb_encode(&ostream, Message2_fields, &msg2));
52 TEST(ostream.bytes_written == Message2_size);
53
54 TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
55
56 TEST(istream.bytes_left == 0);
57 TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) == 0);
58 TEST(msg2_deserialized.has_data);
59
60 memset(msg2_buffer, 0, sizeof(msg2_buffer));
61 ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
62 TEST(pb_encode(&ostream, Message2_fields, &msg2_no_data));
63 istream = pb_istream_from_buffer(msg2_buffer, ostream.bytes_written);
64 TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
65 TEST(!msg2_deserialized.has_data);
66 TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) != 0);
67 }
68
69 if (status != 0)
70 fprintf(stdout, "\n\nSome tests FAILED!\n");
71
72 return status;
73}
MOSAPI int memcmp(const void *s1, const void *s2, size_t n)
Definition mos_string.c:142
#define stdout
Definition mos_stdio.h:31
int main()
uint_least8_t pb_byte_t
Definition pb.h:227
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
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
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
#define TEST(x)
Definition test.h:3
#define COMMENT(x)
Definition unittests.h:17