source: trunk/test/unit/real.cpp @ 994

Last change on this file since 994 was 994, checked in by sam, 11 years ago

core: implement shift operators for reals; they're useful for fast
multiplications by integers, especially powers of two.

File size: 5.2 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cmath>
16
17#include "core.h"
18#include "lol/unit.h"
19
20namespace lol
21{
22
23LOLUNIT_FIXTURE(RealTest)
24{
25    LOLUNIT_TEST(FloatToReal)
26    {
27        float a1 = real(0.0f);
28        float a2 = real(-0.0f);
29        float a3 = real(1.0f);
30        float a4 = real(-1.0f);
31        float a5 = real(1.5f);
32        float a6 = real(12345678.0f);
33
34        LOLUNIT_ASSERT_EQUAL(a1, 0.0f);
35        LOLUNIT_ASSERT_EQUAL(a2, -0.0f);
36        LOLUNIT_ASSERT_EQUAL(a3, 1.0f);
37        LOLUNIT_ASSERT_EQUAL(a4, -1.0f);
38        LOLUNIT_ASSERT_EQUAL(a5, 1.5f);
39        LOLUNIT_ASSERT_EQUAL(a6, 12345678.0f);
40    }
41
42    LOLUNIT_TEST(DoubleToReal)
43    {
44        double a1 = real(0.0);
45        double a2 = real(-0.0);
46        double a3 = real(1.0);
47        double a4 = real(-1.0);
48        double a5 = real(1.5);
49        double a6 = real(1234567876543210.0);
50
51        LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, 0.0, 0.0);
52        LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, -0.0, 0.0);
53        LOLUNIT_ASSERT_DOUBLES_EQUAL(a3, 1.0, 0.0);
54        LOLUNIT_ASSERT_DOUBLES_EQUAL(a4, -1.0, 0.0);
55        LOLUNIT_ASSERT_DOUBLES_EQUAL(a5, 1.5, 0.0);
56        LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 1234567876543210.0, 0.0);
57    }
58
59    LOLUNIT_TEST(UnaryMinus)
60    {
61        float a1 = - real(1.0f);
62        float a2 = - real(-1.0f);
63        float a3 = - real(0.0f);
64        float a4 = - real(-0.0f);
65
66        LOLUNIT_ASSERT_EQUAL(a1, -1.0f);
67        LOLUNIT_ASSERT_EQUAL(a2, 1.0f);
68        LOLUNIT_ASSERT_EQUAL(a3, -0.0f);
69        LOLUNIT_ASSERT_EQUAL(a4, 0.0f);
70    }
71
72    LOLUNIT_TEST(Comparison)
73    {
74        LOLUNIT_ASSERT(real(1.0f) > real(0.5f));
75        LOLUNIT_ASSERT(real(1.0f) >= real(0.5f));
76        LOLUNIT_ASSERT(real(1.0f) >= real(1.0f));
77
78        LOLUNIT_ASSERT(real(-1.0f) < real(-0.5f));
79        LOLUNIT_ASSERT(real(-1.0f) <= real(-0.5f));
80        LOLUNIT_ASSERT(real(-1.0f) <= real(-1.0f));
81
82        LOLUNIT_ASSERT(real(-1.0f) < real(0.5f));
83        LOLUNIT_ASSERT(real(-0.5f) < real(1.0f));
84        LOLUNIT_ASSERT(real(-1.0f) <= real(0.5f));
85        LOLUNIT_ASSERT(real(-0.5f) <= real(1.0f));
86
87        LOLUNIT_ASSERT(real(1.0f) > real(-0.5f));
88        LOLUNIT_ASSERT(real(0.5f) > real(-1.0f));
89        LOLUNIT_ASSERT(real(1.0f) >= real(-0.5f));
90        LOLUNIT_ASSERT(real(0.5f) >= real(-1.0f));
91    }
92
93    LOLUNIT_TEST(Addition)
94    {
95        float a1 = real(1.0f) + real(0.0f);
96        float a2 = real(0.0f) + real(1.0f);
97        float a3 = real(1.0f) + real(1.0f);
98        float a4 = real(-1.0f) + real(-1.0f);
99        float a5 = real(1.0f) + real(0.125f);
100        double a6 = real(3.13609818956293918)
101                  + real(0.00005972154828114);
102        float a7 = real(1.0f) + real(-0.125f);
103        double a8 = real(0.10000000002) + real(-2.0e-11);
104
105        LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
106        LOLUNIT_ASSERT_EQUAL(a2, 1.0f);
107        LOLUNIT_ASSERT_EQUAL(a3, 2.0f);
108        LOLUNIT_ASSERT_EQUAL(a4, -2.0f);
109        LOLUNIT_ASSERT_EQUAL(a5, 1.125f);
110        LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 3.1361579, 0.000001);
111        LOLUNIT_ASSERT_EQUAL(a7, 0.875f);
112        LOLUNIT_ASSERT_DOUBLES_EQUAL(a8, 0.1, 1.0e-13);
113    }
114
115    LOLUNIT_TEST(Subtraction)
116    {
117        float a1 = real(1.0f) + real(1e20f) - real(1e20f);
118
119        LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
120    }
121
122    LOLUNIT_TEST(Multiplication)
123    {
124        real x(1.25f);
125        real y(1.5f);
126        real z(1.99999f);
127        real w(-1.5f);
128
129        float m1 = x * x;
130        float m2 = y * y;
131        float m3 = z * z;
132        float m4 = w * w;
133
134        LOLUNIT_ASSERT_EQUAL(m1, 1.25f * 1.25f);
135        LOLUNIT_ASSERT_EQUAL(m2, 1.5f * 1.5f);
136        LOLUNIT_ASSERT_EQUAL(m3, 1.99999f * 1.99999f);
137        LOLUNIT_ASSERT_EQUAL(m4, -1.5f * -1.5f);
138    }
139
140    LOLUNIT_TEST(Division)
141    {
142        real a1(1.0f);
143        real a2(2.0f);
144
145        float m1 = a1 / a1;
146        float m2 = a2 / a1;
147        float m3 = a1 / a2;
148        float m4 = a2 / a2;
149        float m5 = a1 / -a2;
150
151        LOLUNIT_ASSERT_EQUAL(m1, 1.0f);
152        LOLUNIT_ASSERT_EQUAL(m2, 2.0f);
153        LOLUNIT_ASSERT_EQUAL(m3, 0.5f);
154        LOLUNIT_ASSERT_EQUAL(m4, 1.0f);
155        LOLUNIT_ASSERT_EQUAL(m5, -0.5f);
156    }
157
158    LOLUNIT_TEST(Shift)
159    {
160        real a1(1.5);
161        real a2(-1.5);
162        real a3(0.0);
163
164        LOLUNIT_ASSERT_EQUAL((double)(a1 >> 7), 0.01171875);
165        LOLUNIT_ASSERT_EQUAL((double)(a1 >> -7), 192.0);
166        LOLUNIT_ASSERT_EQUAL((double)(a1 << 7), 192.0);
167        LOLUNIT_ASSERT_EQUAL((double)(a1 << -7), 0.01171875);
168
169        LOLUNIT_ASSERT_EQUAL((double)(a2 >> 7), -0.01171875);
170        LOLUNIT_ASSERT_EQUAL((double)(a2 >> -7), -192.0);
171        LOLUNIT_ASSERT_EQUAL((double)(a2 << 7), -192.0);
172        LOLUNIT_ASSERT_EQUAL((double)(a2 << -7), -0.01171875);
173
174        LOLUNIT_ASSERT_EQUAL((double)(a3 >> 7), 0.0);
175        LOLUNIT_ASSERT_EQUAL((double)(a3 >> -7), 0.0);
176        LOLUNIT_ASSERT_EQUAL((double)(a3 << 7), 0.0);
177        LOLUNIT_ASSERT_EQUAL((double)(a3 << -7), 0.0);
178    }
179};
180
181} /* namespace lol */
182
Note: See TracBrowser for help on using the repository browser.