diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2023-10-19 11:43:05 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2023-10-19 11:43:05 +0300 |
commit | 31eb134c3fe886d9cc721e40594899eac78e8d2b (patch) | |
tree | c5c447bb0fb9cd966db01122e86bc7fb7a265577 /tests | |
parent | 15a0ec97eccc63db6334c3c900ad1095d572a81c (diff) |
Add test cases for initializer macro special cases (#806)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/cxx_callback_datatype/SConscript | 1 | ||||
-rw-r--r-- | tests/cxx_callback_datatype/cxx_callback_datatype.cpp | 40 | ||||
-rw-r--r-- | tests/cxx_callback_datatype/message.proto | 2 | ||||
-rw-r--r-- | tests/initializers/SConscript | 6 | ||||
-rw-r--r-- | tests/initializers/initializertest.proto | 10 | ||||
-rw-r--r-- | tests/initializers/test_initializer.c | 8 |
6 files changed, 48 insertions, 19 deletions
diff --git a/tests/cxx_callback_datatype/SConscript b/tests/cxx_callback_datatype/SConscript index ecdab0b..e3021ea 100644 --- a/tests/cxx_callback_datatype/SConscript +++ b/tests/cxx_callback_datatype/SConscript @@ -1,3 +1,4 @@ +# Test wrapping of a C++ class inside struct using callback_datatype option. Import('env') import os diff --git a/tests/cxx_callback_datatype/cxx_callback_datatype.cpp b/tests/cxx_callback_datatype/cxx_callback_datatype.cpp index f95b4f6..2b06d37 100644 --- a/tests/cxx_callback_datatype/cxx_callback_datatype.cpp +++ b/tests/cxx_callback_datatype/cxx_callback_datatype.cpp @@ -1,3 +1,4 @@ +// Test wrapping of a C++ class inside struct using callback_datatype option. #include "message.pb.hpp" #include <pb_encode.h> @@ -7,9 +8,10 @@ #include <cstdio> // See tests/alltypes_callback, tests/oneoff_callback and examples/network_server for more... -bool TestMessage_submessages_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field) +bool TestMessage_values_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field) { if (ostream != NULL) { + // Encoding callback, serialize items from vector const std::vector<int> &v = *(const std::vector<int> *)field->pData; for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) { if (!pb_encode_tag_for_field(ostream, field)) { @@ -22,6 +24,7 @@ bool TestMessage_submessages_callback(pb_istream_t *istream, pb_ostream_t *ostre } } } else if (istream != NULL) { + // Decoding callback, add items to vector std::vector<int> &v = *(std::vector<int> *)field->pData; SubMessage tmp; if (!pb_decode(istream, SubMessage_fields, &tmp)) { @@ -35,26 +38,27 @@ bool TestMessage_submessages_callback(pb_istream_t *istream, pb_ostream_t *ostre extern "C" bool TestMessage_callback(pb_istream_t *istream, pb_ostream_t *ostream, const pb_field_t *field) { - if (field->tag == TestMessage_submessages_tag) { - return TestMessage_submessages_callback(istream, ostream, field); + if (field->tag == TestMessage_values_tag) { + return TestMessage_values_callback(istream, ostream, field); } return true; } extern "C" int main() { - std::vector<int> source; - source.push_back(5); - source.push_back(4); - source.push_back(3); - source.push_back(2); - source.push_back(1); + TestMessage source = TestMessage_init_zero; // Not strictly necessary to initialize, just using it to test the initializer. + source.values.push_back(5); + source.values.push_back(4); + source.values.push_back(3); + source.values.push_back(2); + source.values.push_back(1); std::vector<uint8_t> serialized; - pb_ostream_t sizestream = {0}; - pb_encode(&sizestream, TestMessage_fields, &source); - serialized.resize(sizestream.bytes_written); + size_t size = 0; + pb_get_encoded_size(&size, TestMessage_fields, &source); + serialized.resize(size); + pb_ostream_t outstream = pb_ostream_from_buffer(&serialized.front(), serialized.size()); if (!pb_encode(&outstream, TestMessage_fields, &source)) { fprintf(stderr, "Failed to encode: %s\n", PB_GET_ERROR(&outstream)); @@ -62,16 +66,16 @@ int main() { } - std::vector<int> destination; + TestMessage destination; pb_istream_t instream = pb_istream_from_buffer(&serialized.front(), outstream.bytes_written); if (!pb_decode(&instream, TestMessage_fields, &destination)) { fprintf(stderr, "Failed to decode: %s\n", PB_GET_ERROR(&instream)); return 2; } - if (source != destination) { + if (source.values != destination.values) { fprintf(stderr, "Result does not match\n"); - fprintf(stderr, "source(%d): ", (int)source.size()); - for (std::vector<int>::iterator i = source.begin(); i != source.end(); ++i) + fprintf(stderr, "source(%d): ", (int)source.values.size()); + for (std::vector<int>::iterator i = source.values.begin(); i != source.values.end(); ++i) { fprintf(stderr, "%d, ", *i); } @@ -79,8 +83,8 @@ int main() { for (unsigned i = 0; i != std::min(serialized.size(), outstream.bytes_written); ++i) { fprintf(stderr, "0x%02x ", serialized[i]); } - fprintf(stderr, "\ndestination(%d): ", (int)destination.size()); - for (std::vector<int>::iterator i = destination.begin(); i != destination.end(); ++i) + fprintf(stderr, "\ndestination(%d): ", (int)destination.values.size()); + for (std::vector<int>::iterator i = destination.values.begin(); i != destination.values.end(); ++i) { fprintf(stderr, "%d, ", *i); } diff --git a/tests/cxx_callback_datatype/message.proto b/tests/cxx_callback_datatype/message.proto index 605f289..092d35d 100644 --- a/tests/cxx_callback_datatype/message.proto +++ b/tests/cxx_callback_datatype/message.proto @@ -10,5 +10,5 @@ message SubMessage { message TestMessage { // Instead of std::vector<SubMessage> callback handles wrapping/unwrapping of the int. - repeated SubMessage submessages = 1 [(nanopb).callback_datatype = "std::vector<int>"]; + repeated SubMessage values = 1 [(nanopb).callback_datatype = "std::vector<int>"]; } diff --git a/tests/initializers/SConscript b/tests/initializers/SConscript new file mode 100644 index 0000000..059af39 --- /dev/null +++ b/tests/initializers/SConscript @@ -0,0 +1,6 @@ +# Test initializers when overriding callback datatype + +Import('env') + +env.NanopbProto("initializertest.proto") +env.Object("test_initializer.c") diff --git a/tests/initializers/initializertest.proto b/tests/initializers/initializertest.proto new file mode 100644 index 0000000..2d192f9 --- /dev/null +++ b/tests/initializers/initializertest.proto @@ -0,0 +1,10 @@ +syntax = "proto2"; +import "nanopb.proto"; + +message TestMessage { + required int32 field1 = 1; + repeated string field2 = 2 [(nanopb).callback_datatype = "struct MyType*"]; + repeated string field3 = 3 [(nanopb).callback_datatype = "struct { int a; int b; }"]; + repeated string field4 = 4 [(nanopb).callback_datatype = "struct { struct { int x; } y; }", + (nanopb).initializer = "{{5}}"]; +} diff --git a/tests/initializers/test_initializer.c b/tests/initializers/test_initializer.c new file mode 100644 index 0000000..930e5e8 --- /dev/null +++ b/tests/initializers/test_initializer.c @@ -0,0 +1,8 @@ +#include "initializertest.pb.h" + +int main() +{ + TestMessage msg1 = TestMessage_init_zero; + TestMessage msg2 = TestMessage_init_default; + return msg1.field1 + msg2.field1; /* Mark variables as used for compiler */ +} |