Changeset 912 for trunk/test/half.cpp
 Timestamp:
 Sep 7, 2011, 7:17:07 PM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test/half.cpp
r911 r912 13 13 #endif 14 14 15 #include <cppunit/extensions/HelperMacros.h>16 #include <cppunit/TestCaller.h>17 #include <cppunit/TestCase.h>18 #include <cppunit/TestSuite.h>19 20 15 #include <cmath> 21 16 22 17 #include "core.h" 18 #include "lol/unit.h" 23 19 24 20 /* Ensure isnan() is present even on systems that don't define it, or … … 38 34 { 39 35 40 class HalfTest : public CppUnit::TestCase 36 LOLUNIT_FIXTURE(HalfTest) 41 37 { 42 CPPUNIT_TEST_SUITE(HalfTest);43 CPPUNIT_TEST(test_half_from_float);44 CPPUNIT_TEST(test_half_makeaccurate);45 CPPUNIT_TEST(test_half_makebits);46 CPPUNIT_TEST(test_half_is_nan);47 CPPUNIT_TEST(test_half_is_inf);48 CPPUNIT_TEST(test_half_is_finite);49 CPPUNIT_TEST(test_half_is_normal);50 CPPUNIT_TEST(test_half_classify);51 CPPUNIT_TEST(test_half_to_float);52 CPPUNIT_TEST(test_half_to_int);53 CPPUNIT_TEST(test_float_op_half);54 CPPUNIT_TEST(test_half_op_float);55 CPPUNIT_TEST_SUITE_END();56 57 38 public: 58 HalfTest() : CppUnit::TestCase("Matrix Test") {} 59 60 void setUp() 61 { 62 } 63 39 void setUp() {} 64 40 void tearDown() {} 65 41 66 void test_half_from_float()42 LOLUNIT_TEST(test_half_from_float) 67 43 { 68 44 for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++) … … 70 46 half a = (half)pairs[i].f; 71 47 uint16_t b = pairs[i].x; 72 CPPUNIT_ASSERT_EQUAL(a.bits, b);73 } 74 } 75 76 void test_half_makeaccurate()48 LOLUNIT_ASSERT_EQUAL(a.bits, b); 49 } 50 } 51 52 LOLUNIT_TEST(test_half_makeaccurate) 77 53 { 78 54 for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++) … … 80 56 half a = half::makeaccurate(pairs[i].f); 81 57 uint16_t b = pairs[i].x; 82 CPPUNIT_ASSERT_EQUAL(a.bits, b);83 } 84 } 85 86 void test_half_makebits()58 LOLUNIT_ASSERT_EQUAL(a.bits, b); 59 } 60 } 61 62 LOLUNIT_TEST(test_half_makebits) 87 63 { 88 64 for (unsigned int i = 0; i < 0x10000; i++) … … 90 66 half a = half::makebits(i); 91 67 uint16_t b = i; 92 CPPUNIT_ASSERT_EQUAL(a.bits, b);93 } 94 } 95 96 void test_half_is_nan()97 { 98 CPPUNIT_ASSERT(half::makebits(0x7c01).is_nan());99 CPPUNIT_ASSERT(half::makebits(0xfc01).is_nan());100 CPPUNIT_ASSERT(half::makebits(0x7e00).is_nan());101 CPPUNIT_ASSERT(half::makebits(0xfe00).is_nan());102 103 CPPUNIT_ASSERT(!half::makebits(0x7c00).is_nan());104 CPPUNIT_ASSERT(!half::makebits(0xfc00).is_nan());105 106 CPPUNIT_ASSERT(!half(0.0f).is_nan());107 CPPUNIT_ASSERT(!half(0.0f).is_nan());108 CPPUNIT_ASSERT(!half(2.0f).is_nan());109 CPPUNIT_ASSERT(!half(2.0f).is_nan());110 } 111 112 void test_half_is_inf()113 { 114 CPPUNIT_ASSERT(half(65536.0f).is_inf());115 CPPUNIT_ASSERT(half(65536.0f).is_inf());116 117 CPPUNIT_ASSERT(!half(0.0f).is_inf());118 CPPUNIT_ASSERT(!half(0.0f).is_inf());119 CPPUNIT_ASSERT(!half(65535.0f).is_inf());120 CPPUNIT_ASSERT(!half(65535.0f).is_inf());121 122 CPPUNIT_ASSERT(half::makebits(0x7c00).is_inf());123 CPPUNIT_ASSERT(half::makebits(0xfc00).is_inf());124 125 CPPUNIT_ASSERT(!half::makebits(0x7e00).is_inf());126 CPPUNIT_ASSERT(!half::makebits(0xfe00).is_inf());127 } 128 129 void test_half_is_finite()130 { 131 CPPUNIT_ASSERT(half(0.0f).is_finite());132 CPPUNIT_ASSERT(half(0.0f).is_finite());133 CPPUNIT_ASSERT(half(65535.0f).is_finite());134 CPPUNIT_ASSERT(half(65535.0f).is_finite());135 136 CPPUNIT_ASSERT(!half(65536.0f).is_finite());137 CPPUNIT_ASSERT(!half(65536.0f).is_finite());138 139 CPPUNIT_ASSERT(!half::makebits(0x7c00).is_finite());140 CPPUNIT_ASSERT(!half::makebits(0xfc00).is_finite());141 142 CPPUNIT_ASSERT(!half::makebits(0x7e00).is_finite());143 CPPUNIT_ASSERT(!half::makebits(0xfe00).is_finite());144 } 145 146 void test_half_is_normal()147 { 148 CPPUNIT_ASSERT(half(0.0f).is_normal());149 CPPUNIT_ASSERT(half(0.0f).is_normal());150 CPPUNIT_ASSERT(half(65535.0f).is_normal());151 CPPUNIT_ASSERT(half(65535.0f).is_normal());152 153 CPPUNIT_ASSERT(!half(65536.0f).is_normal());154 CPPUNIT_ASSERT(!half(65536.0f).is_normal());155 156 CPPUNIT_ASSERT(!half::makebits(0x7c00).is_normal());157 CPPUNIT_ASSERT(!half::makebits(0xfc00).is_normal());158 159 CPPUNIT_ASSERT(!half::makebits(0x7e00).is_normal());160 CPPUNIT_ASSERT(!half::makebits(0xfe00).is_normal());161 } 162 163 void test_half_classify()68 LOLUNIT_ASSERT_EQUAL(a.bits, b); 69 } 70 } 71 72 LOLUNIT_TEST(test_half_is_nan) 73 { 74 LOLUNIT_ASSERT(half::makebits(0x7c01).is_nan()); 75 LOLUNIT_ASSERT(half::makebits(0xfc01).is_nan()); 76 LOLUNIT_ASSERT(half::makebits(0x7e00).is_nan()); 77 LOLUNIT_ASSERT(half::makebits(0xfe00).is_nan()); 78 79 LOLUNIT_ASSERT(!half::makebits(0x7c00).is_nan()); 80 LOLUNIT_ASSERT(!half::makebits(0xfc00).is_nan()); 81 82 LOLUNIT_ASSERT(!half(0.0f).is_nan()); 83 LOLUNIT_ASSERT(!half(0.0f).is_nan()); 84 LOLUNIT_ASSERT(!half(2.0f).is_nan()); 85 LOLUNIT_ASSERT(!half(2.0f).is_nan()); 86 } 87 88 LOLUNIT_TEST(test_half_is_inf) 89 { 90 LOLUNIT_ASSERT(half(65536.0f).is_inf()); 91 LOLUNIT_ASSERT(half(65536.0f).is_inf()); 92 93 LOLUNIT_ASSERT(!half(0.0f).is_inf()); 94 LOLUNIT_ASSERT(!half(0.0f).is_inf()); 95 LOLUNIT_ASSERT(!half(65535.0f).is_inf()); 96 LOLUNIT_ASSERT(!half(65535.0f).is_inf()); 97 98 LOLUNIT_ASSERT(half::makebits(0x7c00).is_inf()); 99 LOLUNIT_ASSERT(half::makebits(0xfc00).is_inf()); 100 101 LOLUNIT_ASSERT(!half::makebits(0x7e00).is_inf()); 102 LOLUNIT_ASSERT(!half::makebits(0xfe00).is_inf()); 103 } 104 105 LOLUNIT_TEST(test_half_is_finite) 106 { 107 LOLUNIT_ASSERT(half(0.0f).is_finite()); 108 LOLUNIT_ASSERT(half(0.0f).is_finite()); 109 LOLUNIT_ASSERT(half(65535.0f).is_finite()); 110 LOLUNIT_ASSERT(half(65535.0f).is_finite()); 111 112 LOLUNIT_ASSERT(!half(65536.0f).is_finite()); 113 LOLUNIT_ASSERT(!half(65536.0f).is_finite()); 114 115 LOLUNIT_ASSERT(!half::makebits(0x7c00).is_finite()); 116 LOLUNIT_ASSERT(!half::makebits(0xfc00).is_finite()); 117 118 LOLUNIT_ASSERT(!half::makebits(0x7e00).is_finite()); 119 LOLUNIT_ASSERT(!half::makebits(0xfe00).is_finite()); 120 } 121 122 LOLUNIT_TEST(test_half_is_normal) 123 { 124 LOLUNIT_ASSERT(half(0.0f).is_normal()); 125 LOLUNIT_ASSERT(half(0.0f).is_normal()); 126 LOLUNIT_ASSERT(half(65535.0f).is_normal()); 127 LOLUNIT_ASSERT(half(65535.0f).is_normal()); 128 129 LOLUNIT_ASSERT(!half(65536.0f).is_normal()); 130 LOLUNIT_ASSERT(!half(65536.0f).is_normal()); 131 132 LOLUNIT_ASSERT(!half::makebits(0x7c00).is_normal()); 133 LOLUNIT_ASSERT(!half::makebits(0xfc00).is_normal()); 134 135 LOLUNIT_ASSERT(!half::makebits(0x7e00).is_normal()); 136 LOLUNIT_ASSERT(!half::makebits(0xfe00).is_normal()); 137 } 138 139 LOLUNIT_TEST(test_half_classify) 164 140 { 165 141 for (uint32_t i = 0; i < 0x10000; i++) … … 168 144 if (h.is_nan()) 169 145 { 170 CPPUNIT_ASSERT(!h.is_inf());171 CPPUNIT_ASSERT(!h.is_normal());172 CPPUNIT_ASSERT(!h.is_finite());146 LOLUNIT_ASSERT(!h.is_inf()); 147 LOLUNIT_ASSERT(!h.is_normal()); 148 LOLUNIT_ASSERT(!h.is_finite()); 173 149 } 174 150 else if (h.is_inf()) 175 151 { 176 CPPUNIT_ASSERT(!h.is_normal());177 CPPUNIT_ASSERT(!h.is_finite());152 LOLUNIT_ASSERT(!h.is_normal()); 153 LOLUNIT_ASSERT(!h.is_finite()); 178 154 } 179 155 else 180 156 { 181 CPPUNIT_ASSERT(h.is_finite());182 } 183 } 184 } 185 186 void test_half_to_float()157 LOLUNIT_ASSERT(h.is_finite()); 158 } 159 } 160 } 161 162 LOLUNIT_TEST(test_half_to_float) 187 163 { 188 164 for (size_t i = 0; i < sizeof(pairs) / sizeof(*pairs); i++) … … 190 166 float a = (float)half::makebits(pairs[i].x); 191 167 float b = pairs[i].f; 192 CPPUNIT_ASSERT_EQUAL(a, b);168 LOLUNIT_ASSERT_EQUAL(a, b); 193 169 } 194 170 … … 200 176 if (h.is_nan()) 201 177 { 202 CPPUNIT_ASSERT(isnan(f));203 CPPUNIT_ASSERT(g.is_nan());178 LOLUNIT_ASSERT(isnan(f)); 179 LOLUNIT_ASSERT(g.is_nan()); 204 180 } 205 181 else 206 182 { 207 CPPUNIT_ASSERT(!isnan(f));208 CPPUNIT_ASSERT_EQUAL(g.bits, h.bits);209 } 210 } 211 } 212 213 void test_half_to_int()214 { 215 CPPUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);216 CPPUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0);217 CPPUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);218 CPPUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0);219 CPPUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);220 CPPUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1);221 CPPUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);222 CPPUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1);223 CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);224 CPPUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504);225 } 226 227 void test_float_op_half()183 LOLUNIT_ASSERT(!isnan(f)); 184 LOLUNIT_ASSERT_EQUAL(g.bits, h.bits); 185 } 186 } 187 } 188 189 LOLUNIT_TEST(test_half_to_int) 190 { 191 LOLUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0); 192 LOLUNIT_ASSERT_EQUAL((int)(half)(0.0f), 0); 193 LOLUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0); 194 LOLUNIT_ASSERT_EQUAL((int)(half)(0.9f), 0); 195 LOLUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1); 196 LOLUNIT_ASSERT_EQUAL((int)(half)(1.0f), 1); 197 LOLUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1); 198 LOLUNIT_ASSERT_EQUAL((int)(half)(1.9f), 1); 199 LOLUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504); 200 LOLUNIT_ASSERT_EQUAL((int)(half)(65504.0f), 65504); 201 } 202 203 LOLUNIT_TEST(test_float_op_half) 228 204 { 229 205 half zero = 0; … … 232 208 233 209 float a = zero + one; 234 CPPUNIT_ASSERT_EQUAL(1.0f, a);210 LOLUNIT_ASSERT_EQUAL(1.0f, a); 235 211 a += zero; 236 CPPUNIT_ASSERT_EQUAL(1.0f, a);212 LOLUNIT_ASSERT_EQUAL(1.0f, a); 237 213 a = zero; 238 CPPUNIT_ASSERT_EQUAL(1.0f, a);214 LOLUNIT_ASSERT_EQUAL(1.0f, a); 239 215 a *= one; 240 CPPUNIT_ASSERT_EQUAL(1.0f, a);216 LOLUNIT_ASSERT_EQUAL(1.0f, a); 241 217 a /= one; 242 CPPUNIT_ASSERT_EQUAL(1.0f, a);218 LOLUNIT_ASSERT_EQUAL(1.0f, a); 243 219 244 220 float b = one + zero; 245 CPPUNIT_ASSERT_EQUAL(1.0f, b);221 LOLUNIT_ASSERT_EQUAL(1.0f, b); 246 222 b += one; 247 CPPUNIT_ASSERT_EQUAL(2.0f, b);223 LOLUNIT_ASSERT_EQUAL(2.0f, b); 248 224 b *= two; 249 CPPUNIT_ASSERT_EQUAL(4.0f, b);225 LOLUNIT_ASSERT_EQUAL(4.0f, b); 250 226 b = two; 251 CPPUNIT_ASSERT_EQUAL(2.0f, b);227 LOLUNIT_ASSERT_EQUAL(2.0f, b); 252 228 b /= two; 253 CPPUNIT_ASSERT_EQUAL(1.0f, b);229 LOLUNIT_ASSERT_EQUAL(1.0f, b); 254 230 255 231 float c = one  zero; 256 CPPUNIT_ASSERT_EQUAL(1.0f, c);232 LOLUNIT_ASSERT_EQUAL(1.0f, c); 257 233 258 234 float d = two  one; 259 CPPUNIT_ASSERT_EQUAL(1.0f, d);235 LOLUNIT_ASSERT_EQUAL(1.0f, d); 260 236 261 237 float e = two + (one); 262 CPPUNIT_ASSERT_EQUAL(1.0f, e);238 LOLUNIT_ASSERT_EQUAL(1.0f, e); 263 239 264 240 float f = (two * two) / (one + one); 265 CPPUNIT_ASSERT_EQUAL(2.0f, f);266 } 267 268 void test_half_op_float()241 LOLUNIT_ASSERT_EQUAL(2.0f, f); 242 } 243 244 LOLUNIT_TEST(test_half_op_float) 269 245 { 270 246 half zero = 0; … … 274 250 275 251 half a = one + 0.0f; 276 CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);252 LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); 277 253 a += 0.0f; 278 CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);254 LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); 279 255 a = 0.0f; 280 CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);256 LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); 281 257 a *= 1.0f; 282 CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);258 LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); 283 259 a /= 1.0f; 284 CPPUNIT_ASSERT_EQUAL(one.bits, a.bits);260 LOLUNIT_ASSERT_EQUAL(one.bits, a.bits); 285 261 286 262 half b = one + 0.0f; 287 CPPUNIT_ASSERT_EQUAL(one.bits, b.bits);263 LOLUNIT_ASSERT_EQUAL(one.bits, b.bits); 288 264 b += 1.0f; 289 CPPUNIT_ASSERT_EQUAL(two.bits, b.bits);265 LOLUNIT_ASSERT_EQUAL(two.bits, b.bits); 290 266 b *= 2.0f; 291 CPPUNIT_ASSERT_EQUAL(four.bits, b.bits);267 LOLUNIT_ASSERT_EQUAL(four.bits, b.bits); 292 268 b = 2.0f; 293 CPPUNIT_ASSERT_EQUAL(two.bits, b.bits);269 LOLUNIT_ASSERT_EQUAL(two.bits, b.bits); 294 270 b /= 2.0f; 295 CPPUNIT_ASSERT_EQUAL(one.bits, b.bits);271 LOLUNIT_ASSERT_EQUAL(one.bits, b.bits); 296 272 297 273 half c = 1.0f  zero; 298 CPPUNIT_ASSERT_EQUAL(one.bits, c.bits);274 LOLUNIT_ASSERT_EQUAL(one.bits, c.bits); 299 275 300 276 half d = 2.0f  one; 301 CPPUNIT_ASSERT_EQUAL(one.bits, d.bits);277 LOLUNIT_ASSERT_EQUAL(one.bits, d.bits); 302 278 303 279 half e = 2.0f + (one); 304 CPPUNIT_ASSERT_EQUAL(one.bits, e.bits);280 LOLUNIT_ASSERT_EQUAL(one.bits, e.bits); 305 281 306 282 half f = (2.0f * two) / (1.0f + one); 307 CPPUNIT_ASSERT_EQUAL(two.bits, f.bits);283 LOLUNIT_ASSERT_EQUAL(two.bits, f.bits); 308 284 } 309 285 … … 330 306 }; 331 307 332 CPPUNIT_TEST_SUITE_REGISTRATION(HalfTest);308 LOLUNIT_SETUP_FIXTURE(HalfTest); 333 309 334 310 } /* namespace lol */
Note: See TracChangeset
for help on using the changeset viewer.