| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- /*
- * Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
- *
- * This file is part of Embedded Proto.
- *
- * Embedded Proto is open source software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as published
- * by the Free Software Foundation, version 3 of the license.
- *
- * Embedded Proto is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
- *
- * For commercial and closed source application please visit:
- * <https://EmbeddedProto.com/license/>.
- *
- * Embedded AMS B.V.
- * Info:
- * info at EmbeddedProto dot com
- *
- * Postal address:
- * Johan Huizingalaan 763a
- * 1066 VH, Amsterdam
- * the Netherlands
- */
- #include "gtest/gtest.h"
- #include <WireFormatter.h>
- #include <ReadBufferMock.h>
- #include <WriteBufferMock.h>
- #include <cstdint>
- #include <limits>
- // EAMS message definitions
- #include <simple_types.h>
- using ::testing::_;
- using ::testing::InSequence;
- using ::testing::Return;
- using ::testing::SetArgReferee;
- namespace test_EmbeddedAMS_SimpleTypes
- {
- TEST(SimpleTypes, zero)
- {
- InSequence s;
- // See if an empty message results in no data been pushed.
- ::Test_Simple_Types msg;
- Mocks::WriteBufferMock buffer;
- EXPECT_CALL(buffer, push(_)).Times(0);
- EXPECT_CALL(buffer, push(_,_)).Times(0);
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
- EXPECT_EQ(0, msg.serialized_size());
- }
- TEST(SimpleTypes, serialize_one)
- {
- InSequence s;
-
- // Using a protobuf message and the google protobuf implementation test is serialization is
- // correct.
- ::Test_Simple_Types msg;
- Mocks::WriteBufferMock buffer;
- msg.set_a_int32(1);
- msg.set_a_int64(1);
- msg.set_a_uint32(1);
- msg.set_a_uint64(1);
- msg.set_a_sint32(1);
- msg.set_a_sint64(1);
- msg.set_a_bool(true);
- msg.set_a_enum(Test_Enum::ONE);
- msg.set_a_fixed64(1);
- msg.set_a_sfixed64(1);
- msg.set_a_double(1.0);
- msg.set_a_fixed32(1);
- msg.set_a_sfixed32(1);
- msg.set_a_float(1.0F);
- uint8_t expected[] = {0x08, 0x01,
- 0x10, 0x01,
- 0x18, 0x01,
- 0x20, 0x01,
- 0x28, 0x02,
- 0x30, 0x02,
- 0x38, 0x01,
- 0x40, 0x01,
- 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x51, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
- 0x65, 0x01, 0x00, 0x00, 0x00,
- 0x6d, 0x01, 0x00, 0x00, 0x00,
- 0x75, 0x00, 0x00, 0x80, 0x3f};
- for(auto e : expected) {
- EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
- }
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
- EXPECT_EQ(58, msg.serialized_size());
- }
- TEST(SimpleTypes, serialize_max)
- {
- InSequence s;
-
- // Using a protobuf message and the google protobuf implementation test is serialization is
- // correct.
- ::Test_Simple_Types msg;
- Mocks::WriteBufferMock buffer;
- msg.set_a_int32(std::numeric_limits<int32_t>::max());
- msg.set_a_int64(std::numeric_limits<int64_t>::max());
- msg.set_a_uint32(std::numeric_limits<uint32_t>::max());
- msg.set_a_uint64(std::numeric_limits<uint64_t>::max());
- msg.set_a_sint32(std::numeric_limits<int32_t>::max());
- msg.set_a_sint64(std::numeric_limits<int64_t>::max());
- msg.set_a_bool(true);
- msg.set_a_enum(Test_Enum::TWOBILLION);
- msg.set_a_fixed64(std::numeric_limits<uint64_t>::max());
- msg.set_a_sfixed64(std::numeric_limits<int64_t>::max());
- msg.set_a_double(std::numeric_limits<double>::max());
- msg.set_a_fixed32(std::numeric_limits<uint32_t>::max());
- msg.set_a_sfixed32(std::numeric_limits<int32_t>::max());
- msg.set_a_float(std::numeric_limits<float>::max());
- uint8_t expected[] = {0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0x07,
- 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x7F, 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
- 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
- 0x28, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F,
- 0x30, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
- 0x38, 0x01,
- 0x40, 0x80, 0xA8, 0xD6, 0xB9, 0x07,
- 0x49, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x51, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
- 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F,
- 0x65, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x6D, 0xFF, 0xFF, 0xFF, 0x7F,
- 0x75, 0xFF, 0xFF, 0x7F, 0x7F};
-
- for(auto e : expected) {
- EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
- }
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
- EXPECT_EQ(100, msg.serialized_size());
- }
- TEST(SimpleTypes, serialize_min)
- {
- InSequence s;
-
- // Using a protobuf message and the google protobuf implementation test is serialization is
- // correct.
- ::Test_Simple_Types msg;
- Mocks::WriteBufferMock buffer;
- msg.set_a_int32(std::numeric_limits<int32_t>::min());
- msg.set_a_int64(std::numeric_limits<int64_t>::min());
- msg.set_a_uint32(std::numeric_limits<uint32_t>::min());
- msg.set_a_uint64(std::numeric_limits<uint64_t>::min());
- msg.set_a_sint32(std::numeric_limits<int32_t>::min());
- msg.set_a_sint64(std::numeric_limits<int64_t>::min());
- msg.set_a_bool(false);
- msg.set_a_enum(Test_Enum::ZERO);
- msg.set_a_fixed64(std::numeric_limits<uint64_t>::min());
- msg.set_a_sfixed64(std::numeric_limits<int64_t>::min());
- msg.set_a_double(std::numeric_limits<double>::lowest());
- msg.set_a_fixed32(std::numeric_limits<uint32_t>::min());
- msg.set_a_sfixed32(std::numeric_limits<int32_t>::min());
- msg.set_a_float(std::numeric_limits<float>::lowest());
- uint8_t expected[] = {0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
- 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
- 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
- 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
- 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
- 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF,
- 0x6D, 0x00, 0x00, 0x00, 0x80,
- 0x75, 0xFF, 0xFF, 0x7F, 0xFF};
-
- for(auto e : expected) {
- EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
- }
-
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
- EXPECT_EQ(62, msg.serialized_size());
- }
- TEST(SimpleTypes, serialize_smalest_real)
- {
- InSequence s;
-
- // Using a protobuf message and the google protobuf implementation test is serialization is
- // correct.
- ::Test_Simple_Types msg;
- Mocks::WriteBufferMock buffer;
- msg.set_a_double(std::numeric_limits<double>::min());
- msg.set_a_float(std::numeric_limits<float>::min());
- uint8_t expected[] = {0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
- 0x75, 0x00, 0x00, 0x80, 0x00 };
-
- for(auto e : expected) {
- EXPECT_CALL(buffer, push(e)).Times(1).WillOnce(Return(true));
- }
-
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.serialize(buffer));
- EXPECT_EQ(14, msg.serialized_size());
- }
- TEST(SimpleTypes, serialize_fault_buffer_full_varint)
- {
- InSequence s;
-
- // Using a protobuf message and the google protobuf implementation test is serialization is
- // correct.
- ::Test_Simple_Types msg;
- Mocks::WriteBufferMock buffer;
- // Just set some large value.
- msg.set_a_uint32(std::numeric_limits<uint32_t>::max());
- // Allow for some bytes to be serialized
- EXPECT_CALL(buffer, push(_)).Times(1).WillOnce(Return(true));
- EXPECT_CALL(buffer, push(_)).Times(1).WillOnce(Return(true));
- // And then fail
- EXPECT_CALL(buffer, push(_)).Times(2).WillOnce(Return(false));
- EXPECT_EQ(::EmbeddedProto::Error::BUFFER_FULL, msg.serialize(buffer));
- }
- TEST(SimpleTypes, deserialize_zero)
- {
- InSequence s;
- Mocks::ReadBufferMock buffer;
- ::Test_Simple_Types msg;
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
- EXPECT_EQ(0, msg.get_a_int32());
- EXPECT_EQ(0, msg.get_a_int64());
- EXPECT_EQ(0U, msg.get_a_uint32());
- EXPECT_EQ(0U, msg.get_a_uint64());
- EXPECT_EQ(0, msg.get_a_sint32());
- EXPECT_EQ(0, msg.get_a_sint64());
- EXPECT_EQ(false, msg.get_a_bool());
- EXPECT_EQ(Test_Enum::ZERO, msg.get_a_enum());
- EXPECT_EQ(0U, msg.get_a_fixed64());
- EXPECT_EQ(0, msg.get_a_sfixed64());
- EXPECT_EQ(0.0, msg.get_a_double());
- EXPECT_EQ(0U, msg.get_a_fixed32());
- EXPECT_EQ(0, msg.get_a_sfixed32());
- EXPECT_EQ(0.0F, msg.get_a_float());
- }
- TEST(SimpleTypes, deserialize_one)
- {
- InSequence s;
- Mocks::ReadBufferMock buffer;
-
- ON_CALL(buffer, get_size()).WillByDefault(Return(58));
- ::Test_Simple_Types msg;
- uint8_t referee[] = { 0x08, 0x01,
- 0x10, 0x01,
- 0x18, 0x01,
- 0x20, 0x01,
- 0x28, 0x02,
- 0x30, 0x02,
- 0x38, 0x01,
- 0x40, 0x01,
- 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x51, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
- 0x65, 0x01, 0x00, 0x00, 0x00,
- 0x6d, 0x01, 0x00, 0x00, 0x00,
- 0x75, 0x00, 0x00, 0x80, 0x3f};
- for(auto r: referee) {
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
- }
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
- EXPECT_EQ(1, msg.get_a_int32());
- EXPECT_EQ(1, msg.get_a_int64());
- EXPECT_EQ(1U, msg.get_a_uint32());
- EXPECT_EQ(1U, msg.get_a_uint64());
- EXPECT_EQ(1, msg.get_a_sint32());
- EXPECT_EQ(1, msg.get_a_sint64());
- EXPECT_EQ(true, msg.get_a_bool());
- EXPECT_EQ(Test_Enum::ONE, msg.get_a_enum());
- EXPECT_EQ(1U, msg.get_a_fixed64());
- EXPECT_EQ(1, msg.get_a_sfixed64());
- EXPECT_EQ(1.0, msg.get_a_double());
- EXPECT_EQ(1U, msg.get_a_fixed32());
- EXPECT_EQ(1, msg.get_a_sfixed32());
- EXPECT_EQ(1.0F, msg.get_a_float());
- }
- TEST(SimpleTypes, deserialize_max)
- {
- InSequence s;
- Mocks::ReadBufferMock buffer;
-
- ON_CALL(buffer, get_size()).WillByDefault(Return(58));
- ::Test_Simple_Types msg;
- uint8_t referee[] = { 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x07, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x7F, 0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
- 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
- 0x28, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F,
- 0x30, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
- 0x38, 0x01,
- 0x40, 0x80, 0xA8, 0xD6, 0xB9, 0x07,
- 0x49, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x51, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,
- 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F,
- 0x65, 0xFF, 0xFF, 0xFF, 0xFF,
- 0x6D, 0xFF, 0xFF, 0xFF, 0x7F,
- 0x75, 0xFF, 0xFF, 0x7F, 0x7F};
- for(auto r: referee) {
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
- }
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
- EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_a_int32());
- EXPECT_EQ(std::numeric_limits<int64_t>::max(), msg.get_a_int64());
- EXPECT_EQ(std::numeric_limits<uint32_t>::max(), msg.get_a_uint32());
- EXPECT_EQ(std::numeric_limits<uint64_t>::max(), msg.get_a_uint64());
- EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_a_sint32());
- EXPECT_EQ(std::numeric_limits<int64_t>::max(), msg.get_a_sint64());
- EXPECT_EQ(true, msg.get_a_bool());
- EXPECT_EQ(Test_Enum::TWOBILLION, msg.get_a_enum());
- EXPECT_EQ(std::numeric_limits<uint64_t>::max(), msg.get_a_fixed64());
- EXPECT_EQ(std::numeric_limits<int64_t>::max(), msg.get_a_sfixed64());
- EXPECT_EQ(std::numeric_limits<double>::max(), msg.get_a_double());
- EXPECT_EQ(std::numeric_limits<uint32_t>::max(), msg.get_a_fixed32());
- EXPECT_EQ(std::numeric_limits<int32_t>::max(), msg.get_a_sfixed32());
- EXPECT_EQ(std::numeric_limits<float>::max(), msg.get_a_float());
- }
- TEST(SimpleTypes, deserialize_min)
- {
- InSequence s;
- Mocks::ReadBufferMock buffer;
-
- ON_CALL(buffer, get_size()).WillByDefault(Return(58));
- ::Test_Simple_Types msg;
- uint8_t referee[] = { 0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
- 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
- 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
- 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
- 0x51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
- 0x59, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF,
- 0x6D, 0x00, 0x00, 0x00, 0x80,
- 0x75, 0xFF, 0xFF, 0x7F, 0xFF};
- for(auto r: referee) {
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
- }
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
- EXPECT_EQ(std::numeric_limits<int32_t>::min(), msg.get_a_int32());
- EXPECT_EQ(std::numeric_limits<int64_t>::min(), msg.get_a_int64());
- EXPECT_EQ(std::numeric_limits<uint32_t>::min(), msg.get_a_uint32());
- EXPECT_EQ(std::numeric_limits<uint64_t>::min(), msg.get_a_uint64());
- EXPECT_EQ(std::numeric_limits<int32_t>::min(), msg.get_a_sint32());
- EXPECT_EQ(std::numeric_limits<int64_t>::min(), msg.get_a_sint64());
- EXPECT_EQ(false, msg.get_a_bool());
- EXPECT_EQ(Test_Enum::ZERO, msg.get_a_enum());
- EXPECT_EQ(std::numeric_limits<uint64_t>::min(), msg.get_a_fixed64());
- EXPECT_EQ(std::numeric_limits<int64_t>::min(), msg.get_a_sfixed64());
- EXPECT_EQ(std::numeric_limits<double>::lowest(), msg.get_a_double());
- EXPECT_EQ(std::numeric_limits<uint32_t>::min(), msg.get_a_fixed32());
- EXPECT_EQ(std::numeric_limits<int32_t>::min(), msg.get_a_sfixed32());
- EXPECT_EQ(std::numeric_limits<float>::lowest(), msg.get_a_float());
- }
- TEST(SimpleTypes, deserialize_smalest_real)
- {
- InSequence s;
-
- ::Test_Simple_Types msg;
- Mocks::ReadBufferMock buffer;
- uint8_t referee[] = {0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
- 0x75, 0x00, 0x00, 0x80, 0x00 };
-
- for(auto r: referee) {
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
- }
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
-
- EXPECT_EQ(::EmbeddedProto::Error::NO_ERRORS, msg.deserialize(buffer));
- EXPECT_EQ(std::numeric_limits<double>::min(), msg.get_a_double());
- EXPECT_EQ(std::numeric_limits<float>::min(), msg.get_a_float());
- }
- TEST(SimpleTypes, deserialize_fault_end_of_buffer_fixed)
- {
- InSequence s;
- Mocks::ReadBufferMock buffer;
-
- ON_CALL(buffer, get_size()).WillByDefault(Return(58));
- ::Test_Simple_Types msg;
- uint8_t referee[] = {0x49, 0xFF, 0xFF}; // End half way through a fixed size value.
- for(auto r: referee) {
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
- }
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
- EXPECT_EQ(::EmbeddedProto::Error::END_OF_BUFFER, msg.deserialize(buffer));
- }
- TEST(SimpleTypes, deserialize_fault_end_of_buffer_bool)
- {
- InSequence s;
- Mocks::ReadBufferMock buffer;
-
- ON_CALL(buffer, get_size()).WillByDefault(Return(58));
- ::Test_Simple_Types msg;
- uint8_t referee[] = {0x38}; // Just the tag of a bool but no data
- for(auto r: referee) {
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(r), Return(true)));
- }
- EXPECT_CALL(buffer, pop(_)).Times(1).WillOnce(Return(false));
- EXPECT_EQ(::EmbeddedProto::Error::END_OF_BUFFER, msg.deserialize(buffer));
- }
- } // End of namespace test_EmbeddedAMS_SimpleTypes
|