agx: Port minifloat tests to GTest

These tests predate using GTest in the compiler. Now that we do, we'd like to
have the tests together so they run regularly.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Eric Engestrom <eric@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17824>
This commit is contained in:
Alyssa Rosenzweig 2022-07-31 13:16:52 -04:00 committed by Marge Bot
parent 31b9b04880
commit 8066ef9d30
4 changed files with 55 additions and 41 deletions

View file

@ -82,32 +82,4 @@ agx_minifloat_exact(float f)
return memcmp(&f, &f_, sizeof(float)) == 0;
}
#ifndef NDEBUG
static inline void
agx_minifloat_tests(void)
{
/* Decode some representative values */
assert(agx_minifloat_decode(0) == 0.0f);
assert(agx_minifloat_decode(25) == 0.390625f);
assert(agx_minifloat_decode(135) == -0.109375f);
assert(agx_minifloat_decode(255) == -31.0);
/* Verify exactness */
assert(agx_minifloat_exact(0.0f));
assert(agx_minifloat_exact(0.390625f));
assert(agx_minifloat_exact(-0.109375f));
assert(agx_minifloat_exact(-31.0));
assert(!agx_minifloat_exact(3.141f));
assert(!agx_minifloat_exact(2.718f));
assert(!agx_minifloat_exact(1.618f));
/* Check that all values round trip */
for (unsigned i = 0; i < 0x100; ++i) {
float f = agx_minifloat_decode(i);
assert(agx_minifloat_encode(f) == i);
assert(agx_minifloat_exact(f));
}
}
#endif
#endif

View file

@ -207,17 +207,6 @@ disassemble(const char *filename, bool verbose)
free(code);
}
static void
tests()
{
#ifndef NDEBUG
agx_minifloat_tests();
printf("Pass.\n");
#else
fprintf(stderr, "tests not compiled in NDEBUG mode");
#endif
}
int
main(int argc, char **argv)
{
@ -232,8 +221,6 @@ main(int argc, char **argv)
disassemble(argv[2], false);
else if (strcmp(argv[1], "disasm-verbose") == 0)
disassemble(argv[2], true);
else if (strcmp(argv[1], "test") == 0)
tests();
else
unreachable("Unknown command. Valid: compile/disasm/disasm-verbose");

View file

@ -86,6 +86,7 @@ if with_tests
executable(
'agx_tests',
files(
'test/test-minifloat.cpp',
'test/test-optimizer.cpp',
'test/test-lower-pseudo.cpp',
'test/test-lower-parallel-copy.cpp',

View file

@ -0,0 +1,54 @@
/*
* Copyright (C) 2021-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_test.h"
#include <gtest/gtest.h>
TEST(Minifloat, RepresentativeValues)
{
EXPECT_EQ(agx_minifloat_decode(0), 0.0f);
EXPECT_EQ(agx_minifloat_decode(25), 0.390625f);
EXPECT_EQ(agx_minifloat_decode(135), -0.109375f);
EXPECT_EQ(agx_minifloat_decode(255), -31.0);
}
TEST(Minifloat, Exactness)
{
EXPECT_TRUE(agx_minifloat_exact(0.0f));
EXPECT_TRUE(agx_minifloat_exact(0.390625f));
EXPECT_TRUE(agx_minifloat_exact(-0.109375f));
EXPECT_TRUE(agx_minifloat_exact(-31.0));
EXPECT_FALSE(agx_minifloat_exact(3.141f));
EXPECT_FALSE(agx_minifloat_exact(2.718f));
EXPECT_FALSE(agx_minifloat_exact(1.618f));
}
TEST(Minifloat, AllValuesRoundtrip)
{
for (unsigned i = 0; i < 0x100; ++i) {
float f = agx_minifloat_decode(i);
EXPECT_EQ(agx_minifloat_encode(f), i);
EXPECT_TRUE(agx_minifloat_exact(f));
}
}