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

Last change on this file since 964 was 942, checked in by sam, 12 years ago

test: reorganise test suite and benchmark code.

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