From 1d92a4858415affb58bcbf1b2d10e628469483a5 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Mon, 18 Jun 2012 16:43:48 +0900 Subject: [PATCH] add libs/bitset/test/bitset.cpp --- libs/bitset/test/bitset.cpp | 204 ++++++++++++++++++++++++++++++++++++ sprout/bitset.hpp | 31 +++--- testspr/sprout.cpp | 2 + 3 files changed, 223 insertions(+), 14 deletions(-) create mode 100644 libs/bitset/test/bitset.cpp diff --git a/libs/bitset/test/bitset.cpp b/libs/bitset/test/bitset.cpp new file mode 100644 index 00000000..b3e3a348 --- /dev/null +++ b/libs/bitset/test/bitset.cpp @@ -0,0 +1,204 @@ +#ifndef SPROUT_LIBS_BITSET_TEST_BITSET_CPP +#define SPROUT_LIBS_BITSET_TEST_BITSET_CPP + +#include +#include +#include + +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 +#endif + +#endif // #ifndef SPROUT_LIBS_BITSET_TEST_BITSET_CPP diff --git a/sprout/bitset.hpp b/sprout/bitset.hpp index 8685c82a..7eb6e0e7 100644 --- a/sprout/bitset.hpp +++ b/sprout/bitset.hpp @@ -882,13 +882,13 @@ namespace sprout { template inline SPROUT_CONSTEXPR bitset operator|(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; - template + template inline SPROUT_CONSTEXPR bitset operator^(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; template inline std::basic_istream& operator>>(std::basic_istream& lhs, sprout::bitset& rhs); - template + template inline std::basic_ostream& operator<<(std::basic_ostream& lhs, sprout::bitset const& rhs); @@ -1349,16 +1349,19 @@ namespace sprout { } public: - friend sprout::bitset - sprout::operator& (sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; - friend sprout::bitset - sprout::operator| (sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; - friend sprout::bitset - sprout::operator^ (sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; + template + friend SPROUT_CONSTEXPR sprout::bitset + sprout::operator&(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; + template + friend SPROUT_CONSTEXPR sprout::bitset + sprout::operator|(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; + template + friend SPROUT_CONSTEXPR sprout::bitset + sprout::operator^(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT; template friend std::basic_istream& sprout::operator>>(std::basic_istream& lhs, sprout::bitset& rhs); - template + template friend std::basic_ostream& sprout::operator<<(std::basic_ostream& lhs, sprout::bitset const& rhs); @@ -1370,17 +1373,17 @@ namespace sprout { template inline SPROUT_CONSTEXPR bitset operator&(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT { - return bitset(lhs.do_and(rhs)); + return sprout::bitset(lhs.do_and(rhs)); } template inline SPROUT_CONSTEXPR bitset operator|(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT { - return bitset(lhs.do_or(rhs)); + return sprout::bitset(lhs.do_or(rhs)); } - template + template inline SPROUT_CONSTEXPR bitset operator^(sprout::bitset const& lhs, sprout::bitset const& rhs) SPROUT_NOEXCEPT { - return bitset(lhs.do_xor(rhs)); + return sprout::bitset(lhs.do_xor(rhs)); } template inline std::basic_istream& @@ -1430,7 +1433,7 @@ namespace sprout { } return lhs; } - template + template inline std::basic_ostream& operator<<(std::basic_ostream& lhs, sprout::bitset const& rhs) { std::basic_string tmp; diff --git a/testspr/sprout.cpp b/testspr/sprout.cpp index a66ee8ba..e140a2c3 100644 --- a/testspr/sprout.cpp +++ b/testspr/sprout.cpp @@ -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(); }