MOS Source Code
Loading...
Searching...
No Matches
test_naming_style_c.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdbool.h>
3#include <string.h>
4#include <assert.h>
5#include <pb_encode.h>
6#include <pb_decode.h>
7#include "unittests.h"
8#include "naming_style.pb.h"
9
10int main()
11{
12 int status = 0;
13 main_message_t message = MAIN_MESSAGE_INIT_ZERO;
14
15 /* Verify that all members have the expected names */
16 message.lucky_number = 13;
17 message.required_number = 1;
18 message.repeated_number[0] = 1;
19 message.repeated_ints = NULL;
20
21 message.my_enum1 = MY_ENUM1_ENUM_THIRD;
22 message.my_enum2 = MY_ENUM2_ENUM2_ENTRY;
23 message.my_enum3 = MY_ENUM2_ENUM2_ENTRY;
24 message.my_enum4.arg = NULL;
25
26 message.string_values1 = NULL;
27 message.string_values2[0][0] = 'a';
28 message.optional_string.arg = NULL;
29 message.required_string[0] = 'a';
30
31 message.repeated_fixed32[0] = 1;
32
33 message.required_bytes1[0] = 0;
34 message.required_bytes2.size = 0;
35 message.repeated_bytes1_count = 0;
36 message.repeated_bytes2 = NULL;
37
38 message.has_sub_message1 = true;
39 message.sub_message1.has_test_value = true;
40 message.sub_message1.test_value = 0;
41 message.sub_message2.arg = NULL;
42 message.sub_message3.test_value = 0;
43
44 message.which_one_of_name = MAIN_MESSAGE_TEST_MESSAGE2_TAG;
45 message.one_of_name.test_message2.has_test_value = true;
46 message.one_of_name.test_message2.test_value = 5;
47
48 message.which_one_of_name2 = MAIN_MESSAGE_TEST_MESSAGE5_TAG;
49 message.test_message5.test_value = 5;
50
51 TEST(strcmp("ENTRY_FIRST", my_enum1_name(MY_ENUM1_ENTRY_FIRST)) == 0);
52 TEST(my_enum1_valid(MY_ENUM1_ENTRY_FIRST) == true);
53 TEST(my_enum2_valid(MY_ENUM2_ENUM2_ENTRY) == true);
54
55 /* Verify that the descriptor structure is at least mostly correct
56 * by doing a round-trip encoding test.
57 */
58 {
59 uint8_t buffer1[256];
60 uint8_t buffer2[256];
61 pb_ostream_t ostream1 = pb_ostream_from_buffer(buffer1, sizeof(buffer1));
62 pb_ostream_t ostream2 = pb_ostream_from_buffer(buffer2, sizeof(buffer2));
63 pb_istream_t istream;
64 main_message_t message2 = MAIN_MESSAGE_INIT_ZERO;
65
66 TEST(pb_encode(&ostream1, &main_message_t_msg, &message));
67
68 istream = pb_istream_from_buffer(buffer1, ostream1.bytes_written);
69 TEST(pb_decode(&istream, &main_message_t_msg, &message2));
70
71 /* Encoding a second time should produce same output */
72 TEST(pb_encode(&ostream2, &main_message_t_msg, &message2));
73
74 TEST(ostream2.bytes_written == ostream1.bytes_written);
75 TEST(memcmp(buffer1, buffer2, ostream1.bytes_written) == 0);
76 }
77
78 return status;
79}
MOSAPI int memcmp(const void *s1, const void *s2, size_t n)
Definition mos_string.c:142
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
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
#define NULL
Definition pb_syshdr.h:46
unsigned char uint8_t
Definition pb_syshdr.h:20
#define TEST(x)
Definition test.h:3
int main()