Add tests for c-level code

This commit is contained in:
Andrew Cassidy 2022-05-22 22:20:30 -07:00
parent c2d4e9be4d
commit bf35983b2d
10 changed files with 111 additions and 120 deletions

3
.gitmodules vendored
View File

@ -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

View File

@ -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"

1
external/utest vendored Submodule

@ -0,0 +1 @@
Subproject commit 06abbd1978f7e9c5f61642d80b9d300e3a8c4ccf

View File

@ -20,6 +20,7 @@
#include "_bindings.h"
#include <pybind11/pybind11.h>
#include <utest.h>
#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<const char *>(t.Data()), t.NBytes()); });
texture.def("tobytes",
[](const Texture &t) { return py::bytes(reinterpret_cast<const char *>(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

View File

@ -0,0 +1,86 @@
/* Quicktex Texture Compression Library
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
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 <http://www.gnu.org/licenses/>.
*/
#include "TestSIMD.h"
#include <utest.h>
#include <array>
#include <cassert>
#include <cstdint>
#include <numeric>
#include <type_traits>
#include <xsimd/xsimd.hpp>
#include "../VecUtil.h"
namespace quicktex::tests {
template <typename T> constexpr auto make_arrays() {
std::vector<std::array<T, xsimd::batch<T>::size>> arrays;
std::array<T, xsimd::batch<T>::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<T>::max());
arrays.push_back(buffer);
if (std::is_signed_v<T>) {
std::iota(buffer.begin(), buffer.end(), -1);
arrays.push_back(buffer);
buffer.fill(-1);
arrays.push_back(buffer);
buffer.fill(std::numeric_limits<T>::min());
arrays.push_back(buffer);
}
return arrays;
}
template <typename T, size_t N> long int sumArray(std::array<T, N> const &arg) {
return std::accumulate(arg.begin(), arg.end(), (long)0);
}
#define UTEST_WHADD(TYPE) \
UTEST(simd, whadd_##TYPE) { \
for (auto arr : make_arrays<TYPE>()) { \
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

View File

@ -1,62 +0,0 @@
/* Quicktex Texture Compression Library
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
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 <http://www.gnu.org/licenses/>.
*/
#include "TestSIMD.h"
#include <array>
#include <cassert>
#include <cstdint>
#include <numeric>
#include <xsimd/xsimd.hpp>
#include "../VecUtil.h"
namespace quicktex::tests {
void test_widening_hadd() {
const auto vec_size = xsimd::batch<int16_t>::size;
std::array<int16_t, vec_size> 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<int16_t>::max());
v = xsimd::load_unaligned(&buffer[0]);
sum = simd::whadd(v);
assert(sum == std::numeric_limits<int16_t>::max() * (int)vec_size);
buffer.fill(std::numeric_limits<int16_t>::min());
v = xsimd::load_unaligned(&buffer[0]);
sum = simd::whadd(v);
assert(sum == std::numeric_limits<int16_t>::min() * (int)vec_size);
}
} // namespace quicktex::tests

View File

@ -1,44 +0,0 @@
/* Quicktex Texture Compression Library
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
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 <http://www.gnu.org/licenses/>.
*/
#include "../_bindings.h"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
#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

View File

@ -181,6 +181,10 @@ template <class > struct next_size;
template <class T> using next_size_t = typename next_size<T>::type;
template <class T> struct Tag { using type = T; };
template <> struct next_size<int_least8_t> : Tag<int_least16_t> { };
template <> struct next_size<int_least16_t> : Tag<int_least32_t> { };
template <> struct next_size<int_least32_t> : Tag<int_least64_t> { };
template <> struct next_size<int8_t> : Tag<int16_t> { };
template <> struct next_size<int16_t> : Tag<int32_t> { };
template <> struct next_size<int32_t> : Tag<int64_t> { };
template <> struct next_size<uint8_t> : Tag<uint16_t> { };
template <> struct next_size<uint16_t> : Tag<uint32_t> { };
template <> struct next_size<uint32_t> : Tag<uint64_t> { };

View File

@ -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()