Just some test for wrappers of c-style arrays

This commit is contained in:
King_DuckZ 2021-03-12 00:11:28 +01:00
parent 31546e83c6
commit cdb2650241
3 changed files with 94 additions and 0 deletions

View File

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

View File

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

View File

@ -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 <gtest/gtest.h>
#include <cstddef>
namespace vwr {
template <> struct VectorWrapperInfo<unsigned short int[3]> {
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<unsigned short int[4]> {
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<unsigned short int[3]> vec3;
typedef vwr::Vec<unsigned short int[4]> 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());
}