mirror of https://github.com/mosra/magnum.git
Browse Source
Because it somewhat confusingly may have implied that it's really composed of 8-bit bools, and not bits. The same reasoning was used to pick the name for Corrade's Containers::BitArray. Backwards compatibility aliases are in place as usual, however the internal BoolVectorConverter is now BitVectorConverter and there unfortunately cannot be any backwards compatibility. This breaks only GLM and Eigen integration in the magnum-integration repo, which I'm fixing immediately. I don't expect any user code to use this internal helper. For regular vectors maybe, for this one definitely not.pull/578/head
38 changed files with 1054 additions and 916 deletions
@ -0,0 +1,421 @@
|
||||
#ifndef Magnum_Math_BitVector_h |
||||
#define Magnum_Math_BitVector_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
/** @file
|
||||
* @brief Class @ref Magnum::Math::BitVector |
||||
* @m_since_latest |
||||
*/ |
||||
|
||||
#include <type_traits> |
||||
#include <Corrade/Containers/sequenceHelpers.h> |
||||
#ifndef CORRADE_NO_DEBUG |
||||
#include <Corrade/Utility/Debug.h> |
||||
#endif |
||||
|
||||
#include "Magnum/Types.h" |
||||
#include "Magnum/Math/Math.h" |
||||
#include "Magnum/Math/Tags.h" |
||||
|
||||
namespace Magnum { namespace Math { |
||||
|
||||
namespace Implementation { |
||||
template<std::size_t, class> struct BitVectorConverter; |
||||
|
||||
template<class T> constexpr T repeat(T value, std::size_t) { return value; } |
||||
} |
||||
|
||||
/**
|
||||
@brief Vector of bits |
||||
@tparam size Bit count |
||||
@m_since_latest |
||||
|
||||
Result of component-wise comparison from @ref Vector. The boolean values are |
||||
stored as bits in array of unsigned bytes, unused bits have undefined value |
||||
which doesn't affect comparison or @ref all() / @ref none() / @ref any() |
||||
functions. See also @ref matrix-vector for brief introduction. |
||||
|
||||
@section Math-BitVector-indexing Bit indexing |
||||
|
||||
Value at position 0 is the lowest bit of the first byte passed in constructor. |
||||
Value at position 8 is the lowest bit of the second byte passed in constructor. |
||||
For example: |
||||
|
||||
@snippet MagnumMath-cpp14.cpp BitVector-indexing |
||||
|
||||
@section Math-BitVector-boolean Boolean operations |
||||
|
||||
The class implements @cpp && @ce, @cpp || @ce and @cpp ! @ce operators |
||||
component-wise, in other words equivalently to @cpp & @ce, @cpp | @ce and |
||||
@cpp ~ @ce. This is done in order to have consistent behavior with boolean |
||||
operations on scalar types --- in the following example, it causes the final |
||||
conversion to @cpp bool @ce done at the end (instead of it happening already in |
||||
the boolean subexpressions). Combined with @ref operator bool() returning |
||||
@cpp true @ce only if all bits are set, this means the condition will be passed |
||||
only if @cpp b @ce is around @cpp a @ce in *all dimensions*, and work the same |
||||
way as if the variables were just scalars: |
||||
|
||||
@snippet MagnumMath.cpp BitVector-boolean |
||||
|
||||
@see @ref Magnum::BitVector2, @ref Magnum::BitVector3, @ref Magnum::BitVector4 |
||||
*/ |
||||
template<std::size_t size> class BitVector { |
||||
static_assert(size != 0, "BitVector cannot have zero elements"); |
||||
|
||||
public: |
||||
enum: std::size_t { |
||||
Size = size, /**< Vector size */ |
||||
DataSize = (size-1)/8+1 /**< Vector storage size */ |
||||
}; |
||||
|
||||
/**
|
||||
* @brief Default constructor |
||||
* |
||||
* Equivalent to @ref BitVector(ZeroInitT). |
||||
*/ |
||||
constexpr /*implicit*/ BitVector() noexcept: _data{} {} |
||||
|
||||
/** @brief Construct a zero-filled boolean vector */ |
||||
constexpr explicit BitVector(ZeroInitT) noexcept: _data{} {} |
||||
|
||||
/** @brief Construct without initializing the contents */ |
||||
explicit BitVector(Magnum::NoInitT) noexcept {} |
||||
|
||||
/**
|
||||
* @brief Construct a boolean vector from segment values |
||||
* @param first Value for first 8bit segment |
||||
* @param next Values for next Bbit segments |
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
template<class ...T> constexpr /*implicit*/ BitVector(UnsignedByte first, T... next) noexcept; |
||||
#else |
||||
template<class ...T, class U = typename std::enable_if<sizeof...(T)+1 == DataSize, bool>::type> constexpr /*implicit*/ BitVector(UnsignedByte first, T... next) noexcept: _data{first, UnsignedByte(next)...} {} |
||||
#endif |
||||
|
||||
/** @brief Construct a boolean vector with one value for all fields */ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
explicit BitVector(T value) noexcept; |
||||
#else |
||||
template<class T, class U = typename std::enable_if<std::is_same<bool, T>::value && size != 1, bool>::type> constexpr explicit BitVector(T value) noexcept: BitVector(typename Corrade::Containers::Implementation::GenerateSequence<DataSize>::Type{}, value ? FullSegmentMask : 0) {} |
||||
#endif |
||||
|
||||
/** @brief Construct a boolean vector from external representation */ |
||||
template<class U, class V = decltype(Implementation::BitVectorConverter<size, U>::from(std::declval<U>()))> constexpr explicit BitVector(const U& other) noexcept: BitVector{Implementation::BitVectorConverter<size, U>::from(other)} {} |
||||
|
||||
/** @brief Convert a boolean vector to external representation */ |
||||
template<class U, class V = decltype(Implementation::BitVectorConverter<size, U>::to(std::declval<BitVector<size>>()))> constexpr explicit operator U() const { |
||||
return Implementation::BitVectorConverter<size, U>::to(*this); |
||||
} |
||||
|
||||
/**
|
||||
* @brief Raw data |
||||
* |
||||
* Contrary to what Doxygen shows, returns reference to an |
||||
* one-dimensional fixed-size array of @ref DataSize elements, i.e. |
||||
* @cpp UnsignedByte(&)[DataSize] @ce. |
||||
* @see @ref operator[](), @ref set() |
||||
* @todoc Fix once there's a possibility to patch the signature in a |
||||
* post-processing step (https://github.com/mosra/m.css/issues/56)
|
||||
*/ |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
UnsignedByte* data(); |
||||
constexpr const UnsignedByte* data() const; /**< @overload */ |
||||
#else |
||||
auto data() -> UnsignedByte(&)[DataSize] { return _data; } |
||||
constexpr auto data() const -> const UnsignedByte(&)[DataSize] { return _data; } |
||||
#endif |
||||
|
||||
/** @brief Bit at given position */ |
||||
constexpr bool operator[](std::size_t i) const { |
||||
return (_data[i/8] >> i%8) & 0x01; |
||||
} |
||||
|
||||
/** @brief Set a bit at given position */ |
||||
BitVector<size>& set(std::size_t i, bool value) { |
||||
value ? _data[i/8] |= (1 << i%8) : |
||||
_data[i/8] &= ~(1 << i%8); |
||||
return *this; |
||||
} |
||||
|
||||
/** @brief Equality comparison */ |
||||
bool operator==(const BitVector<size>& other) const; |
||||
|
||||
/** @brief Non-equality comparison */ |
||||
bool operator!=(const BitVector<size>& other) const { |
||||
return !operator==(other); |
||||
} |
||||
|
||||
/**
|
||||
* @brief Boolean conversion |
||||
* |
||||
* Equivalent to @ref all(). |
||||
* @see @ref any(), @ref none() |
||||
*/ |
||||
explicit operator bool() const { return all(); } |
||||
|
||||
/**
|
||||
* @brief Whether all bits are set |
||||
* |
||||
* @see @ref none(), @ref any(), @ref operator bool() |
||||
*/ |
||||
bool all() const; |
||||
|
||||
/**
|
||||
* @brief Whether no bits are set |
||||
* |
||||
* @see @ref all(), @ref any(), @ref operator bool() |
||||
*/ |
||||
bool none() const; |
||||
|
||||
/**
|
||||
* @brief Whether any bit is set |
||||
* |
||||
* @see @ref all(), @ref none(), @ref operator bool() |
||||
*/ |
||||
bool any() const { return !none(); } |
||||
|
||||
/** @brief Bitwise inversion */ |
||||
BitVector<size> operator~() const; |
||||
|
||||
/**
|
||||
* @brief Component-wise boolean negation |
||||
* @m_since{2019,10} |
||||
* |
||||
* Equivalent to @ref operator~(). See @ref Math-BitVector-boolean for |
||||
* more information. |
||||
*/ |
||||
BitVector<size> operator!() const { return operator~(); } |
||||
|
||||
/**
|
||||
* @brief Bitwise AND and assign |
||||
* |
||||
* The computation is done in-place. |
||||
*/ |
||||
BitVector<size>& operator&=(const BitVector<size>& other) { |
||||
for(std::size_t i = 0; i != DataSize; ++i) |
||||
_data[i] &= other._data[i]; |
||||
|
||||
return *this; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Bitwise AND |
||||
* |
||||
* @see @ref operator&=() |
||||
*/ |
||||
BitVector<size> operator&(const BitVector<size>& other) const { |
||||
return BitVector<size>(*this) &= other; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Component-wise boolean AND |
||||
* @m_since{2019,10} |
||||
* |
||||
* Equivalent to @ref operator&(). See @ref Math-BitVector-boolean for |
||||
* more information. |
||||
*/ |
||||
BitVector<size> operator&&(const BitVector<size>& other) const { |
||||
return BitVector<size>(*this) &= other; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Bitwise OR and assign |
||||
* |
||||
* The computation is done in-place. |
||||
*/ |
||||
BitVector<size>& operator|=(const BitVector<size>& other) { |
||||
for(std::size_t i = 0; i != DataSize; ++i) |
||||
_data[i] |= other._data[i]; |
||||
|
||||
return *this; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Bitwise OR |
||||
* |
||||
* @see @ref operator|=() |
||||
*/ |
||||
BitVector<size> operator|(const BitVector<size>& other) const { |
||||
return BitVector<size>(*this) |= other; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Component-wise boolean OR |
||||
* @m_since{2019,10} |
||||
* |
||||
* Equivalent to @ref operator|(). See @ref Math-BitVector-boolean for |
||||
* more information. |
||||
*/ |
||||
BitVector<size> operator||(const BitVector<size>& other) const { |
||||
return BitVector<size>(*this) |= other; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Bitwise XOR and assign |
||||
* |
||||
* The computation is done in-place. |
||||
*/ |
||||
BitVector<size>& operator^=(const BitVector<size>& other) { |
||||
for(std::size_t i = 0; i != DataSize; ++i) |
||||
_data[i] ^= other._data[i]; |
||||
|
||||
return *this; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Bitwise XOR |
||||
* |
||||
* @see @ref operator^=() |
||||
*/ |
||||
BitVector<size> operator^(const BitVector<size>& other) const { |
||||
return BitVector<size>(*this) ^= other; |
||||
} |
||||
|
||||
private: |
||||
enum: UnsignedByte { |
||||
FullSegmentMask = 0xFF, |
||||
LastSegmentMask = (1 << size%8) - 1 |
||||
}; |
||||
|
||||
/* Implementation for Vector<size, T>::Vector(U) */ |
||||
template<std::size_t ...sequence> constexpr explicit BitVector(Corrade::Containers::Implementation::Sequence<sequence...>, UnsignedByte value): _data{Implementation::repeat(value, sequence)...} {} |
||||
|
||||
UnsignedByte _data[(size-1)/8+1]; |
||||
}; |
||||
|
||||
#ifndef CORRADE_NO_DEBUG |
||||
/**
|
||||
@debugoperator{BitVector} |
||||
@m_since_latest |
||||
|
||||
In order to avoid potential confusion, prints the value as a comma-separated sequence of binary literals, so the output corresponds to how the value would |
||||
be constructed. For example, |
||||
|
||||
@snippet MagnumMath-cpp14.cpp BitVector-debug |
||||
|
||||
<b></b> |
||||
|
||||
@m_class{m-noindent} |
||||
|
||||
prints as |
||||
|
||||
@code{.shell-session} |
||||
BitVector(0b1010) BitVector(0b00001000, 0b00000011, 0b100) |
||||
@endcode |
||||
|
||||
Note that this, on the other hand, makes mapping to bit indices less obvious |
||||
--- see @ref Math-BitVector-indexing for more information. |
||||
*/ |
||||
template<std::size_t size> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const BitVector<size>& value) { |
||||
debug << "BitVector(0b" << Corrade::Utility::Debug::nospace; |
||||
|
||||
/* Print the full bytes comma-separated */ |
||||
for(std::size_t byte = 0; byte != BitVector<size>::DataSize - 1; ++byte) { |
||||
for(std::size_t i = 0; i != 8; ++i) |
||||
debug << (((value.data()[byte] >> (8 - i - 1)) & 1) ? "1" : "0") |
||||
<< Corrade::Utility::Debug::nospace; |
||||
debug << ", 0b" << Corrade::Utility::Debug::nospace; |
||||
} |
||||
|
||||
/* Print the last (potentially) partial byte */ |
||||
constexpr std::size_t suffixSize = size%8 ? size%8 : 8; |
||||
for(std::size_t i = 0; i != suffixSize; ++i) |
||||
debug << (((value.data()[size/8] >> (suffixSize - i - 1)) & 1) ? "1" : "0") |
||||
<< Corrade::Utility::Debug::nospace; |
||||
|
||||
return debug << ")"; |
||||
} |
||||
#endif |
||||
|
||||
template<std::size_t size> inline bool BitVector<size>::operator==(const BitVector<size>& other) const { |
||||
for(std::size_t i = 0; i != size/8; ++i) |
||||
if(_data[i] != other._data[i]) return false; |
||||
|
||||
/* Check last segment */ |
||||
if(size%8 && (_data[DataSize-1] & LastSegmentMask) != (other._data[DataSize-1] & LastSegmentMask)) |
||||
return false; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
template<std::size_t size> inline bool BitVector<size>::all() const { |
||||
/* Check all full segments */ |
||||
for(std::size_t i = 0; i != size/8; ++i) |
||||
if(_data[i] != FullSegmentMask) return false; |
||||
|
||||
/* Check last segment */ |
||||
if(size%8 && (_data[DataSize-1] & LastSegmentMask) != LastSegmentMask) |
||||
return false; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
template<std::size_t size> inline bool BitVector<size>::none() const { |
||||
/* Check all full segments */ |
||||
for(std::size_t i = 0; i != size/8; ++i) |
||||
if(_data[i]) return false; |
||||
|
||||
/* Check last segment */ |
||||
if(size%8 && (_data[DataSize-1] & LastSegmentMask)) |
||||
return false; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
template<std::size_t size> inline BitVector<size> BitVector<size>::operator~() const { |
||||
BitVector<size> out{Magnum::NoInit}; |
||||
|
||||
for(std::size_t i = 0; i != DataSize; ++i) |
||||
out._data[i] = ~_data[i]; |
||||
|
||||
return out; |
||||
} |
||||
|
||||
namespace Implementation { |
||||
|
||||
template<std::size_t size> struct StrictWeakOrdering<BitVector<size>> { |
||||
bool operator()(const BitVector<size>& a, const BitVector<size>& b) const { |
||||
auto ad = a.data(); |
||||
auto bd = b.data(); |
||||
for(std::size_t i = 0; i < BitVector<size>::DataSize - 1; ++i) { |
||||
if(ad[i] < bd[i]) |
||||
return true; |
||||
if(ad[i] > bd[i]) |
||||
return false; |
||||
} |
||||
|
||||
/* Mask last element with to hide padding bits */ |
||||
constexpr UnsignedByte mask = UnsignedByte(0xFF) >> (BitVector<size>::DataSize * 8 - size); |
||||
constexpr std::size_t i = BitVector<size>::DataSize - 1; |
||||
return (ad[i] & mask) < (bd[i] & mask); |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,379 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include <sstream> |
||||
#include <Corrade/TestSuite/Tester.h> |
||||
#include <Corrade/Utility/DebugStl.h> |
||||
#include <Corrade/Utility/TypeTraits.h> /* CORRADE_STD_IS_TRIVIALLY_TRAITS_SUPPORTED */ |
||||
|
||||
#include "Magnum/Math/BitVector.h" |
||||
#include "Magnum/Math/StrictWeakOrdering.h" |
||||
|
||||
struct BVec3 { |
||||
bool x, y, z; |
||||
}; |
||||
|
||||
namespace Magnum { namespace Math { |
||||
|
||||
namespace Implementation { |
||||
|
||||
template<> struct BitVectorConverter<3, BVec3> { |
||||
constexpr static BitVector<3> from(const BVec3& other) { |
||||
return (other.x << 0)|(other.y << 1)|(other.z << 2); |
||||
} |
||||
|
||||
constexpr static BVec3 to(const BitVector<3>& other) { |
||||
return {other[0], other[1], other[2]}; |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
namespace Test { namespace { |
||||
|
||||
struct BitVectorTest: Corrade::TestSuite::Tester { |
||||
explicit BitVectorTest(); |
||||
|
||||
void construct(); |
||||
void constructDefault(); |
||||
void constructNoInit(); |
||||
void constructOneValue(); |
||||
void constructOneElement(); |
||||
void constructCopy(); |
||||
void convert(); |
||||
|
||||
void data(); |
||||
|
||||
void compare(); |
||||
void compareUndefined(); |
||||
void convertBool(); |
||||
void all(); |
||||
void none(); |
||||
void any(); |
||||
|
||||
void bitInverse(); |
||||
void bitAndOrXor(); |
||||
void booleanOperationEquivalents(); |
||||
|
||||
void strictWeakOrdering(); |
||||
|
||||
void debug(); |
||||
}; |
||||
|
||||
static_assert(BitVector<15>::DataSize == 2, "Improper DataSize"); |
||||
static_assert(BitVector<16>::DataSize == 2, "Improper DataSize"); |
||||
static_assert(BitVector<17>::DataSize == 3, "Improper DataSize"); |
||||
|
||||
typedef Math::BitVector<19> BitVector19; |
||||
|
||||
BitVectorTest::BitVectorTest() { |
||||
addTests({&BitVectorTest::construct, |
||||
&BitVectorTest::constructDefault, |
||||
&BitVectorTest::constructNoInit, |
||||
&BitVectorTest::constructOneValue, |
||||
&BitVectorTest::constructOneElement, |
||||
&BitVectorTest::constructCopy, |
||||
&BitVectorTest::convert, |
||||
|
||||
&BitVectorTest::data, |
||||
|
||||
&BitVectorTest::compare, |
||||
&BitVectorTest::compareUndefined, |
||||
&BitVectorTest::convertBool, |
||||
&BitVectorTest::all, |
||||
&BitVectorTest::none, |
||||
&BitVectorTest::any, |
||||
|
||||
&BitVectorTest::bitInverse, |
||||
&BitVectorTest::bitAndOrXor, |
||||
&BitVectorTest::booleanOperationEquivalents, |
||||
|
||||
&BitVectorTest::strictWeakOrdering, |
||||
|
||||
&BitVectorTest::debug}); |
||||
} |
||||
|
||||
void BitVectorTest::construct() { |
||||
constexpr BitVector19 a = {0xa5, 0x5f, 0x07}; |
||||
CORRADE_COMPARE(a, BitVector19(0xa5, 0x5f, 0x07)); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BitVector19, UnsignedByte, UnsignedByte, UnsignedByte>::value); |
||||
} |
||||
|
||||
void BitVectorTest::constructDefault() { |
||||
constexpr BitVector19 a; |
||||
constexpr BitVector19 b{ZeroInit}; |
||||
CORRADE_COMPARE(a, BitVector19(0x00, 0x00, 0x00)); |
||||
CORRADE_COMPARE(b, BitVector19(0x00, 0x00, 0x00)); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_default_constructible<BitVector19>::value); |
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BitVector19, ZeroInitT>::value); |
||||
|
||||
/* Implicit construction is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<ZeroInitT, BitVector19>::value); |
||||
} |
||||
|
||||
void BitVectorTest::constructNoInit() { |
||||
BitVector19 a{0xa5, 0x5f, 0x07}; |
||||
new(&a) BitVector19{Magnum::NoInit}; |
||||
{ |
||||
/* Explicitly check we're not on Clang because certain Clang-based IDEs
|
||||
inherit __GNUC__ if GCC is used instead of leaving it at 4 like |
||||
Clang itself does */ |
||||
#if defined(CORRADE_TARGET_GCC) && !defined(CORRADE_TARGET_CLANG) && __GNUC__*100 + __GNUC_MINOR__ >= 601 && __OPTIMIZE__ |
||||
CORRADE_EXPECT_FAIL("GCC 6.1+ misoptimizes and overwrites the value."); |
||||
#endif |
||||
CORRADE_COMPARE(a, BitVector19(0xa5, 0x5f, 0x07)); |
||||
} |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BitVector19, Magnum::NoInitT>::value); |
||||
|
||||
/* Implicit construction is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<Magnum::NoInitT, BitVector19>::value); |
||||
} |
||||
|
||||
void BitVectorTest::constructOneValue() { |
||||
constexpr BitVector19 a(false); |
||||
CORRADE_COMPARE(a, BitVector19(0x00, 0x00, 0x00)); |
||||
|
||||
constexpr BitVector19 b(true); |
||||
CORRADE_COMPARE(b, BitVector19(0xff, 0xff, 0x07)); |
||||
|
||||
CORRADE_VERIFY(!std::is_convertible<bool, BitVector19>::value); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BitVector19, bool>::value); |
||||
} |
||||
|
||||
void BitVectorTest::constructOneElement() { |
||||
typedef BitVector<1> BitVector1; |
||||
|
||||
constexpr BitVector1 a = 0x01; |
||||
CORRADE_COMPARE(a, BitVector1(0x01)); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BitVector1, UnsignedByte>::value); |
||||
} |
||||
|
||||
void BitVectorTest::constructCopy() { |
||||
constexpr BitVector19 a = {0xa5, 0x5f, 0x07}; |
||||
constexpr BitVector19 b(a); |
||||
CORRADE_COMPARE(b, BitVector19(0xa5, 0x5f, 0x07)); |
||||
|
||||
#ifdef CORRADE_STD_IS_TRIVIALLY_TRAITS_SUPPORTED |
||||
CORRADE_VERIFY(std::is_trivially_copy_constructible<BitVector19>::value); |
||||
CORRADE_VERIFY(std::is_trivially_copy_assignable<BitVector19>::value); |
||||
#endif |
||||
CORRADE_VERIFY(std::is_nothrow_copy_constructible<BitVector19>::value); |
||||
CORRADE_VERIFY(std::is_nothrow_copy_assignable<BitVector19>::value); |
||||
} |
||||
|
||||
void BitVectorTest::convert() { |
||||
constexpr BVec3 a{false, true, true}; |
||||
constexpr BitVector<3> b{0x6}; |
||||
|
||||
constexpr BitVector<3> c{a}; |
||||
CORRADE_COMPARE(c, b); |
||||
|
||||
constexpr BVec3 d(b); |
||||
CORRADE_COMPARE(d.x, a.x); |
||||
CORRADE_COMPARE(d.y, a.y); |
||||
CORRADE_COMPARE(d.z, a.z); |
||||
|
||||
/* Implicit conversion is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<BVec3, BitVector<3>>::value); |
||||
CORRADE_VERIFY(!std::is_convertible<BitVector<3>, BVec3>::value); |
||||
} |
||||
|
||||
void BitVectorTest::data() { |
||||
/* 0b00001000, 0b00000011, 0b100 */ |
||||
constexpr BitVector19 ca(0x08, 0x03, 0x04); |
||||
|
||||
CORRADE_VERIFY(!ca[0] && !ca[1] && !ca[2]); |
||||
CORRADE_VERIFY(ca[3]); |
||||
CORRADE_VERIFY(!ca[4] && !ca[5] && !ca[6] && !ca[7]); |
||||
CORRADE_VERIFY(ca[8]); |
||||
CORRADE_VERIFY(ca[9]); |
||||
CORRADE_VERIFY(!ca[10] && !ca[11] && !ca[12] && !ca[13] && !ca[14] && !ca[15] && !ca[16] && !ca[17]); |
||||
CORRADE_VERIFY(ca[18]); |
||||
|
||||
constexpr bool b = ca[9]; |
||||
CORRADE_COMPARE(b, true); |
||||
|
||||
BitVector19 a(0x08, 0x03, 0x04); |
||||
a.set(15, true); |
||||
CORRADE_VERIFY(a[15]); |
||||
CORRADE_COMPARE(a, BitVector19(0x08, 0x83, 0x04)); |
||||
a.set(15, false); |
||||
CORRADE_VERIFY(!a[15]); |
||||
CORRADE_COMPARE(a, BitVector19(0x08, 0x03, 0x04)); |
||||
|
||||
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Apparently dereferencing pointer is verboten */ |
||||
constexpr |
||||
#endif |
||||
UnsignedByte c = *ca.data(); |
||||
CORRADE_COMPARE(a.data()[1], 0x03); |
||||
CORRADE_COMPARE(c, 0x08); |
||||
|
||||
/* It actually returns an array */ |
||||
CORRADE_COMPARE(Corrade::Containers::arraySize(a.data()), 3); |
||||
CORRADE_COMPARE(Corrade::Containers::arraySize(ca.data()), 3); |
||||
} |
||||
|
||||
void BitVectorTest::compare() { |
||||
BitVector19 a(0xa5, 0x5f, 0x07); |
||||
CORRADE_VERIFY(a == a); |
||||
|
||||
/* Change in full segments */ |
||||
BitVector19 b(0xa3, 0x5f, 0x07); |
||||
BitVector19 c(0xa5, 0x98, 0x07); |
||||
CORRADE_VERIFY(a != b); |
||||
CORRADE_VERIFY(a != c); |
||||
|
||||
/* Change in last bit */ |
||||
BitVector19 d(0xa5, 0x5f, 0x06); |
||||
CORRADE_VERIFY(a != d); |
||||
} |
||||
|
||||
void BitVectorTest::compareUndefined() { |
||||
BitVector19 a(0xa5, 0x5f, 0x07); |
||||
|
||||
/* Change in unused part of last segment */ |
||||
BitVector19 b(0xa5, 0x5f, 0x0f); |
||||
CORRADE_VERIFY(a == b); |
||||
|
||||
/* Change in used part of last segment */ |
||||
BitVector19 c(0xa5, 0x5f, 0x03); |
||||
CORRADE_VERIFY(a != c); |
||||
} |
||||
|
||||
void BitVectorTest::convertBool() { |
||||
CORRADE_VERIFY(BitVector19(0xff, 0xff, 0x07)); |
||||
CORRADE_VERIFY(!bool(BitVector19(0xff, 0xff, 0x04))); |
||||
CORRADE_VERIFY(!bool(BitVector19(0x00, 0x00, 0x00))); |
||||
CORRADE_VERIFY(BitVector19(0xff, 0xff, 0xff)); |
||||
|
||||
/* Using ! before and after bool conversion will produce a different
|
||||
result -- first is equivalent to !a.all(), while second is (~a).all() */ |
||||
CORRADE_COMPARE(!bool(BitVector19(0xff, 0xff, 0x04)), true); |
||||
CORRADE_COMPARE(bool(!BitVector19(0xff, 0xff, 0x04)), false); |
||||
|
||||
/* Implicit conversion is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<BitVector19, bool>::value); |
||||
} |
||||
|
||||
void BitVectorTest::all() { |
||||
CORRADE_VERIFY(BitVector19(0xff, 0xff, 0x07).all()); |
||||
|
||||
/* Last segment - bit in used and unused part */ |
||||
CORRADE_VERIFY(BitVector19(0xff, 0xff, 0x0f).all()); |
||||
CORRADE_VERIFY(!BitVector19(0xff, 0xff, 0x04).all()); |
||||
} |
||||
|
||||
void BitVectorTest::none() { |
||||
CORRADE_VERIFY(BitVector19(0x00, 0x00, 0x00).none()); |
||||
|
||||
/* Last segment - bit in used and unused part */ |
||||
CORRADE_VERIFY(BitVector19(0x00, 0x00, 0x08).none()); |
||||
CORRADE_VERIFY(!BitVector19(0x00, 0x00, 0x04).none()); |
||||
} |
||||
|
||||
void BitVectorTest::any() { |
||||
CORRADE_VERIFY(BitVector19(0x00, 0x01, 0x00).any()); |
||||
|
||||
/* Last segment - bit in used and unused part */ |
||||
CORRADE_VERIFY(BitVector19(0x00, 0x00, 0x04).any()); |
||||
CORRADE_VERIFY(!BitVector19(0x00, 0x00, 0x08).any()); |
||||
} |
||||
|
||||
void BitVectorTest::bitInverse() { |
||||
CORRADE_COMPARE(~BitVector19(0xa5, 0x5f, 0x03), BitVector19(0x5a, 0xa0, 0x04)); |
||||
CORRADE_COMPARE(!BitVector19(0xa5, 0x5f, 0x03), BitVector19(0x5a, 0xa0, 0x04)); |
||||
} |
||||
|
||||
void BitVectorTest::bitAndOrXor() { |
||||
BitVector19 a(0xa5, 0x5f, 0x03); |
||||
BitVector19 b(0x37, 0xf3, 0x06); |
||||
|
||||
CORRADE_COMPARE(a & b, BitVector19(0x25, 0x53, 0x02)); |
||||
CORRADE_COMPARE(a && b, BitVector19(0x25, 0x53, 0x02)); |
||||
|
||||
CORRADE_COMPARE(a | b, BitVector19(0xb7, 0xff, 0x07)); |
||||
CORRADE_COMPARE(a || b, BitVector19(0xb7, 0xff, 0x07)); |
||||
|
||||
CORRADE_COMPARE(a ^ b, BitVector19(0x92, 0xac, 0x05)); |
||||
} |
||||
|
||||
void BitVectorTest::booleanOperationEquivalents() { |
||||
Math::BitVector<2> a{0x3}; |
||||
Math::BitVector<2> b{0x2}; |
||||
|
||||
CORRADE_COMPARE(!(a || b), !a && !b); |
||||
CORRADE_COMPARE(!(a || b), ~(a | b)); |
||||
CORRADE_COMPARE(!a && !b, ~a & ~b); |
||||
} |
||||
|
||||
void BitVectorTest::strictWeakOrdering() { |
||||
BitVector<11> a, b, c; |
||||
|
||||
a.set(0, true); |
||||
a.set(1, true); |
||||
|
||||
c.set(7, true); |
||||
|
||||
b.set(8, true); |
||||
|
||||
StrictWeakOrdering o; |
||||
CORRADE_VERIFY( o(b, a)); |
||||
CORRADE_VERIFY(!o(a, b)); |
||||
CORRADE_VERIFY(!o(c, b)); |
||||
CORRADE_VERIFY( o(a, c)); |
||||
CORRADE_VERIFY(!o(c, a)); |
||||
|
||||
CORRADE_VERIFY(!o(a, a)); |
||||
|
||||
/* Check uninitialized padding reads */ |
||||
a.set(8, true); |
||||
a.set(10, true); |
||||
b = a; |
||||
a.data()[1] |= 0x08; |
||||
b.data()[1] |= 0x20; |
||||
a.data()[1] |= 0x40; |
||||
b.data()[1] |= 0x80; |
||||
|
||||
CORRADE_VERIFY(!o(a, b)); |
||||
CORRADE_VERIFY(!o(b, a)); |
||||
} |
||||
|
||||
void BitVectorTest::debug() { |
||||
std::ostringstream o; |
||||
|
||||
/* 0b00100101 0b01010011 0b010 */ |
||||
Debug(&o) << BitVector19(0x25, 0x53, 0x02); |
||||
|
||||
CORRADE_COMPARE(o.str(), "BitVector(0b00100101, 0b01010011, 0b010)\n"); |
||||
} |
||||
|
||||
}}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Math::Test::BitVectorTest) |
||||
@ -1,379 +0,0 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, |
||||
2020, 2021, 2022 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include <sstream> |
||||
#include <Corrade/TestSuite/Tester.h> |
||||
#include <Corrade/Utility/DebugStl.h> |
||||
#include <Corrade/Utility/TypeTraits.h> /* CORRADE_STD_IS_TRIVIALLY_TRAITS_SUPPORTED */ |
||||
|
||||
#include "Magnum/Math/BoolVector.h" |
||||
#include "Magnum/Math/StrictWeakOrdering.h" |
||||
|
||||
struct BVec3 { |
||||
bool x, y, z; |
||||
}; |
||||
|
||||
namespace Magnum { namespace Math { |
||||
|
||||
namespace Implementation { |
||||
|
||||
template<> struct BoolVectorConverter<3, BVec3> { |
||||
constexpr static BoolVector<3> from(const BVec3& other) { |
||||
return (other.x << 0)|(other.y << 1)|(other.z << 2); |
||||
} |
||||
|
||||
constexpr static BVec3 to(const BoolVector<3>& other) { |
||||
return {other[0], other[1], other[2]}; |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
namespace Test { namespace { |
||||
|
||||
struct BoolVectorTest: Corrade::TestSuite::Tester { |
||||
explicit BoolVectorTest(); |
||||
|
||||
void construct(); |
||||
void constructDefault(); |
||||
void constructNoInit(); |
||||
void constructOneValue(); |
||||
void constructOneElement(); |
||||
void constructCopy(); |
||||
void convert(); |
||||
|
||||
void data(); |
||||
|
||||
void compare(); |
||||
void compareUndefined(); |
||||
void convertBool(); |
||||
void all(); |
||||
void none(); |
||||
void any(); |
||||
|
||||
void bitInverse(); |
||||
void bitAndOrXor(); |
||||
void booleanOperationEquivalents(); |
||||
|
||||
void strictWeakOrdering(); |
||||
|
||||
void debug(); |
||||
}; |
||||
|
||||
static_assert(BoolVector<15>::DataSize == 2, "Improper DataSize"); |
||||
static_assert(BoolVector<16>::DataSize == 2, "Improper DataSize"); |
||||
static_assert(BoolVector<17>::DataSize == 3, "Improper DataSize"); |
||||
|
||||
typedef Math::BoolVector<19> BoolVector19; |
||||
|
||||
BoolVectorTest::BoolVectorTest() { |
||||
addTests({&BoolVectorTest::construct, |
||||
&BoolVectorTest::constructDefault, |
||||
&BoolVectorTest::constructNoInit, |
||||
&BoolVectorTest::constructOneValue, |
||||
&BoolVectorTest::constructOneElement, |
||||
&BoolVectorTest::constructCopy, |
||||
&BoolVectorTest::convert, |
||||
|
||||
&BoolVectorTest::data, |
||||
|
||||
&BoolVectorTest::compare, |
||||
&BoolVectorTest::compareUndefined, |
||||
&BoolVectorTest::convertBool, |
||||
&BoolVectorTest::all, |
||||
&BoolVectorTest::none, |
||||
&BoolVectorTest::any, |
||||
|
||||
&BoolVectorTest::bitInverse, |
||||
&BoolVectorTest::bitAndOrXor, |
||||
&BoolVectorTest::booleanOperationEquivalents, |
||||
|
||||
&BoolVectorTest::strictWeakOrdering, |
||||
|
||||
&BoolVectorTest::debug}); |
||||
} |
||||
|
||||
void BoolVectorTest::construct() { |
||||
constexpr BoolVector19 a = {0xa5, 0x5f, 0x07}; |
||||
CORRADE_COMPARE(a, BoolVector19(0xa5, 0x5f, 0x07)); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BoolVector19, UnsignedByte, UnsignedByte, UnsignedByte>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::constructDefault() { |
||||
constexpr BoolVector19 a; |
||||
constexpr BoolVector19 b{ZeroInit}; |
||||
CORRADE_COMPARE(a, BoolVector19(0x00, 0x00, 0x00)); |
||||
CORRADE_COMPARE(b, BoolVector19(0x00, 0x00, 0x00)); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_default_constructible<BoolVector19>::value); |
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BoolVector19, ZeroInitT>::value); |
||||
|
||||
/* Implicit construction is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<ZeroInitT, BoolVector19>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::constructNoInit() { |
||||
BoolVector19 a{0xa5, 0x5f, 0x07}; |
||||
new(&a) BoolVector19{Magnum::NoInit}; |
||||
{ |
||||
/* Explicitly check we're not on Clang because certain Clang-based IDEs
|
||||
inherit __GNUC__ if GCC is used instead of leaving it at 4 like |
||||
Clang itself does */ |
||||
#if defined(CORRADE_TARGET_GCC) && !defined(CORRADE_TARGET_CLANG) && __GNUC__*100 + __GNUC_MINOR__ >= 601 && __OPTIMIZE__ |
||||
CORRADE_EXPECT_FAIL("GCC 6.1+ misoptimizes and overwrites the value."); |
||||
#endif |
||||
CORRADE_COMPARE(a, BoolVector19(0xa5, 0x5f, 0x07)); |
||||
} |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BoolVector19, Magnum::NoInitT>::value); |
||||
|
||||
/* Implicit construction is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<Magnum::NoInitT, BoolVector19>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::constructOneValue() { |
||||
constexpr BoolVector19 a(false); |
||||
CORRADE_COMPARE(a, BoolVector19(0x00, 0x00, 0x00)); |
||||
|
||||
constexpr BoolVector19 b(true); |
||||
CORRADE_COMPARE(b, BoolVector19(0xff, 0xff, 0x07)); |
||||
|
||||
CORRADE_VERIFY(!std::is_convertible<bool, BoolVector19>::value); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BoolVector19, bool>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::constructOneElement() { |
||||
typedef BoolVector<1> BoolVector1; |
||||
|
||||
constexpr BoolVector1 a = 0x01; |
||||
CORRADE_COMPARE(a, BoolVector1(0x01)); |
||||
|
||||
CORRADE_VERIFY(std::is_nothrow_constructible<BoolVector1, UnsignedByte>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::constructCopy() { |
||||
constexpr BoolVector19 a = {0xa5, 0x5f, 0x07}; |
||||
constexpr BoolVector19 b(a); |
||||
CORRADE_COMPARE(b, BoolVector19(0xa5, 0x5f, 0x07)); |
||||
|
||||
#ifdef CORRADE_STD_IS_TRIVIALLY_TRAITS_SUPPORTED |
||||
CORRADE_VERIFY(std::is_trivially_copy_constructible<BoolVector19>::value); |
||||
CORRADE_VERIFY(std::is_trivially_copy_assignable<BoolVector19>::value); |
||||
#endif |
||||
CORRADE_VERIFY(std::is_nothrow_copy_constructible<BoolVector19>::value); |
||||
CORRADE_VERIFY(std::is_nothrow_copy_assignable<BoolVector19>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::convert() { |
||||
constexpr BVec3 a{false, true, true}; |
||||
constexpr BoolVector<3> b{0x6}; |
||||
|
||||
constexpr BoolVector<3> c{a}; |
||||
CORRADE_COMPARE(c, b); |
||||
|
||||
constexpr BVec3 d(b); |
||||
CORRADE_COMPARE(d.x, a.x); |
||||
CORRADE_COMPARE(d.y, a.y); |
||||
CORRADE_COMPARE(d.z, a.z); |
||||
|
||||
/* Implicit conversion is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<BVec3, BoolVector<3>>::value); |
||||
CORRADE_VERIFY(!std::is_convertible<BoolVector<3>, BVec3>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::data() { |
||||
/* 0b00001000, 0b00000011, 0b100 */ |
||||
constexpr BoolVector19 ca(0x08, 0x03, 0x04); |
||||
|
||||
CORRADE_VERIFY(!ca[0] && !ca[1] && !ca[2]); |
||||
CORRADE_VERIFY(ca[3]); |
||||
CORRADE_VERIFY(!ca[4] && !ca[5] && !ca[6] && !ca[7]); |
||||
CORRADE_VERIFY(ca[8]); |
||||
CORRADE_VERIFY(ca[9]); |
||||
CORRADE_VERIFY(!ca[10] && !ca[11] && !ca[12] && !ca[13] && !ca[14] && !ca[15] && !ca[16] && !ca[17]); |
||||
CORRADE_VERIFY(ca[18]); |
||||
|
||||
constexpr bool b = ca[9]; |
||||
CORRADE_COMPARE(b, true); |
||||
|
||||
BoolVector19 a(0x08, 0x03, 0x04); |
||||
a.set(15, true); |
||||
CORRADE_VERIFY(a[15]); |
||||
CORRADE_COMPARE(a, BoolVector19(0x08, 0x83, 0x04)); |
||||
a.set(15, false); |
||||
CORRADE_VERIFY(!a[15]); |
||||
CORRADE_COMPARE(a, BoolVector19(0x08, 0x03, 0x04)); |
||||
|
||||
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Apparently dereferencing pointer is verboten */ |
||||
constexpr |
||||
#endif |
||||
UnsignedByte c = *ca.data(); |
||||
CORRADE_COMPARE(a.data()[1], 0x03); |
||||
CORRADE_COMPARE(c, 0x08); |
||||
|
||||
/* It actually returns an array */ |
||||
CORRADE_COMPARE(Corrade::Containers::arraySize(a.data()), 3); |
||||
CORRADE_COMPARE(Corrade::Containers::arraySize(ca.data()), 3); |
||||
} |
||||
|
||||
void BoolVectorTest::compare() { |
||||
BoolVector19 a(0xa5, 0x5f, 0x07); |
||||
CORRADE_VERIFY(a == a); |
||||
|
||||
/* Change in full segments */ |
||||
BoolVector19 b(0xa3, 0x5f, 0x07); |
||||
BoolVector19 c(0xa5, 0x98, 0x07); |
||||
CORRADE_VERIFY(a != b); |
||||
CORRADE_VERIFY(a != c); |
||||
|
||||
/* Change in last bit */ |
||||
BoolVector19 d(0xa5, 0x5f, 0x06); |
||||
CORRADE_VERIFY(a != d); |
||||
} |
||||
|
||||
void BoolVectorTest::compareUndefined() { |
||||
BoolVector19 a(0xa5, 0x5f, 0x07); |
||||
|
||||
/* Change in unused part of last segment */ |
||||
BoolVector19 b(0xa5, 0x5f, 0x0f); |
||||
CORRADE_VERIFY(a == b); |
||||
|
||||
/* Change in used part of last segment */ |
||||
BoolVector19 c(0xa5, 0x5f, 0x03); |
||||
CORRADE_VERIFY(a != c); |
||||
} |
||||
|
||||
void BoolVectorTest::convertBool() { |
||||
CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x07)); |
||||
CORRADE_VERIFY(!bool(BoolVector19(0xff, 0xff, 0x04))); |
||||
CORRADE_VERIFY(!bool(BoolVector19(0x00, 0x00, 0x00))); |
||||
CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0xff)); |
||||
|
||||
/* Using ! before and after bool conversion will produce a different
|
||||
result -- first is equivalent to !a.all(), while second is (~a).all() */ |
||||
CORRADE_COMPARE(!bool(BoolVector19(0xff, 0xff, 0x04)), true); |
||||
CORRADE_COMPARE(bool(!BoolVector19(0xff, 0xff, 0x04)), false); |
||||
|
||||
/* Implicit conversion is not allowed */ |
||||
CORRADE_VERIFY(!std::is_convertible<BoolVector19, bool>::value); |
||||
} |
||||
|
||||
void BoolVectorTest::all() { |
||||
CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x07).all()); |
||||
|
||||
/* Last segment - bit in used and unused part */ |
||||
CORRADE_VERIFY(BoolVector19(0xff, 0xff, 0x0f).all()); |
||||
CORRADE_VERIFY(!BoolVector19(0xff, 0xff, 0x04).all()); |
||||
} |
||||
|
||||
void BoolVectorTest::none() { |
||||
CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x00).none()); |
||||
|
||||
/* Last segment - bit in used and unused part */ |
||||
CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x08).none()); |
||||
CORRADE_VERIFY(!BoolVector19(0x00, 0x00, 0x04).none()); |
||||
} |
||||
|
||||
void BoolVectorTest::any() { |
||||
CORRADE_VERIFY(BoolVector19(0x00, 0x01, 0x00).any()); |
||||
|
||||
/* Last segment - bit in used and unused part */ |
||||
CORRADE_VERIFY(BoolVector19(0x00, 0x00, 0x04).any()); |
||||
CORRADE_VERIFY(!BoolVector19(0x00, 0x00, 0x08).any()); |
||||
} |
||||
|
||||
void BoolVectorTest::bitInverse() { |
||||
CORRADE_COMPARE(~BoolVector19(0xa5, 0x5f, 0x03), BoolVector19(0x5a, 0xa0, 0x04)); |
||||
CORRADE_COMPARE(!BoolVector19(0xa5, 0x5f, 0x03), BoolVector19(0x5a, 0xa0, 0x04)); |
||||
} |
||||
|
||||
void BoolVectorTest::bitAndOrXor() { |
||||
BoolVector19 a(0xa5, 0x5f, 0x03); |
||||
BoolVector19 b(0x37, 0xf3, 0x06); |
||||
|
||||
CORRADE_COMPARE(a & b, BoolVector19(0x25, 0x53, 0x02)); |
||||
CORRADE_COMPARE(a && b, BoolVector19(0x25, 0x53, 0x02)); |
||||
|
||||
CORRADE_COMPARE(a | b, BoolVector19(0xb7, 0xff, 0x07)); |
||||
CORRADE_COMPARE(a || b, BoolVector19(0xb7, 0xff, 0x07)); |
||||
|
||||
CORRADE_COMPARE(a ^ b, BoolVector19(0x92, 0xac, 0x05)); |
||||
} |
||||
|
||||
void BoolVectorTest::booleanOperationEquivalents() { |
||||
Math::BoolVector<2> a{0x3}; |
||||
Math::BoolVector<2> b{0x2}; |
||||
|
||||
CORRADE_COMPARE(!(a || b), !a && !b); |
||||
CORRADE_COMPARE(!(a || b), ~(a | b)); |
||||
CORRADE_COMPARE(!a && !b, ~a & ~b); |
||||
} |
||||
|
||||
void BoolVectorTest::strictWeakOrdering() { |
||||
BoolVector<11> a, b, c; |
||||
|
||||
a.set(0, true); |
||||
a.set(1, true); |
||||
|
||||
c.set(7, true); |
||||
|
||||
b.set(8, true); |
||||
|
||||
StrictWeakOrdering o; |
||||
CORRADE_VERIFY( o(b, a)); |
||||
CORRADE_VERIFY(!o(a, b)); |
||||
CORRADE_VERIFY(!o(c, b)); |
||||
CORRADE_VERIFY( o(a, c)); |
||||
CORRADE_VERIFY(!o(c, a)); |
||||
|
||||
CORRADE_VERIFY(!o(a, a)); |
||||
|
||||
/* Check uninitialized padding reads */ |
||||
a.set(8, true); |
||||
a.set(10, true); |
||||
b = a; |
||||
a.data()[1] |= 0x08; |
||||
b.data()[1] |= 0x20; |
||||
a.data()[1] |= 0x40; |
||||
b.data()[1] |= 0x80; |
||||
|
||||
CORRADE_VERIFY(!o(a, b)); |
||||
CORRADE_VERIFY(!o(b, a)); |
||||
} |
||||
|
||||
void BoolVectorTest::debug() { |
||||
std::ostringstream o; |
||||
|
||||
/* 0b00100101 0b01010011 0b010 */ |
||||
Debug(&o) << BoolVector19(0x25, 0x53, 0x02); |
||||
|
||||
CORRADE_COMPARE(o.str(), "BoolVector(0b00100101, 0b01010011, 0b010)\n"); |
||||
} |
||||
|
||||
}}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::Math::Test::BoolVectorTest) |
||||
Loading…
Reference in new issue