diff options
author | Martin Donath <scifish@gmail.com> | 2013-12-08 23:25:32 +0100 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2013-12-29 18:35:57 +0200 |
commit | 4ae3b2e5661b154cd1f7e545f6052d271306ff25 (patch) | |
tree | 90b8fb3619eedaf9c0f3e8a5ac7ffd494467b99c /tests | |
parent | 4f37c083d532a782e478b2cef6a02d028613564d (diff) |
Generating and encoding messages with dynamic allocaiton
Diffstat (limited to 'tests')
-rw-r--r-- | tests/alltypes_pointer/SConscript | 12 | ||||
-rw-r--r-- | tests/alltypes_pointer/alltypes.options | 2 | ||||
-rw-r--r-- | tests/alltypes_pointer/alltypes.proto | 93 | ||||
-rw-r--r-- | tests/alltypes_pointer/encode_alltypes_pointer.c | 142 |
4 files changed, 249 insertions, 0 deletions
diff --git a/tests/alltypes_pointer/SConscript b/tests/alltypes_pointer/SConscript new file mode 100644 index 00000000..b0e35042 --- /dev/null +++ b/tests/alltypes_pointer/SConscript @@ -0,0 +1,12 @@ +# Build and run a test that encodes and decodes a message that contains +# all of the Protocol Buffers data types. + +Import("env") + +env.NanopbProto(["alltypes", "alltypes.options"]) +enc = env.Program(["encode_alltypes_pointer.c", "alltypes.pb.c", "$COMMON/pb_encode.o"]) +# dec = env.Program(["decode_alltypes_pointer.c", "alltypes.pb.c", "$COMMON/pb_decode.o"]) + +env.RunTest(enc) +# env.RunTest([dec, "encode_alltypes.output"]) + diff --git a/tests/alltypes_pointer/alltypes.options b/tests/alltypes_pointer/alltypes.options new file mode 100644 index 00000000..330860aa --- /dev/null +++ b/tests/alltypes_pointer/alltypes.options @@ -0,0 +1,2 @@ +* type:FT_POINTER + diff --git a/tests/alltypes_pointer/alltypes.proto b/tests/alltypes_pointer/alltypes.proto new file mode 100644 index 00000000..a2cf8bbc --- /dev/null +++ b/tests/alltypes_pointer/alltypes.proto @@ -0,0 +1,93 @@ +message SubMessage { + required string substuff1 = 1 [default = "1"]; + required int32 substuff2 = 2 [default = 2]; + optional fixed32 substuff3 = 3 [default = 3]; +} + +message EmptyMessage { + +} + +enum MyEnum { + Zero = 0; + First = 1; + Second = 2; + Truth = 42; +} + +message AllTypes { + required int32 req_int32 = 1; + required int64 req_int64 = 2; + required uint32 req_uint32 = 3; + required uint64 req_uint64 = 4; + required sint32 req_sint32 = 5; + required sint64 req_sint64 = 6; + required bool req_bool = 7; + + required fixed32 req_fixed32 = 8; + required sfixed32 req_sfixed32= 9; + required float req_float = 10; + + required fixed64 req_fixed64 = 11; + required sfixed64 req_sfixed64= 12; + required double req_double = 13; + + required string req_string = 14; + required bytes req_bytes = 15; + required SubMessage req_submsg = 16; + required MyEnum req_enum = 17; + required EmptyMessage req_emptymsg = 18; + + + repeated int32 rep_int32 = 21; + repeated int64 rep_int64 = 22; + repeated uint32 rep_uint32 = 23; + repeated uint64 rep_uint64 = 24; + repeated sint32 rep_sint32 = 25; + repeated sint64 rep_sint64 = 26; + repeated bool rep_bool = 27; + + repeated fixed32 rep_fixed32 = 28; + repeated sfixed32 rep_sfixed32= 29; + repeated float rep_float = 30; + + repeated fixed64 rep_fixed64 = 31; + repeated sfixed64 rep_sfixed64= 32; + repeated double rep_double = 33; + + repeated string rep_string = 34; + repeated bytes rep_bytes = 35; + repeated SubMessage rep_submsg = 36; + repeated MyEnum rep_enum = 37; + repeated EmptyMessage rep_emptymsg = 38; + + optional int32 opt_int32 = 41 [default = 4041]; + optional int64 opt_int64 = 42 [default = 4042]; + optional uint32 opt_uint32 = 43 [default = 4043]; + optional uint64 opt_uint64 = 44 [default = 4044]; + optional sint32 opt_sint32 = 45 [default = 4045]; + optional sint64 opt_sint64 = 46 [default = 4046]; + optional bool opt_bool = 47 [default = false]; + + optional fixed32 opt_fixed32 = 48 [default = 4048]; + optional sfixed32 opt_sfixed32= 49 [default = 4049]; + optional float opt_float = 50 [default = 4050]; + + optional fixed64 opt_fixed64 = 51 [default = 4051]; + optional sfixed64 opt_sfixed64= 52 [default = 4052]; + optional double opt_double = 53 [default = 4053]; + + optional string opt_string = 54 [default = "4054"]; + optional bytes opt_bytes = 55 [default = "4055"]; + optional SubMessage opt_submsg = 56; + optional MyEnum opt_enum = 57 [default = Second]; + optional EmptyMessage opt_emptymsg = 58; + + // Just to make sure that the size of the fields has been calculated + // properly, i.e. otherwise a bug in last field might not be detected. + required int32 end = 99; + + + extensions 200 to 255; +} + diff --git a/tests/alltypes_pointer/encode_alltypes_pointer.c b/tests/alltypes_pointer/encode_alltypes_pointer.c new file mode 100644 index 00000000..cb2fe3d5 --- /dev/null +++ b/tests/alltypes_pointer/encode_alltypes_pointer.c @@ -0,0 +1,142 @@ +/* Attempts to test all the datatypes supported by ProtoBuf. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <pb_encode.h> +#include "alltypes.pb.h" +#include "test_helpers.h" + +int main(int argc, char **argv) +{ + int mode = (argc > 1) ? atoi(argv[1]) : 0; + + /* Initialize values to encode */ + int32_t value_int32 = -1000; + int64_t value_int64 = -10000000000; + + uint32_t value_uint32 = 1000; + uint64_t value_uint64 = 10000000000; + + bool value_bool = true; + float value_float = 1000.0f; + double value_double = 1000.0f; + + char *value_string = "1000"; + AllTypes_req_bytes_t value_req_bytes; + AllTypes_rep_bytes_t value_rep_bytes; + AllTypes_opt_bytes_t value_opt_bytes; + + SubMessage value_submessage = {0}; + MyEnum value_enum = MyEnum_Truth; + EmptyMessage value_empty_message = {0}; + + /* Initialize the structure with constants */ + AllTypes alltypes = {0}; + + alltypes.req_int32 = &value_int32; + alltypes.req_int64 = &value_int64; + alltypes.req_uint32 = &value_uint32; + alltypes.req_uint64 = &value_uint64; + alltypes.req_sint32 = &value_int32; + alltypes.req_sint64 = &value_int64; + alltypes.req_bool = &value_bool; + + alltypes.req_fixed32 = &value_uint32; + alltypes.req_sfixed32 = &value_int32; + alltypes.req_float = &value_float; + + alltypes.req_fixed64 = &value_uint64; + alltypes.req_sfixed64 = &value_int64; + alltypes.req_double = &value_double; + + value_req_bytes.bytes = (uint8_t*)"1000"; + value_req_bytes.size = 4; + + alltypes.req_string = value_string; + alltypes.req_bytes = &value_req_bytes; + + value_submessage.substuff1 = value_string; + value_submessage.substuff2 = &value_int32; + + alltypes.req_submsg = &value_submessage; + alltypes.req_enum = &value_enum; + alltypes.req_emptymsg = &value_empty_message; + + alltypes.rep_int32_count = 1; alltypes.rep_int32 = &value_int32; + alltypes.rep_int64_count = 1; alltypes.rep_int64 = &value_int64; + alltypes.rep_uint32_count = 1; alltypes.rep_uint32 = &value_uint32; + alltypes.rep_uint64_count = 1; alltypes.rep_uint64 = &value_uint64; + alltypes.rep_sint32_count = 1; alltypes.rep_sint32 = &value_int32; + alltypes.rep_sint64_count = 1; alltypes.rep_sint64 = &value_int64; + alltypes.rep_bool_count = 1; alltypes.rep_bool = &value_bool; + + alltypes.rep_fixed32_count = 1; alltypes.rep_fixed32 = &value_uint32; + alltypes.rep_sfixed32_count = 1; alltypes.rep_sfixed32 = &value_int32; + alltypes.rep_float_count = 1; alltypes.rep_float = &value_float; + + alltypes.rep_fixed64_count = 1; alltypes.rep_fixed64 = &value_uint64; + alltypes.rep_sfixed64_count = 1; alltypes.rep_sfixed64 = &value_int64; + alltypes.rep_double_count = 1; alltypes.rep_double = &value_double; + + value_rep_bytes.bytes = (uint8_t*)"1000"; + value_rep_bytes.size = 4; + + alltypes.rep_string_count = 1; alltypes.rep_string = (char **)&value_string; + alltypes.rep_bytes_count = 0; alltypes.rep_bytes = &value_rep_bytes; + + alltypes.rep_submsg_count = 1; alltypes.rep_submsg = &value_submessage; + alltypes.rep_enum_count = 1; alltypes.rep_enum = &value_enum; + alltypes.rep_emptymsg_count = 1; alltypes.rep_emptymsg = &value_empty_message; + + if (mode != 0) + { + /* Fill in values for optional fields */ + alltypes.opt_int32 = &value_int32; + alltypes.opt_int64 = &value_int64; + alltypes.opt_uint32 = &value_uint32; + alltypes.opt_uint64 = &value_uint64; + alltypes.opt_sint32 = &value_int32; + alltypes.opt_sint64 = &value_int64; + alltypes.opt_bool = &value_bool; + + alltypes.opt_fixed32 = &value_uint32; + alltypes.opt_sfixed32 = &value_int32; + alltypes.opt_float = &value_float; + + alltypes.opt_fixed64 = &value_uint64; + alltypes.opt_sfixed64 = &value_int64; + alltypes.opt_double = &value_double; + + value_opt_bytes.bytes = (uint8_t*)"1000"; + value_opt_bytes.size = 4; + + alltypes.opt_string = value_string; + alltypes.opt_bytes = &value_opt_bytes; + + alltypes.opt_submsg = &value_submessage; + alltypes.opt_enum = &value_enum; + alltypes.opt_emptymsg = &value_empty_message; + } + + alltypes.end = &value_int32; + + { + uint8_t buffer[4096]; + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + + /* Now encode it and check if we succeeded. */ + if (pb_encode(&stream, AllTypes_fields, &alltypes)) + { + /*SET_BINARY_MODE(stdout); + fwrite(buffer, 1, stream.bytes_written, stdout);*/ /* TODO: use this to validate decoding, when implemented */ + return 0; /* Success */ + } + else + { + fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream)); + return 1; /* Failure */ + } + } +} |