source: trunk/test/half.cpp @ 868

Last change on this file since 868 was 868, checked in by sam, 8 years ago

test: add first test routines for the half class.

  • Property svn:keywords set to Id
File size: 2.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 "core.h"
21
22namespace lol
23{
24
25class HalfTest : public CppUnit::TestCase
26{
27    CPPUNIT_TEST_SUITE(HalfTest);
28    CPPUNIT_TEST(test_half_makebits);
29    CPPUNIT_TEST(test_half_makeslow);
30    CPPUNIT_TEST(test_half_makefast);
31    CPPUNIT_TEST(test_half_to_float);
32    CPPUNIT_TEST_SUITE_END();
33
34public:
35    HalfTest() : CppUnit::TestCase("Matrix Test") {}
36
37    void setUp()
38    {
39    }
40
41    void tearDown() {}
42
43    void test_half_makebits()
44    {
45        for (unsigned int i = 0; i < 0x10000; i++)
46        {
47            half a = half::makebits(i);
48            uint16_t b = i;
49            CPPUNIT_ASSERT_EQUAL(a.bits(), b);
50        }
51    }
52
53    void test_half_makeslow()
54    {
55        for (size_t i = 0; i < 8; i++)
56        {
57            half a = half::makeslow(array1[i].f);
58            uint16_t b = array1[i].x;
59            CPPUNIT_ASSERT_EQUAL(a.bits(), b);
60        }
61    }
62
63    void test_half_makefast()
64    {
65        for (size_t i = 0; i < 8; i++)
66        {
67            half a = half::makefast(array1[i].f);
68            uint16_t b = array1[i].x;
69            CPPUNIT_ASSERT_EQUAL(a.bits(), b);
70        }
71    }
72
73    void test_half_to_float()
74    {
75#if 0
76        for (unsigned int i = 0; i < 0x10000; i++)
77        {
78            half a = half::makebits(i);
79            half b = half::makefast((float)a);
80            //CPPUNIT_ASSERT_EQUAL(a.bits(), b.bits());
81        }
82    }
83#endif
84    }
85
86private:
87    struct TestPair { float f; uint16_t x; };
88
89    static TestPair const array1[];
90};
91
92HalfTest::TestPair const HalfTest::array1[] =
93{
94    { 0.0f,     0x0000 },
95    { -0.0f,    0x8000 },
96    { 1.0f,     0x3c00 },
97    { -1.0f,    0xbc00 },
98    { 2.0f,     0x4000 },
99    { 0.5f,     0x3800 },
100    { 0.125f,   0x3000 },
101    { 15.9375f, 0x4bf8 },
102};
103
104CPPUNIT_TEST_SUITE_REGISTRATION(HalfTest);
105
106} /* namespace lol */
107
Note: See TracBrowser for help on using the repository browser.