source: trunk/test/unit/trig.cpp @ 1146

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

win32: some compilation fixes here and there.

  • Property svn:keywords set to Id
File size: 4.4 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(TrigTest)
24{
25    LOLUNIT_TEST(Sin)
26    {
27        for (int i = -10000; i < 10000; i++)
28        {
29            double f = (double)i * (1.0 / 1000.0);
30#if defined __GNUC__
31            double a = __builtin_sin(f);
32#else
33            double a = sin(f);
34#endif
35            double b = lol_sin(f);
36            LOLUNIT_SET_CONTEXT(f);
37            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
38        }
39
40        for (int i = -10000; i < 10000; i++)
41        {
42            double f = (double)i * (1.0 / 100000.0);
43#if defined __GNUC__
44            double a = __builtin_sin(f);
45#else
46            double a = sin(f);
47#endif
48            double b = lol_sin(f);
49            LOLUNIT_SET_CONTEXT(f);
50            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
51        }
52    }
53
54    LOLUNIT_TEST(Cos)
55    {
56        for (int i = -10000; i < 10000; i++)
57        {
58            double f = (double)i * (1.0 / 1000.0);
59#if defined __GNUC__
60            double a = __builtin_cos(f);
61#else
62            double a = cos(f);
63#endif
64            double b = lol_cos(f);
65            LOLUNIT_SET_CONTEXT(f);
66            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
67        }
68
69        for (int i = -10000; i < 10000; i++)
70        {
71            double f = (double)i * (1.0 / 100000.0);
72#if defined __GNUC__
73            double a = __builtin_cos(f);
74#else
75            double a = cos(f);
76#endif
77            double b = lol_cos(f);
78            LOLUNIT_SET_CONTEXT(f);
79            LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
80        }
81    }
82
83    LOLUNIT_TEST(SinCos)
84    {
85        for (int i = -10000; i < 10000; i++)
86        {
87            double f = (double)i * (1.0 / 1000.0);
88#if defined __GNUC__
89            double a1 = __builtin_sin(f);
90            double a2 = __builtin_cos(f);
91#else
92            double a1 = sin(f);
93            double a2 = cos(f);
94#endif
95            double b1, b2;
96            lol_sincos(f, &b1, &b2);
97            LOLUNIT_SET_CONTEXT(f);
98            LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, std::fabs(f) * 1e-11);
99            LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, std::fabs(f) * 1e-11);
100        }
101
102        for (int i = -10000; i < 10000; i++)
103        {
104            double f = (double)i * (1.0 / 100000.0);
105#if defined __GNUC__
106            double a1 = __builtin_sin(f);
107            double a2 = __builtin_cos(f);
108#else
109            double a1 = sin(f);
110            double a2 = cos(f);
111#endif
112            double b1, b2;
113            lol_sincos(f, &b1, &b2);
114            LOLUNIT_SET_CONTEXT(f);
115            LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, std::fabs(f) * 1e-11);
116            LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, std::fabs(f) * 1e-11);
117        }
118    }
119
120    LOLUNIT_TEST(Tan)
121    {
122        for (int i = -100000; i < 100000; i++)
123        {
124            double f = (double)i * (1.0 / 10000.0);
125#if defined __GNUC__
126            double a = __builtin_tan(f);
127#else
128            double a = tan(f);
129#endif
130            double b = lol_tan(f);
131            LOLUNIT_SET_CONTEXT(f);
132            if (std::fabs(a) > 1e4)
133                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * std::fabs(a) * 1e-11);
134            else if (std::fabs(a) > 1.0)
135                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * 1e-11);
136            else
137                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
138        }
139
140        for (int i = -10000; i < 10000; i++)
141        {
142            double f = (double)i * (1.0 / 100000.0);
143#if defined __GNUC__
144            double a = __builtin_tan(f);
145#else
146            double a = tan(f);
147#endif
148            double b = lol_tan(f);
149            LOLUNIT_SET_CONTEXT(f);
150            if (std::fabs(a) > 1e4)
151                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * std::fabs(a) * 1e-11);
152            else if (std::fabs(a) > 1.0)
153                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(a) * 1e-11);
154            else
155                LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, std::fabs(f) * 1e-11);
156        }
157    }
158};
159
160} /* namespace lol */
161
Note: See TracBrowser for help on using the repository browser.