Line data Source code
1 : /* Attempts to test all the datatypes supported by ProtoBuf. 2 : */ 3 : 4 : #include <stdio.h> 5 : #include <stdlib.h> 6 : #include <string.h> 7 : #include <pb_encode.h> 8 : #include "alltypes.pb.h" 9 : #include "test_helpers.h" 10 : 11 2 : int main(int argc, char **argv) 12 : { 13 2 : int mode = (argc > 1) ? atoi(argv[1]) : 0; 14 : 15 : /* Initialize the structure with constants */ 16 2 : AllTypes alltypes = AllTypes_init_zero; 17 : 18 2 : alltypes.req_int32 = -1001; 19 2 : alltypes.req_uint32 = 1003; 20 2 : alltypes.req_sint32 = -1005; 21 2 : alltypes.req_bool = true; 22 : 23 2 : alltypes.req_fixed32 = 1008; 24 2 : alltypes.req_sfixed32 = -1009; 25 2 : alltypes.req_float = 1010.0f; 26 : 27 2 : strcpy(alltypes.req_string, "1014"); 28 2 : alltypes.req_bytes.size = 4; 29 2 : memcpy(alltypes.req_bytes.bytes, "1015", 4); 30 2 : strcpy(alltypes.req_submsg.substuff1, "1016"); 31 2 : alltypes.req_submsg.substuff2 = 1016; 32 2 : alltypes.req_enum = MyEnum_Truth; 33 2 : memcpy(alltypes.req_fbytes, "1019", 4); 34 : 35 2 : alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = -2001; 36 2 : alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003; 37 2 : alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = -2005; 38 2 : alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true; 39 : 40 2 : alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008; 41 2 : alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = -2009; 42 2 : alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f; 43 : 44 2 : alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014"); 45 2 : alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4; 46 2 : memcpy(alltypes.rep_bytes[4].bytes, "2015", 4); 47 : 48 2 : alltypes.rep_submsg_count = 5; 49 2 : strcpy(alltypes.rep_submsg[4].substuff1, "2016"); 50 2 : alltypes.rep_submsg[4].substuff2 = 2016; 51 2 : alltypes.rep_submsg[4].has_substuff3 = true; 52 2 : alltypes.rep_submsg[4].substuff3 = 2016; 53 : 54 2 : alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth; 55 2 : alltypes.rep_emptymsg_count = 5; 56 : 57 2 : alltypes.rep_fbytes_count = 5; 58 2 : memcpy(alltypes.rep_fbytes[4], "2019", 4); 59 : 60 2 : alltypes.req_limits.int32_min = INT32_MIN; 61 2 : alltypes.req_limits.int32_max = INT32_MAX; 62 2 : alltypes.req_limits.uint32_min = 0; 63 2 : alltypes.req_limits.uint32_max = UINT32_MAX; 64 2 : alltypes.req_limits.enum_min = HugeEnum_Negative; 65 2 : alltypes.req_limits.enum_max = HugeEnum_Positive; 66 : 67 2 : if (mode != 0) 68 : { 69 : /* Fill in values for optional fields */ 70 1 : alltypes.has_opt_int32 = true; 71 1 : alltypes.opt_int32 = 3041; 72 1 : alltypes.has_opt_uint32 = true; 73 1 : alltypes.opt_uint32 = 3043; 74 1 : alltypes.has_opt_sint32 = true; 75 1 : alltypes.opt_sint32 = 3045; 76 1 : alltypes.has_opt_bool = true; 77 1 : alltypes.opt_bool = true; 78 : 79 1 : alltypes.has_opt_fixed32 = true; 80 1 : alltypes.opt_fixed32 = 3048; 81 1 : alltypes.has_opt_sfixed32 = true; 82 1 : alltypes.opt_sfixed32 = 3049; 83 1 : alltypes.has_opt_float = true; 84 1 : alltypes.opt_float = 3050.0f; 85 : 86 1 : alltypes.has_opt_string = true; 87 1 : strcpy(alltypes.opt_string, "3054"); 88 1 : alltypes.has_opt_bytes = true; 89 1 : alltypes.opt_bytes.size = 4; 90 1 : memcpy(alltypes.opt_bytes.bytes, "3055", 4); 91 1 : alltypes.has_opt_submsg = true; 92 1 : strcpy(alltypes.opt_submsg.substuff1, "3056"); 93 1 : alltypes.opt_submsg.substuff2 = 3056; 94 1 : alltypes.has_opt_enum = true; 95 1 : alltypes.opt_enum = MyEnum_Truth; 96 1 : alltypes.has_opt_emptymsg = true; 97 1 : alltypes.has_opt_fbytes = true; 98 1 : memcpy(alltypes.opt_fbytes, "3059", 4); 99 : 100 1 : alltypes.which_oneof = AllTypes_oneof_msg1_tag; 101 1 : strcpy(alltypes.oneof.oneof_msg1.substuff1, "4059"); 102 1 : alltypes.oneof.oneof_msg1.substuff2 = 4059; 103 : } 104 : 105 2 : alltypes.end = 1099; 106 : 107 : { 108 : uint8_t buffer[AllTypes_size]; 109 2 : pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); 110 : 111 : /* Now encode it and check if we succeeded. */ 112 2 : if (pb_encode(&stream, AllTypes_fields, &alltypes)) 113 : { 114 : SET_BINARY_MODE(stdout); 115 2 : fwrite(buffer, 1, stream.bytes_written, stdout); 116 2 : return 0; /* Success */ 117 : } 118 : else 119 : { 120 0 : fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream)); 121 0 : return 1; /* Failure */ 122 : } 123 : } 124 : }