1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

add libs/bitset/test/bitset.cpp

This commit is contained in:
bolero-MURAKAMI 2012-06-18 16:43:48 +09:00
parent c091f0ef0c
commit 1d92a48584
3 changed files with 223 additions and 14 deletions

204
libs/bitset/test/bitset.cpp Normal file
View file

@ -0,0 +1,204 @@
#ifndef SPROUT_LIBS_BITSET_TEST_BITSET_CPP
#define SPROUT_LIBS_BITSET_TEST_BITSET_CPP
#include <sstream>
#include <sprout/bitset.hpp>
#include <testspr/tools.hpp>
namespace testspr {
static void bitset_test() {
using namespace sprout;
{
typedef sprout::bitset<40> bitset_t;
SPROUT_STATIC_CONSTEXPR auto bits1 = bitset_t(0xDEADBEEF);
// 20.5.2 bitset operations:
// operator&=
{
auto bits = bits1;
bits &= bitset_t(0xDEADFACE);
TESTSPR_ASSERT(bits == bitset_t(0xDEADBACE));
}
// operator|=
{
auto bits = bits1;
bits |= bitset_t(0xDEADFACE);
TESTSPR_ASSERT(bits == bitset_t(0xDEADFEEF));
}
// operator^=
{
auto bits = bits1;
bits ^= bitset_t(0xDEADFACE);
TESTSPR_ASSERT(bits == bitset_t(0x4421));
}
// operator<<=
{
auto bits = bits1;
bits <<= 10;
TESTSPR_ASSERT(bits == bitset_t(0x7AB6FBBC00ull));
}
// operator>>=
{
auto bits = bits1;
bits >>= 10;
TESTSPR_ASSERT(bits == bitset_t(0x37AB6Full));
}
// set
TESTSPR_BOTH_ASSERT(bits1.set() == bitset_t(0xFFFFFFFFFFull));
TESTSPR_BOTH_ASSERT(bits1.set(0, false) == bitset_t(0xDEADBEEE));
TESTSPR_BOTH_ASSERT(bits1.set(4, true) == bitset_t(0xDEADBEFF));
{
auto bits = bits1;
bits.set();
TESTSPR_ASSERT(bits == bitset_t(0xFFFFFFFFFFull));
}
{
auto bits = bits1;
bits.set(0, false);
TESTSPR_ASSERT(bits == bitset_t(0xDEADBEEE));
}
{
auto bits = bits1;
bits.set(4, true);
TESTSPR_ASSERT(bits == bitset_t(0xDEADBEFF));
}
// reset
TESTSPR_BOTH_ASSERT(bits1.reset() == bitset_t(0x0));
TESTSPR_BOTH_ASSERT(bits1.reset(0) == bitset_t(0xDEADBEEE));
{
auto bits = bits1;
bits.reset();
TESTSPR_ASSERT(bits == bitset_t(0x0));
}
{
auto bits = bits1;
bits.reset(0);
TESTSPR_ASSERT(bits == bitset_t(0xDEADBEEE));
}
// operator~
TESTSPR_BOTH_ASSERT(~bits1 == bitset_t(0xFF21524110ull));
TESTSPR_BOTH_ASSERT(~~bits1 == bitset_t(0xDEADBEEF));
// flip
TESTSPR_BOTH_ASSERT(bits1.flip() == bitset_t(0xFF21524110ull));
TESTSPR_BOTH_ASSERT(bits1.flip(0) == bitset_t(0xDEADBEEE));
TESTSPR_BOTH_ASSERT(bits1.flip(4) == bitset_t(0xDEADBEFF));
{
auto bits = bits1;
bits.flip();
TESTSPR_ASSERT(bits == bitset_t(0xFF21524110ull));
}
{
auto bits = bits1;
bits.flip(0);
TESTSPR_ASSERT(bits == bitset_t(0xDEADBEEE));
}
{
auto bits = bits1;
bits.flip(4);
TESTSPR_ASSERT(bits == bitset_t(0xDEADBEFF));
}
// element access:
// operator[]
TESTSPR_BOTH_ASSERT(bits1[0] == 1);
TESTSPR_BOTH_ASSERT(bits1[4] == 0);
{
auto bits = bits1;
TESTSPR_ASSERT(bits[0] == 1);
TESTSPR_ASSERT(bits[4] == 0);
bits[0] = ~bits[0];
bits[4] = ~bits[4];
TESTSPR_ASSERT(bits[0] == 0);
TESTSPR_ASSERT(bits[4] == 1);
}
// to_ulong
TESTSPR_BOTH_ASSERT(bits1.to_ulong() == 0xDEADBEEF);
// to_ullong
TESTSPR_BOTH_ASSERT(bits1.to_ullong() == 0xDEADBEEF);
// to_string
TESTSPR_ASSERT(bits1.to_string() == "0000000011011110101011011011111011101111");
// count
TESTSPR_BOTH_ASSERT(bits1.count() == 24);
// size
TESTSPR_BOTH_ASSERT(bits1.size() == 40);
// operator==
TESTSPR_BOTH_ASSERT(bits1 == bitset_t(0xDEADBEEF));
TESTSPR_BOTH_ASSERT(!(bits1 == bitset_t(0xDEADFACE)));
// operator!=
TESTSPR_BOTH_ASSERT(!(bits1 != bitset_t(0xDEADBEEF)));
TESTSPR_BOTH_ASSERT(bits1 != bitset_t(0xDEADFACE));
// test
TESTSPR_BOTH_ASSERT(bits1.test(0) == 1);
TESTSPR_BOTH_ASSERT(bits1.test(4) == 0);
// all
TESTSPR_BOTH_ASSERT(!bits1.all());
TESTSPR_BOTH_ASSERT(bitset_t(0xFFFFFFFFFFull).all());
// any
TESTSPR_BOTH_ASSERT(bits1.any());
TESTSPR_BOTH_ASSERT(!bitset_t(0x0).any());
// none
TESTSPR_BOTH_ASSERT(!bits1.none());
TESTSPR_BOTH_ASSERT(bitset_t(0x0).none());
// operator<<
TESTSPR_BOTH_ASSERT((bits1 << 10) == bitset_t(0x7AB6FBBC00ull));
// operator>>
TESTSPR_BOTH_ASSERT((bits1 >> 10) == bitset_t(0x37AB6Full));
// 20.5.4 bitset operators:
// operator&
TESTSPR_BOTH_ASSERT((bits1 & bitset_t(0xDEADFACE)) == bitset_t(0xDEADBACE));
// operator|
TESTSPR_BOTH_ASSERT((bits1 | bitset_t(0xDEADFACE)) == bitset_t(0xDEADFEEF));
// operator^
TESTSPR_BOTH_ASSERT((bits1 ^ bitset_t(0xDEADFACE)) == bitset_t(0x4421));
// operator<<
{
std::ostringstream os;
os << bits1;
TESTSPR_ASSERT(os.str() == "0000000011011110101011011011111011101111");
}
// operator>>
{
std::istringstream is("0000000011011110101011011011111011101111");
auto bits = bitset_t();
is >> bits;
TESTSPR_ASSERT(bits == bitset_t(0xDEADBEEF));
}
}
}
} // namespace testspr
#ifndef TESTSPR_CPP_INCLUDE
# define TESTSPR_TEST_FUNCTION testspr::bitset_test
# include <testspr/include_main.hpp>
#endif
#endif // #ifndef SPROUT_LIBS_BITSET_TEST_BITSET_CPP

View file

@ -1349,12 +1349,15 @@ namespace sprout {
}
public:
friend sprout::bitset<N>
sprout::operator& <N>(sprout::bitset<N> const& lhs, sprout::bitset<N> const& rhs) SPROUT_NOEXCEPT;
friend sprout::bitset<N>
sprout::operator| <N>(sprout::bitset<N> const& lhs, sprout::bitset<N> const& rhs) SPROUT_NOEXCEPT;
friend sprout::bitset<N>
sprout::operator^ <N>(sprout::bitset<N> const& lhs, sprout::bitset<N> const& rhs) SPROUT_NOEXCEPT;
template<std::size_t M>
friend SPROUT_CONSTEXPR sprout::bitset<M>
sprout::operator&(sprout::bitset<M> const& lhs, sprout::bitset<M> const& rhs) SPROUT_NOEXCEPT;
template<std::size_t M>
friend SPROUT_CONSTEXPR sprout::bitset<M>
sprout::operator|(sprout::bitset<M> const& lhs, sprout::bitset<M> const& rhs) SPROUT_NOEXCEPT;
template<std::size_t M>
friend SPROUT_CONSTEXPR sprout::bitset<M>
sprout::operator^(sprout::bitset<M> const& lhs, sprout::bitset<M> const& rhs) SPROUT_NOEXCEPT;
template<typename Char, typename Traits, std::size_t M>
friend std::basic_istream<Char, Traits>&
sprout::operator>>(std::basic_istream<Char, Traits>& lhs, sprout::bitset<M>& rhs);
@ -1370,17 +1373,17 @@ namespace sprout {
template<std::size_t N>
inline SPROUT_CONSTEXPR bitset<N>
operator&(sprout::bitset<N> const& lhs, sprout::bitset<N> const& rhs) SPROUT_NOEXCEPT {
return bitset<N>(lhs.do_and(rhs));
return sprout::bitset<N>(lhs.do_and(rhs));
}
template<std::size_t N>
inline SPROUT_CONSTEXPR bitset<N>
operator|(sprout::bitset<N> const& lhs, sprout::bitset<N> const& rhs) SPROUT_NOEXCEPT {
return bitset<N>(lhs.do_or(rhs));
return sprout::bitset<N>(lhs.do_or(rhs));
}
template<std::size_t N>
inline SPROUT_CONSTEXPR bitset<N>
operator^(sprout::bitset<N> const& lhs, sprout::bitset<N> const& rhs) SPROUT_NOEXCEPT {
return bitset<N>(lhs.do_xor(rhs));
return sprout::bitset<N>(lhs.do_xor(rhs));
}
template<typename Char, typename Traits, std::size_t N>
inline std::basic_istream<Char, Traits>&

View file

@ -8,6 +8,7 @@
#include "../libs/array/test/array.cpp"
#include "../libs/string/test/string.cpp"
#include "../libs/bitset/test/bitset.cpp"
#include "../libs/algorithm/test/algorithm.cpp"
#include "../libs/random/test/random.cpp"
@ -19,6 +20,7 @@ namespace testspr {
static void sprout_test() {
testspr::array_test();
testspr::string_test();
testspr::bitset_test();
testspr::algorithm_test();
testspr::random_test();
}