summaryrefslogtreecommitdiffstats
path: root/CAN-binder/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c
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/enum_sizes/enumsizes_unittests.c
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/enum_sizes/enumsizes_unittests.c')
-rw-r--r--CAN-binder/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/CAN-binder/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c b/CAN-binder/libs/nanopb/tests/enum_sizes/enumsizes_unittests.c
new file mode 100644
index 0000000..5606895
--- /dev/null
+++ b/CAN-binder/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;
+}