Changeset 180:4f16b7a7b610 in rrlib_serialization


Ignore:
Timestamp:
26.11.2018 09:58:59 (3 weeks ago)
Author:
Max Reichardt <mreichardt@…>
Branch:
17.03
Parents:
177:8675cefad5cd (diff), 179:aaab91d300ac (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Phase:
public
Tags:
tip
Message:

Merge with 14.08

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tests/serialization.cpp

    r176 r180  
    425425    TestFloatingPointStringPair(-34.500000000000000000555555); 
    426426    TestFloatingPointStringPair(-550000000000055.00000); 
     427    TestFloatingPointStringPair(0.20804581); 
     428    TestFloatingPointStringPair(1.20804581549854e-10); 
     429    TestFloatingPointStringPair(1e-50); 
     430    TestFloatingPointStringPair(1.e33); 
     431    TestFloatingPointStringPair(1.2345236234623462346234623462346e40); 
    427432  } 
    428433 
  • tests/serialization.cpp

    r179 r180  
    2020// 
    2121//---------------------------------------------------------------------- 
    22 /*!\file    serialization.cpp 
     22/*!\file    rrlib/serialization/tests/serialization.cpp 
    2323 * 
    2424 * \author  Michael Arndt 
     25 * \author  Max Reichardt 
    2526 * 
    2627 * \date    2014-03-17 
     
    6667{ 
    6768 
    68 // We check this here so that not all programs using rrlib_serialization include <set> by default 
     69//---------------------------------------------------------------------- 
     70// Forward declarations / typedefs / enums 
     71//---------------------------------------------------------------------- 
     72 
     73class tNonSerializable 
     74{}; 
     75 
     76//---------------------------------------------------------------------- 
     77// Const values 
     78//---------------------------------------------------------------------- 
     79 
     80//---------------------------------------------------------------------- 
     81// Implementation 
     82//---------------------------------------------------------------------- 
     83template <typename T> 
     84constexpr int Test() 
     85{ 
     86  static_assert(std::is_same<T, bool>::value, "Test"); 
     87  return 0; 
     88} 
     89 
     90// Some type trait tests (checked here instead of header in order to shorten compile times) 
     91static_assert(IsSerializableContainer<int>::value == false, "Incorrect trait implementation"); 
     92static_assert(IsSerializableContainer<std::vector<int>>::value == true, "Incorrect trait implementation"); 
     93static_assert(IsSerializableContainer<std::vector<bool>>::value == true, "Incorrect trait implementation"); 
     94static_assert(IsSerializableContainer<std::map<int, std::string>>::value == true, "Incorrect trait implementation"); 
     95static_assert(IsSerializableMap<std::vector<int>>::value == false, "Incorrect trait implementation"); 
     96static_assert(IsSerializableMap<std::map<int, std::string>>::value == true, "Incorrect trait implementation"); 
     97static_assert(ContainerSerialization<std::pair<size_t, std::string>>::cBINARY_SERIALIZABLE, "Incorrect trait implementation"); 
     98static_assert(IsBinarySerializable<std::map<size_t, std::string>>::value == true, "Incorrect trait implementation"); 
     99static_assert(IsXMLSerializable<std::map<size_t, std::string>>::value == true, "Incorrect trait implementation"); 
     100static_assert(IsConstElementContainer<std::vector<std::string>>::value == false, "Incorrect trait implementation"); 
     101 
     102static_assert(IsStringSerializable<bool>::value, "Incorrect trait implementation"); 
     103static_assert(!IsStringSerializable<std::vector<bool>>::value, "Incorrect trait implementation"); 
     104static_assert(detail::IsStringInputSerializable<typename std::vector<bool>::reference>::value, "Incorrect trait implementation"); 
     105 
    69106static_assert(IsSerializableContainer<std::set<std::string>>::value == true, "Incorrect trait implementation"); 
    70107static_assert(IsConstElementContainer<std::set<std::string>>::value == true, "Incorrect trait implementation"); 
    71108 
    72 //---------------------------------------------------------------------- 
    73 // Forward declarations / typedefs / enums 
    74 //---------------------------------------------------------------------- 
    75  
    76 //---------------------------------------------------------------------- 
    77 // Const values 
    78 //---------------------------------------------------------------------- 
    79  
    80 //---------------------------------------------------------------------- 
    81 // Implementation 
    82 //---------------------------------------------------------------------- 
     109static_assert(IsBinarySerializable<std::tuple<>>::value == true, "Incorrect trait implementation"); 
     110static_assert(IsBinarySerializable<std::tuple<int, std::array<std::string, 4>, std::vector<tEnumSigned>>>::value == true, "Incorrect trait implementation"); 
     111static_assert(IsBinarySerializable<std::tuple<int, tNonSerializable>>::value == false, "Incorrect trait implementation"); 
     112static_assert(IsBinarySerializable<std::tuple<int, std::array<tNonSerializable, 4>>>::value == false, "Incorrect trait implementation"); 
     113static_assert(IsBinarySerializable<std::pair<int, std::array<std::string, 4>>>::value == true, "Incorrect trait implementation"); 
     114static_assert(IsBinarySerializable<std::pair<tNonSerializable, std::array<tNonSerializable, 4>>>::value == false, "Incorrect trait implementation"); 
     115static_assert(IsBinarySerializable<std::pair<tNonSerializable, std::vector<tNonSerializable>>>::value == false, "Incorrect trait implementation"); 
     116static_assert(IsBinarySerializable<int&>::value == true, "Incorrect trait implementation"); 
     117static_assert(IsBinarySerializable<std::tuple<int&, std::string&>>::value == true, "Incorrect trait implementation"); 
     118 
     119static_assert(IsXMLSerializable<std::tuple<>>::value == true, "Incorrect trait implementation"); 
     120static_assert(IsXMLSerializable<std::tuple<int, std::array<std::string, 4>, std::vector<tEnumSigned>>>::value == true, "Incorrect trait implementation"); 
     121static_assert(IsXMLSerializable<std::tuple<tNonSerializable, std::array<tNonSerializable, 4>>>::value == false, "Incorrect trait implementation"); 
     122static_assert(IsXMLSerializable<std::tuple<std::map<int, int>, std::array<std::string, 4>>>::value == true, "Incorrect trait implementation"); 
     123static_assert(IsXMLSerializable<std::pair<int, std::array<std::string, 4>>>::value == true, "Incorrect trait implementation"); 
     124static_assert(IsXMLSerializable<std::pair<tNonSerializable, std::array<tNonSerializable, 4>>>::value == false, "Incorrect trait implementation"); 
     125static_assert(IsXMLSerializable<int&>::value == true, "Incorrect trait implementation"); 
     126static_assert(IsXMLSerializable<std::tuple<int&, std::string&>>::value == true, "Incorrect trait implementation"); 
     127 
    83128 
    84129class TestSerialization : public util::tUnitTestSuite 
    85130{ 
    86131  RRLIB_UNIT_TESTS_BEGIN_SUITE(TestSerialization); 
     132  RRLIB_UNIT_TESTS_ADD_TEST(TestStringSerialization); 
    87133  RRLIB_UNIT_TESTS_ADD_TEST(TestXMLMap); 
    88134  RRLIB_UNIT_TESTS_ADD_TEST(TestBinaryMap); 
     135  RRLIB_UNIT_TESTS_ADD_TEST(TestBinarySet); 
    89136  RRLIB_UNIT_TESTS_ADD_TEST(TestEnumsBinary); 
    90137  RRLIB_UNIT_TESTS_ADD_TEST(TestEnumsString); 
    91138  RRLIB_UNIT_TESTS_ADD_TEST(TestFloatingPointStrings); 
     139  RRLIB_UNIT_TESTS_ADD_TEST(TestTuplesPairsArrays); 
    92140  RRLIB_UNIT_TESTS_END_SUITE; 
     141 
    93142 
    94143private: 
     
    142191  } 
    143192 
     193  void TestBinarySet() 
     194  { 
     195    std::set<std::string> set; 
     196    set.emplace("Zero"); 
     197    set.emplace("One"); 
     198    set.emplace("Two"); 
     199 
     200    std::set<std::string> other_set = TestBinarySerialization(set); 
     201 
     202    RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("There must be the correct number of elements in the set", (size_t) 3, other_set.size()); 
     203    RRLIB_UNIT_TESTS_ASSERT_MESSAGE("Set must contain the element", other_set.find("Zero") != other_set.end()); 
     204    RRLIB_UNIT_TESTS_ASSERT_MESSAGE("Set must contain the element", other_set.find("One") != other_set.end()); 
     205    RRLIB_UNIT_TESTS_ASSERT_MESSAGE("Set must contain the element", other_set.find("Two") != other_set.end()); 
     206  } 
     207 
    144208  void TestEnumsBinary() 
    145209  { 
     210    if (!make_builder::internal::GetEnumStrings<tEnumSigned>().non_standard_values) 
     211    { 
     212      RRLIB_LOG_PRINT(WARNING, "No enum values are available. They are only available when using the clang plugin for generating enum strings - instead of doxygen (which is deprecated). Skipping test."); 
     213      return; 
     214    } 
     215 
    146216    // serialize to memory 
    147217    rrlib::serialization::tMemoryBuffer mb; 
     
    164234  void TestEnumsString() 
    165235  { 
     236    if (!make_builder::internal::GetEnumStrings<tEnumSigned>().non_standard_values) 
     237    { 
     238      RRLIB_LOG_PRINT(WARNING, "No enum values are available. They are only available when using the clang plugin for generating enum strings - instead of doxygen (which is deprecated). Skipping test."); 
     239      return; 
     240    } 
     241 
    166242    RRLIB_UNIT_TESTS_ASSERT(tEnumSigned::MIN_VALUE == Deserialize<tEnumSigned>(Serialize(tEnumSigned::MIN_VALUE))); 
    167243    RRLIB_UNIT_TESTS_ASSERT(tEnumSigned::MAX_VALUE == Deserialize<tEnumSigned>(Serialize(tEnumSigned::MAX_VALUE))); 
    168244    RRLIB_UNIT_TESTS_ASSERT(tEnumUnsigned::MIN_VALUE == Deserialize<tEnumUnsigned>(Serialize(tEnumUnsigned::MIN_VALUE))); 
    169245    RRLIB_UNIT_TESTS_ASSERT(tEnumUnsigned::MAX_VALUE == Deserialize<tEnumUnsigned>(Serialize(tEnumUnsigned::MAX_VALUE))); 
     246    RRLIB_UNIT_TESTS_ASSERT(tStandardEnum::WORD == Deserialize<tStandardEnum>(Serialize(tStandardEnum::WORD))); 
     247    typedef typename std::underlying_type<tEnumSigned>::type tEnumSignedUnderlying; 
     248    typedef typename std::underlying_type<tEnumUnsigned>::type tEnumUnsignedUnderlying; 
     249    typedef typename std::underlying_type<tEnumUnsigned>::type tEnumStandardUnderlying; 
     250    RRLIB_UNIT_TESTS_ASSERT(tEnumSigned::MIN_VALUE == Deserialize<tEnumSigned>(Serialize(static_cast<tEnumSignedUnderlying>(tEnumSigned::MIN_VALUE)))); 
     251    RRLIB_UNIT_TESTS_ASSERT(tEnumSigned::MAX_VALUE == Deserialize<tEnumSigned>(Serialize(static_cast<tEnumSignedUnderlying>(tEnumSigned::MAX_VALUE)))); 
     252    RRLIB_UNIT_TESTS_ASSERT(tEnumUnsigned::MIN_VALUE == Deserialize<tEnumUnsigned>(Serialize(static_cast<tEnumUnsignedUnderlying>(tEnumUnsigned::MIN_VALUE)))); 
     253    RRLIB_UNIT_TESTS_ASSERT(tEnumUnsigned::MAX_VALUE == Deserialize<tEnumUnsigned>(Serialize(static_cast<tEnumUnsignedUnderlying>(tEnumUnsigned::MAX_VALUE)))); 
     254    RRLIB_UNIT_TESTS_ASSERT(tStandardEnum::WORD == Deserialize<tStandardEnum>(Serialize(static_cast<tEnumStandardUnderlying>(tStandardEnum::WORD)))); 
     255    RRLIB_UNIT_TESTS_ASSERT(tStandardEnum::WORD == Deserialize<tStandardEnum>("    3  ")); 
    170256 
    171257    // produce warning messages 
    172258    RRLIB_UNIT_TESTS_ASSERT(tEnumSigned::MIN_VALUE == Deserialize<tEnumSigned>("Invalid string for testing ignore warning (" + std::to_string(std::numeric_limits<int64_t>::min()) + ")")); 
    173     RRLIB_UNIT_TESTS_ASSERT(tEnumUnsigned::MAX_VALUE == Deserialize<tEnumUnsigned>("Invalid string for testing ignore warning (" + std::to_string(std::numeric_limits<uint64_t>::max()) + ")")); 
     259    RRLIB_UNIT_TESTS_ASSERT(tEnumUnsigned::MAX_VALUE == Deserialize<tEnumUnsigned>("Invalid string for testing ignore warning ( " + std::to_string(std::numeric_limits<uint64_t>::max()) + " )")); 
    174260 
    175261    // Enum-based-Flags 
     
    187273    RRLIB_UNIT_TESTS_ASSERT(many == Deserialize<tFlags>(Serialize(many))); 
    188274    RRLIB_UNIT_TESTS_ASSERT(all == Deserialize<tFlags>(Serialize(all))); 
     275  } 
     276 
     277  /*! 
     278   * Helper method for testing binary serialization for an object of type T 
     279   * 
     280   * \param value Object of type T 
     281   * \return Object created from deserializing the serialized object passed to this function 
     282   */ 
     283  template <typename T> 
     284  T TestBinarySerialization(const T &value, size_t expected_size = 0) 
     285  { 
     286    // serialize to memory 
     287    rrlib::serialization::tMemoryBuffer mb; 
     288    rrlib::serialization::tOutputStream os(mb); 
     289 
     290    os << value; 
     291    os.Close(); 
     292 
     293    if (expected_size > 0) 
     294    { 
     295      RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("Serialized size must be correct", expected_size, mb.GetSize()); 
     296    } 
     297 
     298    // deserialize to original type 
     299    rrlib::serialization::tInputStream is(mb); 
     300    T val; 
     301    is >> val; 
     302    RRLIB_UNIT_TESTS_ASSERT_MESSAGE("After de-serializing to original type, value must be correct", value == val); 
     303    //RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("After de-serializing to original type, value must be correct", value, val); 
     304    return val; 
     305  } 
     306 
     307  template <size_t MAX_TEST_STRING_LENGTH> 
     308  void TestStringSerialization(const std::vector<std::string>& test_strings, size_t serialization_count, size_t max_length) 
     309  { 
     310    // serialize to memory 
     311    rrlib::serialization::tMemoryBuffer mb; 
     312    rrlib::serialization::tOutputStream os(mb); 
     313    for (size_t i = 0; i < serialization_count; i++) 
     314    { 
     315      for (const std::string & string : test_strings) 
     316      { 
     317        os << string; 
     318      } 
     319    } 
     320    os.Close(); 
     321 
     322    // Deserialize in different ways 
     323    rrlib::serialization::tInputStream input1(mb), input2(mb), input3(mb), input4(mb), input5(mb), input6(mb), input7(mb), input8(mb); 
     324    std::string string_buffer; 
     325    std::stringstream string_stream; 
     326    char char_buffer[MAX_TEST_STRING_LENGTH]; 
     327    for (size_t i = 0; i < serialization_count; i++) 
     328    { 
     329      for (const std::string & string : test_strings) 
     330      { 
     331        // without max_length 
     332        input1 >> string_buffer; 
     333        RRLIB_UNIT_TESTS_EQUALITY(string, string_buffer); 
     334        string_buffer = input2.ReadString(); 
     335        RRLIB_UNIT_TESTS_EQUALITY(string, string_buffer); 
     336        string_stream.str(std::string()); 
     337        string_stream.clear(); // reset stream 
     338        input3.ReadString(string_stream); 
     339        RRLIB_UNIT_TESTS_EQUALITY(string, string_stream.str()); 
     340        input4.ReadString(char_buffer, true); 
     341        RRLIB_UNIT_TESTS_EQUALITY(string, std::string(char_buffer)); 
     342        //RRLIB_UNIT_TESTS_ASSERT(string == char_buffer); 
     343 
     344        // with max_length < string_length 
     345        input5.ReadString(string_buffer, max_length); 
     346        RRLIB_UNIT_TESTS_EQUALITY(string.substr(0, max_length), string_buffer); 
     347        input5.ReadString(string_buffer); 
     348        RRLIB_UNIT_TESTS_EQUALITY(string.substr(max_length), string_buffer); 
     349 
     350        string_buffer = input6.ReadString(max_length); 
     351        RRLIB_UNIT_TESTS_EQUALITY(string.substr(0, max_length), string_buffer); 
     352        string_buffer = input6.ReadString(); 
     353        RRLIB_UNIT_TESTS_EQUALITY(string.substr(max_length), string_buffer); 
     354 
     355        string_stream.str(std::string()); 
     356        string_stream.clear(); // reset stream 
     357        input7.ReadString(string_stream, max_length); 
     358        RRLIB_UNIT_TESTS_EQUALITY(string.substr(0, max_length), string_stream.str()); 
     359        string_stream.str(std::string()); 
     360        string_stream.clear(); // reset stream 
     361        input7.ReadString(string_stream); 
     362        RRLIB_UNIT_TESTS_EQUALITY(string.substr(max_length), string_stream.str()); 
     363 
     364        char small_char_buffer[max_length + 1]; 
     365        input8.ReadString(small_char_buffer, max_length + 1, true); 
     366        RRLIB_UNIT_TESTS_ASSERT(string.substr(0, max_length) == small_char_buffer); 
     367        input8.ReadString(char_buffer, true); 
     368        RRLIB_UNIT_TESTS_ASSERT(string.substr(max_length) == char_buffer); 
     369      } 
     370    } 
     371  } 
     372 
     373  void TestStringSerialization() 
     374  { 
     375    TestStringSerialization<7>({ "string", "123456", "qwertz" }, 8192, 4); 
     376    TestStringSerialization<8>({ "str", "123", "qwe" }, 8192, 2); 
     377 
     378    std::ostringstream string_buffer; 
     379    for (size_t i = 0; i < 8400; i++) 
     380    { 
     381      string_buffer << static_cast<char>('a' + (i % 26)); 
     382    } 
     383    TestStringSerialization<10000>({ string_buffer.str() }, 10, 1070); 
    189384  } 
    190385 
     
    236431    TestFloatingPointStringPair(1.2345236234623462346234623462346e40); 
    237432  } 
     433 
     434  void TestTuplesPairsArrays() 
     435  { 
     436    std::tuple<std::string, int, std::vector<int>> test_tuple("Test", 4, std::vector<int>(4)); 
     437    TestBinarySerialization(test_tuple); 
     438 
     439    std::tuple<std::pair<std::string, int>, double> test_tuple2(std::pair<std::string, int>("Test", 4), -1.4); 
     440    TestBinarySerialization(test_tuple2, 5 + sizeof(int) + sizeof(double)); 
     441 
     442    std::array<int, 4> test_array = { 1, 5, 15, -2 }; 
     443    TestBinarySerialization(test_array, 4 * sizeof(int)); 
     444 
     445    std::array<std::pair<double, int>, 3> test_array2; 
     446    test_array2.fill(std::pair<double, int>(4.555, -1)); 
     447    TestBinarySerialization(test_array2, 3 * (sizeof(double) + sizeof(int))); 
     448 
     449    std::tuple<std::map<int, int>, std::string> test_tuple3; 
     450    std::get<0>(test_tuple3)[4] = 5; 
     451    std::get<0>(test_tuple3)[2] = 7; 
     452    std::get<1>(test_tuple3) = "Test"; 
     453    TestBinarySerialization(test_tuple3); 
     454  } 
    238455}; 
    239456 
Note: See TracChangeset for help on using the changeset viewer.