summaryrefslogtreecommitdiffstats
path: root/tests/test_encode3.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-08-26 10:57:51 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2012-08-26 10:57:51 +0300
commita7a6cdad8885b48a648ad69c9c0a3f01e0707934 (patch)
tree44c56f10d9ed6aed552ebeb701ed231d4fc16a05 /tests/test_encode3.c
parent1463e687e36c8dd404d33c6ef1cba61b574adc1e (diff)
Added alltypes-testcases for optional fields and default values.
Diffstat (limited to 'tests/test_encode3.c')
-rw-r--r--tests/test_encode3.c55
1 files changed, 52 insertions, 3 deletions
diff --git a/tests/test_encode3.c b/tests/test_encode3.c
index 8128ea4a..1a48f06b 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 */
}
}