Line data Source code
1 : #include <pb_encode.h> 2 : #include <pb_decode.h> 3 : #include <unittests.h> 4 : #include "optional.pb.h" 5 : 6 1 : int main() 7 : { 8 1 : int status = 0; 9 : uint8_t buf[256]; 10 : size_t msglen; 11 : 12 1 : COMMENT("Test encoding message with optional field") 13 : { 14 1 : pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf)); 15 1 : TestMessage msg = TestMessage_init_zero; 16 : 17 1 : msg.has_opt_int = true; 18 1 : msg.opt_int = 99; 19 1 : msg.normal_int = 100; 20 1 : msg.opt_int2 = 101; 21 : 22 1 : TEST(pb_encode(&stream, TestMessage_fields, &msg)); 23 1 : msglen = stream.bytes_written; 24 : } 25 : 26 1 : COMMENT("Test decoding message with optional field") 27 : { 28 1 : pb_istream_t stream = pb_istream_from_buffer(buf, msglen); 29 1 : TestMessage msg = TestMessage_init_zero; 30 : 31 : /* These fields should be missing from the message 32 : * so the values wouldn't be overwritten. */ 33 1 : msg.opt_int2 = 5; 34 1 : msg.normal_int2 = 6; 35 : 36 1 : TEST(pb_decode_noinit(&stream, TestMessage_fields, &msg)); 37 1 : TEST(msg.has_opt_int); 38 1 : TEST(msg.opt_int == 99); 39 1 : TEST(msg.normal_int == 100); 40 1 : TEST(!msg.has_opt_int2); 41 1 : TEST(msg.opt_int2 == 5); 42 1 : TEST(msg.normal_int2 == 6); 43 : } 44 : 45 1 : return status; 46 : } 47 :