OpenWalnut  1.4.0
WIOTools_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WIOTOOLS_TEST_H
00026 #define WIOTOOLS_TEST_H
00027 
00028 #include <string>
00029 #include <fstream>
00030 
00031 #include <cxxtest/TestSuite.h>
00032 
00033 #include "../WIOTools.h"
00034 
00035 /**
00036  * Unit test WIOTools functions.
00037  */
00038 class WIOToolsTest : public CxxTest::TestSuite
00039 {
00040 public:
00041     /**
00042      * When switching byte order, the first and last bytes should be swapped
00043      * and the bytes inbetween too.
00044      */
00045     void testByteOrderSwitching( void )
00046     {
00047         uint32_t x = 1;
00048         TS_ASSERT_EQUALS( x, 1 );
00049         x = switchByteOrder( x );
00050         TS_ASSERT_EQUALS( x, 16777216 );
00051         x = switchByteOrder( x );
00052         TS_ASSERT_EQUALS( x, 1 );
00053     }
00054 
00055     /**
00056      * Since templates should work on multiple types we just use double here.
00057      */
00058     void testByteOrderSwitchingOnFloats( void )
00059     {
00060         double x = 3.141592653589793238462643383279502884197169399375105820974;
00061         double original = x;
00062         x = switchByteOrder( x );
00063         TS_ASSERT_DIFFERS( x, original );
00064         x = switchByteOrder( x );
00065         TS_ASSERT_EQUALS( x, original );
00066     }
00067 
00068     /**
00069      * On single bytes we should do nothing.
00070      */
00071     void testByteOrderSwitchingOnSingleBytes( void )
00072     {
00073         char x = 1;
00074         TS_ASSERT_EQUALS( switchByteOrder( x ), x );
00075     }
00076 
00077     /**
00078      * When switching the byte order of an whole array every element should be
00079      * switched.
00080      */
00081     void testByteOrderSwitchOnArray( void )
00082     {
00083         uint32_t x[] = { 1, 16777216 };
00084         switchByteOrderOfArray( x, 2 );
00085         TS_ASSERT_EQUALS( x[0], 16777216 );
00086         TS_ASSERT_EQUALS( x[1], 1 );
00087     }
00088 
00089     /**
00090      * Test reading a text file in a string.
00091      */
00092     void testReadFileIntoString( void )
00093     {
00094         std::string expected = "Hello Pansen!\r\n";
00095         std::string actual = readFileIntoString( boost::filesystem::path( W_FIXTURE_PATH + "hello.world" ) );
00096         TS_ASSERT_EQUALS( expected, actual );
00097     }
00098 
00099     /**
00100      * Writes a text file, and afterwards checks if its the same, by reading it.
00101      */
00102     void testWriteStringIntoFile( void )
00103     {
00104         std::string content = "Hello Pansen!\r\n";
00105         boost::filesystem::path fpath = tempFilename();
00106         writeStringIntoFile( fpath, content );
00107 
00108         std::ifstream input( fpath.string().c_str() );
00109         std::string actual;
00110         actual.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() );
00111         input.close();
00112         TS_ASSERT_EQUALS( content, actual );
00113         TS_ASSERT( boost::filesystem::exists( fpath ) );
00114         boost::filesystem::remove( fpath );
00115     }
00116 };
00117 
00118 #endif  // WIOTOOLS_TEST_H