[1] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #include "BitwiseTests.h" |
---|
| 30 | #include "OgreBitwise.h" |
---|
| 31 | // Register the suite |
---|
| 32 | |
---|
| 33 | using namespace Ogre; |
---|
| 34 | |
---|
| 35 | CPPUNIT_TEST_SUITE_REGISTRATION( BitwiseTests ); |
---|
| 36 | |
---|
| 37 | void BitwiseTests::setUp() |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | void BitwiseTests::tearDown() |
---|
| 42 | { |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | void BitwiseTests::testFixedPointConversion() |
---|
| 47 | { |
---|
| 48 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x0,1,8), (unsigned int)0x00); |
---|
| 49 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,1,8), (unsigned int)0xFF); |
---|
| 50 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x2,2,8), (unsigned int)0xAA); // 10101010 |
---|
| 51 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,2,8), (unsigned int)0x55); // 01010101 |
---|
| 52 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x2,2,9), (unsigned int)0x155); // 1 01010101 |
---|
| 53 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,2,9), (unsigned int)0x0AA); // 0 10101010 |
---|
| 54 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0xFE,8,3), (unsigned int)0x7); // 111 |
---|
| 55 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0xFE,8,9), (unsigned int)0x1FD); // 111111101 |
---|
| 56 | |
---|
| 57 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFloat(0xFF,8), 1.0f); |
---|
| 58 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFloat(0x00,8), 0.0f); |
---|
| 59 | |
---|
| 60 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(1.0f,8), (unsigned int)0xFF); |
---|
| 61 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(0.0f,8), (unsigned int)0x00); |
---|
| 62 | |
---|
| 63 | // Test clamping |
---|
| 64 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(-1.0f,8), (unsigned int)0x00); |
---|
| 65 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(2.0f,8), (unsigned int)0xFF); |
---|
| 66 | |
---|
| 67 | // Test circular conversion |
---|
| 68 | bool failed = false; |
---|
| 69 | for(int x=0; x<0x100; x++) |
---|
| 70 | if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,8),8) != x) |
---|
| 71 | failed = true; |
---|
| 72 | CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 8 bit failed",!failed); |
---|
| 73 | |
---|
| 74 | failed = false; |
---|
| 75 | for(int x=0; x<0x10; x++) |
---|
| 76 | if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,4),4) != x) |
---|
| 77 | failed = true; |
---|
| 78 | CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 4 bit failed",!failed); |
---|
| 79 | |
---|
| 80 | failed = false; |
---|
| 81 | for(int x=0; x<0x1000; x++) |
---|
| 82 | if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,12),12) != x) |
---|
| 83 | failed = true; |
---|
| 84 | CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 12 bit failed",!failed); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void BitwiseTests::testIntReadWrite() |
---|
| 88 | { |
---|
| 89 | // Test reading and writing ints |
---|
| 90 | uint32 testje = 0x12345678; |
---|
| 91 | assert(Bitwise::intRead(&testje, 4) == 0x12345678); |
---|
| 92 | uint16 testje2 = 0x1234; |
---|
| 93 | assert(Bitwise::intRead(&testje2, 2) == 0x1234); |
---|
| 94 | uint8 testje3 = 0xD3; |
---|
| 95 | assert(Bitwise::intRead(&testje3, 1) == 0xD3); |
---|
| 96 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
---|
| 97 | uint8 testje4[] = {0x12, 0x34, 0x56}; |
---|
| 98 | #else |
---|
| 99 | uint8 testje4[] = {0x56, 0x34, 0x12}; |
---|
| 100 | #endif |
---|
| 101 | assert(Bitwise::intRead(&testje4, 3) == 0x123456); |
---|
| 102 | |
---|
| 103 | Bitwise::intWrite(&testje, 4, 0x87654321); |
---|
| 104 | assert(testje == 0x87654321); |
---|
| 105 | |
---|
| 106 | Bitwise::intWrite(&testje2, 2, 0x4321); |
---|
| 107 | assert(testje2 == 0x4321); |
---|
| 108 | |
---|
| 109 | Bitwise::intWrite(&testje3, 1, 0x12); |
---|
| 110 | assert(testje3 == 0x12); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | void BitwiseTests::testHalf() |
---|
| 114 | { |
---|
| 115 | /* |
---|
| 116 | for(int x=0; x<0x100; x++) |
---|
| 117 | { |
---|
| 118 | float f = (float)x/255.0f; |
---|
| 119 | uint32 fi = *reinterpret_cast<uint32*>(&f); |
---|
| 120 | uint16 g = Bitwise::floatToHalf(f); |
---|
| 121 | float h = Bitwise::halfToFloat(g); |
---|
| 122 | uint32 hi = *reinterpret_cast<uint32*>(&h); |
---|
| 123 | int i = h*256.0f; |
---|
| 124 | std::cerr << x << " " << fi << " " << std::hex << std::setw(4) << g << " " << hi << " " << i << std::endl; |
---|
| 125 | } |
---|
| 126 | */ |
---|
| 127 | } |
---|