source: OGRE/trunk/ogrenew/Tests/OgreMain/src/BitwiseTests.cpp @ 692

Revision 692, 4.5 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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