diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2012-08-26 10:57:51 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2012-08-26 10:57:51 +0300 |
commit | a7a6cdad8885b48a648ad69c9c0a3f01e0707934 (patch) | |
tree | 44c56f10d9ed6aed552ebeb701ed231d4fc16a05 | |
parent | 1463e687e36c8dd404d33c6ef1cba61b574adc1e (diff) |
Added alltypes-testcases for optional fields and default values.
-rw-r--r-- | tests/Makefile | 3 | ||||
-rw-r--r-- | tests/alltypes.proto | 25 | ||||
-rw-r--r-- | tests/test_decode3.c | 109 | ||||
-rw-r--r-- | tests/test_encode3.c | 55 |
4 files changed, 180 insertions, 12 deletions
diff --git a/tests/Makefile b/tests/Makefile index 5a5d94a..5014221 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -65,7 +65,8 @@ run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_ "`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ] ./test_encode3 | ./test_decode3 - ./test_encode3 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null + ./test_encode3 1 | ./test_decode3 1 + ./test_encode3 1 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null ./test_missing_fields diff --git a/tests/alltypes.proto b/tests/alltypes.proto index edaaa62..b9003a7 100644 --- a/tests/alltypes.proto +++ b/tests/alltypes.proto @@ -1,8 +1,9 @@ import "nanopb.proto"; message SubMessage { - required string substuff1 = 1 [(nanopb).max_size = 16]; - required int32 substuff2 = 2; + required string substuff1 = 1 [(nanopb).max_size = 16, default = "1"]; + required int32 substuff2 = 2 [default = 2]; + optional fixed32 substuff3 = 3 [default = 3]; } enum MyEnum { @@ -56,7 +57,27 @@ message AllTypes { repeated SubMessage rep_submsg = 36 [(nanopb).max_count = 5]; repeated MyEnum rep_enum = 37 [(nanopb).max_count = 5]; + 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 [(nanopb).max_size = 16, default = "4054"]; + optional bytes opt_bytes = 55 [(nanopb).max_size = 16, default = "4055"]; + optional SubMessage opt_submsg = 56; + optional MyEnum opt_enum = 57 [default = Second]; + // 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; diff --git a/tests/test_decode3.c b/tests/test_decode3.c index 4f55b55..4b7ee9e 100644 --- a/tests/test_decode3.c +++ b/tests/test_decode3.c @@ -1,10 +1,11 @@ -/* Tests the decoding of all types. Currently only in the 'required' variety. +/* Tests the decoding of all types. * This is the counterpart of test_encode3. * Run e.g. ./test_encode3 | ./test_decode3 */ #include <stdio.h> #include <string.h> +#include <stdlib.h> #include <pb_decode.h> #include "alltypes.pb.h" @@ -15,9 +16,12 @@ /* This function is called once from main(), it handles the decoding and checks the fields. */ -bool check_alltypes(pb_istream_t *stream) +bool check_alltypes(pb_istream_t *stream, int mode) { - AllTypes alltypes = {}; + AllTypes alltypes; + + /* Fill with garbage to better detect initialization errors */ + memset(&alltypes, 0xAA, sizeof(alltypes)); if (!pb_decode(stream, AllTypes_fields, &alltypes)) return false; @@ -43,6 +47,7 @@ bool check_alltypes(pb_istream_t *stream) TEST(memcmp(alltypes.req_bytes.bytes, "1015", 4) == 0); TEST(strcmp(alltypes.req_submsg.substuff1, "1016") == 0); TEST(alltypes.req_submsg.substuff2 == 1016); + TEST(alltypes.req_submsg.substuff3 == 3); TEST(alltypes.req_enum == MyEnum_Truth); TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == 2001 && alltypes.rep_int32[0] == 0); @@ -68,26 +73,118 @@ bool check_alltypes(pb_istream_t *stream) TEST(alltypes.rep_submsg_count == 5); TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0'); TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0); + TEST(alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == 3); TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero); + if (mode == 0) + { + /* Expect default values */ + TEST(alltypes.has_opt_int32 == false); + TEST(alltypes.opt_int32 == 4041); + TEST(alltypes.has_opt_int64 == false); + TEST(alltypes.opt_int64 == 4042); + TEST(alltypes.has_opt_uint32 == false); + TEST(alltypes.opt_uint32 == 4043); + TEST(alltypes.has_opt_uint64 == false); + TEST(alltypes.opt_uint64 == 4044); + TEST(alltypes.has_opt_sint32 == false); + TEST(alltypes.opt_sint32 == 4045); + TEST(alltypes.has_opt_sint64 == false); + TEST(alltypes.opt_sint64 == 4046); + TEST(alltypes.has_opt_bool == false); + TEST(alltypes.opt_bool == false); + + TEST(alltypes.has_opt_fixed32 == false); + TEST(alltypes.opt_fixed32 == 4048); + TEST(alltypes.has_opt_sfixed32 == false); + TEST(alltypes.opt_sfixed32 == 4049); + TEST(alltypes.has_opt_float == false); + TEST(alltypes.opt_float == 4050.0f); + + TEST(alltypes.has_opt_fixed64 == false); + TEST(alltypes.opt_fixed64 == 4051); + TEST(alltypes.has_opt_sfixed64 == false); + TEST(alltypes.opt_sfixed64 == 4052); + TEST(alltypes.has_opt_double == false); + TEST(alltypes.opt_double == 4053.0); + + TEST(alltypes.has_opt_string == false); + TEST(strcmp(alltypes.opt_string, "4054") == 0); + TEST(alltypes.has_opt_bytes == false); + TEST(alltypes.opt_bytes.size == 4); + TEST(memcmp(alltypes.opt_bytes.bytes, "4055", 4) == 0); + TEST(alltypes.has_opt_submsg == false); + TEST(strcmp(alltypes.opt_submsg.substuff1, "1") == 0); + TEST(alltypes.opt_submsg.substuff2 == 2); + TEST(alltypes.opt_submsg.substuff3 == 3); + TEST(alltypes.has_opt_enum == false); + TEST(alltypes.opt_enum == MyEnum_Second); + } + else + { + /* Expect filled-in values */ + TEST(alltypes.has_opt_int32 == true); + TEST(alltypes.opt_int32 == 3041); + TEST(alltypes.has_opt_int64 == true); + TEST(alltypes.opt_int64 == 3042); + TEST(alltypes.has_opt_uint32 == true); + TEST(alltypes.opt_uint32 == 3043); + TEST(alltypes.has_opt_uint64 == true); + TEST(alltypes.opt_uint64 == 3044); + TEST(alltypes.has_opt_sint32 == true); + TEST(alltypes.opt_sint32 == 3045); + TEST(alltypes.has_opt_sint64 == true); + TEST(alltypes.opt_sint64 == 3046); + TEST(alltypes.has_opt_bool == true); + TEST(alltypes.opt_bool == true); + + TEST(alltypes.has_opt_fixed32 == true); + TEST(alltypes.opt_fixed32 == 3048); + TEST(alltypes.has_opt_sfixed32 == true); + TEST(alltypes.opt_sfixed32 == 3049); + TEST(alltypes.has_opt_float == true); + TEST(alltypes.opt_float == 3050.0f); + + TEST(alltypes.has_opt_fixed64 == true); + TEST(alltypes.opt_fixed64 == 3051); + TEST(alltypes.has_opt_sfixed64 == true); + TEST(alltypes.opt_sfixed64 == 3052); + TEST(alltypes.has_opt_double == true); + TEST(alltypes.opt_double == 3053.0); + + TEST(alltypes.has_opt_string == true); + TEST(strcmp(alltypes.opt_string, "3054") == 0); + TEST(alltypes.has_opt_bytes == true); + TEST(alltypes.opt_bytes.size == 4); + TEST(memcmp(alltypes.opt_bytes.bytes, "3055", 4) == 0); + TEST(alltypes.has_opt_submsg == true); + TEST(strcmp(alltypes.opt_submsg.substuff1, "3056") == 0); + TEST(alltypes.opt_submsg.substuff2 == 3056); + TEST(alltypes.opt_submsg.substuff3 == 3); + TEST(alltypes.has_opt_enum == true); + TEST(alltypes.opt_enum == MyEnum_Truth); + } TEST(alltypes.end == 1099); return true; } -int main() +int main(int argc, char **argv) { + /* Whether to expect the optional values or the default values. */ + int mode = (argc > 1) ? atoi(argv[1]) : 0; + /* Read the data into buffer */ - uint8_t buffer[512]; + uint8_t buffer[1024]; size_t count = fread(buffer, 1, sizeof(buffer), stdin); /* Construct a pb_istream_t for reading from the buffer */ pb_istream_t stream = pb_istream_from_buffer(buffer, count); /* Decode and print out the stuff */ - if (!check_alltypes(&stream)) + if (!check_alltypes(&stream, mode)) { printf("Parsing failed: %s\n", PB_GET_ERROR(&stream)); return 1; diff --git a/tests/test_encode3.c b/tests/test_encode3.c index 8128ea4..1a48f06 100644 --- a/tests/test_encode3.c +++ b/tests/test_encode3.c @@ -1,14 +1,16 @@ /* Attempts to test all the datatypes supported by ProtoBuf. - * Currently only tests the 'required' variety. */ #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <pb_encode.h> #include "alltypes.pb.h" -int main() +int main(int argc, char **argv) { + int mode = (argc > 1) ? atoi(argv[1]) : 0; + /* Initialize the structure with constants */ AllTypes alltypes = {0}; @@ -58,12 +60,58 @@ int main() alltypes.rep_submsg_count = 5; strcpy(alltypes.rep_submsg[4].substuff1, "2016"); alltypes.rep_submsg[4].substuff2 = 2016; + alltypes.rep_submsg[4].has_substuff3 = true; + alltypes.rep_submsg[4].substuff3 = 2016; alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth; + if (mode != 0) + { + /* Fill in values for optional fields */ + alltypes.has_opt_int32 = true; + alltypes.opt_int32 = 3041; + alltypes.has_opt_int64 = true; + alltypes.opt_int64 = 3042; + alltypes.has_opt_uint32 = true; + alltypes.opt_uint32 = 3043; + alltypes.has_opt_uint64 = true; + alltypes.opt_uint64 = 3044; + alltypes.has_opt_sint32 = true; + alltypes.opt_sint32 = 3045; + alltypes.has_opt_sint64 = true; + alltypes.opt_sint64 = 3046; + alltypes.has_opt_bool = true; + alltypes.opt_bool = true; + + alltypes.has_opt_fixed32 = true; + alltypes.opt_fixed32 = 3048; + alltypes.has_opt_sfixed32 = true; + alltypes.opt_sfixed32 = 3049; + alltypes.has_opt_float = true; + alltypes.opt_float = 3050.0f; + + alltypes.has_opt_fixed64 = true; + alltypes.opt_fixed64 = 3051; + alltypes.has_opt_sfixed64 = true; + alltypes.opt_sfixed64 = 3052; + alltypes.has_opt_double = true; + alltypes.opt_double = 3053.0; + + alltypes.has_opt_string = true; + strcpy(alltypes.opt_string, "3054"); + alltypes.has_opt_bytes = true; + alltypes.opt_bytes.size = 4; + memcpy(alltypes.opt_bytes.bytes, "3055", 4); + alltypes.has_opt_submsg = true; + strcpy(alltypes.opt_submsg.substuff1, "3056"); + alltypes.opt_submsg.substuff2 = 3056; + alltypes.has_opt_enum = true; + alltypes.opt_enum = MyEnum_Truth; + } + alltypes.end = 1099; - uint8_t buffer[512]; + uint8_t buffer[1024]; pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); /* Now encode it and check if we succeeded. */ @@ -74,6 +122,7 @@ int main() } else { + fprintf(stderr, "Encoding failed!\n"); return 1; /* Failure */ } } |