1 /*
2  * Copyright (C) 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_pack.h"
25 
26 #include <gtest/gtest.h>
27 
28 const struct {
29    float f;
30    uint16_t encoded;
31    bool inexact;
32 } lod_cases[] = {
33    /* Lower bound clamp */
34    { -INFINITY, 0x00, true },
35    { -0.1, 0x00, true },
36    { -0.0, 0x00, true },
37 
38    /* Exact bounds */
39    { 0.0, 0x00 },
40    { 14.0, 0x380 },
41 
42    /* Upper bound clamp */
43    { 14.1, 0x380, true },
44    { 18.1, 0x380, true },
45    { INFINITY, 0x380, true },
46 };
47 
48 class LODClamp : public testing::Test {
49 };
50 
TEST_F(LODClamp, Encode)51 TEST_F(LODClamp, Encode)
52 {
53    for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i)
54       ASSERT_EQ(__float_to_lod(lod_cases[i].f), lod_cases[i].encoded);
55 }
56 
TEST_F(LODClamp, Decode)57 TEST_F(LODClamp, Decode)
58 {
59    for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i) {
60       if (lod_cases[i].inexact)
61          continue;
62 
63       uint8_t cl[4] = { 0 };
64       memcpy(cl, &lod_cases[i].encoded, sizeof(lod_cases[i].encoded));
65 
66       ASSERT_EQ(__gen_unpack_lod(cl, 0, 10), lod_cases[i].f);
67    }
68 }
69