commit 8a370f172a193b11dd34a6a33d7a6a1f3d620b24
parent 8d1cecdccfa104aabbb9ab7767703a4dd03169a4
Author: d.levin256@gmail.com <d.levin256@gmail.com>
Date: Fri, 1 Jul 2016 16:07:07 +0300
New test: vec_test
Diffstat:
2 files changed, 118 insertions(+), 0 deletions(-)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
@@ -27,6 +27,7 @@ add_executable(basic_vector_test basic_vector_test.cpp ${KFR_SRC})
add_executable(dft_test dft_test.cpp ${KFR_SRC})
add_executable(empty_test empty_test.cpp ${KFR_SRC})
add_executable(complex_test complex_test.cpp ${KFR_SRC})
+add_executable(vec_test vec_test.cpp ${KFR_SRC})
find_package(PythonInterp 2.7)
@@ -42,6 +43,8 @@ if (PYTHONINTERP_FOUND)
COMMAND ${PROJECT_BINARY_DIR}/tests/dft_test)
add_test(NAME complex_test
COMMAND ${PROJECT_BINARY_DIR}/tests/complex_test)
+ add_test(NAME vec_test
+ COMMAND ${PROJECT_BINARY_DIR}/tests/vec_test)
else ()
message(WARNING "Install Python to run tests")
endif ()
diff --git a/tests/vec_test.cpp b/tests/vec_test.cpp
@@ -0,0 +1,115 @@
+/**
+ * KFR (http://kfrlib.com)
+ * Copyright (C) 2016 D Levin
+ * See LICENSE.txt for details
+ */
+
+#include <kfr/io/tostring.hpp>
+
+#include "testo/testo.hpp"
+#include <kfr/math.hpp>
+#include <kfr/vec.hpp>
+
+using namespace kfr;
+
+TEST(vec_concat)
+{
+ CHECK(concat(vec<f32, 1>{ 1 }, vec<f32, 2>{ 2, 3 }, vec<f32, 1>{ 4 }, vec<f32, 3>{ 5, 6, 7 }) //
+ == vec<f32, 7>{ 1, 2, 3, 4, 5, 6, 7 });
+}
+
+TEST(vec_split)
+{
+ vec<f32, 1> a1;
+ vec<f32, 2> a23;
+ vec<f32, 1> a4;
+ vec<f32, 3> a567;
+ split(vec<f32, 7>{ 1, 2, 3, 4, 5, 6, 7 }, a1, a23, a4, a567);
+ CHECK(a1 == vec<f32, 1>{ 1 });
+ CHECK(a23 == vec<f32, 2>{ 2, 3 });
+ CHECK(a4 == vec<f32, 1>{ 4 });
+ CHECK(a567 == vec<f32, 3>{ 5, 6, 7 });
+}
+
+TEST(vec_broadcast)
+{
+ CHECK(broadcast<5>(3.f) == vec<f32, 5>{ 3, 3, 3, 3, 3 });
+ CHECK(broadcast<6>(1.f, 2.f) == vec<f32, 6>{ 1, 2, 1, 2, 1, 2 });
+ CHECK(broadcast<6>(1.f, 2.f, 3.f) == vec<f32, 6>{ 1, 2, 3, 1, 2, 3 });
+}
+
+TEST(vec_resize)
+{
+ CHECK(resize<5>(make_vector(3.f)) == vec<f32, 5>{ 3, 3, 3, 3, 3 });
+ CHECK(resize<6>(make_vector(1.f, 2.f)) == vec<f32, 6>{ 1, 2, 1, 2, 1, 2 });
+ CHECK(resize<6>(make_vector(1.f, 2.f, 3.f)) == vec<f32, 6>{ 1, 2, 3, 1, 2, 3 });
+}
+
+TEST(vec_make_vector)
+{
+ const signed char ch = -1;
+ CHECK(make_vector(1, 2, ch) == vec<i32, 3>{ 1, 2, -1 });
+ CHECK(make_vector(1, 2, -100ll) == vec<i64, 3>{ 1, 2, -100 });
+ CHECK(make_vector<i64>(1, 2, ch) == vec<i64, 3>{ 1, 2, -1 });
+ CHECK(make_vector<f32>(1, 2, ch) == vec<f32, 3>{ 1, 2, -1 });
+
+ CHECK(make_vector(f64x2{ 1, 2 }, f64x2{ 10, 20 }) ==
+ vec<vec<f64, 2>, 2>{ f64x2{ 1, 2 }, f64x2{ 10, 20 } });
+ CHECK(make_vector(1.f, f32x2{ 10, 20 }) == vec<vec<f32, 2>, 2>{ f32x2{ 1, 1 }, f32x2{ 10, 20 } });
+}
+
+TEST(vec_apply)
+{
+ CHECK(apply([](int x) { return x + 1; }, make_vector(1, 2, 3, 4, 5)) == make_vector(2, 3, 4, 5, 6));
+ CHECK(apply(fn_sqr(), make_vector(1, 2, 3, 4, 5)) == make_vector(1, 4, 9, 16, 25));
+}
+
+TEST(vec_tovec)
+{
+ const __m128 x = _mm_set_ps(4.f, 3.f, 2.f, 1.f);
+ CHECK(tovec(x) == vec<f32, 4>(1, 2, 3, 4));
+ const __m128d y = _mm_set_pd(2.0, 1.0);
+ CHECK(tovec(y) == vec<f64, 2>(1, 2));
+ const simd<f64, 7> z{ 1, 2, 3, 4, 5, 6, 7 };
+ CHECK(tovec(z) == vec<f64, 7>(1, 2, 3, 4, 5, 6, 7));
+}
+
+TEST(vec_zerovector)
+{
+ CHECK(zerovector<f32, 3>() == f32x3{ 0, 0, 0 });
+ CHECK(zerovector<i16, 3>() == i16x3{ 0, 0, 0 });
+ CHECK(zerovector(f64x8{}) == f64x8{ 0, 0, 0, 0, 0, 0, 0, 0 });
+}
+
+TEST(vec_allonesvector)
+{
+ CHECK(~allonesvector<f32, 3>() == f32x3{ 0, 0, 0 });
+ CHECK(allonesvector<i16, 3>() == i16x3{ -1, -1, -1 });
+ CHECK(allonesvector<u8, 3>() == u8x3{ 255, 255, 255 });
+}
+
+TEST(vec_low_high)
+{
+ CHECK(low(vec<u8, 8>(1, 2, 3, 4, 5, 6, 7, 8)) == vec<u8, 4>(1, 2, 3, 4));
+ CHECK(high(vec<u8, 8>(1, 2, 3, 4, 5, 6, 7, 8)) == vec<u8, 4>(5, 6, 7, 8));
+
+ CHECK(low(vec<u8, 7>(1, 2, 3, 4, 5, 6, 7)) == vec<u8, 4>(1, 2, 3, 4));
+ CHECK(high(vec<u8, 7>(1, 2, 3, 4, 5, 6, 7)) == vec<u8, 3>(5, 6, 7));
+
+ CHECK(low(vec<u8, 6>(1, 2, 3, 4, 5, 6)) == vec<u8, 4>(1, 2, 3, 4));
+ CHECK(high(vec<u8, 6>(1, 2, 3, 4, 5, 6)) == vec<u8, 2>(5, 6));
+
+ CHECK(low(vec<u8, 5>(1, 2, 3, 4, 5)) == vec<u8, 4>(1, 2, 3, 4));
+ CHECK(high(vec<u8, 5>(1, 2, 3, 4, 5)) == vec<u8, 1>(5));
+
+ CHECK(low(vec<u8, 4>(1, 2, 3, 4)) == vec<u8, 2>(1, 2));
+ CHECK(high(vec<u8, 4>(1, 2, 3, 4)) == vec<u8, 2>(3, 4));
+
+ CHECK(low(vec<u8, 3>(1, 2, 3)) == vec<u8, 2>(1, 2));
+ CHECK(high(vec<u8, 3>(1, 2, 3)) == vec<u8, 1>(3));
+
+ CHECK(low(vec<u8, 2>(1, 2)) == vec<u8, 1>(1));
+ CHECK(high(vec<u8, 2>(1, 2)) == vec<u8, 1>(2));
+}
+
+int main(int argc, char** argv) { return testo::run_all("", true); }