diff --git a/.gitmodules b/.gitmodules index 49f1431..007937d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "external/xsimd"] path = external/xsimd url = https://github.com/xtensor-stack/xsimd.git +[submodule "external/utest"] + path = external/utest + url = https://github.com/sheredom/utest.h.git diff --git a/CMakeLists.txt b/CMakeLists.txt index fff8c15..a0858a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,11 +11,12 @@ find_package(pybind11 CONFIG REQUIRED) find_package(OpenMP) add_subdirectory(external/xsimd) +include_directories(external/utest) # Collect source files file(GLOB SOURCE_FILES "quicktex/*.cpp" - "quicktex/tests/*.cpp" + "quicktex/ctests/*.cpp" "quicktex/s3tc/*.cpp" "quicktex/s3tc/bc1/*.cpp" "quicktex/s3tc/bc3/*.cpp" @@ -26,7 +27,7 @@ file(GLOB SOURCE_FILES file(GLOB HEADER_FILES "quicktex/*.h" - "quicktex/tests/*.h" + "quicktex/ctests/*.h" "quicktex/s3tc/*.h" "quicktex/s3tc/bc1/*.h" "quicktex/s3tc/bc3/*.h" diff --git a/external/utest b/external/utest new file mode 160000 index 0000000..06abbd1 --- /dev/null +++ b/external/utest @@ -0,0 +1 @@ +Subproject commit 06abbd1978f7e9c5f61642d80b9d300e3a8c4ccf diff --git a/quicktex/_bindings.cpp b/quicktex/_bindings.cpp index b71180b..c56f682 100644 --- a/quicktex/_bindings.cpp +++ b/quicktex/_bindings.cpp @@ -20,6 +20,7 @@ #include "_bindings.h" #include +#include #include "Color.h" #include "Decoder.h" @@ -30,12 +31,13 @@ #define STRINGIFY(x) #x #define MACRO_STRINGIFY(x) STRINGIFY(x) +UTEST_STATE(); + namespace py = pybind11; namespace quicktex::bindings { void InitS3TC(py::module_ &m); -void InitCTests(py::module_ &m); PYBIND11_MODULE(_quicktex, m) { m.doc() = "More Stuff"; @@ -58,7 +60,8 @@ PYBIND11_MODULE(_quicktex, m) { texture.def_property_readonly("height", &Texture::Height); texture.def_buffer([](Texture &t) { return py::buffer_info(t.Data(), t.NBytes()); }); - texture.def("tobytes", [](const Texture &t) { return py::bytes(reinterpret_cast(t.Data()), t.NBytes()); }); + texture.def("tobytes", + [](const Texture &t) { return py::bytes(reinterpret_cast(t.Data()), t.NBytes()); }); // RawTexture @@ -70,7 +73,9 @@ PYBIND11_MODULE(_quicktex, m) { DefSubscript2D(raw_texture, &RawTexture::GetPixel, &RawTexture::SetPixel, &RawTexture::Size); InitS3TC(m); - InitCTests(m); + + // CTests + m.def("_run_ctests", []() { return utest_main(0, NULL); }); } } // namespace quicktex::bindings \ No newline at end of file diff --git a/quicktex/ctests/TestSIMD.cpp b/quicktex/ctests/TestSIMD.cpp new file mode 100644 index 0000000..9cd7f42 --- /dev/null +++ b/quicktex/ctests/TestSIMD.cpp @@ -0,0 +1,86 @@ +/* Quicktex Texture Compression Library + Copyright (C) 2021 Andrew Cassidy + Partially derived from rgbcx.h written by Richard Geldreich + and licenced under the public domain + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . + */ + +#include "TestSIMD.h" + +#include + +#include +#include +#include +#include +#include +#include + +#include "../VecUtil.h" + +namespace quicktex::tests { + +template constexpr auto make_arrays() { + std::vector::size>> arrays; + std::array::size> buffer; + + std::iota(buffer.begin(), buffer.end(), 1); + arrays.push_back(buffer); + + buffer.fill(1); + arrays.push_back(buffer); + + buffer.fill(0); + arrays.push_back(buffer); + + buffer.fill(std::numeric_limits::max()); + arrays.push_back(buffer); + + if (std::is_signed_v) { + std::iota(buffer.begin(), buffer.end(), -1); + arrays.push_back(buffer); + + buffer.fill(-1); + arrays.push_back(buffer); + + buffer.fill(std::numeric_limits::min()); + arrays.push_back(buffer); + } + + return arrays; +} + +template long int sumArray(std::array const &arg) { + return std::accumulate(arg.begin(), arg.end(), (long)0); +} + +#define UTEST_WHADD(TYPE) \ + UTEST(simd, whadd_##TYPE) { \ + for (auto arr : make_arrays()) { \ + auto v = xsimd::load_unaligned(&arr[0]); \ + auto vsum = simd::whadd(v); \ + auto ssum = sumArray(arr); \ + ASSERT_EQ(vsum, ssum); \ + } \ + } + +UTEST_WHADD(int8_t); +UTEST_WHADD(uint8_t); +UTEST_WHADD(int16_t); +UTEST_WHADD(uint16_t); +UTEST_WHADD(int32_t); +UTEST_WHADD(uint32_t); + +} // namespace quicktex::tests \ No newline at end of file diff --git a/quicktex/tests/TestSIMD.h b/quicktex/ctests/TestSIMD.h similarity index 100% rename from quicktex/tests/TestSIMD.h rename to quicktex/ctests/TestSIMD.h diff --git a/quicktex/tests/TestSIMD.cpp b/quicktex/tests/TestSIMD.cpp deleted file mode 100644 index 7c1be9c..0000000 --- a/quicktex/tests/TestSIMD.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* Quicktex Texture Compression Library - Copyright (C) 2021 Andrew Cassidy - Partially derived from rgbcx.h written by Richard Geldreich - and licenced under the public domain - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . - */ - -#include "TestSIMD.h" - -#include -#include -#include -#include - -#include -#include "../VecUtil.h" - -namespace quicktex::tests { - -void test_widening_hadd() { - const auto vec_size = xsimd::batch::size; - std::array buffer; - - std::iota(buffer.begin(), buffer.end(), 1); - auto v = xsimd::load_unaligned(&buffer[0]); - auto sum = simd::whadd(v); - assert(sum == vec_size / 2 * (vec_size + 1)); // Gauss formula - - buffer.fill(1); - v = xsimd::load_unaligned(&buffer[0]); - sum = simd::whadd(v); - assert(sum == vec_size); - - buffer.fill(0); - v = xsimd::load_unaligned(&buffer[0]); - sum = simd::whadd(v); - assert(sum == 0); - - buffer.fill(std::numeric_limits::max()); - v = xsimd::load_unaligned(&buffer[0]); - sum = simd::whadd(v); - assert(sum == std::numeric_limits::max() * (int)vec_size); - - buffer.fill(std::numeric_limits::min()); - v = xsimd::load_unaligned(&buffer[0]); - sum = simd::whadd(v); - assert(sum == std::numeric_limits::min() * (int)vec_size); - -} -} // namespace quicktex::tests \ No newline at end of file diff --git a/quicktex/tests/_bindings.cpp b/quicktex/tests/_bindings.cpp deleted file mode 100644 index e5b18fc..0000000 --- a/quicktex/tests/_bindings.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* Quicktex Texture Compression Library - Copyright (C) 2021 Andrew Cassidy - Partially derived from rgbcx.h written by Richard Geldreich - and licenced under the public domain - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program 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 Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . - */ - -#include "../_bindings.h" - -#include -#include - -#include -#include -#include -#include -#include - -#include "TestSIMD.h" - -namespace py = pybind11; -namespace quicktex::bindings { - -using namespace pybind11::literals; -using namespace quicktex::tests; - -void InitCTests(py::module_ &quicktex) { - py::module_ ctests = quicktex.def_submodule("_ctests", "Internal tests for C-level functions"); - -// ctests.def("test_WidenSumS16", &TestWidenSumS16); -} -} // namespace quicktex::bindings \ No newline at end of file diff --git a/quicktex/util.h b/quicktex/util.h index d0aff20..be7d936 100644 --- a/quicktex/util.h +++ b/quicktex/util.h @@ -181,6 +181,10 @@ template struct next_size; template using next_size_t = typename next_size::type; template struct Tag { using type = T; }; -template <> struct next_size : Tag { }; -template <> struct next_size : Tag { }; -template <> struct next_size : Tag { }; \ No newline at end of file +template <> struct next_size : Tag { }; +template <> struct next_size : Tag { }; +template <> struct next_size : Tag { }; + +template <> struct next_size : Tag { }; +template <> struct next_size : Tag { }; +template <> struct next_size : Tag { }; \ No newline at end of file diff --git a/tests/test_ctest.py b/tests/test_ctest.py index 1c515e8..64e6b21 100644 --- a/tests/test_ctest.py +++ b/tests/test_ctest.py @@ -1,8 +1,5 @@ -import unittest - -import _quicktex._ctests as c +from _quicktex import _run_ctests -class TestCTest(unittest.TestCase): - def test_WidenSumS16(self): - c.test_WidenSumS16() +def test_ctests(): + _run_ctests()