Merge branch 'dev_get_rid_of_ternary_operator'
[apps/agl-service-can-low-level.git] / tests / missing_fields / missing_fields.c
1 /* Checks that missing required fields are detected properly */
2
3 #include <stdio.h>
4 #include <pb_encode.h>
5 #include <pb_decode.h>
6 #include "missing_fields.pb.h"
7
8 int main()
9 {
10     uint8_t buffer[512];
11     
12     /* Create a message with one missing field */
13     {
14         MissingField msg = {0};
15         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
16         
17         if (!pb_encode(&stream, MissingField_fields, &msg))
18         {
19             printf("Encode failed.\n");
20             return 1;
21         }
22     }
23
24     /* Test that it decodes properly if we don't require that field */
25     {
26         MissingField msg = {0};
27         pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
28         
29         if (!pb_decode(&stream, MissingField_fields, &msg))
30         {
31             printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
32             return 2;
33         }
34     }
35     
36     /* Test that it does *not* decode properly if we require the field */
37     {
38         AllFields msg = {0};
39         pb_istream_t stream = pb_istream_from_buffer(buffer, sizeof(buffer));
40         
41         if (pb_decode(&stream, AllFields_fields, &msg))
42         {
43             printf("Decode didn't detect missing field.\n");
44             return 3;
45         }
46     }
47     
48     return 0; /* All ok */
49 }
50