File size: 9,858 Bytes
a93901d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
#include <glm/gtc/random.hpp>
#include <glm/gtc/epsilon.hpp>
#include <glm/gtc/type_precision.hpp>
#if GLM_LANG & GLM_LANG_CXX0X_FLAG
#	include <array>
#endif

std::size_t const TestSamples = 10000;

int test_linearRand()
{
	int Error = 0;

	glm::int32 const Min = 16;
	glm::int32 const Max = 32;

	{
		glm::u8vec2 AMin(std::numeric_limits<glm::u8>::max());
		glm::u8vec2 AMax(std::numeric_limits<glm::u8>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::u8vec2 A = glm::linearRand(glm::u8vec2(Min), glm::u8vec2(Max));
				AMin = glm::min(AMin, A);
				AMax = glm::max(AMax, A);

				if(!glm::all(glm::lessThanEqual(A, glm::u8vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(A, glm::u8vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(AMin, glm::u8vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(AMax, glm::u8vec2(Max))) ? 0 : 1;
			assert(!Error);
		}

		glm::u16vec2 BMin(std::numeric_limits<glm::u16>::max());
		glm::u16vec2 BMax(std::numeric_limits<glm::u16>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::u16vec2 B = glm::linearRand(glm::u16vec2(Min), glm::u16vec2(Max));
				BMin = glm::min(BMin, B);
				BMax = glm::max(BMax, B);

				if(!glm::all(glm::lessThanEqual(B, glm::u16vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(B, glm::u16vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(BMin, glm::u16vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(BMax, glm::u16vec2(Max))) ? 0 : 1;
			assert(!Error);
		}

		glm::u32vec2 CMin(std::numeric_limits<glm::u32>::max());
		glm::u32vec2 CMax(std::numeric_limits<glm::u32>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::u32vec2 C = glm::linearRand(glm::u32vec2(Min), glm::u32vec2(Max));
				CMin = glm::min(CMin, C);
				CMax = glm::max(CMax, C);

				if(!glm::all(glm::lessThanEqual(C, glm::u32vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(C, glm::u32vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(CMin, glm::u32vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(CMax, glm::u32vec2(Max))) ? 0 : 1;
			assert(!Error);
		}

		glm::u64vec2 DMin(std::numeric_limits<glm::u64>::max());
		glm::u64vec2 DMax(std::numeric_limits<glm::u64>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::u64vec2 D = glm::linearRand(glm::u64vec2(Min), glm::u64vec2(Max));
				DMin = glm::min(DMin, D);
				DMax = glm::max(DMax, D);

				if(!glm::all(glm::lessThanEqual(D, glm::u64vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(D, glm::u64vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(DMin, glm::u64vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(DMax, glm::u64vec2(Max))) ? 0 : 1;
			assert(!Error);
		}
	}

	{
		glm::i8vec2 AMin(std::numeric_limits<glm::i8>::max());
		glm::i8vec2 AMax(std::numeric_limits<glm::i8>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::i8vec2 A = glm::linearRand(glm::i8vec2(Min), glm::i8vec2(Max));
				AMin = glm::min(AMin, A);
				AMax = glm::max(AMax, A);

				if(!glm::all(glm::lessThanEqual(A, glm::i8vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(A, glm::i8vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(AMin, glm::i8vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(AMax, glm::i8vec2(Max))) ? 0 : 1;
			assert(!Error);
		}

		glm::i16vec2 BMin(std::numeric_limits<glm::i16>::max());
		glm::i16vec2 BMax(std::numeric_limits<glm::i16>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::i16vec2 B = glm::linearRand(glm::i16vec2(Min), glm::i16vec2(Max));
				BMin = glm::min(BMin, B);
				BMax = glm::max(BMax, B);

				if(!glm::all(glm::lessThanEqual(B, glm::i16vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(B, glm::i16vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(BMin, glm::i16vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(BMax, glm::i16vec2(Max))) ? 0 : 1;
			assert(!Error);
		}

		glm::i32vec2 CMin(std::numeric_limits<glm::i32>::max());
		glm::i32vec2 CMax(std::numeric_limits<glm::i32>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::i32vec2 C = glm::linearRand(glm::i32vec2(Min), glm::i32vec2(Max));
				CMin = glm::min(CMin, C);
				CMax = glm::max(CMax, C);

				if(!glm::all(glm::lessThanEqual(C, glm::i32vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(C, glm::i32vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(CMin, glm::i32vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(CMax, glm::i32vec2(Max))) ? 0 : 1;
			assert(!Error);
		}

		glm::i64vec2 DMin(std::numeric_limits<glm::i64>::max());
		glm::i64vec2 DMax(std::numeric_limits<glm::i64>::min());
		{
			for(std::size_t i = 0; i < TestSamples; ++i)
			{
				glm::i64vec2 D = glm::linearRand(glm::i64vec2(Min), glm::i64vec2(Max));
				DMin = glm::min(DMin, D);
				DMax = glm::max(DMax, D);

				if(!glm::all(glm::lessThanEqual(D, glm::i64vec2(Max))))
					++Error;
				if(!glm::all(glm::greaterThanEqual(D, glm::i64vec2(Min))))
					++Error;
				assert(!Error);
			}

			Error += glm::all(glm::equal(DMin, glm::i64vec2(Min))) ? 0 : 1;
			Error += glm::all(glm::equal(DMax, glm::i64vec2(Max))) ? 0 : 1;
			assert(!Error);
		}
	}

	for(std::size_t i = 0; i < TestSamples; ++i)
	{
		glm::f32vec2 const A(glm::linearRand(glm::f32vec2(static_cast<float>(Min)), glm::f32vec2(static_cast<float>(Max))));
		if(!glm::all(glm::lessThanEqual(A, glm::f32vec2(static_cast<float>(Max)))))
			++Error;
		if(!glm::all(glm::greaterThanEqual(A, glm::f32vec2(static_cast<float>(Min)))))
			++Error;

		glm::f64vec2 const B(glm::linearRand(glm::f64vec2(Min), glm::f64vec2(Max)));
		if(!glm::all(glm::lessThanEqual(B, glm::f64vec2(Max))))
			++Error;
		if(!glm::all(glm::greaterThanEqual(B, glm::f64vec2(Min))))
			++Error;
		assert(!Error);
	}

	{
		float ResultFloat = 0.0f;
		double ResultDouble = 0.0;
		for(std::size_t i = 0; i < TestSamples; ++i)
		{
			ResultFloat += glm::linearRand(-1.0f, 1.0f);
			ResultDouble += glm::linearRand(-1.0, 1.0);
		}

		Error += glm::epsilonEqual(ResultFloat, 0.0f, 0.0001f);
		Error += glm::epsilonEqual(ResultDouble, 0.0, 0.0001);
		assert(!Error);
	}

	return Error;
}

int test_circularRand()
{
	int Error = 0;

	{
		std::size_t Max = TestSamples;
		float ResultFloat = 0.0f;
		double ResultDouble = 0.0;
		double Radius = 2.0;

		for(std::size_t i = 0; i < Max; ++i)
		{
			ResultFloat += glm::length(glm::circularRand(1.0f));
			ResultDouble += glm::length(glm::circularRand(Radius));
		}

		Error += glm::epsilonEqual(ResultFloat, float(Max), 0.01f) ? 0 : 1;
		Error += glm::epsilonEqual(ResultDouble, double(Max) * double(Radius), 0.01) ? 0 : 1;
		assert(!Error);
	}

	return Error;
}

int test_sphericalRand()
{
	int Error = 0;

	{
		std::size_t Max = TestSamples;
		float ResultFloatA = 0.0f;
		float ResultFloatB = 0.0f;
		float ResultFloatC = 0.0f;
		double ResultDoubleA = 0.0;
		double ResultDoubleB = 0.0;
		double ResultDoubleC = 0.0;

		for(std::size_t i = 0; i < Max; ++i)
		{
			ResultFloatA += glm::length(glm::sphericalRand(1.0f));
			ResultDoubleA += glm::length(glm::sphericalRand(1.0));
			ResultFloatB += glm::length(glm::sphericalRand(2.0f));
			ResultDoubleB += glm::length(glm::sphericalRand(2.0));
			ResultFloatC += glm::length(glm::sphericalRand(3.0f));
			ResultDoubleC += glm::length(glm::sphericalRand(3.0));
		}

		Error += glm::epsilonEqual(ResultFloatA, float(Max), 0.01f) ? 0 : 1;
		Error += glm::epsilonEqual(ResultDoubleA, double(Max), 0.0001) ? 0 : 1;
		Error += glm::epsilonEqual(ResultFloatB, float(Max * 2), 0.01f) ? 0 : 1;
		Error += glm::epsilonEqual(ResultDoubleB, double(Max * 2), 0.0001) ? 0 : 1;
		Error += glm::epsilonEqual(ResultFloatC, float(Max * 3), 0.01f) ? 0 : 1;
		Error += glm::epsilonEqual(ResultDoubleC, double(Max * 3), 0.01) ? 0 : 1;
		assert(!Error);
	}

	return Error;
}

int test_diskRand()
{
	int Error = 0;

	{
		float ResultFloat = 0.0f;
		double ResultDouble = 0.0;

		for(std::size_t i = 0; i < TestSamples; ++i)
		{
			ResultFloat += glm::length(glm::diskRand(2.0f));
			ResultDouble += glm::length(glm::diskRand(2.0));
		}

		Error += ResultFloat < float(TestSamples) * 2.f ? 0 : 1;
		Error += ResultDouble < double(TestSamples) * 2.0 ? 0 : 1;
		assert(!Error);
	}

	return Error;
}

int test_ballRand()
{
	int Error = 0;

	{
		float ResultFloat = 0.0f;
		double ResultDouble = 0.0;

		for(std::size_t i = 0; i < TestSamples; ++i)
		{
			ResultFloat += glm::length(glm::ballRand(2.0f));
			ResultDouble += glm::length(glm::ballRand(2.0));
		}

		Error += ResultFloat < float(TestSamples) * 2.f ? 0 : 1;
		Error += ResultDouble < double(TestSamples) * 2.0 ? 0 : 1;
		assert(!Error);
	}

	return Error;
}
/*
#if(GLM_LANG & GLM_LANG_CXX0X_FLAG)
int test_grid()
{
	int Error = 0;

	typedef std::array<int, 8> colors;
	typedef std::array<int, 8 * 8> grid;

	grid Grid;
	colors Colors;

	grid GridBest;
	colors ColorsBest;

	while(true)
	{
		for(std::size_t i = 0; i < Grid.size(); ++i)
			Grid[i] = int(glm::linearRand(0.0, 8.0 * 8.0 * 8.0 - 1.0) / 64.0);

		for(std::size_t i = 0; i < Grid.size(); ++i)
			++Colors[Grid[i]];

		bool Exit = true;
		for(std::size_t i = 0; i < Colors.size(); ++i)
		{
			if(Colors[i] == 8)
				continue;

			Exit = false;
			break;
		}

		if(Exit == true)
			break;
	}

	return Error;
}
#endif
*/
int main()
{
	int Error = 0;

	Error += test_linearRand();
	Error += test_circularRand();
	Error += test_sphericalRand();
	Error += test_diskRand();
	Error += test_ballRand();
/*
#if(GLM_LANG & GLM_LANG_CXX0X_FLAG)
	Error += test_grid();
#endif
*/
	return Error;
}