From cdb2650241195a311a0d7e1bf1500a132a8d9947 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 12 Mar 2021 00:11:28 +0100 Subject: [PATCH] Just some test for wrappers of c-style arrays --- test/unit/CMakeLists.txt | 1 + test/unit/meson.build | 1 + test/unit/test_c_array.cpp | 92 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 test/unit/test_c_array.cpp diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 3c61516..7946da2 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(${PROJECT_NAME} test_offset_getters.cpp test_custom_type.cpp test_min_max.cpp + test_c_array.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/test/unit/meson.build b/test/unit/meson.build index 2bbe4e8..c76b5bd 100644 --- a/test/unit/meson.build +++ b/test/unit/meson.build @@ -10,6 +10,7 @@ exec_target = executable(meson.project_name(), 'test_offset_getters.cpp', 'test_custom_type.cpp', 'test_min_max.cpp', + 'test_c_array.cpp', install: false, dependencies: [gtest_dep, vectorwrapper_dep], cpp_args: compiler_opts, diff --git a/test/unit/test_c_array.cpp b/test/unit/test_c_array.cpp new file mode 100644 index 0000000..00dd13c --- /dev/null +++ b/test/unit/test_c_array.cpp @@ -0,0 +1,92 @@ +/* + * Copyright 2015-2020 Michele "King_DuckZ" Santullo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "vectorwrapper/vectorwrapper.hpp" +#include +#include + +namespace vwr { +template <> struct VectorWrapperInfo { + enum { dimensions = 3 }; + using scalar_type = unsigned short int; + using vector_type = unsigned short int[3]; + using higer_vector_type = unsigned short int[4]; + enum { + offset_x = sizeof(scalar_type) * 0, + offset_y = sizeof(scalar_type) * 1, + offset_z = sizeof(scalar_type) * 2 + }; +}; + +template <> struct VectorWrapperInfo { + enum { dimensions = 4 }; + using scalar_type = unsigned short int; + using vector_type = unsigned short int[4]; + using lower_vector_type = unsigned short int[3]; + enum { + offset_x = sizeof(scalar_type) * 0, + offset_y = sizeof(scalar_type) * 1, + offset_z = sizeof(scalar_type) * 2, + offset_w = sizeof(scalar_type) * 3 + }; +}; +} //namespace vwr + +namespace { +typedef vwr::Vec vec3; +typedef vwr::Vec vec4; +} //unnamed namespace + +TEST(vwr, c_array) { + constexpr const unsigned short int test_val = 0b1010101010101010; + vec4 myvec4(test_val); + vec3 myvec3(myvec4.xyz()); + + EXPECT_EQ(myvec3, myvec4.xyz()); + EXPECT_EQ(test_val, myvec3.x()); + EXPECT_EQ(test_val, myvec3.y()); + EXPECT_EQ(test_val, myvec3.z()); + + for (int z = 0; z < 4; ++z) { + const auto val = myvec4.data()[z]; + EXPECT_EQ(test_val, val); + } + + myvec4.data()[1] = 5; + { + unsigned short int vals[4] { test_val, 5, test_val, test_val }; + for (int z = 0; z < 4; ++z) { + const auto val = myvec4.data()[z]; + EXPECT_EQ(vals[z], val); + } + } + + myvec4.w() = 0xFFFFU; + { + unsigned short int vals[4] { test_val, 5, test_val, 0xFFFFU }; + for (int z = 0; z < 4; ++z) { + const auto val = myvec4.data()[z]; + EXPECT_EQ(vals[z], val); + } + } + + myvec3.x() = 1001U; + myvec3.y() = 1503U; + myvec3.z() = 2007U; + EXPECT_EQ(4511U, myvec3.x() + myvec3.y() + myvec3.z()); + + EXPECT_NE(myvec3, myvec4.xyz()); +}