source: trunk/test/trig.cpp @ 911

Last change on this file since 911 was 911, checked in by sam, 9 years ago

test: add a few Visual Studio 2010 project files and fix testsuite code
so that it builds properly.

  • 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 <cppunit/extensions/HelperMacros.h>
16#include <cppunit/TestCaller.h>
17#include <cppunit/TestCase.h>
18#include <cppunit/TestSuite.h>
19
20#include <cmath>
21
22#include "core.h"
23
24namespace lol
25{
26
27class TrigTest : public CppUnit::TestCase
28{
29    CPPUNIT_TEST_SUITE(TrigTest);
30    CPPUNIT_TEST(test_sin);
31    CPPUNIT_TEST_SUITE_END();
32
33public:
34    TrigTest() : CppUnit::TestCase("Trigonometry Test") {}
35
36    void setUp() {}
37
38    void tearDown() {}
39
40    void test_sin()
41    {
42        for (int i = -10000; i < 10000; i++)
43        {
44            double f = (double)i * (1.0 / 1000.0);
45#if defined __GNUC__
46            double a = __builtin_sin(f);
47#else
48            double a = sin(f);
49#endif
50            double b = lol_sin(f);
51            CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
52        }
53
54        for (int i = -10000; i < 10000; i++)
55        {
56            double f = (double)i * (1.0 / 100000.0);
57#if defined __GNUC__
58            double a = __builtin_sin(f);
59#else
60            double a = sin(f);
61#endif
62            double b = lol_sin(f);
63            CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
64        }
65
66        for (int i = -10000; i < 10000; i++)
67        {
68            double f = (double)i * (1.0 / 1000.0);
69#if defined __GNUC__
70            double a = __builtin_cos(f);
71#else
72            double a = cos(f);
73#endif
74            double b = lol_cos(f);
75            CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
76        }
77
78        for (int i = -10000; i < 10000; i++)
79        {
80            double f = (double)i * (1.0 / 100000.0);
81#if defined __GNUC__
82            double a = __builtin_cos(f);
83#else
84            double a = cos(f);
85#endif
86            double b = lol_cos(f);
87            CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
88        }
89
90        for (int i = -10000; i < 10000; i++)
91        {
92            double f = (double)i * (1.0 / 1000.0);
93#if defined __GNUC__
94            double a1 = __builtin_sin(f);
95            double a2 = __builtin_cos(f);
96#else
97            double a1 = sin(f);
98            double a2 = cos(f);
99#endif
100            double b1, b2;
101            lol_sincos(f, &b1, &b2);
102            CPPUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
103            CPPUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
104        }
105
106        for (int i = -10000; i < 10000; i++)
107        {
108            double f = (double)i * (1.0 / 100000.0);
109#if defined __GNUC__
110            double a1 = __builtin_sin(f);
111            double a2 = __builtin_cos(f);
112#else
113            double a1 = sin(f);
114            double a2 = cos(f);
115#endif
116            double b1, b2;
117            lol_sincos(f, &b1, &b2);
118            CPPUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
119            CPPUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
120        }
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            if (fabs(a) > 1e4)
132                CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
133            else if (fabs(a) > 1.0)
134                CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
135            else
136                CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
137        }
138
139        for (int i = -10000; i < 10000; i++)
140        {
141            double f = (double)i * (1.0 / 100000.0);
142#if defined __GNUC__
143            double a = __builtin_tan(f);
144#else
145            double a = tan(f);
146#endif
147            double b = lol_tan(f);
148            if (fabs(a) > 1e4)
149                CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
150            else if (fabs(a) > 1.0)
151                CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
152            else
153                CPPUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
154        }
155    }
156};
157
158CPPUNIT_TEST_SUITE_REGISTRATION(TrigTest);
159
160} /* namespace lol */
161
Note: See TracBrowser for help on using the repository browser.