kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

commit 2e3f7f6f9cb1d504e8ca0a1531466b7dcf505a3e
parent e99023628731f61360916c3ecb2c1020c0a2fc61
Author: d.levin256@gmail.com <d.levin256@gmail.com>
Date:   Mon,  2 Dec 2019 16:11:48 +0000

fraction tests

Diffstat:
M.gitignore | 10++++++++--
Minclude/kfr/base/expression.hpp | 9++++++---
Minclude/kfr/base/fraction.hpp | 18++++++++++++++++++
Minclude/kfr/base/generators.hpp | 2+-
Minclude/kfr/dsp/mixdown.hpp | 2+-
Msources.cmake | 1+
Mtests/CMakeLists.txt | 8++++++++
Mtests/all_tests.cpp | 5-----
Mtests/expression_test.cpp | 5+++++
Atests/unit/base/fraction.cpp | 49+++++++++++++++++++++++++++++++++++++++++++++++++
10 files changed, 97 insertions(+), 12 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -94,4 +94,10 @@ cmake-* *.TMP temp_audio_file.wav -.DS_Store -\ No newline at end of file +.DS_Store + +# Coverage +*.profraw +*.profdata +*.lcov +lcov.info +\ No newline at end of file diff --git a/include/kfr/base/expression.hpp b/include/kfr/base/expression.hpp @@ -163,14 +163,17 @@ void test_expression(const E& expr, size_t size, Fn&& fn, const char* expression return; size = size_min(size, 200); constexpr size_t maxsize = 2 + ilog2(vector_width<T> * 2); + size_t g = 1; for (size_t i = 0; i < size;) { - const size_t next_size = - std::min(prev_poweroftwo(size - i), static_cast<size_t>(1) << (std::rand() % maxsize)); + const size_t next_size = std::min(prev_poweroftwo(size - i), g); + g *= 2; + if (g > (1 << (maxsize - 1))) + g = 1; cswitch(csize<1> << csizeseq<maxsize>, next_size, [&](auto x) { constexpr size_t nsize = val_of(decltype(x)()); - ::testo::scope s(as_string("i = ", i)); + ::testo::scope s(as_string("i = ", i, " width = ", nsize)); test->check(c <= get_elements(expr, cinput, i, vec_shape<T, nsize>()) == internal::get_fn_value<T, nsize>(i, fn), expression); diff --git a/include/kfr/base/fraction.hpp b/include/kfr/base/fraction.hpp @@ -25,6 +25,7 @@ */ #pragma once +#include "../cometa/string.hpp" #include "../simd/types.hpp" namespace kfr @@ -133,3 +134,19 @@ private: static i64 lcm(i64 a, i64 b) { return std::abs(a * b) / gcd(a, b); } }; } // namespace kfr + +namespace cometa +{ +template <> +struct representation<kfr::fraction> +{ + using type = std::string; + static std::string get(const kfr::fraction& value) + { + if (value.denominator == 1) + return as_string(value.numerator); + else + return as_string(value.numerator, "/", value.denominator); + } +}; +} // namespace cometa +\ No newline at end of file diff --git a/include/kfr/base/generators.hpp b/include/kfr/base/generators.hpp @@ -48,7 +48,7 @@ struct generator : input_expression constexpr static bool is_incremental = true; template <typename U, size_t N> - friend KFR_INTRINSIC vec<U, N> get_elements(const generator& self, cinput_t, size_t, vec_shape<U, N> t) + friend KFR_INTRINSIC vec<U, N> get_elements(const generator& self, cinput_t, size_t index, vec_shape<U, N> t) { return self.generate(t); } diff --git a/include/kfr/dsp/mixdown.hpp b/include/kfr/dsp/mixdown.hpp @@ -65,7 +65,7 @@ CMT_GNU_CONSTEXPR f64x2x2 matrix_sum_diff() return { f64x2{ 1, 1 }, f64x2{ 1, -1 } }; } template <int = 0> -CMT_GNU_CONSTEXPR f64x2x2 matrix_halvesum_halfdiff() +CMT_GNU_CONSTEXPR f64x2x2 matrix_halfsum_halfdiff() { return { f64x2{ 0.5, 0.5 }, f64x2{ 0.5, -0.5 } }; } diff --git a/sources.cmake b/sources.cmake @@ -172,6 +172,7 @@ set( set( KFR_UNITTEST_SRC ${PROJECT_SOURCE_DIR}/tests/unit/base/conversion.cpp + ${PROJECT_SOURCE_DIR}/tests/unit/base/fraction.cpp ${PROJECT_SOURCE_DIR}/tests/unit/base/random.cpp ${PROJECT_SOURCE_DIR}/tests/unit/base/reduce.cpp ${PROJECT_SOURCE_DIR}/tests/unit/graphics/color.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt @@ -24,6 +24,14 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-fdiagnostics-absolute-paths) endif () +option(ENABLE_COVERAGE "Enable coverage reporting" OFF) + +if (ENABLE_COVERAGE) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-fprofile-instr-generate -fcoverage-mapping) + endif () +endif () + if (ENABLE_DFT) add_definitions(-DHAVE_DFT) endif () diff --git a/tests/all_tests.cpp b/tests/all_tests.cpp @@ -34,11 +34,6 @@ FORCE_LINK(avx2) int main() { println(library_version(), " running on ", cpu_runtime()); - if (get_cpu() < cpu_t::native) - { - println("CPU is not supported"); - return -1; - } #ifdef HAVE_MPFR mpfr::scoped_precision p(64); #endif diff --git a/tests/expression_test.cpp b/tests/expression_test.cpp @@ -42,6 +42,11 @@ TEST(padded) CHECK_EXPRESSION(padded(truncate(counter(), 6), -1), infinite_size, [](size_t i) { return i >= 6 ? -1 : i; }); + + CHECK_EXPRESSION(padded(truncate(counter(), 0), -1), infinite_size, [](size_t i) { return -1; }); + + CHECK_EXPRESSION(padded(truncate(counter(), 501), -1), infinite_size, + [](size_t i) { return i >= 501 ? -1 : i; }); } TEST(rebind) diff --git a/tests/unit/base/fraction.cpp b/tests/unit/base/fraction.cpp @@ -0,0 +1,49 @@ +/** + * KFR (http://kfrlib.com) + * Copyright (C) 2016 D Levin + * See LICENSE.txt for details + */ + +#include <kfr/base/fraction.hpp> +#include <kfr/io.hpp> + +namespace kfr +{ +inline namespace CMT_ARCH_NAME +{ + +TEST(fraction) +{ + fraction f = 1; + f = f / 3; + CHECK(f == fraction{ 1, 3 }); + CHECK(+f == fraction{ 1, 3 }); + CHECK(-f == fraction{ -1, 3 }); + CHECK(static_cast<double>(f) == 1.0 / 3.0); + CHECK(static_cast<float>(f) == 1.0f / 3.0f); + CHECK(static_cast<i64>(f) == 0); + + fraction x{ 1, 5 }; + CHECK(f + x == fraction{ 8, 15 }); + CHECK(f - x == fraction{ 2, 15 }); + CHECK(f * x == fraction{ 1, 15 }); + CHECK(f / x == fraction{ 5, 3 }); + + CHECK(f > x); + CHECK(f >= x); + CHECK(!(f < x)); + CHECK(!(f <= x)); + + f += fraction(1, 3); + CHECK(f == fraction{ 2, 3 }); + f -= fraction(1, 6); + CHECK(f == fraction{ 1, 2 }); + f *= fraction(1, 7); + CHECK(f == fraction{ 1, 14 }); + f /= fraction(1, 2); + CHECK(f == fraction{ 1, 7 }); + + CHECK(fraction{ 100, 200 } == fraction{ 1, 2 }); +} +} // namespace CMT_ARCH_NAME +} // namespace kfr