1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "lib/self_test.h"
19 
20 #include "maths/Sqrt.h"
21 
22 #include <boost/random/mersenne_twister.hpp>
23 #include <boost/random/uniform_int.hpp>
24 #include <boost/random/variate_generator.hpp>
25 
26 class TestSqrt : public CxxTest::TestSuite
27 {
28 public:
t(u32 n)29 	void t(u32 n)
30 	{
31 		TS_ASSERT_EQUALS(isqrt64((u64)n*(u64)n), n);
32 	}
33 
s(u64 n,u64 exp)34 	void s(u64 n, u64 exp)
35 	{
36 		TS_ASSERT_EQUALS((u64)isqrt64(n), exp);
37 	}
38 
test_sqrt()39 	void test_sqrt()
40 	{
41 		t(0);
42 		t(1);
43 		t(2);
44 		t(255);
45 		t(256);
46 		t(257);
47 		t(65535);
48 		t(65536);
49 		t(65537);
50 		t(16777215);
51 		t(16777216);
52 		t(16777217);
53 		t(2147483647);
54 		t(2147483648u);
55 		t(2147483649u);
56 		t(4294967295u);
57 
58 		s(2, 1);
59 		s(3, 1);
60 		s(4, 2);
61 		s(255, 15);
62 		s(256, 16);
63 		s(257, 16);
64 		s(65535, 255);
65 		s(65536, 256);
66 		s(65537, 256);
67 		s(999999, 999);
68 		s(1000000, 1000);
69 		s(1000001, 1000);
70 		s((u64)-1, 4294967295u);
71 	}
72 
test_random()73 	void test_random()
74 	{
75 		// Test with some random u64s, to make sure the output agrees with floor(sqrt(double))
76 		// (TODO: This might be making non-portable assumptions about sqrt(double))
77 
78 		boost::mt19937 rng;
79 		boost::uniform_int<u64> ints(0, (u64)-1);
80 		boost::variate_generator<boost::mt19937&, boost::uniform_int<u64> > gen(rng, ints);
81 
82 		for (size_t i = 0; i < 1024; ++i)
83 		{
84 			u64 n = gen();
85 			s(n, (u64)sqrt((double)n));
86 		}
87 	}
88 };
89