Compare commits

...

6 Commits

Author SHA1 Message Date
Petteri Aimonen
1aa61f108a Fix signedness warning in example_unions. 2012-08-04 14:34:19 +03:00
Petteri Aimonen
b582bc9bf6 Fix bug in decoder with packed arrays.
Update issue 23
Status: FixedInGit
2012-07-31 19:12:57 +03:00
Petteri Aimonen
5703ad0c55 Extend 'alltypes' testcase to cover repeated fields. 2012-07-31 19:10:33 +03:00
Petteri Aimonen
0a5b6852ec Additional unsigned vs. signed fix for tag 2012-07-19 09:05:36 +03:00
Petteri Aimonen
8524de39ce Fix an unsigned vs. signed warning on some compiler. 2012-07-18 21:21:07 +03:00
Petteri Aimonen
10b5da12dc Fixed a few compiler warnings, added check.
Main code is now compiled (for tests) with -pedantic -Wextra.
The test programs are not as strictly bound, but this should
improve the chances that atleast the core library compiles with
most compilers without warnings.
2012-07-18 21:09:13 +03:00
8 changed files with 121 additions and 35 deletions

View File

@@ -17,7 +17,7 @@
const pb_field_t* decode_unionmessage_type(pb_istream_t *stream)
{
pb_wire_type_t wire_type;
int tag;
uint32_t tag;
bool eof;
while (pb_decode_tag(stream, &wire_type, &tag, &eof))

View File

@@ -117,7 +117,7 @@ bool checkreturn pb_skip_string(pb_istream_t *stream)
return pb_read(stream, NULL, length);
}
bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, int *tag, bool *eof)
bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof)
{
uint32_t temp;
*eof = false;
@@ -253,7 +253,7 @@ static bool pb_field_next(pb_field_iterator_t *iter)
return notwrapped;
}
static bool checkreturn pb_field_find(pb_field_iterator_t *iter, int tag)
static bool checkreturn pb_field_find(pb_field_iterator_t *iter, uint32_t tag)
{
int start = iter->field_index;
@@ -288,6 +288,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
&& PB_LTYPE(iter->current->type) <= PB_LTYPE_LAST_PACKABLE)
{
/* Packed array */
bool status;
size_t *size = (size_t*)iter->pSize;
pb_istream_t substream;
if (!make_string_substream(stream, &substream))
@@ -300,7 +301,9 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t
return false;
(*size)++;
}
return (substream.bytes_left == 0);
status = (substream.bytes_left == 0);
stream->state = substream.state;
return status;
}
else
{
@@ -409,7 +412,7 @@ static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_str
bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
{
uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {}; /* Used to check for required fields */
uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0}; /* Used to check for required fields */
pb_field_iterator_t iter;
pb_message_set_to_defaults(fields, dest_struct);
@@ -418,7 +421,7 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void
while (stream->bytes_left)
{
int tag;
uint32_t tag;
pb_wire_type_t wire_type;
bool eof;

View File

@@ -48,7 +48,7 @@ bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struc
* You may want to use these from your caller or callbacks.
*/
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, int *tag, bool *eof);
bool pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, uint32_t *tag, bool *eof);
bool pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type);
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest);

View File

@@ -99,7 +99,7 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
}
else
{
pb_ostream_t sizestream = {0};
pb_ostream_t sizestream = {0,0,0,0};
p = pData;
for (i = 0; i < count; i++)
{
@@ -314,7 +314,7 @@ bool checkreturn pb_encode_string(pb_ostream_t *stream, const uint8_t *buffer, s
bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct)
{
/* First calculate the message size using a non-writing substream. */
pb_ostream_t substream = {0};
pb_ostream_t substream = {0,0,0,0};
size_t size;
bool status;

View File

@@ -3,6 +3,9 @@ LDFLAGS=--coverage
DEPS=../pb_decode.h ../pb_encode.h ../pb.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h missing_fields.pb.h
TESTS=test_decode1 test_encode1 decode_unittests encode_unittests test_no_messages
# More strict checks for the core part of nanopb
CFLAGS_CORE=-pedantic -Wextra
all: breakpoints $(TESTS) run_unittests
clean:
@@ -13,9 +16,9 @@ clean:
$(CC) $(CFLAGS) -c -o $@ $<
pb_encode.o: ../pb_encode.c $(DEPS)
$(CC) $(CFLAGS) -c -o $@ $<
$(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
pb_decode.o: ../pb_decode.c $(DEPS)
$(CC) $(CFLAGS) -c -o $@ $<
$(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
test_decode1: test_decode1.o pb_decode.o person.pb.o
test_decode2: test_decode2.o pb_decode.o person.pb.o

View File

@@ -6,6 +6,7 @@ message SubMessage {
}
enum MyEnum {
Zero = 0;
First = 1;
Second = 2;
Truth = 42;
@@ -33,6 +34,29 @@ message AllTypes {
required SubMessage req_submsg = 16;
required MyEnum req_enum = 17;
repeated int32 rep_int32 = 21 [(nanopb).max_count = 5];
repeated int64 rep_int64 = 22 [(nanopb).max_count = 5];
repeated uint32 rep_uint32 = 23 [(nanopb).max_count = 5];
repeated uint64 rep_uint64 = 24 [(nanopb).max_count = 5];
repeated sint32 rep_sint32 = 25 [(nanopb).max_count = 5];
repeated sint64 rep_sint64 = 26 [(nanopb).max_count = 5];
repeated bool rep_bool = 27 [(nanopb).max_count = 5];
repeated fixed32 rep_fixed32 = 28 [(nanopb).max_count = 5];
repeated sfixed32 rep_sfixed32= 29 [(nanopb).max_count = 5];
repeated float rep_float = 30 [(nanopb).max_count = 5];
repeated fixed64 rep_fixed64 = 31 [(nanopb).max_count = 5];
repeated sfixed64 rep_sfixed64= 32 [(nanopb).max_count = 5];
repeated double rep_double = 33 [(nanopb).max_count = 5];
repeated string rep_string = 34 [(nanopb).max_size = 16, (nanopb).max_count = 5];
repeated bytes rep_bytes = 35 [(nanopb).max_size = 16, (nanopb).max_count = 5];
repeated SubMessage rep_submsg = 36 [(nanopb).max_count = 5];
repeated MyEnum rep_enum = 37 [(nanopb).max_count = 5];
// Just to make sure that the size of the fields has been calculated
// properly, i.e. otherwise a bug in last field might not be detected.
required int32 end = 99;

View File

@@ -45,6 +45,33 @@ bool check_alltypes(pb_istream_t *stream)
TEST(alltypes.req_submsg.substuff2 == 1016);
TEST(alltypes.req_enum == MyEnum_Truth);
TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == 2001 && alltypes.rep_int32[0] == 0);
TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == 2002 && alltypes.rep_int64[0] == 0);
TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == 2005 && alltypes.rep_sint32[0] == 0);
TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == 2006 && alltypes.rep_sint64[0] == 0);
TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == 2009 && alltypes.rep_sfixed32[0] == 0);
TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == 2012 && alltypes.rep_sfixed64[0] == 0);
TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0);
TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0);
TEST(alltypes.rep_submsg_count == 5);
TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0);
TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
TEST(alltypes.end == 1099);
return true;

View File

@@ -3,36 +3,65 @@
*/
#include <stdio.h>
#include <string.h>
#include <pb_encode.h>
#include "alltypes.pb.h"
int main()
{
/* Initialize the structure with constants */
AllTypes alltypes = {
1001,
1002,
1003,
1004,
1005,
1006,
true,
1008,
1009,
1010.0f,
1011,
1012,
1013.0,
"1014",
{4, "1015"},
{"1016", 1016},
MyEnum_Truth,
1099
};
AllTypes alltypes = {0};
alltypes.req_int32 = 1001;
alltypes.req_int64 = 1002;
alltypes.req_uint32 = 1003;
alltypes.req_uint64 = 1004;
alltypes.req_sint32 = 1005;
alltypes.req_sint64 = 1006;
alltypes.req_bool = true;
alltypes.req_fixed32 = 1008;
alltypes.req_sfixed32 = 1009;
alltypes.req_float = 1010.0f;
alltypes.req_fixed64 = 1011;
alltypes.req_sfixed64 = 1012;
alltypes.req_double = 1013.0;
strcpy(alltypes.req_string, "1014");
alltypes.req_bytes.size = 4;
memcpy(alltypes.req_bytes.bytes, "1015", 4);
strcpy(alltypes.req_submsg.substuff1, "1016");
alltypes.req_submsg.substuff2 = 1016;
alltypes.req_enum = MyEnum_Truth;
alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = 2001;
alltypes.rep_int64_count = 5; alltypes.rep_int64[4] = 2002;
alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003;
alltypes.rep_uint64_count = 5; alltypes.rep_uint64[4] = 2004;
alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = 2005;
alltypes.rep_sint64_count = 5; alltypes.rep_sint64[4] = 2006;
alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true;
alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008;
alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = 2009;
alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f;
alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64[4] = 2011;
alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64[4] = 2012;
alltypes.rep_double_count = 5; alltypes.rep_double[4] = 2013.0;
alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014");
alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4;
memcpy(alltypes.rep_bytes[4].bytes, "2015", 4);
alltypes.rep_submsg_count = 5;
strcpy(alltypes.rep_submsg[4].substuff1, "2016");
alltypes.rep_submsg[4].substuff2 = 2016;
alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth;
alltypes.end = 1099;
uint8_t buffer[512];
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));