From 9fa64062400aac4a1544d62f9dbacd0732452bc1 Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 6 Jul 2015 15:23:11 +0200 Subject: [PATCH] Some initial tests for libdevcore/FixedHash --- libdevcore/FixedHash.h | 2 +- test/libdevcore/FixedHash.cpp | 106 ++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 test/libdevcore/FixedHash.cpp diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index c7c551c2b..e9600ed0c 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -102,7 +102,7 @@ public: FixedHash operator&(FixedHash const& _c) const { return FixedHash(*this) &= _c; } FixedHash operator~() const { FixedHash ret; for (unsigned i = 0; i < N; ++i) ret[i] = ~m_data[i]; return ret; } - /// @returns true if all bytes in @a _c are set in this object. + /// @returns true if all one-bits in @a _c are set in this object. bool contains(FixedHash const& _c) const { return (*this & _c) == _c; } /// @returns a particular byte from the hash. diff --git a/test/libdevcore/FixedHash.cpp b/test/libdevcore/FixedHash.cpp new file mode 100644 index 000000000..9a6ebe0e0 --- /dev/null +++ b/test/libdevcore/FixedHash.cpp @@ -0,0 +1,106 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum 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. + + cpp-ethereum 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 cpp-ethereum. If not, see . +*/ +/** @file FixedHash.cpp + * @author Lefterus + * @date 2015 + */ + +#include +#include "../TestHelper.h" + +using namespace std; +using namespace dev; + +namespace dev +{ +namespace test +{ + +BOOST_AUTO_TEST_SUITE(FixedHashTests) + +BOOST_AUTO_TEST_CASE(FixedHashComparisons) +{ + FixedHash<4> h1(sha3("abcd")); + FixedHash<4> h2(sha3("abcd")); + FixedHash<4> h3(sha3("aadd")); + FixedHash<4> h4(0xBAADF00D); + FixedHash<4> h5(0xAAAAAAAA); + FixedHash<4> h6(0xBAADF00D); + + BOOST_CHECK(h1 == h2); + BOOST_CHECK(h2 != h3); + + BOOST_CHECK(h4 > h5); + BOOST_CHECK(h5 < h4); + BOOST_CHECK(h6 <= h4); + BOOST_CHECK(h6 >= h4); +} + +BOOST_AUTO_TEST_CASE(FixedHashXOR) +{ + FixedHash<2> h1("0xAAAA"); + FixedHash<2> h2("0xBBBB"); + + BOOST_CHECK((h1 ^ h2) == FixedHash<2>("0x1111")); + h1 ^= h2; + BOOST_CHECK(h1 == FixedHash<2>("0x1111")); +} + +BOOST_AUTO_TEST_CASE(FixedHashOR) +{ + FixedHash<4> h1("0xD3ADB33F"); + FixedHash<4> h2("0xBAADF00D"); + FixedHash<4> res("0xFBADF33F"); + + BOOST_CHECK((h1 | h2) == res); + h1 |= h2; + BOOST_CHECK(h1 == res); +} + +BOOST_AUTO_TEST_CASE(FixedHashAND) +{ + FixedHash<4> h1("0xD3ADB33F"); + FixedHash<4> h2("0xBAADF00D"); + FixedHash<4> h3("0x92aDB00D"); + + BOOST_CHECK((h1 & h2) == h3); + h1 &= h2; + BOOST_CHECK(h1 = h3); +} + +BOOST_AUTO_TEST_CASE(FixedHashInvert) +{ + FixedHash<4> h1("0xD3ADB33F"); + FixedHash<4> h2("0x2C524CC0"); + + BOOST_CHECK(~h1 == h2); +} + +BOOST_AUTO_TEST_CASE(FixedHashContains) +{ + FixedHash<4> h1("0xD3ADB331"); + FixedHash<4> h2("0x0000B331"); + FixedHash<4> h3("0x0000000C"); + + BOOST_CHECK(h1.contains(h2)); + BOOST_CHECK(!h1.contains(h3)); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +}