Line data Source code
1 : #include "test.pb.h" 2 : #include <unittests.h> 3 : #include <pb_encode.h> 4 : #include <pb_decode.h> 5 : 6 2 : static bool write_array(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) 7 : { 8 : int i; 9 12 : for (i = 0; i < 5; i++) 10 : { 11 10 : if (!pb_encode_tag_for_field(stream, field)) 12 0 : return false; 13 10 : if (!pb_encode_varint(stream, 1000 + i)) 14 0 : return false; 15 : } 16 : 17 2 : return true; 18 : } 19 : 20 5 : static bool read_array(pb_istream_t *stream, const pb_field_t *field, void **arg) 21 : { 22 : uint32_t i; 23 5 : int *sum = *arg; 24 : 25 5 : if (!pb_decode_varint32(stream, &i)) 26 0 : return false; 27 : 28 5 : *sum += i; 29 : 30 5 : return true; 31 : } 32 : 33 1 : int main() 34 : { 35 1 : int status = 0; 36 1 : pb_byte_t buf[128] = {0}; 37 : pb_size_t msglen; 38 : 39 : { 40 1 : MainMessage msg = MainMessage_init_zero; 41 1 : pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf)); 42 1 : msg.submsg.foo.funcs.encode = &write_array; 43 1 : TEST(pb_encode(&stream, MainMessage_fields, &msg)); 44 1 : msglen = stream.bytes_written; 45 : } 46 : 47 : { 48 1 : MainMessage msg = MainMessage_init_zero; 49 1 : pb_istream_t stream = pb_istream_from_buffer(buf, msglen); 50 1 : int sum = 0; 51 1 : msg.submsg.foo.funcs.decode = &read_array; 52 1 : msg.submsg.foo.arg = ∑ 53 1 : TEST(pb_decode(&stream, MainMessage_fields, &msg)); 54 1 : TEST(sum == 1000 + 1001 + 1002 + 1003 + 1004); 55 : } 56 : 57 1 : return status; 58 : } 59 :