1 /*
2 * Copyright (C) 2021-2022 Alyssa Rosenzweig
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "agx_test.h"
25
26 #include <gtest/gtest.h>
27
TEST(Minifloat,RepresentativeValues)28 TEST(Minifloat, RepresentativeValues)
29 {
30 EXPECT_EQ(agx_minifloat_decode(0), 0.0f);
31 EXPECT_EQ(agx_minifloat_decode(25), 0.390625f);
32 EXPECT_EQ(agx_minifloat_decode(135), -0.109375f);
33 EXPECT_EQ(agx_minifloat_decode(255), -31.0);
34 }
35
TEST(Minifloat,Exactness)36 TEST(Minifloat, Exactness)
37 {
38 EXPECT_TRUE(agx_minifloat_exact(0.0f));
39 EXPECT_TRUE(agx_minifloat_exact(0.390625f));
40 EXPECT_TRUE(agx_minifloat_exact(-0.109375f));
41 EXPECT_TRUE(agx_minifloat_exact(-31.0));
42 EXPECT_FALSE(agx_minifloat_exact(3.141f));
43 EXPECT_FALSE(agx_minifloat_exact(2.718f));
44 EXPECT_FALSE(agx_minifloat_exact(1.618f));
45 }
46
TEST(Minifloat,AllValuesRoundtrip)47 TEST(Minifloat, AllValuesRoundtrip)
48 {
49 for (unsigned i = 0; i < 0x100; ++i) {
50 float f = agx_minifloat_decode(i);
51 EXPECT_EQ(agx_minifloat_encode(f), i);
52 EXPECT_TRUE(agx_minifloat_exact(f));
53 }
54 }
55