Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / test / src / TestAngle.cpp
bloba5d0d78484152c8a1101f42419e1b81037f937a3
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Math/Angle.hpp"
24 #include "TestUtil.hpp"
26 #include <stdio.h>
28 int main(int argc, char **argv)
30 plan_tests(105);
32 // Test Native() and Native()
33 ok1(equals(Angle::Native(fixed(0)).Native(), fixed(0)));
34 ok1(equals(Angle::Native(fixed(1)).Native(), fixed(1)));
35 ok1(equals(Angle::Native(fixed(2)).Native(), fixed(2)));
36 ok1(equals(Angle::Native(fixed(90)).Native(), fixed(90)));
38 // Test zero()
39 ok1(equals(Angle::Zero().Native(), fixed(0)));
41 // test constants
42 ok1(equals(Angle::QuarterCircle().Radians(), fixed_half_pi));
43 ok1(equals(Angle::HalfCircle().Radians(), fixed_pi));
44 ok1(equals((Angle::HalfCircle() + Angle::QuarterCircle()).Radians(),
45 fixed_pi + fixed_half_pi));
46 ok1(equals(Angle::FullCircle().Radians(), fixed_two_pi));
48 // Test degrees()
49 #ifdef RADIANS
50 ok1(equals(Angle::Degrees(0).Native(), fixed(0)));
51 ok1(equals(Angle::Degrees(90).Native(), fixed_half_pi));
52 ok1(equals(Angle::Degrees(180).Native(), fixed_pi));
53 ok1(equals(Angle::Degrees(270).Native(), fixed_pi + fixed_half_pi));
54 ok1(equals(Angle::Degrees(360).Native(), fixed_two_pi));
56 ok1(equals(Angle::Radians(fixed(0)).Native(), fixed(0)));
57 ok1(equals(Angle::Radians(fixed_half_pi).Native(), fixed_half_pi));
58 ok1(equals(Angle::Radians(fixed_pi).Native(), fixed_pi));
59 ok1(equals(Angle::Radians(fixed_pi + fixed_half_pi).Native(),
60 fixed_pi + fixed_half_pi));
61 ok1(equals(Angle::Radians(fixed_two_pi).Native(), fixed_two_pi));
62 #else
63 ok1(equals(Angle::Degrees(0).Native(), fixed(0)));
64 ok1(equals(Angle::Degrees(90).Native(), fixed(90)));
65 ok1(equals(Angle::Degrees(180).Native(), fixed(180)));
66 ok1(equals(Angle::Degrees(270).Native(), fixed(270)));
67 ok1(equals(Angle::Degrees(360).Native(), fixed(360)));
69 ok1(equals(Angle::Radians(fixed(0)).Native(), fixed(0)));
70 ok1(equals(Angle::Radians(fixed_half_pi).Native(), fixed(90)));
71 ok1(equals(Angle::Radians(fixed_pi).Native(), fixed(180)));
72 ok1(equals(Angle::Radians(fixed_pi + fixed_half_pi).Native(), fixed(270)));
73 ok1(equals(Angle::Radians(fixed_two_pi).Native(), fixed(360)));
74 #endif
76 // Test Degrees()
77 ok1(equals(Angle::Degrees(90).Degrees(), 90));
78 ok1(equals(Angle::Degrees(-90).Degrees(), -90));
80 // Test Radians()
81 ok1(equals(Angle::Degrees(90).Radians(), fixed_half_pi));
82 ok1(equals(Angle::Degrees(-90).Radians(), -fixed_half_pi));
84 // Test Hours()
85 ok1(equals(Angle::Degrees(90).Hours(), fixed(6)));
86 ok1(equals(Angle::Degrees(-90).Hours(), -fixed(6)));
88 // Test DMS()
89 ok1(equals(Angle::DMS(90, 30, 15).Degrees(),
90 fixed(90.504166667)));
92 // Test AbsoluteDegrees()
93 ok1(equals(Angle::Degrees(90).AbsoluteDegrees(), 90));
94 ok1(equals(Angle::Degrees(-90).AbsoluteDegrees(), 90));
96 // Test AbsoluteRadians()
97 ok1(equals(Angle::Degrees(90).AbsoluteRadians(), fixed_half_pi));
98 ok1(equals(Angle::Degrees(-90).AbsoluteRadians(), fixed_half_pi));
100 // Test Reciprocal()
101 ok1(equals(Angle::Degrees(90).Reciprocal(), 270));
102 ok1(equals(Angle::Degrees(270).Reciprocal(), 90));
104 // Test AsBearing()
105 ok1(equals(Angle::Degrees(361).AsBearing(), 1));
106 ok1(equals(Angle::Degrees(180).AsBearing(), 180));
107 ok1(equals(Angle::Degrees(-180).AsBearing(), 180));
108 ok1(equals(Angle::Degrees(-270).AsBearing(), 90));
110 // Test AsDelta()
111 ok1(equals(Angle::Degrees(90).AsDelta(), 90));
112 ok1(equals(Angle::Degrees(179).AsDelta(), 179));
113 ok1(equals(Angle::Degrees(-179).AsDelta(), -179));
114 ok1(equals(Angle::Degrees(270).AsDelta(), -90));
116 // Test Between()
117 ok1(Angle::QuarterCircle().Between(Angle::Zero(), Angle::HalfCircle()));
118 ok1(!Angle::QuarterCircle().Between(Angle::HalfCircle(), Angle::Zero()));
119 ok1(Angle::Zero().Between(Angle::Degrees(270), Angle::Degrees(90)));
121 // Test sin()
122 ok1(equals(Angle::Zero().sin(), 0));
123 ok1(equals(Angle::Degrees(90).sin(), 1));
124 ok1(equals(Angle::Degrees(180).sin(), 0));
125 ok1(equals(Angle::Degrees(270).sin(), -1));
126 ok1(equals(Angle::Degrees(360).sin(), 0));
128 // Test cos()
129 ok1(equals(Angle::Zero().cos(), 1));
130 ok1(equals(Angle::Degrees(90).cos(), 0));
131 ok1(equals(Angle::Degrees(180).cos(), -1));
132 ok1(equals(Angle::Degrees(270).cos(), 0));
133 ok1(equals(Angle::Degrees(360).cos(), 1));
135 // Test fastsine()
136 ok1(equals(Angle::Zero().fastsine(), 0));
137 ok1(equals(Angle::Degrees(90).fastsine(), 1));
138 ok1(equals(Angle::Degrees(180).fastsine(), 0));
139 ok1(equals(Angle::Degrees(270).fastsine(), -1));
140 ok1(equals(Angle::Degrees(360).fastsine(), 0));
142 // Test fastcosine()
143 ok1(equals(Angle::Zero().fastcosine(), 1));
144 ok1(equals(Angle::Degrees(90).fastcosine(), 0));
145 ok1(equals(Angle::Degrees(180).fastcosine(), -1));
146 ok1(equals(Angle::Degrees(270).fastcosine(), 0));
147 ok1(equals(Angle::Degrees(360).fastcosine(), 1));
149 // Test BiSector()
150 ok1(equals(Angle::Degrees(270).HalfAngle(Angle::Degrees(180)), 45));
151 ok1(equals(Angle::Degrees(90).HalfAngle(Angle::Zero()), 225));
152 ok1(equals(Angle::Degrees(90).HalfAngle(Angle::Degrees(180)), 315));
154 // Test Fraction()
155 ok1(equals(Angle::Zero().Fraction(Angle::Degrees(90), fixed(0.25)), 22.5));
156 ok1(equals(Angle::Zero().Fraction(Angle::Degrees(90), fixed(0.5)), 45));
157 ok1(equals(Angle::Zero().Fraction(Angle::Degrees(90), fixed(0.75)), 67.5));
159 // Test Sign()
160 ok1(Angle::Degrees(90).Sign() == 1);
161 ok1(Angle::Zero().Sign() == 0);
162 ok1(Angle::Degrees(-90).Sign() == -1);
164 ok1(Angle::Degrees(90).Sign(fixed(1)) == 1);
165 ok1(Angle::Degrees(0.5).Sign(fixed(1)) == 0);
166 ok1(Angle::Zero().Sign(fixed(1)) == 0);
167 ok1(Angle::Degrees(-0.5).Sign(fixed(1)) == 0);
168 ok1(Angle::Degrees(-90).Sign(fixed(1)) == -1);
170 // Test SinCos()
171 const auto sc = Angle::Degrees(45).SinCos();
172 fixed sin = sc.first, cos = sc.second;
173 ok1(equals(sin, Angle::Degrees(45).sin()));
174 ok1(equals(cos, Angle::Degrees(45).cos()));
176 // Test Flip()
177 Angle a = Angle::Degrees(90);
178 a.Flip();
179 ok1(equals(a.Degrees(), -fixed(90)));
181 // Test Flipped()
182 ok1(equals(Angle::Zero().Flipped().Degrees(), fixed(0)));
183 ok1(equals(Angle::Degrees(90).Flipped().Degrees(), -fixed(90)));
184 ok1(equals(Angle::Degrees(180).Flipped().Degrees(), -fixed(180)));
185 ok1(equals(Angle::Degrees(270).Flipped().Degrees(), -fixed(270)));
187 // Test Half()
188 ok1(equals(Angle::Degrees(90).Half().Degrees(), fixed(45)));
189 ok1(equals(Angle::Degrees(180).Half().Degrees(), fixed(90)));
191 // Test FromXY()
192 ok1(equals(Angle::FromXY(fixed(1), fixed(0)), 0));
193 ok1(equals(Angle::FromXY(fixed(1), fixed(1)), 45));
194 ok1(equals(Angle::FromXY(fixed(0), fixed(1)), 90));
195 ok1(equals(Angle::FromXY(fixed(-1), fixed(1)), 135));
196 ok1(equals(Angle::FromXY(fixed(-1), fixed(0)), 180));
197 ok1(equals(Angle::FromXY(fixed(0), fixed(-1)), -90));
198 ok1(equals(Angle::FromXY(fixed(1), fixed(-1)), -45));
200 // Test CompareRoughly()
201 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(0)));
202 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(5)));
203 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(9.9)));
204 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(10)));
205 ok1(!Angle::Degrees(0).CompareRoughly(Angle::Degrees(10.1)));
206 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(-9.9)));
207 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(-10)));
208 ok1(!Angle::Degrees(0).CompareRoughly(Angle::Degrees(-10.1)));
210 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(4), Angle::Degrees(5)));
211 ok1(Angle::Degrees(0).CompareRoughly(Angle::Degrees(-4), Angle::Degrees(5)));
212 ok1(!Angle::Degrees(0).CompareRoughly(Angle::Degrees(6), Angle::Degrees(5)));
213 ok1(!Angle::Degrees(0).CompareRoughly(Angle::Degrees(-6), Angle::Degrees(5)));
215 return exit_status();