Publishing nanopb-0.3.6
[apps/agl-service-can-low-level.git] / tests / backwards_compatibility / decode_legacy.c
index b74172f..5f5b6bb 100644 (file)
@@ -1,16 +1,17 @@
 /* Tests the decoding of all types.
- * This is a backwards-compatibility test, using bc_alltypes.pb.h.
- * It is similar to test_decode3, but duplicated in order to allow
- * test_decode3 to test any new features introduced later.
+ * This is a backwards-compatibility test, using alltypes_legacy.h.
+ * It is similar to decode_alltypes, but duplicated in order to allow
+ * decode_alltypes to test any new features introduced later.
  *
- * Run e.g. ./bc_encode | ./bc_decode
+ * Run e.g. ./encode_legacy | ./decode_legacy
  */
 
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <pb_decode.h>
-#include "bc_alltypes.pb.h"
+#include "alltypes_legacy.h"
+#include "test_helpers.h"
 
 #define TEST(x) if (!(x)) { \
     printf("Test " #x " failed.\n"); \
    the decoding and checks the fields. */
 bool check_alltypes(pb_istream_t *stream, int mode)
 {
-    AllTypes alltypes;
-    
-    /* Fill with garbage to better detect initialization errors */
-    memset(&alltypes, 0xAA, sizeof(alltypes));
+    AllTypes alltypes = {0};
     
     if (!pb_decode(stream, AllTypes_fields, &alltypes))
         return false;
@@ -176,15 +174,19 @@ bool check_alltypes(pb_istream_t *stream, int mode)
 
 int main(int argc, char **argv)
 {
+    uint8_t buffer[1024];
+    size_t count;
+    pb_istream_t stream;
+
     /* 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[1024];
-    size_t count = fread(buffer, 1, sizeof(buffer), stdin);
+    SET_BINARY_MODE(stdin);
+    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);
+    stream = pb_istream_from_buffer(buffer, count);
     
     /* Decode and print out the stuff */
     if (!check_alltypes(&stream, mode))