summaryrefslogtreecommitdiffstats
path: root/libs/nanopb/tests/enum_sizes
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-06-20 10:24:05 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-06-20 10:24:05 +0000
commit32e25cbca210a359b09768537b6f443fe90a3070 (patch)
tree3309794c15d8a8f8e9c1c08cad072ee1378813ba /libs/nanopb/tests/enum_sizes
parent76c43dec62b2e21cd6446360c00d4fe6b437533f (diff)
Separation Generator to a dedicated repo
Change-Id: Id94831651c3266861435272a6e36c7884bef2c45 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'libs/nanopb/tests/enum_sizes')
-rw-r--r--libs/nanopb/tests/enum_sizes/SConscript12
-rw-r--r--libs/nanopb/tests/enum_sizes/enumsizes.proto86
-rw-r--r--libs/nanopb/tests/enum_sizes/enumsizes_unittests.c72
3 files changed, 170 insertions, 0 deletions
diff --git a/libs/nanopb/tests/enum_sizes/SConscript b/libs/nanopb/tests/enum_sizes/SConscript
new file mode 100644
index 00000000..048592ed
--- /dev/null
+++ b/libs/nanopb/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/libs/nanopb/tests/enum_sizes/enumsizes.proto b/libs/nanopb/tests/enum_sizes/enumsizes.proto
new file mode 100644
index 00000000..a85d4160
--- /dev/null
+++ b/libs/nanopb/tests/enum_sizes/enumsizes.proto
@@ -0,0 +1,86 @@
+/* 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).
+ */
+
+syntax = "proto2";
+
+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/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c b/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c
new file mode 100644
index 00000000..5606895a
--- /dev/null
+++ b/libs/nanopb/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;
+}