test_package_serializer.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // Copyright 2022 Universal Robots A/S
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of the {copyright_holder} nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 // -- END LICENSE BLOCK ------------------------------------------------
30 
31 #include <gtest/gtest.h>
33 
34 using namespace urcl;
35 
36 TEST(package_serializer, serialize_string)
37 {
38  uint8_t buffer[4096];
39  std::string message = "serialized string";
40  size_t expected_size = message.size();
41  size_t actual_size = comm::PackageSerializer::serialize(buffer, message);
42 
43  EXPECT_EQ(expected_size, actual_size);
44 
45  uint8_t expected_buffer[] = { 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65,
46  0x64, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67 };
47  for (unsigned int i = 0; i < actual_size; ++i)
48  {
49  EXPECT_EQ(expected_buffer[i], buffer[i]);
50  }
51 }
52 
53 TEST(package_serializer, serialize_double)
54 {
55  uint8_t buffer[sizeof(double)];
56  size_t expected_size = sizeof(double);
57  size_t actual_size = comm::PackageSerializer::serialize(buffer, 2.341);
58 
59  EXPECT_EQ(expected_size, actual_size);
60 
61  uint8_t expected_buffer[] = { 0x40, 0x02, 0xba, 0x5e, 0x35, 0x3f, 0x7c, 0xee };
62  for (unsigned int i = 0; i < actual_size; ++i)
63  {
64  EXPECT_EQ(expected_buffer[i], buffer[i]);
65  }
66 }
67 
68 TEST(package_serializer, serialize_int32)
69 {
70  uint8_t buffer[sizeof(int32_t)];
71  size_t expected_size = sizeof(int32_t);
72  size_t actual_size = comm::PackageSerializer::serialize<int32_t>(buffer, 2341);
73 
74  EXPECT_EQ(expected_size, actual_size);
75 
76  uint8_t expected_buffer[] = { 0x00, 0x00, 0x09, 0x25 };
77  for (unsigned int i = 0; i < actual_size; ++i)
78  {
79  EXPECT_EQ(expected_buffer[i], buffer[i]);
80  }
81 }
82 
83 int main(int argc, char* argv[])
84 {
85  ::testing::InitGoogleTest(&argc, argv);
86 
87  return RUN_ALL_TESTS();
88 }
TEST(package_serializer, serialize_string)
int main(int argc, char *argv[])
static size_t serialize(uint8_t *buffer, T val)
A generalized serialization method for arbitrary datatypes.


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47