Code reduction optimization by sharing common field descriptor information for numeric field types

This commit is contained in:
Daniel Kan
2012-02-24 13:46:36 -08:00
parent 0cdc623050
commit 34aa2031c4
17 changed files with 883 additions and 279 deletions

View File

@@ -1,12 +1,23 @@
CFLAGS=-ansi -Wall -Werror -I .. -g -O0 --coverage
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
TESTS=test_decode1 test_encode1 decode_unittests encode_unittests
DEPS=../pb_decode.h ../pb_encode.h ../pb.h ../pb_field.h person.pb.h callbacks.pb.h unittests.h unittestproto.pb.h alltypes.pb.h aligntype.pb.h
TESTS=test_decode1 test_encode1 test_encode2 test_decode2 test_encode3 test_decode3 test_encode4 test_decode4 decode_unittests encode_unittests test_encode_callbacks test_decode_callbacks
CC_VER := $(shell gcc --version | grep gcc)
ifneq "$(CC_VER)" ""
CFLAGS += -m32
LDFLAGS += -m32
endif
ifndef PB_PATH
PBPATHOPT=-I/usr/include -I/usr/local/include
else
PBPATHOPT=-I$(PB_PATH)
endif
all: breakpoints $(TESTS) run_unittests
clean:
rm -f $(TESTS) person.pb* alltypes.pb* *.o *.gcda *.gcno
rm -f breakpoints $(TESTS) *.pb *.pb.c *.pb.h *.o *.gcda *.gcno
%.o: %.c
%.o: %.c $(DEPS)
@@ -16,20 +27,24 @@ pb_encode.o: ../pb_encode.c $(DEPS)
$(CC) $(CFLAGS) -c -o $@ $<
pb_decode.o: ../pb_decode.c $(DEPS)
$(CC) $(CFLAGS) -c -o $@ $<
pb_field.o: ../pb_field.c $(DEPS)
$(CC) $(CFLAGS) -c -o $@ $<
test_decode1: test_decode1.o pb_decode.o person.pb.o
test_decode2: test_decode2.o pb_decode.o person.pb.o
test_decode3: test_decode3.o pb_decode.o alltypes.pb.o
test_encode1: test_encode1.o pb_encode.o person.pb.o
test_encode2: test_encode2.o pb_encode.o person.pb.o
test_encode3: test_encode3.o pb_encode.o alltypes.pb.o
test_decode_callbacks: test_decode_callbacks.o pb_decode.o callbacks.pb.o
test_encode_callbacks: test_encode_callbacks.o pb_encode.o callbacks.pb.o
decode_unittests: decode_unittests.o pb_decode.o unittestproto.pb.o
encode_unittests: encode_unittests.o pb_encode.o unittestproto.pb.o
test_decode1: test_decode1.o pb_decode.o pb_field.o person.pb.o
test_decode2: test_decode2.o pb_decode.o pb_field.o person.pb.o
test_decode3: test_decode3.o pb_decode.o pb_field.o alltypes.pb.o
test_decode4: test_decode4.o pb_decode.o pb_field.o aligntype.pb.o
test_encode1: test_encode1.o pb_encode.o pb_field.o person.pb.o
test_encode2: test_encode2.o pb_encode.o pb_field.o person.pb.o
test_encode3: test_encode3.o pb_encode.o pb_field.o alltypes.pb.o
test_encode4: test_encode4.o pb_encode.o pb_field.o aligntype.pb.o
test_decode_callbacks: test_decode_callbacks.o pb_decode.o pb_field.o callbacks.pb.o
test_encode_callbacks: test_encode_callbacks.o pb_encode.o pb_field.o callbacks.pb.o
decode_unittests: decode_unittests.o pb_decode.o pb_field.o unittestproto.pb.o
encode_unittests: encode_unittests.o pb_encode.o pb_field.o unittestproto.pb.o
%.pb: %.proto
protoc -I. -I../generator -I/usr/include -o$@ $<
protoc -I. -I../generator $(PBPATHOPT) -o$@ $<
%.pb.c %.pb.h: %.pb ../generator/nanopb_generator.py
python ../generator/nanopb_generator.py $<
@@ -41,26 +56,29 @@ coverage: run_unittests
gcov pb_encode.gcda
gcov pb_decode.gcda
run_unittests: decode_unittests encode_unittests test_encode1 test_encode2 test_encode3 test_decode1 test_decode2 test_decode3 test_encode_callbacks test_decode_callbacks
run_unittests: $(TESTS)
rm -f *.gcda
./decode_unittests > /dev/null
./encode_unittests > /dev/null
[ "`./test_encode1 | ./test_decode1`" = \
"`./test_encode1 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
"`./test_encode1 | protoc --decode=Person -I. -I../generator $(PBPATHOPT) person.proto`" ]
[ "`./test_encode2 | ./test_decode1`" = \
"`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
"`./test_encode2 | protoc --decode=Person -I. -I../generator $(PBPATHOPT) person.proto`" ]
[ "`./test_encode2 | ./test_decode2`" = \
"`./test_encode2 | protoc --decode=Person -I. -I../generator -I/usr/include person.proto`" ]
"`./test_encode2 | protoc --decode=Person -I. -I../generator $(PBPATHOPT) person.proto`" ]
[ "`./test_encode_callbacks | ./test_decode_callbacks`" = \
"`./test_encode_callbacks | protoc --decode=TestMessage callbacks.proto`" ]
./test_encode3 | ./test_decode3
./test_encode3 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null
./test_encode3 | protoc --decode=AllTypes -I. -I../generator $(PBPATHOPT) alltypes.proto >/dev/null
./test_encode4 | ./test_decode4
./test_encode4 | protoc --decode=AlignTypes -I. -I../generator $(PBPATHOPT) aligntype.proto >/dev/null
run_fuzztest: test_decode2
bash -c 'I=1; while true; do cat /dev/urandom | ./test_decode2 > /dev/null; I=$$(($$I+1)); echo -en "\r$$I"; done'

80
tests/aligntype.proto Normal file
View File

@@ -0,0 +1,80 @@
import "nanopb.proto";
message SubMessage {
required string substuff1 = 1 [(nanopb).max_size = 16];
required int32 substuff2 = 2;
}
enum MyEnum {
First = 1;
Second = 2;
Truth = 42;
}
message AlignTypes {
required bool req_bool = 1;
required int32 req_int32 = 2;
required SubMessage req_submsg = 3;
required int64 req_int64 = 4;
required string req_string = 5 [(nanopb).max_size = 16];
required MyEnum req_enum = 6;
required bytes req_bytes = 7 [(nanopb).max_size = 16];
required sint32 req_sint32 = 8;
required bool req_bool_2 = 9;
required sint64 req_sint64 = 10;
required bool req_bool_3 = 11;
required fixed32 req_fixed32 = 12;
required bool req_bool_4 = 13;
required fixed64 req_fixed64 = 14;
required bool req_bool_5 = 15;
required float req_float = 16;
required bool req_bool_6 = 17;
required double req_double = 18;
optional bool opt_bool = 19;
optional int32 opt_int32 = 20;
optional SubMessage opt_submsg = 21;
optional int64 opt_int64 = 22;
optional string opt_string = 23 [(nanopb).max_size = 16];
optional MyEnum opt_enum = 24;
optional bytes opt_bytes = 25 [(nanopb).max_size = 16];
optional sint32 opt_sint32 = 26;
optional bool opt_bool_2 = 27;
optional sint64 opt_sint64 = 28;
optional bool opt_bool_3 = 29;
optional fixed32 opt_fixed32 = 30;
optional bool opt_bool_4 = 31;
optional fixed64 opt_fixed64 = 32;
optional bool opt_bool_5 = 33;
optional float opt_float = 34;
optional bool opt_bool_6 = 35;
optional double opt_double = 36;
required bool req_bool_aligned = 37;
required int32 req_int32_aligned = 38;
required int64 req_int64_aligned = 39;
required MyEnum req_enum_aligned = 40;
required sint32 req_sint32_aligned = 41;
required sint64 req_sint64_aligned = 42;
required fixed32 req_fixed32_aligned = 43;
required fixed64 req_fixed64_aligned = 44;
required float req_float_aligned = 45;
required double req_double_aligned = 46;
optional bool opt_bool_aligned = 47;
optional int32 opt_int32_aligned = 48;
optional int64 opt_int64_aligned = 49;
optional MyEnum opt_enum_aligned = 50;
optional sint32 opt_sint32_aligned = 51;
optional sint64 opt_sint64_aligned = 52;
optional fixed32 opt_fixed32_aligned = 53;
optional fixed64 opt_fixed64_aligned = 54;
optional float opt_float_aligned = 55;
optional double opt_double_aligned = 56;
// 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;
}

139
tests/test_decode4.c Normal file
View File

@@ -0,0 +1,139 @@
/* Tests the decoding of all types. Currently only in the 'required' variety.
* This is the counterpart of test_encode3.
* Run e.g. ./test_encode3 | ./test_decode3
*/
#include <stdio.h>
#include <string.h>
#include <pb_decode.h>
#include "aligntype.pb.h"
#define TEST(x) if (!(x)) { \
printf("Test " #x " failed.\n"); \
return false; \
}
/* This function is called once from main(), it handles
the decoding and checks the fields. */
bool check_aligntypes(pb_istream_t *stream)
{
AlignTypes aligntypes = {};
if (!pb_decode(stream, AlignTypes_fields, &aligntypes))
return false;
TEST(aligntypes.req_bool == true);
TEST(aligntypes.req_int32 == 1001);
TEST(strcmp(aligntypes.req_submsg.substuff1, "1002") == 0);
TEST(aligntypes.req_submsg.substuff2 == 1002);
TEST(aligntypes.req_int64 == 1003);
TEST(strcmp(aligntypes.req_string, "1004") == 0);
TEST(aligntypes.req_enum == MyEnum_Truth);
TEST(aligntypes.req_bytes.size == 4);
TEST(memcmp(aligntypes.req_bytes.bytes, "1005", 4) == 0);
TEST(aligntypes.req_sint32 == 1006);
TEST(aligntypes.req_bool_2 == true);
TEST(aligntypes.req_sint64 == 1007);
TEST(aligntypes.req_bool_3 == true);
TEST(aligntypes.req_fixed32 == 1008);
TEST(aligntypes.req_bool_4 == true);
TEST(aligntypes.req_fixed64 == 1009);
TEST(aligntypes.req_bool_5 == true);
TEST(aligntypes.req_float == 1010.0f);
TEST(aligntypes.req_bool_5 == true);
TEST(aligntypes.req_double == 1011.0f);
TEST(aligntypes.has_opt_bool == true);
TEST(aligntypes.opt_bool == true);
TEST(aligntypes.has_opt_int32 == true);
TEST(aligntypes.opt_int32 == 1001);
TEST(aligntypes.has_opt_submsg == true);
TEST(strcmp(aligntypes.opt_submsg.substuff1, "1002") == 0);
TEST(aligntypes.opt_submsg.substuff2 == 1002);
TEST(aligntypes.has_opt_int64 == true);
TEST(aligntypes.opt_int64 == 1003);
TEST(aligntypes.has_opt_string == true);
TEST(strcmp(aligntypes.opt_string, "1004") == 0);
TEST(aligntypes.has_opt_enum == true);
TEST(aligntypes.opt_enum == MyEnum_Truth);
TEST(aligntypes.has_opt_bytes == true);
TEST(aligntypes.opt_bytes.size == 4);
TEST(memcmp(aligntypes.opt_bytes.bytes, "1005", 4) == 0);
TEST(aligntypes.has_opt_sint32 == true);
TEST(aligntypes.opt_sint32 == 1006);
TEST(aligntypes.has_opt_bool_2 == true);
TEST(aligntypes.opt_bool_2 == true);
TEST(aligntypes.has_opt_sint64 == true);
TEST(aligntypes.opt_sint64 == 1007);
TEST(aligntypes.has_opt_bool_3 == true);
TEST(aligntypes.opt_bool_3 == true);
TEST(aligntypes.has_opt_fixed32 == true);
TEST(aligntypes.opt_fixed32 == 1008);
TEST(aligntypes.has_opt_bool_4 == true);
TEST(aligntypes.opt_bool_4 == true);
TEST(aligntypes.has_opt_fixed64 == true);
TEST(aligntypes.opt_fixed64 == 1009);
TEST(aligntypes.has_opt_bool_5 == true);
TEST(aligntypes.opt_bool_5 == true);
TEST(aligntypes.has_opt_float == true);
TEST(aligntypes.opt_float == 1010.0f);
TEST(aligntypes.has_opt_bool_6 == true);
TEST(aligntypes.opt_bool_6 == true);
TEST(aligntypes.has_opt_double == true);
TEST(aligntypes.opt_double == 1011.0f);
TEST(aligntypes.req_bool_aligned == true);
TEST(aligntypes.req_int32_aligned == 1001);
TEST(aligntypes.req_int64_aligned == 1003);
TEST(aligntypes.req_enum_aligned == MyEnum_Truth);
TEST(aligntypes.req_sint32_aligned == 1006);
TEST(aligntypes.req_sint64_aligned == 1007);
TEST(aligntypes.req_fixed32_aligned == 1008);
TEST(aligntypes.req_fixed64_aligned == 1009);
TEST(aligntypes.req_float_aligned == 1010.0f);
TEST(aligntypes.req_double_aligned == 1011.0f);
TEST(aligntypes.has_opt_bool_aligned == true);
TEST(aligntypes.opt_bool_aligned == true);
TEST(aligntypes.has_opt_int32_aligned == true);
TEST(aligntypes.opt_int32_aligned == 1001);
TEST(aligntypes.has_opt_int64_aligned == true);
TEST(aligntypes.opt_int64_aligned == 1003);
TEST(aligntypes.has_opt_enum_aligned == true);
TEST(aligntypes.opt_enum_aligned == MyEnum_Truth);
TEST(aligntypes.has_opt_sint32_aligned == true);
TEST(aligntypes.opt_sint32_aligned == 1006);
TEST(aligntypes.has_opt_sint64_aligned == true);
TEST(aligntypes.opt_sint64_aligned == 1007);
TEST(aligntypes.has_opt_fixed32_aligned == true);
TEST(aligntypes.opt_fixed32_aligned == 1008);
TEST(aligntypes.has_opt_fixed64_aligned == true);
TEST(aligntypes.opt_fixed64_aligned == 1009);
TEST(aligntypes.has_opt_float_aligned == true);
TEST(aligntypes.opt_float_aligned == 1010.0f);
TEST(aligntypes.has_opt_double_aligned == true);
TEST(aligntypes.opt_double_aligned == 1011.0f);
TEST(aligntypes.end == 1099);
return true;
}
int main()
{
/* Read the data into buffer */
uint8_t buffer[512];
size_t 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);
/* Decode and print out the stuff */
if (!check_aligntypes(&stream))
{
printf("Parsing failed.\n");
return 1;
} else {
return 0;
}
}

117
tests/test_encode4.c Normal file
View File

@@ -0,0 +1,117 @@
/* Attempts to test all the datatypes supported by ProtoBuf.
* Currently only tests the 'required' variety.
*/
#include <stdio.h>
#include <pb_encode.h>
#include "aligntype.pb.h"
int main()
{
/* Initialize the structure with constants */
AlignTypes aligntypes = {
true,
1001,
{"1002", 1002},
1003,
"1004",
MyEnum_Truth,
{4, "1005"},
1006,
true,
1007,
true,
1008,
true,
1009,
true,
1010.0f,
true,
1011.0f,
true,
true,
true,
1001,
true,
{"1002", 1002},
true,
1003,
true,
"1004",
true,
MyEnum_Truth,
true,
{4, "1005"},
true,
1006,
true,
true,
true,
1007,
true,
true,
true,
1008,
true,
true,
true,
1009,
true,
true,
true,
1010.0f,
true,
true,
true,
1011.0f,
true,
1001,
1003,
MyEnum_Truth,
1006,
1007,
1008,
1009,
1010.0f,
1011.0f,
true,
true,
true,
1001,
true,
1003,
true,
MyEnum_Truth,
true,
1006,
true,
1007,
true,
1008,
true,
1009,
true,
1010.0f,
true,
1011.0f,
1099
};
uint8_t buffer[512];
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
/* Now encode it and check if we succeeded. */
if (pb_encode(&stream, AlignTypes_fields, &aligntypes))
{
fwrite(buffer, 1, stream.bytes_written, stdout);
return 0; /* Success */
}
else
{
return 1; /* Failure */
}
}

View File

@@ -1,3 +0,0 @@
Test Person7foobar@foobar.com"
555-12345678