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