From 6554790dfb348ca1f62a2ef0999e2d99cc7daf25 Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Sun, 6 Feb 2022 15:03:25 -0500 Subject: [PATCH] asahi: Add LOD clamp packing unit tests With GTest. Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/lib/meson.build | 19 +++++++++ src/asahi/lib/tests/test-lod-clamps.cpp | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/asahi/lib/tests/test-lod-clamps.cpp diff --git a/src/asahi/lib/meson.build b/src/asahi/lib/meson.build index 5860526..e921db1 100644 --- a/src/asahi/lib/meson.build +++ b/src/asahi/lib/meson.build @@ -64,3 +64,22 @@ libasahi_decode = static_library( gnu_symbol_visibility : 'hidden', build_by_default : false, ) + +if with_tests + test( + 'libasahi_tests', + executable( + 'libasahi_tests', + files( + 'tests/test-lod-clamps.cpp', + ), + c_args : [c_msvc_compat_args, no_override_init_args], + gnu_symbol_visibility : 'hidden', + include_directories : [inc_include, inc_src, inc_mesa], + dependencies: [idep_gtest, idep_agx_pack], + link_with : [], + ), + suite : ['asahi'], + protocol : gtest_test_protocol, + ) +endif diff --git a/src/asahi/lib/tests/test-lod-clamps.cpp b/src/asahi/lib/tests/test-lod-clamps.cpp new file mode 100644 index 0000000..3bee31f --- /dev/null +++ b/src/asahi/lib/tests/test-lod-clamps.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2022 Alyssa Rosenzweig + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "agx_pack.h" + +#include + +const struct { + float f; + uint16_t encoded; + bool inexact; +} lod_cases[] = { + /* Lower bound clamp */ + { -INFINITY, 0x00, true }, + { -0.1, 0x00, true }, + { -0.0, 0x00, true }, + + /* Exact bounds */ + { 0.0, 0x00 }, + { 14.0, 0x380 }, + + /* Upper bound clamp */ + { 14.1, 0x380, true }, + { 18.1, 0x380, true }, + { INFINITY, 0x380, true }, +}; + +class LODClamp : public testing::Test { +}; + +TEST_F(LODClamp, Encode) +{ + for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i) + ASSERT_EQ(__float_to_lod(lod_cases[i].f), lod_cases[i].encoded); +} + +TEST_F(LODClamp, Decode) +{ + for (unsigned i = 0; i < ARRAY_SIZE(lod_cases); ++i) { + if (lod_cases[i].inexact) + continue; + + uint8_t cl[4] = { 0 }; + memcpy(cl, &lod_cases[i].encoded, sizeof(lod_cases[i].encoded)); + + ASSERT_EQ(__gen_unpack_lod(cl, 0, 10), lod_cases[i].f); + } +} -- 2.7.4