Update changelog
[apps/agl-service-can-low-level.git] / tests / decode_unittests.c
index 6f2b77e..1e74c34 100644 (file)
@@ -1,7 +1,10 @@
+#define NANOPB_INTERNALS
+
 #include <stdio.h>
 #include <string.h>
 #include "pb_decode.h"
 #include "unittests.h"
+#include "unittestproto.pb.h"
 
 #define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x) - 1)
 
@@ -15,30 +18,12 @@ bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
     return true;
 }
 
-typedef struct { size_t data_count; int32_t data[10]; } IntegerArray;
-const pb_field_t IntegerArray_fields[] = {
-    {1, PB_HTYPE_ARRAY | PB_LTYPE_VARINT, offsetof(IntegerArray, data),
-    pb_delta(IntegerArray, data_count, data),
-    pb_membersize(IntegerArray, data[0]),
-    pb_membersize(IntegerArray, data) / pb_membersize(IntegerArray, data[0])},
-    
-    PB_LAST_FIELD
-};
-
-typedef struct { pb_callback_t data; } CallbackArray;
-const pb_field_t CallbackArray_fields[] = {
-    {1, PB_HTYPE_CALLBACK | PB_LTYPE_VARINT, offsetof(CallbackArray, data),
-    0, pb_membersize(CallbackArray, data), 0},
-    
-    PB_LAST_FIELD
-};
-
 /* Verifies that the stream passed to callback matches the byte array pointed to by arg. */
-bool callback_check(pb_istream_t *stream, const pb_field_t *field, void *arg)
+bool callback_check(pb_istream_t *stream, const pb_field_t *field, void **arg)
 {
     int i;
     uint8_t byte;
-    pb_bytes_array_t *ref = (pb_bytes_array_t*) arg;
+    pb_bytes_array_t *ref = (pb_bytes_array_t*) *arg;
     
     for (i = 0; i < ref->size; i++)
     {
@@ -160,38 +145,46 @@ int main()
     
     {
         pb_istream_t s;
-        pb_field_t f = {1, PB_LTYPE_FIXED, 0, 0, 4, 0, 0};
+        pb_field_t f = {1, PB_LTYPE_FIXED32, 0, 0, 4, 0, 0};
         float d;
         
-        COMMENT("Test pb_dec_fixed using float (failures here may be caused by imperfect rounding)")
-        TEST((s = S("\x00\x00\x00\x00"), pb_dec_fixed(&s, &f, &d) && d == 0.0f))
-        TEST((s = S("\x00\x00\xc6\x42"), pb_dec_fixed(&s, &f, &d) && d == 99.0f))
-        TEST((s = S("\x4e\x61\x3c\xcb"), pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
-        TEST((s = S("\x00"), !pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
+        COMMENT("Test pb_dec_fixed32 using float (failures here may be caused by imperfect rounding)")
+        TEST((s = S("\x00\x00\x00\x00"), pb_dec_fixed32(&s, &f, &d) && d == 0.0f))
+        TEST((s = S("\x00\x00\xc6\x42"), pb_dec_fixed32(&s, &f, &d) && d == 99.0f))
+        TEST((s = S("\x4e\x61\x3c\xcb"), pb_dec_fixed32(&s, &f, &d) && d == -12345678.0f))
+        TEST((s = S("\x00"), !pb_dec_fixed32(&s, &f, &d) && d == -12345678.0f))
     }
     
     {
         pb_istream_t s;
-        pb_field_t f = {1, PB_LTYPE_FIXED, 0, 0, 8, 0, 0};
+        pb_field_t f = {1, PB_LTYPE_FIXED64, 0, 0, 8, 0, 0};
         double d;
         
-        COMMENT("Test pb_dec_fixed using double (failures here may be caused by imperfect rounding)")
-        TEST((s = S("\x00\x00\x00\x00\x00\x00\x00\x00"), pb_dec_fixed(&s, &f, &d) && d == 0.0))
-        TEST((s = S("\x00\x00\x00\x00\x00\xc0\x58\x40"), pb_dec_fixed(&s, &f, &d) && d == 99.0))
-        TEST((s = S("\x00\x00\x00\xc0\x29\x8c\x67\xc1"), pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
+        COMMENT("Test pb_dec_fixed64 using double (failures here may be caused by imperfect rounding)")
+        TEST((s = S("\x00\x00\x00\x00\x00\x00\x00\x00"), pb_dec_fixed64(&s, &f, &d) && d == 0.0))
+        TEST((s = S("\x00\x00\x00\x00\x00\xc0\x58\x40"), pb_dec_fixed64(&s, &f, &d) && d == 99.0))
+        TEST((s = S("\x00\x00\x00\xc0\x29\x8c\x67\xc1"), pb_dec_fixed64(&s, &f, &d) && d == -12345678.0f))
     }
     
     {
         pb_istream_t s;
         struct { size_t size; uint8_t bytes[5]; } d;
-        pb_field_t f = {1, PB_LTYPE_BYTES, 0, 0, 5, 0, 0};
+        pb_field_t f = {1, PB_LTYPE_BYTES, 0, 0, sizeof(d), 0, 0};
         
         COMMENT("Test pb_dec_bytes")
         TEST((s = S("\x00"), pb_dec_bytes(&s, &f, &d) && d.size == 0))
         TEST((s = S("\x01\xFF"), pb_dec_bytes(&s, &f, &d) && d.size == 1 && d.bytes[0] == 0xFF))
-        TEST((s = S("\x06xxxxxx"), !pb_dec_bytes(&s, &f, &d)))
         TEST((s = S("\x05xxxxx"), pb_dec_bytes(&s, &f, &d) && d.size == 5))
         TEST((s = S("\x05xxxx"), !pb_dec_bytes(&s, &f, &d)))
+        
+        /* Note: the size limit on bytes-fields is not strictly obeyed, as
+         * the compiler may add some padding to the struct. Using this padding
+         * is not a very good thing to do, but it is difficult to avoid when
+         * we use only a single uint8_t to store the size of the field.
+         * Therefore this tests against a 10-byte string, while otherwise even
+         * 6 bytes should error out.
+         */
+        TEST((s = S("\x10xxxxxxxxxx"), !pb_dec_bytes(&s, &f, &d)))
     }
     
     {
@@ -224,6 +217,8 @@ int main()
         IntegerArray dest;
         
         COMMENT("Testing pb_decode with packed int32 field")
+        TEST((s = S("\x0A\x00"), pb_decode(&s, IntegerArray_fields, &dest)
+            && dest.data_count == 0))
         TEST((s = S("\x0A\x01\x01"), pb_decode(&s, IntegerArray_fields, &dest)
             && dest.data_count == 1 && dest.data[0] == 1))
         TEST((s = S("\x0A\x0A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"), pb_decode(&s, IntegerArray_fields, &dest)