aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2015-09-12 12:16:58 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2015-09-12 12:16:58 +0300
commit1582038e37771ade214f0627c3ecbf6e1ba69946 (patch)
tree5c305c99771af087f36bb9feb47b041c756f5def /tests
parent612a51c608504f59fcd86c74eedac2903f088a0a (diff)
Add testcase for issue #164
Diffstat (limited to 'tests')
-rw-r--r--tests/enum_sizes/SConscript12
-rw-r--r--tests/enum_sizes/enumsizes.proto84
-rw-r--r--tests/enum_sizes/enumsizes_unittests.c72
3 files changed, 168 insertions, 0 deletions
diff --git a/tests/enum_sizes/SConscript b/tests/enum_sizes/SConscript
new file mode 100644
index 00000000..048592ed
--- /dev/null
+++ b/tests/enum_sizes/SConscript
@@ -0,0 +1,12 @@
+# Test that different sizes of enum fields are properly encoded and decoded.
+
+Import('env')
+
+env.NanopbProto('enumsizes')
+
+p = env.Program(["enumsizes_unittests.c",
+ "enumsizes.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_decode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
diff --git a/tests/enum_sizes/enumsizes.proto b/tests/enum_sizes/enumsizes.proto
new file mode 100644
index 00000000..f9ab0b7f
--- /dev/null
+++ b/tests/enum_sizes/enumsizes.proto
@@ -0,0 +1,84 @@
+/* Test handling of enums with different value ranges.
+ * Depending on compiler and the packed_enum setting, the datatypes
+ * for enums can be either signed or unsigned. In past this has caused
+ * a bit of a problem for the encoder/decoder (issue #164).
+ */
+
+import 'nanopb.proto';
+
+option (nanopb_fileopt).long_names = false;
+
+enum UnpackedUint8 {
+ option (nanopb_enumopt).packed_enum = false;
+ UU8_MIN = 0;
+ UU8_MAX = 255;
+}
+
+enum PackedUint8 {
+ option (nanopb_enumopt).packed_enum = true;
+ PU8_MIN = 0;
+ PU8_MAX = 255;
+}
+
+enum UnpackedInt8 {
+ option (nanopb_enumopt).packed_enum = false;
+ UI8_MIN = -128;
+ UI8_MAX = 127;
+}
+
+enum PackedInt8 {
+ option (nanopb_enumopt).packed_enum = true;
+ PI8_MIN = -128;
+ PI8_MAX = 127;
+}
+
+enum UnpackedUint16 {
+ option (nanopb_enumopt).packed_enum = false;
+ UU16_MIN = 0;
+ UU16_MAX = 65535;
+}
+
+enum PackedUint16 {
+ option (nanopb_enumopt).packed_enum = true;
+ PU16_MIN = 0;
+ PU16_MAX = 65535;
+}
+
+enum UnpackedInt16 {
+ option (nanopb_enumopt).packed_enum = false;
+ UI16_MIN = -32768;
+ UI16_MAX = 32767;
+}
+
+enum PackedInt16 {
+ option (nanopb_enumopt).packed_enum = true;
+ PI16_MIN = -32768;
+ PI16_MAX = 32767;
+}
+
+/* Protobuf supports enums up to 32 bits.
+ * The 32 bit case is covered by HugeEnum in the alltypes test.
+ */
+
+message PackedEnums {
+ required PackedUint8 u8_min = 1;
+ required PackedUint8 u8_max = 2;
+ required PackedInt8 i8_min = 3;
+ required PackedInt8 i8_max = 4;
+ required PackedUint16 u16_min = 5;
+ required PackedUint16 u16_max = 6;
+ required PackedInt16 i16_min = 7;
+ required PackedInt16 i16_max = 8;
+}
+
+message UnpackedEnums {
+ required UnpackedUint8 u8_min = 1;
+ required UnpackedUint8 u8_max = 2;
+ required UnpackedInt8 i8_min = 3;
+ required UnpackedInt8 i8_max = 4;
+ required UnpackedUint16 u16_min = 5;
+ required UnpackedUint16 u16_max = 6;
+ required UnpackedInt16 i16_min = 7;
+ required UnpackedInt16 i16_max = 8;
+}
+
diff --git a/tests/enum_sizes/enumsizes_unittests.c b/tests/enum_sizes/enumsizes_unittests.c
new file mode 100644
index 00000000..5606895a
--- /dev/null
+++ b/tests/enum_sizes/enumsizes_unittests.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <string.h>
+#include <pb_decode.h>
+#include <pb_encode.h>
+#include "unittests.h"
+#include "enumsizes.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ UnpackedEnums msg1 = {
+ UU8_MIN, UU8_MAX,
+ UI8_MIN, UI8_MAX,
+ UU16_MIN, UU16_MAX,
+ UI16_MIN, UI16_MAX,
+ };
+
+ PackedEnums msg2;
+ UnpackedEnums msg3;
+ uint8_t buf[256];
+ size_t msgsize;
+
+ COMMENT("Step 1: unpacked enums -> protobuf");
+ {
+ pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, UnpackedEnums_fields, &msg1));
+ msgsize = s.bytes_written;
+ }
+
+ COMMENT("Step 2: protobuf -> packed enums");
+ {
+ pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
+ TEST(pb_decode(&s, PackedEnums_fields, &msg2));
+
+ TEST(msg1.u8_min == (int)msg2.u8_min);
+ TEST(msg1.u8_max == (int)msg2.u8_max);
+ TEST(msg1.i8_min == (int)msg2.i8_min);
+ TEST(msg1.i8_max == (int)msg2.i8_max);
+ TEST(msg1.u16_min == (int)msg2.u16_min);
+ TEST(msg1.u16_max == (int)msg2.u16_max);
+ TEST(msg1.i16_min == (int)msg2.i16_min);
+ TEST(msg1.i16_max == (int)msg2.i16_max);
+ }
+
+ COMMENT("Step 3: packed enums -> protobuf");
+ {
+ pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, PackedEnums_fields, &msg2));
+ msgsize = s.bytes_written;
+ }
+
+ COMMENT("Step 4: protobuf -> unpacked enums");
+ {
+ pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
+ TEST(pb_decode(&s, UnpackedEnums_fields, &msg3));
+
+ TEST(msg1.u8_min == (int)msg3.u8_min);
+ TEST(msg1.u8_max == (int)msg3.u8_max);
+ TEST(msg1.i8_min == (int)msg3.i8_min);
+ TEST(msg1.i8_max == (int)msg3.i8_max);
+ TEST(msg1.u16_min == (int)msg2.u16_min);
+ TEST(msg1.u16_max == (int)msg2.u16_max);
+ TEST(msg1.i16_min == (int)msg2.i16_min);
+ TEST(msg1.i16_max == (int)msg2.i16_max);
+ }
+
+ if (status != 0)
+ fprintf(stdout, "\n\nSome tests FAILED!\n");
+
+ return status;
+}