Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/tests/test_PackedArray.h
- This file was added.
/* Copyright (C) 2021 Wildfire Games. | |||||
* This file is part of 0 A.D. | |||||
* | |||||
* 0 A.D. 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 2 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "lib/self_test.h" | |||||
#include "simulation2/helpers/PackedArray.h" | |||||
Stan: #include <type_traits> | |||||
enum class SmallEnum | |||||
{ | |||||
ZERO = 0, | |||||
ONE = 1, | |||||
TWO = 2 | |||||
}; | |||||
namespace std | |||||
{ | |||||
template<> class numeric_limits<SmallEnum> { public: static constexpr int digits = 2; }; | |||||
} | |||||
class TestPackedArray : public CxxTest::TestSuite | |||||
{ | |||||
public: | |||||
void setUp() | |||||
{ | |||||
} | |||||
void tearDown() | |||||
{ | |||||
} | |||||
Not Done Inline ActionsIIRC they don't have to be there. phosit: IIRC they don't have to be there. | |||||
void test_compiletime() | |||||
{ | |||||
static_assert(std::is_same_v<PackedArray<SmallEnum, 8>::Storage, u16>); | |||||
static_assert(std::is_same_v<PackedArray<bool, 8>::Storage, u8>); | |||||
static_assert(std::is_same_v<PackedArray<SmallEnum, 28>::Storage, u64>); | |||||
static_assert(sizeof(PackedArray<SmallEnum, 8>::Storage) == 2); | |||||
static_assert(PackedArray<SmallEnum, 8>::MaskBits == 0b11); | |||||
{ | |||||
constexpr PackedArray<SmallEnum, 8> test(SmallEnum::ONE); | |||||
static_assert(test.m_Storage == 0b0101010101010101); | |||||
} | |||||
{ | |||||
constexpr PackedArray<SmallEnum, 8> test(SmallEnum::TWO); | |||||
static_assert(test.m_Storage == 0b1010101010101010); | |||||
} | |||||
} | |||||
void test_basic() | |||||
{ | |||||
PackedArray<SmallEnum, 8> packed; | |||||
packed[4u] = SmallEnum::TWO; | |||||
packed[7u] = SmallEnum::ONE; | |||||
TS_ASSERT_EQUALS(packed[0u], SmallEnum::ZERO) | |||||
TS_ASSERT_EQUALS(packed[4u], SmallEnum::TWO) | |||||
TS_ASSERT_EQUALS(packed[7u], SmallEnum::ONE) | |||||
TS_ASSERT_EQUALS(packed.m_Storage, 0b0100001000000000); | |||||
} | |||||
void test_wraparound() | |||||
{ | |||||
PackedArray<SmallEnum, 6, true> packed; | |||||
packed[2] = SmallEnum::TWO; | |||||
packed[5] = SmallEnum::ONE; | |||||
TS_ASSERT_EQUALS(packed[0], SmallEnum::ZERO) | |||||
TS_ASSERT_EQUALS(packed[2], SmallEnum::TWO) | |||||
TS_ASSERT_EQUALS(packed[5], SmallEnum::ONE) | |||||
TS_ASSERT_EQUALS(packed[-1], SmallEnum::ONE) | |||||
TS_ASSERT_EQUALS(packed.m_Storage, 0b010000100000); | |||||
} | |||||
}; |
Wildfire Games · Phabricator
#include <type_traits>