1 #include <gtest/gtest.h>
2 #include <Unittests/unittests_common.hh>
3 #include <iostream>
4 #include <list>
5 #include <stdint.h>
6
7
8 namespace {
9
10 class OpenMeshSRBinary : public testing::Test {
11
12 protected:
13
14 // This function is called before each test is run
SetUp()15 virtual void SetUp() {
16
17 // Do some initial stuff with the member data here...
18 }
19
20 // This function is called after all tests are through
TearDown()21 virtual void TearDown() {
22
23 // Do some final stuff with the member data here...
24 }
25
26 };
27
28
29
30 /*
31 * ====================================================================
32 * Define tests below
33 * ====================================================================
34 */
35
36 /* Check if len is swapped correctly
37 * when storing strings using the binary serializer.
38 *
39 */
TEST_F(OpenMeshSRBinary,CheckStringSwap)40 TEST_F(OpenMeshSRBinary, CheckStringSwap) {
41 std::string testString = "OpenMesh String";
42 std::stringstream stream("");
43 OpenMesh::IO::binary<std::string>::store(stream,testString,true);
44 std::stringstream stream2("");
45 OpenMesh::IO::binary<std::string>::store(stream2,testString,false);
46 std::string res2 = stream2.str();
47 std::string res = stream.str();
48 uint16_t len, len2;
49 stream.read((char*)&len,2);
50
51 stream2.read( (char*)&len2,2);
52
53 EXPECT_EQ(len2,testString.length());
54 EXPECT_EQ(len,(testString.length()>>8)|(testString.length()<<8));
55 EXPECT_NE(len,len2);
56 }
57
58 /* Check if storing and restoring a string gives proper result
59 * Do that with and without swapping the byte order
60 */
TEST_F(OpenMeshSRBinary,StringStoreRestore)61 TEST_F(OpenMeshSRBinary, StringStoreRestore) {
62
63
64 std::string testString = "OpenMesh String";
65 std::stringstream stream("");
66 OpenMesh::IO::binary<std::string>::store(stream,testString,true);
67 std::stringstream stream2("");
68 OpenMesh::IO::binary<std::string>::store(stream2,testString,false);
69
70
71 std::string restored1, restored2;
72 OpenMesh::IO::binary<std::string>::restore(stream, restored1 , true);
73 OpenMesh::IO::binary<std::string>::restore(stream2, restored2 , false);
74
75 EXPECT_EQ(restored1.length(), restored2.length());
76 EXPECT_EQ(restored1.length(), testString.length());
77 for(size_t i = 0 ; i < testString.length() ; ++i)
78 {
79 EXPECT_EQ(restored1[i] , testString[i]);
80 EXPECT_EQ(restored2[i] , testString[i]);
81 }
82
83
84 }
85
86
87
88 }
89