Line data Source code
1 : /* Attempts to test all the datatypes supported by ProtoBuf. 2 : * This is a backwards-compatibility test, using alltypes_legacy.h. 3 : * It is similar to encode_alltypes, but duplicated in order to allow 4 : * encode_alltypes to test any new features introduced later. 5 : */ 6 : 7 : #include <stdio.h> 8 : #include <stdlib.h> 9 : #include <string.h> 10 : #include <pb_encode.h> 11 : #include "alltypes_legacy.h" 12 : #include "test_helpers.h" 13 : 14 1 : int main(int argc, char **argv) 15 : { 16 1 : int mode = (argc > 1) ? atoi(argv[1]) : 0; 17 : 18 : /* Initialize the structure with constants */ 19 1 : AllTypes alltypes = {0}; 20 : 21 1 : alltypes.req_int32 = -1001; 22 1 : alltypes.req_int64 = -1002; 23 1 : alltypes.req_uint32 = 1003; 24 1 : alltypes.req_uint64 = 1004; 25 1 : alltypes.req_sint32 = -1005; 26 1 : alltypes.req_sint64 = -1006; 27 1 : alltypes.req_bool = true; 28 : 29 1 : alltypes.req_fixed32 = 1008; 30 1 : alltypes.req_sfixed32 = -1009; 31 1 : alltypes.req_float = 1010.0f; 32 : 33 1 : alltypes.req_fixed64 = 1011; 34 1 : alltypes.req_sfixed64 = -1012; 35 1 : alltypes.req_double = 1013.0; 36 : 37 1 : strcpy(alltypes.req_string, "1014"); 38 1 : alltypes.req_bytes.size = 4; 39 1 : memcpy(alltypes.req_bytes.bytes, "1015", 4); 40 1 : strcpy(alltypes.req_submsg.substuff1, "1016"); 41 1 : alltypes.req_submsg.substuff2 = 1016; 42 1 : alltypes.req_enum = MyEnum_Truth; 43 : 44 1 : alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = -2001; 45 1 : alltypes.rep_int64_count = 5; alltypes.rep_int64[4] = -2002; 46 1 : alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003; 47 1 : alltypes.rep_uint64_count = 5; alltypes.rep_uint64[4] = 2004; 48 1 : alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = -2005; 49 1 : alltypes.rep_sint64_count = 5; alltypes.rep_sint64[4] = -2006; 50 1 : alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true; 51 : 52 1 : alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008; 53 1 : alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = -2009; 54 1 : alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f; 55 : 56 1 : alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64[4] = 2011; 57 1 : alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64[4] = -2012; 58 1 : alltypes.rep_double_count = 5; alltypes.rep_double[4] = 2013.0; 59 : 60 1 : alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014"); 61 1 : alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4; 62 1 : memcpy(alltypes.rep_bytes[4].bytes, "2015", 4); 63 : 64 1 : alltypes.rep_submsg_count = 5; 65 1 : strcpy(alltypes.rep_submsg[4].substuff1, "2016"); 66 1 : alltypes.rep_submsg[4].substuff2 = 2016; 67 1 : alltypes.rep_submsg[4].has_substuff3 = true; 68 1 : alltypes.rep_submsg[4].substuff3 = 2016; 69 : 70 1 : alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth; 71 : 72 1 : if (mode != 0) 73 : { 74 : /* Fill in values for optional fields */ 75 0 : alltypes.has_opt_int32 = true; 76 0 : alltypes.opt_int32 = 3041; 77 0 : alltypes.has_opt_int64 = true; 78 0 : alltypes.opt_int64 = 3042; 79 0 : alltypes.has_opt_uint32 = true; 80 0 : alltypes.opt_uint32 = 3043; 81 0 : alltypes.has_opt_uint64 = true; 82 0 : alltypes.opt_uint64 = 3044; 83 0 : alltypes.has_opt_sint32 = true; 84 0 : alltypes.opt_sint32 = 3045; 85 0 : alltypes.has_opt_sint64 = true; 86 0 : alltypes.opt_sint64 = 3046; 87 0 : alltypes.has_opt_bool = true; 88 0 : alltypes.opt_bool = true; 89 : 90 0 : alltypes.has_opt_fixed32 = true; 91 0 : alltypes.opt_fixed32 = 3048; 92 0 : alltypes.has_opt_sfixed32 = true; 93 0 : alltypes.opt_sfixed32 = 3049; 94 0 : alltypes.has_opt_float = true; 95 0 : alltypes.opt_float = 3050.0f; 96 : 97 0 : alltypes.has_opt_fixed64 = true; 98 0 : alltypes.opt_fixed64 = 3051; 99 0 : alltypes.has_opt_sfixed64 = true; 100 0 : alltypes.opt_sfixed64 = 3052; 101 0 : alltypes.has_opt_double = true; 102 0 : alltypes.opt_double = 3053.0; 103 : 104 0 : alltypes.has_opt_string = true; 105 0 : strcpy(alltypes.opt_string, "3054"); 106 0 : alltypes.has_opt_bytes = true; 107 0 : alltypes.opt_bytes.size = 4; 108 0 : memcpy(alltypes.opt_bytes.bytes, "3055", 4); 109 0 : alltypes.has_opt_submsg = true; 110 0 : strcpy(alltypes.opt_submsg.substuff1, "3056"); 111 0 : alltypes.opt_submsg.substuff2 = 3056; 112 0 : alltypes.has_opt_enum = true; 113 0 : alltypes.opt_enum = MyEnum_Truth; 114 : } 115 : 116 1 : alltypes.end = 1099; 117 : 118 : { 119 : uint8_t buffer[1024]; 120 1 : pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); 121 : 122 : /* Now encode it and check if we succeeded. */ 123 1 : if (pb_encode(&stream, AllTypes_fields, &alltypes)) 124 : { 125 : SET_BINARY_MODE(stdout); 126 1 : fwrite(buffer, 1, stream.bytes_written, stdout); 127 1 : return 0; /* Success */ 128 : } 129 : else 130 : { 131 0 : fprintf(stderr, "Encoding failed!\n"); 132 0 : return 1; /* Failure */ 133 : } 134 : } 135 : }