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