Fix bugs in extension support when multiple extension fields are present.
[apps/agl-service-can-low-level.git] / tests / encode_unittests.c
index 926a254..c3634ac 100644 (file)
@@ -1,3 +1,5 @@
+#define NANOPB_INTERNALS
+
 #include <stdio.h>
 #include <string.h>
 #include "pb_encode.h"
@@ -15,7 +17,7 @@ bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
     return true;
 }
 
-bool fieldcallback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
+bool fieldcallback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
 {
     int value = 0x55;
     if (!pb_encode_tag_for_field(stream, field))
@@ -23,7 +25,7 @@ bool fieldcallback(pb_ostream_t *stream, const pb_field_t *field, const void *ar
     return pb_encode_varint(stream, value);
 }
 
-bool crazyfieldcallback(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
+bool crazyfieldcallback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
 {
     /* This callback writes different amount of data the second time. */
     uint32_t *state = (uint32_t*)arg;
@@ -99,15 +101,13 @@ int main()
         COMMENT("Test pb_encode_tag_for_field")
         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x50"));
         
-        field.type = PB_LTYPE_FIXED;
-        field.data_size = 8;
+        field.type = PB_LTYPE_FIXED64;
         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x51"));
         
         field.type = PB_LTYPE_STRING;
         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x52"));
         
-        field.type = PB_LTYPE_FIXED;
-        field.data_size = 4;
+        field.type = PB_LTYPE_FIXED32;
         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x55"));
     }
     
@@ -125,7 +125,6 @@ int main()
         uint8_t buffer[30];
         pb_ostream_t s;
         uint8_t value = 1;
-        int8_t svalue = -1;
         int32_t max = INT32_MAX;
         int32_t min = INT32_MIN;
         int64_t lmax = INT64_MAX;
@@ -134,8 +133,6 @@ int main()
         
         COMMENT("Test pb_enc_varint and pb_enc_svarint")
         TEST(WRITES(pb_enc_varint(&s, &field, &value), "\x01"));
-        TEST(WRITES(pb_enc_svarint(&s, &field, &svalue), "\x01"));
-        TEST(WRITES(pb_enc_svarint(&s, &field, &value), "\x02"));
         
         field.data_size = sizeof(max);
         TEST(WRITES(pb_enc_svarint(&s, &field, &max), "\xfe\xff\xff\xff\x0f"));
@@ -149,26 +146,24 @@ int main()
     {
         uint8_t buffer[30];
         pb_ostream_t s;
-        pb_field_t field = {1, PB_LTYPE_FIXED, 0, 0, sizeof(float)};
         float fvalue;
         double dvalue;
         
-        COMMENT("Test pb_enc_fixed using float")
+        COMMENT("Test pb_enc_fixed32 using float")
         fvalue = 0.0f;
-        TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x00\x00\x00\x00"))
+        TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\x00\x00"))
         fvalue = 99.0f;
-        TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x00\x00\xc6\x42"))
+        TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x00\x00\xc6\x42"))
         fvalue = -12345678.0f;
-        TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x4e\x61\x3c\xcb"))
+        TEST(WRITES(pb_enc_fixed32(&s, NULL, &fvalue), "\x4e\x61\x3c\xcb"))
     
-        COMMENT("Test pb_enc_fixed using double")
-        field.data_size = sizeof(double);
+        COMMENT("Test pb_enc_fixed64 using double")
         dvalue = 0.0;
-        TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
+        TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
         dvalue = 99.0;
-        TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\x00\x00\xc0\x58\x40"))
+        TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\x00\x00\xc0\x58\x40"))
         dvalue = -12345678.0;
-        TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\xc0\x29\x8c\x67\xc1"))
+        TEST(WRITES(pb_enc_fixed64(&s, NULL, &dvalue), "\x00\x00\x00\xc0\x29\x8c\x67\xc1"))
     }
     
     {
@@ -185,12 +180,14 @@ int main()
     {
         uint8_t buffer[30];
         pb_ostream_t s;
-        char value[] = "xyzzy";
+        char value[30] = "xyzzy";
         
         COMMENT("Test pb_enc_string")
-        TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x05xyzzy"))
+        TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x05xyzzy"))
         value[0] = '\0';
-        TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x00"))
+        TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x00"))
+        memset(value, 'x', 30);
+        TEST(WRITES(pb_enc_string(&s, &StringMessage_fields[0], &value), "\x0Axxxxxxxxxx"))
     }
     
     {
@@ -247,6 +244,16 @@ int main()
                     "\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"))
     }
     
+    {
+        uint8_t buffer[20];
+        pb_ostream_t s;
+        IntegerContainer msg = {{5, {1,2,3,4,5}}};
+        
+        COMMENT("Test pb_encode_delimited.")
+        TEST(WRITES(pb_encode_delimited(&s, IntegerContainer_fields, &msg),
+                    "\x09\x0A\x07\x0A\x05\x01\x02\x03\x04\x05"))
+    }
+    
     {
         uint8_t buffer[10];
         pb_ostream_t s;