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

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

test: give more explicit names to unit test cases.

File size: 4.0 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(RealComparison)
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(RealAddition)
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
101        LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
102        LOLUNIT_ASSERT_EQUAL(a2, 1.0f);
103        LOLUNIT_ASSERT_EQUAL(a3, 2.0f);
104        LOLUNIT_ASSERT_EQUAL(a4, -2.0f);
105        LOLUNIT_ASSERT_EQUAL(a5, 1.125f);
106    }
107
108    LOLUNIT_TEST(RealSubtraction)
109    {
110        float a1 = real(1.0f) + real(1e20f) - real(1e20f);
111
112        LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
113    }
114
115    LOLUNIT_TEST(RealMultiplication)
116    {
117        real x(1.25f);
118        real y(1.5f);
119        real z(1.99999f);
120        real w(-1.5f);
121
122        float m1 = x * x;
123        float m2 = y * y;
124        float m3 = z * z;
125        float m4 = w * w;
126
127        LOLUNIT_ASSERT_EQUAL(m1, 1.25f * 1.25f);
128        LOLUNIT_ASSERT_EQUAL(m2, 1.5f * 1.5f);
129        LOLUNIT_ASSERT_EQUAL(m3, 1.99999f * 1.99999f);
130        LOLUNIT_ASSERT_EQUAL(m4, -1.5f * -1.5f);
131    }
132
133    LOLUNIT_TEST(RealDivision)
134    {
135        real a1(1.0f);
136        real a2(2.0f);
137
138        float m1 = a1 / a1;
139        float m2 = a2 / a1;
140        float m3 = a1 / a2;
141        float m4 = a2 / a2;
142
143        LOLUNIT_ASSERT_EQUAL(m1, 1.0f);
144        LOLUNIT_ASSERT_EQUAL(m2, 2.0f);
145        LOLUNIT_ASSERT_EQUAL(m3, 0.5f);
146        LOLUNIT_ASSERT_EQUAL(m4, 1.0f);
147    }
148};
149
150} /* namespace lol */
151
Note: See TracBrowser for help on using the repository browser.