108 linhas
2.3 KiB

  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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cppunit/extensions/HelperMacros.h>
  14. #include <cppunit/TestCaller.h>
  15. #include <cppunit/TestCase.h>
  16. #include <cppunit/TestSuite.h>
  17. #include "core.h"
  18. namespace lol
  19. {
  20. class HalfTest : public CppUnit::TestCase
  21. {
  22. CPPUNIT_TEST_SUITE(HalfTest);
  23. CPPUNIT_TEST(test_half_makebits);
  24. CPPUNIT_TEST(test_half_makeslow);
  25. CPPUNIT_TEST(test_half_makefast);
  26. CPPUNIT_TEST(test_half_to_float);
  27. CPPUNIT_TEST_SUITE_END();
  28. public:
  29. HalfTest() : CppUnit::TestCase("Matrix Test") {}
  30. void setUp()
  31. {
  32. }
  33. void tearDown() {}
  34. void test_half_makebits()
  35. {
  36. for (unsigned int i = 0; i < 0x10000; i++)
  37. {
  38. half a = half::makebits(i);
  39. uint16_t b = i;
  40. CPPUNIT_ASSERT_EQUAL(a.bits(), b);
  41. }
  42. }
  43. void test_half_makeslow()
  44. {
  45. for (size_t i = 0; i < 8; i++)
  46. {
  47. half a = half::makeslow(array1[i].f);
  48. uint16_t b = array1[i].x;
  49. CPPUNIT_ASSERT_EQUAL(a.bits(), b);
  50. }
  51. }
  52. void test_half_makefast()
  53. {
  54. for (size_t i = 0; i < 8; i++)
  55. {
  56. half a = half::makefast(array1[i].f);
  57. uint16_t b = array1[i].x;
  58. CPPUNIT_ASSERT_EQUAL(a.bits(), b);
  59. }
  60. }
  61. void test_half_to_float()
  62. {
  63. #if 0
  64. for (unsigned int i = 0; i < 0x10000; i++)
  65. {
  66. half a = half::makebits(i);
  67. half b = half::makefast((float)a);
  68. //CPPUNIT_ASSERT_EQUAL(a.bits(), b.bits());
  69. }
  70. }
  71. #endif
  72. }
  73. private:
  74. struct TestPair { float f; uint16_t x; };
  75. static TestPair const array1[];
  76. };
  77. HalfTest::TestPair const HalfTest::array1[] =
  78. {
  79. { 0.0f, 0x0000 },
  80. { -0.0f, 0x8000 },
  81. { 1.0f, 0x3c00 },
  82. { -1.0f, 0xbc00 },
  83. { 2.0f, 0x4000 },
  84. { 0.5f, 0x3800 },
  85. { 0.125f, 0x3000 },
  86. { 15.9375f, 0x4bf8 },
  87. };
  88. CPPUNIT_TEST_SUITE_REGISTRATION(HalfTest);
  89. } /* namespace lol */