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-2013 Torus Knot Software Ltd
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #include "BitwiseTests.h"
29 #include "OgreBitwise.h"
30 // Register the suite
31
32 using namespace Ogre;
33
34 CPPUNIT_TEST_SUITE_REGISTRATION( BitwiseTests );
35
setUp()36 void BitwiseTests::setUp()
37 {
38 }
39
tearDown()40 void BitwiseTests::tearDown()
41 {
42 }
43
44
testFixedPointConversion()45 void BitwiseTests::testFixedPointConversion()
46 {
47 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x0,1,8), (unsigned int)0x00);
48 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,1,8), (unsigned int)0xFF);
49 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x2,2,8), (unsigned int)0xAA); // 10101010
50 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,2,8), (unsigned int)0x55); // 01010101
51 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x2,2,9), (unsigned int)0x155); // 1 01010101
52 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0x1,2,9), (unsigned int)0x0AA); // 0 10101010
53 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0xFE,8,3), (unsigned int)0x7); // 111
54 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFixed(0xFE,8,9), (unsigned int)0x1FD); // 111111101
55
56 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFloat(0xFF,8), 1.0f);
57 CPPUNIT_ASSERT_EQUAL(Bitwise::fixedToFloat(0x00,8), 0.0f);
58
59 CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(1.0f,8), (unsigned int)0xFF);
60 CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(0.0f,8), (unsigned int)0x00);
61
62 // Test clamping
63 CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(-1.0f,8), (unsigned int)0x00);
64 CPPUNIT_ASSERT_EQUAL(Bitwise::floatToFixed(2.0f,8), (unsigned int)0xFF);
65
66 // Test circular conversion
67 bool failed = false;
68 for(unsigned int x=0; x<0x100; x++)
69 if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,8),8) != x)
70 failed = true;
71 CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 8 bit failed",!failed);
72
73 failed = false;
74 for(unsigned int x=0; x<0x10; x++)
75 if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,4),4) != x)
76 failed = true;
77 CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 4 bit failed",!failed);
78
79 failed = false;
80 for(unsigned int x=0; x<0x1000; x++)
81 if(Bitwise::floatToFixed(Bitwise::fixedToFloat(x,12),12) != x)
82 failed = true;
83 CPPUNIT_ASSERT_MESSAGE("circular floatToFixed/fixedToFloat for 12 bit failed",!failed);
84 }
85
testIntReadWrite()86 void BitwiseTests::testIntReadWrite()
87 {
88 // Test reading and writing ints
89 uint32 testje = 0x12345678;
90 assert(Bitwise::intRead(&testje, 4) == 0x12345678);
91 uint16 testje2 = 0x1234;
92 assert(Bitwise::intRead(&testje2, 2) == 0x1234);
93 uint8 testje3 = 0xD3;
94 assert(Bitwise::intRead(&testje3, 1) == 0xD3);
95 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
96 uint8 testje4[] = {0x12, 0x34, 0x56};
97 #else
98 uint8 testje4[] = {0x56, 0x34, 0x12};
99 #endif
100 assert(Bitwise::intRead(&testje4, 3) == 0x123456);
101
102 Bitwise::intWrite(&testje, 4, 0x87654321);
103 assert(testje == 0x87654321);
104
105 Bitwise::intWrite(&testje2, 2, 0x4321);
106 assert(testje2 == 0x4321);
107
108 Bitwise::intWrite(&testje3, 1, 0x12);
109 assert(testje3 == 0x12);
110 }
111
testHalf()112 void BitwiseTests::testHalf()
113 {
114 /*
115 for(int x=0; x<0x100; x++)
116 {
117 float f = (float)x/255.0f;
118 uint32 fi = *reinterpret_cast<uint32*>(&f);
119 uint16 g = Bitwise::floatToHalf(f);
120 float h = Bitwise::halfToFloat(g);
121 uint32 hi = *reinterpret_cast<uint32*>(&h);
122 int i = h*256.0f;
123 std::cerr << x << " " << fi << " " << std::hex << std::setw(4) << g << " " << hi << " " << i << std::endl;
124 }
125 */
126 }
127