From 1e713696d72f9929cd446d15efe162642d0066ba Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 28 Jul 2018 00:04:24 +0100 Subject: [PATCH] Add unit test for offset-based getters. --- test/unit/CMakeLists.txt | 1 + test/unit/test_offset_getters.cpp | 141 ++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 test/unit/test_offset_getters.cpp diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 8672eeb..94d2701 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -8,6 +8,7 @@ add_executable(${PROJECT_NAME} test_get_at.cpp test_operators.cpp test_sequence_range.cpp + test_offset_getters.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/test/unit/test_offset_getters.cpp b/test/unit/test_offset_getters.cpp new file mode 100644 index 0000000..284335f --- /dev/null +++ b/test/unit/test_offset_getters.cpp @@ -0,0 +1,141 @@ +/* + * Copyright 2015-2017 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 + +struct Point { + int x, y; +}; +struct Line { + int x, y, z; +}; +struct Hyperline { + int x, y, z, w; +}; + +namespace vwr { + template <> + struct VectorWrapperInfo { + enum { dimensions = 2 }; + + typedef int scalar_type; + typedef Point vector_type; + + typedef Line higher_vector_type; + + enum { + offset_x = offsetof(vector_type, x), + offset_y = offsetof(vector_type, y) + }; + }; + template <> + struct VectorWrapperInfo { + enum { dimensions = 3 }; + + typedef int scalar_type; + typedef Line vector_type; + + typedef Point lower_vector_type; + typedef Hyperline higher_vector_type; + + enum { + offset_x = offsetof(vector_type, x), + offset_y = offsetof(vector_type, y), + offset_z = offsetof(vector_type, z) + }; + }; + template <> + struct VectorWrapperInfo { + enum { dimensions = 4 }; + + typedef int scalar_type; + typedef Hyperline vector_type; + + typedef Line lower_vector_type; + + enum { + offset_x = offsetof(vector_type, x), + offset_y = offsetof(vector_type, y), + offset_z = offsetof(vector_type, z), + offset_w = offsetof(vector_type, w) + }; + }; +} //namespace vwr + +using vec2 = vwr::Vec; +using vec3 = vwr::Vec; +using vec4 = vwr::Vec; + +TEST(vwr, offset_get_at) { + { + vec2 v(300000, 400000); + EXPECT_EQ(v.data().x, 300000); + EXPECT_EQ(v.data().y, 400000); + EXPECT_EQ(v.x(), 300000); + EXPECT_EQ(v.y(), 400000); + v.x()++; + v.y()++; + EXPECT_EQ(v.data().x, 300001); + EXPECT_EQ(v.data().y, 400001); + EXPECT_EQ(v.x(), 300001); + EXPECT_EQ(v.y(), 400001); + } + + { + vec3 v(330000, 440000, 550000); + EXPECT_EQ(v.data().x, 330000); + EXPECT_EQ(v.data().y, 440000); + EXPECT_EQ(v.data().z, 550000); + EXPECT_EQ(v.x(), 330000); + EXPECT_EQ(v.y(), 440000); + EXPECT_EQ(v.z(), 550000); + v.x()++; + v.y()++; + v.z()++; + EXPECT_EQ(v.data().x, 330001); + EXPECT_EQ(v.data().y, 440001); + EXPECT_EQ(v.data().z, 550001); + EXPECT_EQ(v.x(), 330001); + EXPECT_EQ(v.y(), 440001); + EXPECT_EQ(v.z(), 550001); + } + + { + vec4 v(333000, 444000, 555000, 666000); + EXPECT_EQ(v.data().x, 333000); + EXPECT_EQ(v.data().y, 444000); + EXPECT_EQ(v.data().z, 555000); + EXPECT_EQ(v.data().w, 666000); + EXPECT_EQ(v.x(), 333000); + EXPECT_EQ(v.y(), 444000); + EXPECT_EQ(v.z(), 555000); + EXPECT_EQ(v.w(), 666000); + v.x()++; + v.y()++; + v.z()++; + v.w()++; + EXPECT_EQ(v.data().x, 333001); + EXPECT_EQ(v.data().y, 444001); + EXPECT_EQ(v.data().z, 555001); + EXPECT_EQ(v.data().w, 666001); + EXPECT_EQ(v.x(), 333001); + EXPECT_EQ(v.y(), 444001); + EXPECT_EQ(v.z(), 555001); + EXPECT_EQ(v.w(), 666001); + } +}