summaryrefslogtreecommitdiffstats
path: root/CAN-binder/libs/nanopb/tests/regression/issue_166
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-05-02 17:51:53 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-05-02 17:51:53 +0200
commit3102ec9ce009d0f28355c5b7df9c5bd5013e6e75 (patch)
tree80a1ea19ff06cc9308b236a0d8d6358d21dd0381 /CAN-binder/libs/nanopb/tests/regression/issue_166
parent12e680a3c97a2750c657a8c561a79706f3689149 (diff)
parent278ffb890e3d8722e4c7d824baaf221a1e375fc4 (diff)
Add 'CAN-binder/libs/nanopb/' from commit '278ffb890e3d8722e4c7d824baaf221a1e375fc4'
git-subtree-dir: CAN-binder/libs/nanopb git-subtree-mainline: 12e680a3c97a2750c657a8c561a79706f3689149 git-subtree-split: 278ffb890e3d8722e4c7d824baaf221a1e375fc4
Diffstat (limited to 'CAN-binder/libs/nanopb/tests/regression/issue_166')
-rw-r--r--CAN-binder/libs/nanopb/tests/regression/issue_166/SConscript13
-rw-r--r--CAN-binder/libs/nanopb/tests/regression/issue_166/enum_encoded_size.c43
-rw-r--r--CAN-binder/libs/nanopb/tests/regression/issue_166/enums.proto18
3 files changed, 74 insertions, 0 deletions
diff --git a/CAN-binder/libs/nanopb/tests/regression/issue_166/SConscript b/CAN-binder/libs/nanopb/tests/regression/issue_166/SConscript
new file mode 100644
index 00000000..c50b9193
--- /dev/null
+++ b/CAN-binder/libs/nanopb/tests/regression/issue_166/SConscript
@@ -0,0 +1,13 @@
+# Verify that the maximum encoded size is calculated properly
+# for enums.
+
+Import('env')
+
+env.NanopbProto('enums')
+
+p = env.Program(["enum_encoded_size.c",
+ "enums.pb.c",
+ "$COMMON/pb_encode.o",
+ "$COMMON/pb_common.o"])
+env.RunTest(p)
+
diff --git a/CAN-binder/libs/nanopb/tests/regression/issue_166/enum_encoded_size.c b/CAN-binder/libs/nanopb/tests/regression/issue_166/enum_encoded_size.c
new file mode 100644
index 00000000..84e1c7de
--- /dev/null
+++ b/CAN-binder/libs/nanopb/tests/regression/issue_166/enum_encoded_size.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <string.h>
+#include <pb_encode.h>
+#include "unittests.h"
+#include "enums.pb.h"
+
+int main()
+{
+ int status = 0;
+
+ uint8_t buf[256];
+ SignedMsg msg1;
+ UnsignedMsg msg2;
+ pb_ostream_t s;
+
+ {
+ COMMENT("Test negative value of signed enum");
+ /* Negative value should take up the maximum size */
+ msg1.value = SignedEnum_SE_MIN;
+ s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, SignedMsg_fields, &msg1));
+ TEST(s.bytes_written == SignedMsg_size);
+
+ COMMENT("Test positive value of signed enum");
+ /* Positive value should be smaller */
+ msg1.value = SignedEnum_SE_MAX;
+ s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, SignedMsg_fields, &msg1));
+ TEST(s.bytes_written < SignedMsg_size);
+ }
+
+ {
+ COMMENT("Test positive value of unsigned enum");
+ /* This should take up the maximum size */
+ msg2.value = UnsignedEnum_UE_MAX;
+ s = pb_ostream_from_buffer(buf, sizeof(buf));
+ TEST(pb_encode(&s, UnsignedMsg_fields, &msg2));
+ TEST(s.bytes_written == UnsignedMsg_size);
+ }
+
+ return status;
+}
+
diff --git a/CAN-binder/libs/nanopb/tests/regression/issue_166/enums.proto b/CAN-binder/libs/nanopb/tests/regression/issue_166/enums.proto
new file mode 100644
index 00000000..36948044
--- /dev/null
+++ b/CAN-binder/libs/nanopb/tests/regression/issue_166/enums.proto
@@ -0,0 +1,18 @@
+syntax = "proto2";
+
+enum SignedEnum {
+ SE_MIN = -1;
+ SE_MAX = 255;
+}
+
+enum UnsignedEnum {
+ UE_MAX = 65536;
+}
+
+message SignedMsg {
+ required SignedEnum value = 1;
+}
+
+message UnsignedMsg {
+ required UnsignedEnum value = 1;
+}