OpenWalnut  1.5.0dev
WIOTools_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WIOTOOLS_TEST_H
26 #define WIOTOOLS_TEST_H
27 
28 #include <string>
29 #include <fstream>
30 
31 #include <cxxtest/TestSuite.h>
32 
33 #include "../WIOTools.h"
34 
35 /**
36  * Unit test WIOTools functions.
37  */
38 class WIOToolsTest : public CxxTest::TestSuite
39 {
40 public:
41  /**
42  * When switching byte order, the first and last bytes should be swapped
43  * and the bytes inbetween too.
44  */
46  {
47  uint32_t x = 1;
48  TS_ASSERT_EQUALS( x, 1 );
49  x = switchByteOrder( x );
50  TS_ASSERT_EQUALS( x, 16777216 );
51  x = switchByteOrder( x );
52  TS_ASSERT_EQUALS( x, 1 );
53  }
54 
55  /**
56  * Since templates should work on multiple types we just use double here.
57  */
59  {
60  double x = 3.141592653589793238462643383279502884197169399375105820974;
61  double original = x;
62  x = switchByteOrder( x );
63  TS_ASSERT_DIFFERS( x, original );
64  x = switchByteOrder( x );
65  TS_ASSERT_EQUALS( x, original );
66  }
67 
68  /**
69  * On single bytes we should do nothing.
70  */
72  {
73  char x = 1;
74  TS_ASSERT_EQUALS( switchByteOrder( x ), x );
75  }
76 
77  /**
78  * When switching the byte order of an whole array every element should be
79  * switched.
80  */
82  {
83  uint32_t x[] = { 1, 16777216 };
84  switchByteOrderOfArray( x, 2 );
85  TS_ASSERT_EQUALS( x[0], 16777216 );
86  TS_ASSERT_EQUALS( x[1], 1 );
87  }
88 
89  /**
90  * Test reading a text file in a string.
91  */
93  {
94  std::string expected = "Hello Pansen!\r\n";
95  std::string actual = readFileIntoString( boost::filesystem::path( W_FIXTURE_PATH + "hello.world" ) );
96  TS_ASSERT_EQUALS( expected, actual );
97  }
98 
99  /**
100  * Writes a text file, and afterwards checks if its the same, by reading it.
101  */
103  {
104  std::string content = "Hello Pansen!\r\n";
105  boost::filesystem::path fpath = tempFilename();
106  writeStringIntoFile( fpath, content );
107 
108  std::ifstream input( fpath.string().c_str() );
109  std::string actual;
110  actual.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() );
111  input.close();
112  TS_ASSERT_EQUALS( content, actual );
113  TS_ASSERT( boost::filesystem::exists( fpath ) );
114  boost::filesystem::remove( fpath );
115  }
116 };
117 
118 #endif // WIOTOOLS_TEST_H
Unit test WIOTools functions.
Definition: WIOTools_test.h:39
void testByteOrderSwitchingOnFloats(void)
Since templates should work on multiple types we just use double here.
Definition: WIOTools_test.h:58
void testByteOrderSwitching(void)
When switching byte order, the first and last bytes should be swapped and the bytes inbetween too.
Definition: WIOTools_test.h:45
void testReadFileIntoString(void)
Test reading a text file in a string.
Definition: WIOTools_test.h:92
void testByteOrderSwitchingOnSingleBytes(void)
On single bytes we should do nothing.
Definition: WIOTools_test.h:71
void testByteOrderSwitchOnArray(void)
When switching the byte order of an whole array every element should be switched.
Definition: WIOTools_test.h:81
void testWriteStringIntoFile(void)
Writes a text file, and afterwards checks if its the same, by reading it.