Improve alltypes_pointer test case
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 29 Dec 2013 16:35:14 +0000 (18:35 +0200)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 29 Dec 2013 16:35:57 +0000 (18:35 +0200)
tests/alltypes_pointer/SConscript
tests/alltypes_pointer/alltypes.options
tests/alltypes_pointer/encode_alltypes_pointer.c

index b0e3504..81b9fa5 100644 (file)
@@ -1,12 +1,18 @@
-# Build and run a test that encodes and decodes a message that contains
-# all of the Protocol Buffers data types.
+# Encode the AllTypes message using pointers for all fields, and verify the
+# output against the normal AllTypes test case.
 
 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"])
 
+# Encode and compare results
 env.RunTest(enc)
-# env.RunTest([dec, "encode_alltypes.output"])
+env.RunTest("decode_alltypes.output", ["$BUILD/alltypes/decode_alltypes", "encode_alltypes_pointer.output"])
+env.Compare(["encode_alltypes_pointer.output", "$BUILD/alltypes/encode_alltypes.output"])
+
+# Do the same thing with the optional fields present
+#env.RunTest("optionals.output", enc, ARGS = ['1'])
+#env.RunTest("optionals.decout", ["$BUILD/alltypes/decode_alltypes", "optionals.output"], ARGS = ['1'])
+#env.Compare(["optionals.output", "$BUILD/alltypes/optionals.output"])
 
index 330860a..52abeb7 100644 (file)
@@ -1,2 +1,3 @@
+# Generate all fields as pointers.
 * type:FT_POINTER
 
index 7e648e3..dabee73 100644 (file)
@@ -12,107 +12,142 @@ 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";
+    /* Values for required fields */
+    int32_t     req_int32         = -1001;
+    int64_t     req_int64         = -1002;
+    uint32_t    req_uint32        = 1003;
+    uint64_t    req_uint64        = 1004;
+    int32_t     req_sint32        = -1005;
+    int64_t     req_sint64        = -1006;
+    bool        req_bool          = true;
+    uint32_t    req_fixed32       = 1008;
+    int32_t     req_sfixed32      = -1009;
+    float       req_float         = 1010.0f;
+    uint64_t    req_fixed64       = 1011;
+    int64_t     req_sfixed64      = -1012;
+    double      req_double        = 1013.0;
+    char*       req_string        = "1014";
+    pb_bytes_ptr_t req_bytes      = {4, (uint8_t*)"1015"};
+    static int32_t req_substuff   = 1016;
+    SubMessage  req_submsg        = {"1016", &req_substuff};
+    MyEnum      req_enum          = MyEnum_Truth;
+    EmptyMessage req_emptymsg     = {0};
     
-    pb_bytes_ptr_t value_bytes = {4, (uint8_t*)"1000"};
+    int32_t     end               = 1099;
 
-    SubMessage value_submessage = {0};
-    MyEnum value_enum = MyEnum_Truth;
-    EmptyMessage value_empty_message = {0};
-
-    /* Initialize the structure with constants */
-    AllTypes alltypes = {0};
+    /* Values for repeated fields */
+    int32_t     rep_int32[5]      = {0, 0, 0, 0, -2001};
+    int64_t     rep_int64[5]      = {0, 0, 0, 0, -2002};
+    uint32_t    rep_uint32[5]     = {0, 0, 0, 0, 2003};
+    uint64_t    rep_uint64[5]     = {0, 0, 0, 0, 2004};
+    int32_t     rep_sint32[5]     = {0, 0, 0, 0, -2005};
+    int64_t     rep_sint64[5]     = {0, 0, 0, 0, -2006};
+    bool        rep_bool[5]       = {false, false, false, false, true};
+    uint32_t    rep_fixed32[5]    = {0, 0, 0, 0, 2008};
+    int32_t     rep_sfixed32[5]   = {0, 0, 0, 0, -2009};
+    float       rep_float[5]      = {0, 0, 0, 0, 2010.0f};
+    uint64_t    rep_fixed64[5]    = {0, 0, 0, 0, 2011};
+    int64_t     rep_sfixed64[5]   = {0, 0, 0, 0, -2012};
+    double      rep_double[5]     = {0, 0, 0, 0, 2013.0f};
+    char*       rep_string[5]     = {"", "", "", "", "2014"};
+    pb_bytes_ptr_t rep_bytes[5]   = {{0,0}, {0,0}, {0,0}, {0,0}, {4, (uint8_t*)"2015"}};
+    static int32_t rep_sub2zero   = 0;
+    static int32_t rep_substuff2  = 2016;
+    static uint32_t rep_substuff3 = 2016;
+    SubMessage  rep_submsg[5]     = {{"", &rep_sub2zero},
+                                     {"", &rep_sub2zero},
+                                     {"", &rep_sub2zero},
+                                     {"", &rep_sub2zero},
+                                     {"2016", &rep_substuff2, &rep_substuff3}};
+    MyEnum      rep_enum[5]       = {0, 0, 0, 0, MyEnum_Truth};
+    EmptyMessage rep_emptymsg[5]  = {{0}, {0}, {0}, {0}, {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;
-
-    alltypes.req_string        = value_string;
-    
-    alltypes.req_bytes         = &value_bytes;
+    /* Values for optional fields */
+    int32_t     opt_int32         = 3041;
+    int64_t     opt_int64         = 3042;
+    uint32_t    opt_uint32        = 3043;
+    uint64_t    opt_uint64        = 3044;
+    int32_t     opt_sint32        = 3045;
+    int64_t     opt_sint64        = 3046;
+    bool        opt_bool          = true;
+    uint32_t    opt_fixed32       = 3048;
+    int32_t     opt_sfixed32      = 3049;
+    float       opt_float         = 3050.0f;
+    uint64_t    opt_fixed64       = 3051;
+    int64_t     opt_sfixed64      = 3052;
+    double      opt_double        = 3053.0;
+    char*       opt_string        = "3054";
+    pb_bytes_ptr_t opt_bytes      = {4, (uint8_t*)"3055"};
+    static int32_t opt_substuff   = 3056;
+    SubMessage  opt_submsg        = {"3056", &opt_substuff};
+    MyEnum      opt_enum          = MyEnum_Truth;
+    EmptyMessage opt_emptymsg     = {0};
 
-    value_submessage.substuff1 = value_string;
-    value_submessage.substuff2 = &value_int32;
+    /* Initialize the message struct with pointers to the fields. */
+    AllTypes alltypes = {0};
 
-    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.req_int32         = &req_int32;
+    alltypes.req_int64         = &req_int64;
+    alltypes.req_uint32        = &req_uint32;
+    alltypes.req_uint64        = &req_uint64;
+    alltypes.req_sint32        = &req_sint32;
+    alltypes.req_sint64        = &req_sint64;
+    alltypes.req_bool          = &req_bool;
+    alltypes.req_fixed32       = &req_fixed32;
+    alltypes.req_sfixed32      = &req_sfixed32;
+    alltypes.req_float         = &req_float;
+    alltypes.req_fixed64       = &req_fixed64;
+    alltypes.req_sfixed64      = &req_sfixed64;
+    alltypes.req_double        = &req_double;
+    alltypes.req_string        = req_string;
+    alltypes.req_bytes         = &req_bytes;
+    alltypes.req_submsg        = &req_submsg;
+    alltypes.req_enum          = &req_enum;
+    alltypes.req_emptymsg      = &req_emptymsg;
     
-    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;
-
-    alltypes.rep_string_count = 1; alltypes.rep_string = (char **)&value_string;
-    alltypes.rep_bytes_count = 1; alltypes.rep_bytes = &value_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;
+    alltypes.rep_int32_count    = 5; alltypes.rep_int32     = rep_int32;
+    alltypes.rep_int64_count    = 5; alltypes.rep_int64     = rep_int64;
+    alltypes.rep_uint32_count   = 5; alltypes.rep_uint32    = rep_uint32;
+    alltypes.rep_uint64_count   = 5; alltypes.rep_uint64    = rep_uint64;
+    alltypes.rep_sint32_count   = 5; alltypes.rep_sint32    = rep_sint32;
+    alltypes.rep_sint64_count   = 5; alltypes.rep_sint64    = rep_sint64;
+    alltypes.rep_bool_count     = 5; alltypes.rep_bool      = rep_bool;
+    alltypes.rep_fixed32_count  = 5; alltypes.rep_fixed32   = rep_fixed32;
+    alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32  = rep_sfixed32;
+    alltypes.rep_float_count    = 5; alltypes.rep_float     = rep_float;
+    alltypes.rep_fixed64_count  = 5; alltypes.rep_fixed64   = rep_fixed64;
+    alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64  = rep_sfixed64;
+    alltypes.rep_double_count   = 5; alltypes.rep_double    = rep_double;
+    alltypes.rep_string_count   = 5; alltypes.rep_string    = rep_string;
+    alltypes.rep_bytes_count    = 5; alltypes.rep_bytes     = rep_bytes;
+    alltypes.rep_submsg_count   = 5; alltypes.rep_submsg    = rep_submsg;
+    alltypes.rep_enum_count     = 5; alltypes.rep_enum      = rep_enum;
+    alltypes.rep_emptymsg_count = 5; alltypes.rep_emptymsg  = rep_emptymsg;
     
     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;
-    
-      alltypes.opt_string        = value_string;
-      
-      alltypes.opt_bytes         = &value_bytes;
-
-      alltypes.opt_submsg        = &value_submessage;
-      alltypes.opt_enum          = &value_enum;
-      alltypes.opt_emptymsg      = &value_empty_message;
+        alltypes.opt_int32         = &opt_int32;
+        alltypes.opt_int64         = &opt_int64;
+        alltypes.opt_uint32        = &opt_uint32;
+        alltypes.opt_uint64        = &opt_uint64;
+        alltypes.opt_sint32        = &opt_sint32;
+        alltypes.opt_sint64        = &opt_sint64;
+        alltypes.opt_bool          = &opt_bool;
+        alltypes.opt_fixed32       = &opt_fixed32;
+        alltypes.opt_sfixed32      = &opt_sfixed32;
+        alltypes.opt_float         = &opt_float;
+        alltypes.opt_fixed64       = &opt_fixed64;
+        alltypes.opt_sfixed64      = &opt_sfixed64;
+        alltypes.opt_double        = &opt_double;
+        alltypes.opt_string        = opt_string;
+        alltypes.opt_bytes         = &opt_bytes;
+        alltypes.opt_submsg        = &opt_submsg;
+        alltypes.opt_enum          = &opt_enum;
+        alltypes.opt_emptymsg      = &opt_emptymsg;
     }
     
-    alltypes.end = &value_int32;
+    alltypes.end = &end;
     
     {
         uint8_t buffer[4096];
@@ -121,8 +156,8 @@ int main(int argc, char **argv)
         /* 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 */
+            SET_BINARY_MODE(stdout);
+            fwrite(buffer, 1, stream.bytes_written, stdout);
             return 0; /* Success */
         }
         else