[657] | 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-2005 The OGRE Team
|
---|
| 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 | */
|
---|
| 25 | #include "BitwiseTests.h"
|
---|
| 26 | #include "OgreBitwise.h"
|
---|
| 27 | // Register the suite
|
---|
| 28 | CPPUNIT_TEST_SUITE_REGISTRATION( BitwiseTests );
|
---|
| 29 |
|
---|
| 30 | void BitwiseTests::setUp()
|
---|
| 31 | {
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | void BitwiseTests::tearDown()
|
---|
| 35 | {
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | void BitwiseTests::testFixedPointConversion()
|
---|
| 40 | {
|
---|
| 41 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x0,1,8), (unsigned int)0x00);
|
---|
| 42 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,1,8), (unsigned int)0xFF);
|
---|
| 43 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x2,2,8), (unsigned int)0xAA); // 10101010
|
---|
| 44 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,2,8), (unsigned int)0x55); // 01010101
|
---|
| 45 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x2,2,9), (unsigned int)0x155); // 1 01010101
|
---|
| 46 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,2,9), (unsigned int)0x0AA); // 0 10101010
|
---|
| 47 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0xFE,8,3), (unsigned int)0x7); // 111
|
---|
| 48 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0xFE,8,9), (unsigned int)0x1FD); // 111111101
|
---|
| 49 |
|
---|
| 50 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFloat(0xFF,8), 1.0f);
|
---|
| 51 | CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFloat(0x00,8), 0.0f);
|
---|
| 52 |
|
---|
| 53 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(1.0f,8), (unsigned int)0xFF);
|
---|
| 54 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(0.0f,8), (unsigned int)0x00);
|
---|
| 55 |
|
---|
| 56 | // Test clamping
|
---|
| 57 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(-1.0f,8), (unsigned int)0x00);
|
---|
| 58 | CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(2.0f,8), (unsigned int)0xFF);
|
---|
| 59 |
|
---|
| 60 | // Test circular conversion
|
---|
| 61 | bool failed = false;
|
---|
| 62 | for(int x=0; x<0x100; x++)
|
---|
| 63 | if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,8),8) != x)
|
---|
| 64 | failed = true;
|
---|
| 65 | CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 8 bit failed",!failed);
|
---|
| 66 |
|
---|
| 67 | failed = false;
|
---|
| 68 | for(int x=0; x<0x10; x++)
|
---|
| 69 | if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,4),4) != x)
|
---|
| 70 | failed = true;
|
---|
| 71 | CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 4 bit failed",!failed);
|
---|
| 72 |
|
---|
| 73 | failed = false;
|
---|
| 74 | for(int x=0; x<0x1000; x++)
|
---|
| 75 | if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,12),12) != x)
|
---|
| 76 | failed = true;
|
---|
| 77 | CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 12 bit failed",!failed);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void BitwiseTests::testIntReadWrite()
|
---|
| 81 | {
|
---|
| 82 | // Test reading and writing ints
|
---|
| 83 | uint32 testje = 0x12345678;
|
---|
| 84 | assert(Bitwise::intRead(&testje, 4) == 0x12345678);
|
---|
| 85 | uint16 testje2 = 0x1234;
|
---|
| 86 | assert(Bitwise::intRead(&testje2, 2) == 0x1234);
|
---|
| 87 | uint8 testje3 = 0xD3;
|
---|
| 88 | assert(Bitwise::intRead(&testje3, 1) == 0xD3);
|
---|
| 89 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
|
---|
| 90 | uint8 testje4[] = {0x12, 0x34, 0x56};
|
---|
| 91 | #else
|
---|
| 92 | uint8 testje4[] = {0x56, 0x34, 0x12};
|
---|
| 93 | #endif
|
---|
| 94 | assert(Bitwise::intRead(&testje4, 3) == 0x123456);
|
---|
| 95 |
|
---|
| 96 | Bitwise::intWrite(&testje, 4, 0x87654321);
|
---|
| 97 | assert(testje == 0x87654321);
|
---|
| 98 |
|
---|
| 99 | Bitwise::intWrite(&testje2, 2, 0x4321);
|
---|
| 100 | assert(testje2 == 0x4321);
|
---|
| 101 |
|
---|
| 102 | Bitwise::intWrite(&testje3, 1, 0x12);
|
---|
| 103 | assert(testje3 == 0x12);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void BitwiseTests::testHalf()
|
---|
| 107 | {
|
---|
| 108 | /*
|
---|
| 109 | for(int x=0; x<0x100; x++)
|
---|
| 110 | {
|
---|
| 111 | float f = (float)x/255.0f;
|
---|
| 112 | uint32 fi = *reinterpret_cast<uint32*>(&f);
|
---|
| 113 | uint16 g = Bitwise::floatToHalf(f);
|
---|
| 114 | float h = Bitwise::halfToFloat(g);
|
---|
| 115 | uint32 hi = *reinterpret_cast<uint32*>(&h);
|
---|
| 116 | int i = h*256.0f;
|
---|
| 117 | std::cerr << x << " " << fi << " " << std::hex << std::setw(4) << g << " " << hi << " " << i << std::endl;
|
---|
| 118 | }
|
---|
| 119 | */
|
---|
| 120 | }
|
---|