[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 "RadixSortTests.h" |
---|
| 30 | #include "OgreRadixSort.h" |
---|
| 31 | #include "OgreMath.h" |
---|
| 32 | |
---|
| 33 | using namespace Ogre; |
---|
| 34 | |
---|
| 35 | // Regsiter the suite |
---|
| 36 | CPPUNIT_TEST_SUITE_REGISTRATION( RadixSortTests ); |
---|
| 37 | |
---|
| 38 | void RadixSortTests::setUp() |
---|
| 39 | { |
---|
| 40 | srand(time(0)); |
---|
| 41 | } |
---|
| 42 | void RadixSortTests::tearDown() |
---|
| 43 | { |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | class FloatSortFunctor |
---|
| 47 | { |
---|
| 48 | public: |
---|
| 49 | float operator()(const float& p) const |
---|
| 50 | { |
---|
| 51 | return p; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | }; |
---|
| 55 | class IntSortFunctor |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | int operator()(const int& p) const |
---|
| 59 | { |
---|
| 60 | return p; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | class UnsignedIntSortFunctor |
---|
| 66 | { |
---|
| 67 | public: |
---|
| 68 | unsigned int operator()(const unsigned int& p) const |
---|
| 69 | { |
---|
| 70 | return p; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | void RadixSortTests::testFloatVector() |
---|
| 77 | { |
---|
| 78 | std::vector<float> container; |
---|
| 79 | FloatSortFunctor func; |
---|
| 80 | RadixSort<std::vector<float>, float, float> sorter; |
---|
| 81 | |
---|
| 82 | for (int i = 0; i < 1000; ++i) |
---|
| 83 | { |
---|
| 84 | container.push_back((float)Math::RangeRandom(-1e10, 1e10)); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | sorter.sort(container, func); |
---|
| 88 | |
---|
| 89 | std::vector<float>::iterator v = container.begin(); |
---|
| 90 | float lastValue = *v++; |
---|
| 91 | for (;v != container.end(); ++v) |
---|
| 92 | { |
---|
| 93 | CPPUNIT_ASSERT(*v >= lastValue); |
---|
| 94 | lastValue = *v; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | } |
---|
| 99 | void RadixSortTests::testFloatList() |
---|
| 100 | { |
---|
| 101 | std::list<float> container; |
---|
| 102 | FloatSortFunctor func; |
---|
| 103 | RadixSort<std::list<float>, float, float> sorter; |
---|
| 104 | |
---|
| 105 | for (int i = 0; i < 1000; ++i) |
---|
| 106 | { |
---|
| 107 | container.push_back((float)Math::RangeRandom(-1e10, 1e10)); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | sorter.sort(container, func); |
---|
| 111 | |
---|
| 112 | std::list<float>::iterator v = container.begin(); |
---|
| 113 | float lastValue = *v++; |
---|
| 114 | for (;v != container.end(); ++v) |
---|
| 115 | { |
---|
| 116 | CPPUNIT_ASSERT(*v >= lastValue); |
---|
| 117 | lastValue = *v; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | void RadixSortTests::testUnsignedIntList() |
---|
| 121 | { |
---|
| 122 | std::list<unsigned int> container; |
---|
| 123 | UnsignedIntSortFunctor func; |
---|
| 124 | RadixSort<std::list<unsigned int>, unsigned int, unsigned int> sorter; |
---|
| 125 | |
---|
| 126 | for (int i = 0; i < 1000; ++i) |
---|
| 127 | { |
---|
| 128 | container.push_back((unsigned int)Math::RangeRandom(0, 1e10)); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | sorter.sort(container, func); |
---|
| 132 | |
---|
| 133 | std::list<unsigned int>::iterator v = container.begin(); |
---|
| 134 | unsigned int lastValue = *v++; |
---|
| 135 | for (;v != container.end(); ++v) |
---|
| 136 | { |
---|
| 137 | CPPUNIT_ASSERT(*v >= lastValue); |
---|
| 138 | lastValue = *v; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | void RadixSortTests::testIntList() |
---|
| 142 | { |
---|
| 143 | std::list<int> container; |
---|
| 144 | IntSortFunctor func; |
---|
| 145 | RadixSort<std::list<int>, int, int> sorter; |
---|
| 146 | |
---|
| 147 | for (int i = 0; i < 1000; ++i) |
---|
| 148 | { |
---|
| 149 | container.push_back((int)Math::RangeRandom(-1e10, 1e10)); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | sorter.sort(container, func); |
---|
| 153 | |
---|
| 154 | std::list<int>::iterator v = container.begin(); |
---|
| 155 | int lastValue = *v++; |
---|
| 156 | for (;v != container.end(); ++v) |
---|
| 157 | { |
---|
| 158 | CPPUNIT_ASSERT(*v >= lastValue); |
---|
| 159 | lastValue = *v; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | void RadixSortTests::testUnsignedIntVector() |
---|
| 163 | { |
---|
| 164 | std::vector<unsigned int> container; |
---|
| 165 | UnsignedIntSortFunctor func; |
---|
| 166 | RadixSort<std::vector<unsigned int>, unsigned int, unsigned int> sorter; |
---|
| 167 | |
---|
| 168 | for (int i = 0; i < 1000; ++i) |
---|
| 169 | { |
---|
| 170 | container.push_back((unsigned int)Math::RangeRandom(0, 1e10)); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | sorter.sort(container, func); |
---|
| 174 | |
---|
| 175 | std::vector<unsigned int>::iterator v = container.begin(); |
---|
| 176 | unsigned int lastValue = *v++; |
---|
| 177 | for (;v != container.end(); ++v) |
---|
| 178 | { |
---|
| 179 | CPPUNIT_ASSERT(*v >= lastValue); |
---|
| 180 | lastValue = *v; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | void RadixSortTests::testIntVector() |
---|
| 184 | { |
---|
| 185 | std::vector<int> container; |
---|
| 186 | IntSortFunctor func; |
---|
| 187 | RadixSort<std::vector<int>, int, int> sorter; |
---|
| 188 | |
---|
| 189 | for (int i = 0; i < 1000; ++i) |
---|
| 190 | { |
---|
| 191 | container.push_back((int)Math::RangeRandom(-1e10, 1e10)); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | sorter.sort(container, func); |
---|
| 195 | |
---|
| 196 | std::vector<int>::iterator v = container.begin(); |
---|
| 197 | int lastValue = *v++; |
---|
| 198 | for (;v != container.end(); ++v) |
---|
| 199 | { |
---|
| 200 | CPPUNIT_ASSERT(*v >= lastValue); |
---|
| 201 | lastValue = *v; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | |
---|