summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-07-31 12:55:09 +0000
committerPetteri Aimonen <jpa@npb.mail.kapsi.fi>2011-07-31 12:55:09 +0000
commit3959290bc77ae26772bc107128b0a4edd3930361 (patch)
treefefcf27423aeb7fd100b52802f5c519def3a8cc7 /tests
parentf8364310d3ca85d2cf59019bb4bc5e9ff4c52dc3 (diff)
First version of header generator
git-svn-id: https://svn.kapsi.fi/jpa/nanopb@950 e3a754e5-d11d-0410-8d38-ebb782a927b9
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile2
-rw-r--r--tests/decode_unittests.c25
-rw-r--r--tests/person.proto11
-rw-r--r--tests/test_decode1.c73
4 files changed, 36 insertions, 75 deletions
diff --git a/tests/Makefile b/tests/Makefile
index ddf91ebb..35c6f977 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,5 +1,5 @@
CFLAGS=-ansi -Wall -Werror -I .. -g -O0
-DEPS=../pb_decode.c ../pb_decode.h ../pb.h
+DEPS=../pb_decode.c ../pb_decode.h ../pb.h person.h
TESTS=test_decode1 decode_unittests
all: $(TESTS)
diff --git a/tests/decode_unittests.c b/tests/decode_unittests.c
index 746b7e96..7d3b13e5 100644
--- a/tests/decode_unittests.c
+++ b/tests/decode_unittests.c
@@ -104,6 +104,31 @@ int main()
TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 7))
}
+ {
+ pb_istream_t s = S("\x01\xFF\xFF\x03");
+ pb_field_t f = {1, PB_LTYPE_VARINT, 0, 0, 4, 0, 0};
+ uint32_t d;
+ COMMENT("Test pb_dec_varint using uint32_t")
+ TEST(pb_dec_varint(&s, &f, &d) && d == 1)
+
+ /* Verify that no more than data_size is written. */
+ d = 0;
+ f.data_size = 1;
+ TEST(pb_dec_varint(&s, &f, &d) && d == 0xFF)
+ }
+
+ {
+ pb_istream_t s;
+ pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0};
+ int32_t d;
+
+ COMMENT("Test pb_dec_svarint using int32_t")
+ TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
+ TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
+ TEST((s = S("\xfe\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MAX))
+ TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MIN))
+ }
+
if (status != 0)
fprintf(stdout, "\n\nSome tests FAILED!\n");
diff --git a/tests/person.proto b/tests/person.proto
index ec5b4cca..01b2d4e1 100644
--- a/tests/person.proto
+++ b/tests/person.proto
@@ -1,7 +1,10 @@
+import "nanopb.proto";
+
message Person {
- required string name = 1;
+ required string name = 1 [(nanopb).max_size = 40];
required int32 id = 2;
- optional string email = 3;
+ optional string email = 3 [(nanopb).max_size = 40];
+ optional bytes test = 5 [default = "abc\x00\x01\x02"];
enum PhoneType {
MOBILE = 0;
@@ -10,9 +13,9 @@ message Person {
}
message PhoneNumber {
- required string number = 1;
+ required string number = 1 [(nanopb).max_size = 40];
optional PhoneType type = 2 [default = HOME];
}
- repeated PhoneNumber phone = 4;
+ repeated PhoneNumber phone = 4 [(nanopb).max_count = 5];
}
diff --git a/tests/test_decode1.c b/tests/test_decode1.c
index 4b34022f..362c404a 100644
--- a/tests/test_decode1.c
+++ b/tests/test_decode1.c
@@ -1,75 +1,8 @@
#include <stdio.h>
#include <string.h>
#include <stddef.h>
-#include "pb_decode.h"
-
-/* Structures for "Person" message */
-
-typedef enum {
- Person_PhoneType_MOBILE = 0,
- Person_PhoneType_HOME = 1,
- Person_PhoneType_WORK = 2
-} Person_PhoneType;
-
-typedef struct {
- char number[40];
- bool has_type;
- Person_PhoneType type;
-} Person_PhoneNumber;
-
-typedef struct {
- char name[40];
- int32_t id;
- bool has_email;
- char email[40];
- size_t phone_size;
- Person_PhoneNumber phone[5];
-} Person;
-
-/* Field descriptions */
-
-
-const Person_PhoneType Person_PhoneNumber_type_default = Person_PhoneType_HOME;
-
-const pb_field_t Person_PhoneNumber_fields[] = {
- {1, PB_HTYPE_REQUIRED | PB_LTYPE_STRING,
- offsetof(Person_PhoneNumber, number), 0,
- pb_membersize(Person_PhoneNumber, number), 0, 0},
-
- {2, PB_HTYPE_OPTIONAL | PB_LTYPE_VARINT,
- pb_delta(Person_PhoneNumber, type, number),
- pb_delta(Person_PhoneNumber, has_type, type),
- pb_membersize(Person_PhoneNumber, type), 0,
- &Person_PhoneNumber_type_default},
-
- PB_LAST_FIELD
-};
-
-const pb_field_t Person_fields[] = {
- {1, PB_HTYPE_REQUIRED | PB_LTYPE_STRING,
- offsetof(Person, name), 0,
- pb_membersize(Person, name), 0, 0},
-
- {2, PB_HTYPE_REQUIRED | PB_LTYPE_VARINT,
- pb_delta(Person, id, name), 0,
- pb_membersize(Person, id), 0, 0},
-
- {3, PB_HTYPE_OPTIONAL | PB_LTYPE_STRING,
- offsetof(Person, email) - offsetof(Person, id),
- pb_delta(Person, has_email, email),
- pb_membersize(Person, email), 0, 0},
-
- {4, PB_HTYPE_ARRAY | PB_LTYPE_SUBMESSAGE,
- offsetof(Person, phone) - offsetof(Person, email),
- pb_delta(Person, phone_size, phone),
- pb_membersize(Person, phone[0]),
- pb_arraysize(Person, phone),
- Person_PhoneNumber_fields},
-
- PB_LAST_FIELD
-};
-
-/* And now, the actual test program */
+#include <pb_decode.h>
+#include "person.h"
bool print_person(pb_istream_t *stream)
{
@@ -81,7 +14,7 @@ bool print_person(pb_istream_t *stream)
printf("Person: name '%s' id '%d' email '%s'\n", person.name, person.id, person.email);
- for (i = 0; i < person.phone_size; i++)
+ for (i = 0; i < person.phone_count; i++)
{
Person_PhoneNumber *phone = &person.phone[i];
printf("PhoneNumber: number '%s' type '%d'\n", phone->number, phone->type);