/* vim:set ts=2 sw=2 sts=2 et: */ /** * \author Marcus Holland-Moritz (github@mhxnet.de) * \copyright Copyright (c) Marcus Holland-Moritz * * This file is part of dwarfs. * * dwarfs is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * dwarfs is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with dwarfs. If not, see . * * SPDX-License-Identifier: GPL-3.0-only */ #include #include #include using namespace dwarfs::internal; TEST(packed_ptr, initialize) { { packed_ptr pp; EXPECT_EQ(pp.get(), nullptr); EXPECT_EQ(pp.get_data(), 0); } { alignas(8) uint32_t i = 42; packed_ptr pp(&i); EXPECT_EQ(pp.get(), &i); EXPECT_EQ(pp.get_data(), 0); } { packed_ptr pp(nullptr, 0x7); EXPECT_EQ(pp.get(), nullptr); EXPECT_EQ(pp.get_data(), 0x7); } { alignas(8) uint32_t i = 42; packed_ptr pp(&i, 0x7); EXPECT_EQ(pp.get(), &i); EXPECT_EQ(pp.get_data(), 0x7); EXPECT_EQ(*pp, 42); } EXPECT_THAT( [] { packed_ptr pp(nullptr, 0x8); }, testing::ThrowsMessage("data out of bounds")); EXPECT_THAT( [] { packed_ptr pp(reinterpret_cast(0x100004), 0x7); }, testing::ThrowsMessage("pointer is not aligned")); } TEST(packed_ptr, integral) { using ptr_type = std::pair; packed_ptr pp; static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_vfirst), int>); static_assert(std::is_same_vsecond), float>); alignas(4) ptr_type p = {42, 2.0f}; pp.set(&p); EXPECT_EQ(pp.get(), &p); EXPECT_EQ(pp.get_data(), 0); EXPECT_EQ(pp->first, 42); EXPECT_EQ(pp->second, 2.0f); EXPECT_EQ(pp[0].first, 42); EXPECT_EQ(pp[0].second, 2.0f); EXPECT_THAT( [&] { pp.set_data(0x4); }, testing::ThrowsMessage("data out of bounds")); pp.set_data(0x3); EXPECT_EQ(pp.get(), &p); EXPECT_EQ(pp.get_data(), 0x3); EXPECT_THAT( [&] { pp.set(reinterpret_cast(0x100001)); }, testing::ThrowsMessage("pointer is not aligned")); } TEST(packed_ptr, enumeration) { using ptr_type = std::pair; enum class test_enum : unsigned { A = 1, B, C, D }; packed_ptr pp; static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_vfirst), int>); alignas(4) ptr_type p = {42, 2.0f}; pp.set(&p); EXPECT_EQ(pp.get(), &p); EXPECT_EQ(pp.get_data(), static_cast(0)); pp.set_data(test_enum::B); EXPECT_EQ(pp.get(), &p); EXPECT_EQ(pp.get_data(), test_enum::B); EXPECT_THAT( [&] { pp.set_data(test_enum::D); }, testing::ThrowsMessage("data out of bounds")); }