From 20ece8025e1a01895e5823ab0febc12439de1f50 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 4 Sep 2023 23:42:42 +0100 Subject: [PATCH] Test: Add tests for FindLastBit function --- src/tests/CMakeLists.txt | 2 ++ src/tests/bitmath_func.cpp | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/tests/bitmath_func.cpp diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index e0f317fb99..dd0ec5dbb8 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -1,9 +1,11 @@ add_test_files( + bitmath_func.cpp landscape_partial_pixel_z.cpp math_func.cpp ring_buffer.cpp test_main.cpp ../landscape_ppz.cpp ../core/alloc_func.cpp + ../core/bitmath_func.cpp ../core/math_func.cpp ) diff --git a/src/tests/bitmath_func.cpp b/src/tests/bitmath_func.cpp new file mode 100644 index 0000000000..5f5f7c79bb --- /dev/null +++ b/src/tests/bitmath_func.cpp @@ -0,0 +1,58 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file bitmath_func_test.cpp Test functionality from core/bitmath_func. */ + +#include "../stdafx.h" + +#include "../3rdparty/catch2/catch.hpp" + +#include "../core/bitmath_func.hpp" + +extern uint8 FindLastBit64(uint64 x); + +TEST_CASE("FindLastBit tests") +{ + CHECK(FindLastBit(0) == 0); + CHECK(FindLastBit(0) == 0); + CHECK(FindLastBit(0) == 0); + CHECK(FindLastBit(0) == 0); + CHECK(FindLastBit64(0) == 0); + + for (uint i = 0; i < 8; i++) { + uint8 t = (uint8)(1 << i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit64(t) == i); + } + + for (uint i = 8; i < 16; i++) { + uint16 t = (uint16)(1 << i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit64(t) == i); + } + + for (uint i = 16; i < 32; i++) { + uint32 t = (1 << i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit64(t) == i); + } + + for (uint i = 32; i < 64; i++) { + uint64 t = (((uint64)1) << i); + CHECK(FindLastBit(t) == i); + CHECK(FindLastBit64(t) == i); + } + + CHECK(FindLastBit(0x42) == FindLastBit(0x40)); + CHECK(FindLastBit(0xAAAA) == FindLastBit(0x8000)); +}