Move tagged_integer to ripple/basics

This commit is contained in:
Brad Chase
2017-05-18 13:05:58 -04:00
committed by Nik Bougalis
parent d90a0647d6
commit 9ae717c433
9 changed files with 476 additions and 410 deletions

View File

@@ -0,0 +1,239 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/basics/tagged_integer.h>
#include <ripple/beast/unit_test.h>
#include <type_traits>
namespace ripple {
namespace test {
class tagged_integer_test
: public beast::unit_test::suite
{
private:
struct Tag1 { };
struct Tag2 { };
// Static checks that types are not interoperable
using TagUInt1 = tagged_integer <std::uint32_t, Tag1>;
using TagUInt2 = tagged_integer <std::uint32_t, Tag2>;
using TagUInt3 = tagged_integer <std::uint64_t, Tag1>;
// Check construction of tagged_integers
static_assert (std::is_constructible<TagUInt1, std::uint32_t>::value,
"TagUInt1 should be constructible using a std::uint32_t");
static_assert (!std::is_constructible<TagUInt1, std::uint64_t>::value,
"TagUInt1 should not be constructible using a std::uint64_t");
static_assert (std::is_constructible<TagUInt3, std::uint32_t>::value,
"TagUInt3 should be constructible using a std::uint32_t");
static_assert (std::is_constructible<TagUInt3, std::uint64_t>::value,
"TagUInt3 should be constructible using a std::uint64_t");
// Check assignment of tagged_integers
static_assert (!std::is_assignable<TagUInt1, std::uint32_t>::value,
"TagUInt1 should not be assignable with a std::uint32_t");
static_assert (!std::is_assignable<TagUInt1, std::uint64_t>::value,
"TagUInt1 should not be assignable with a std::uint64_t");
static_assert (!std::is_assignable<TagUInt3, std::uint32_t>::value,
"TagUInt3 should not be assignable with a std::uint32_t");
static_assert (!std::is_assignable<TagUInt3, std::uint64_t>::value,
"TagUInt3 should not be assignable with a std::uint64_t");
static_assert (std::is_assignable<TagUInt1, TagUInt1>::value,
"TagUInt1 should be assignable with a TagUInt1");
static_assert (!std::is_assignable<TagUInt1, TagUInt2>::value,
"TagUInt1 should not be assignable with a TagUInt2");
static_assert (std::is_assignable<TagUInt3, TagUInt3>::value,
"TagUInt3 should be assignable with a TagUInt1");
static_assert (!std::is_assignable<TagUInt1, TagUInt3>::value,
"TagUInt1 should not be assignable with a TagUInt3");
static_assert (!std::is_assignable<TagUInt3, TagUInt1>::value,
"TagUInt3 should not be assignable with a TagUInt1");
// Check convertibility of tagged_integers
static_assert (!std::is_convertible<std::uint32_t, TagUInt1>::value,
"std::uint32_t should not be convertible to a TagUInt1");
static_assert (!std::is_convertible<std::uint32_t, TagUInt3>::value,
"std::uint32_t should not be convertible to a TagUInt3");
static_assert (!std::is_convertible<std::uint64_t, TagUInt3>::value,
"std::uint64_t should not be convertible to a TagUInt3");
static_assert (!std::is_convertible<std::uint64_t, TagUInt2>::value,
"std::uint64_t should not be convertible to a TagUInt2");
static_assert (!std::is_convertible<TagUInt1, TagUInt2>::value,
"TagUInt1 should not be convertible to TagUInt2");
static_assert (!std::is_convertible<TagUInt1, TagUInt3>::value,
"TagUInt1 should not be convertible to TagUInt3");
static_assert (!std::is_convertible<TagUInt2, TagUInt3>::value,
"TagUInt2 should not be convertible to a TagUInt3");
public:
void run ()
{
using TagInt = tagged_integer<std::int32_t, Tag1>;
{
testcase ("Comparison Operators");
TagInt const zero(0);
TagInt const one(1);
BEAST_EXPECT(one == one);
BEAST_EXPECT(!(one == zero));
BEAST_EXPECT(one != zero);
BEAST_EXPECT(!(one != one));
BEAST_EXPECT(zero < one);
BEAST_EXPECT(!(one < zero));
BEAST_EXPECT(one > zero);
BEAST_EXPECT(!(zero > one));
BEAST_EXPECT(one >= one);
BEAST_EXPECT(one >= zero);
BEAST_EXPECT(!(zero >= one));
BEAST_EXPECT(zero <= one);
BEAST_EXPECT(zero <= zero);
BEAST_EXPECT(!(one <= zero));
}
{
testcase ("Increment/Decrement Operators");
TagInt const zero(0);
TagInt const one(1);
TagInt a{0};
++a;
BEAST_EXPECT(a == one);
--a;
BEAST_EXPECT(a == zero);
a++;
BEAST_EXPECT(a == one);
a--;
BEAST_EXPECT(a == zero);
}
{
testcase ("Arithmetic Operators");
TagInt a{-2};
BEAST_EXPECT(+a == TagInt{-2});
BEAST_EXPECT(-a == TagInt{2});
BEAST_EXPECT(TagInt{-3} + TagInt{4} == TagInt{1});
BEAST_EXPECT(TagInt{-3} - TagInt{4} == TagInt{-7});
BEAST_EXPECT(TagInt{-3} * TagInt{4} == TagInt{-12});
BEAST_EXPECT(TagInt{8}/TagInt{4} == TagInt{2});
BEAST_EXPECT(TagInt{7} %TagInt{4} == TagInt{3});
BEAST_EXPECT(~TagInt{8} == TagInt{~TagInt::value_type{8}});
BEAST_EXPECT((TagInt{6} & TagInt{3}) == TagInt{2});
BEAST_EXPECT((TagInt{6} | TagInt{3}) == TagInt{7});
BEAST_EXPECT((TagInt{6} ^ TagInt{3}) == TagInt{5});
BEAST_EXPECT((TagInt{4} << TagInt{2}) == TagInt{16});
BEAST_EXPECT((TagInt{16} >> TagInt{2}) == TagInt{4});
}
{
testcase ("Assignment Operators");
TagInt a{-2};
TagInt b{0};
b = a;
BEAST_EXPECT(b == TagInt{-2});
// -3 + 4 == 1
a = TagInt{-3};
a += TagInt{4};
BEAST_EXPECT(a == TagInt{1});
// -3 - 4 == -7
a = TagInt{-3};
a -= TagInt{4};
BEAST_EXPECT(a == TagInt{-7});
// -3 * 4 == -12
a = TagInt{-3};
a *= TagInt{4};
BEAST_EXPECT(a == TagInt{-12});
// 8/4 == 2
a = TagInt{8};
a /= TagInt{4};
BEAST_EXPECT(a == TagInt{2});
// 7 % 4 == 3
a = TagInt{7};
a %= TagInt{4};
BEAST_EXPECT(a == TagInt{3});
// 6 & 3 == 2
a = TagInt{6};
a /= TagInt{3};
BEAST_EXPECT(a == TagInt{2});
// 6 | 3 == 7
a = TagInt{6};
a |= TagInt{3};
BEAST_EXPECT(a == TagInt{7});
// 6 ^ 3 == 5
a = TagInt{6};
a ^= TagInt{3};
BEAST_EXPECT(a == TagInt{5});
// 4 << 2 == 16
a = TagInt{4};
a <<= TagInt{2};
BEAST_EXPECT(a == TagInt{16});
// 16 >> 2 == 4
a = TagInt{16};
a >>= TagInt{2};
BEAST_EXPECT(a == TagInt{4});
}
}
};
BEAST_DEFINE_TESTSUITE(tagged_integer,ripple_basics,ripple);
} // test
} // ripple

View File

@@ -1,154 +0,0 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2014, Nikolaos D. Bougalis <nikb@bougalis.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#if BEAST_INCLUDE_BEASTCONFIG
#include <BeastConfig.h>
#endif
#include <type_traits>
#include <ripple/beast/utility/tagged_integer.h>
#include <ripple/beast/unit_test.h>
namespace beast {
class tagged_integer_test
: public unit_test::suite
{
private:
struct Tag1 { };
struct Tag2 { };
using TagInt1 = tagged_integer <std::uint32_t, Tag1>;
using TagInt2 = tagged_integer <std::uint32_t, Tag2>;
using TagInt3 = tagged_integer <std::uint64_t, Tag1>;
// Check construction of tagged_integers
static_assert (std::is_constructible<TagInt1, std::uint32_t>::value,
"TagInt1 should be constructible using a std::uint32_t");
static_assert (!std::is_constructible<TagInt1, std::uint64_t>::value,
"TagInt1 should not be constructible using a std::uint64_t");
static_assert (std::is_constructible<TagInt3, std::uint32_t>::value,
"TagInt3 should be constructible using a std::uint32_t");
static_assert (std::is_constructible<TagInt3, std::uint64_t>::value,
"TagInt3 should be constructible using a std::uint64_t");
// Check assignment of tagged_integers
static_assert (!std::is_assignable<TagInt1, std::uint32_t>::value,
"TagInt1 should not be assignable with a std::uint32_t");
static_assert (!std::is_assignable<TagInt1, std::uint64_t>::value,
"TagInt1 should not be assignable with a std::uint64_t");
static_assert (!std::is_assignable<TagInt3, std::uint32_t>::value,
"TagInt3 should not be assignable with a std::uint32_t");
static_assert (!std::is_assignable<TagInt3, std::uint64_t>::value,
"TagInt3 should not be assignable with a std::uint64_t");
static_assert (std::is_assignable<TagInt1, TagInt1>::value,
"TagInt1 should be assignable with a TagInt1");
static_assert (!std::is_assignable<TagInt1, TagInt2>::value,
"TagInt1 should not be assignable with a TagInt2");
static_assert (std::is_assignable<TagInt3, TagInt3>::value,
"TagInt3 should be assignable with a TagInt1");
static_assert (!std::is_assignable<TagInt1, TagInt3>::value,
"TagInt1 should not be assignable with a TagInt3");
static_assert (!std::is_assignable<TagInt3, TagInt1>::value,
"TagInt3 should not be assignable with a TagInt1");
// Check convertibility of tagged_integers
static_assert (!std::is_convertible<std::uint32_t, TagInt1>::value,
"std::uint32_t should not be convertible to a TagInt1");
static_assert (!std::is_convertible<std::uint32_t, TagInt3>::value,
"std::uint32_t should not be convertible to a TagInt3");
static_assert (!std::is_convertible<std::uint64_t, TagInt3>::value,
"std::uint64_t should not be convertible to a TagInt3");
static_assert (!std::is_convertible<std::uint64_t, TagInt2>::value,
"std::uint64_t should not be convertible to a TagInt2");
static_assert (!std::is_convertible<TagInt1, TagInt2>::value,
"TagInt1 should not be convertible to TagInt2");
static_assert (!std::is_convertible<TagInt1, TagInt3>::value,
"TagInt1 should not be convertible to TagInt3");
static_assert (!std::is_convertible<TagInt2, TagInt3>::value,
"TagInt2 should not be convertible to a TagInt3");
public:
void run ()
{
TagInt1 const zero (0);
TagInt1 const one (1);
testcase ("Comparison Operators");
expect (zero >= zero, "Should be greater than or equal");
expect (zero == zero, "Should be equal");
expect (one > zero, "Should be greater");
expect (one >= zero, "Should be greater than or equal");
expect (one != zero, "Should not be equal");
unexpected (one < zero, "Should be greater");
unexpected (one <= zero, "Should not be greater than or equal");
unexpected (one == zero, "Should not be equal");
testcase ("Arithmetic Operators");
TagInt1 tmp;
tmp = zero + 0u;
expect (tmp == zero, "Should be equal");
tmp = 1u + zero;
expect (tmp == one, "Should be equal");
expect(--tmp == zero, "Should be equal");
expect(tmp++ == zero, "Should be equal");
expect(tmp == one, "Should be equal");
expect(tmp-- == one, "Should be equal");
expect(tmp == zero, "Should be equal");
expect(++tmp == one, "Should be equal");
tmp = zero;
tmp += 1u;
expect(tmp == one, "Should be equal");
tmp -= 1u;
expect(tmp == zero, "Should be equal");
}
};
BEAST_DEFINE_TESTSUITE(tagged_integer,utility,beast);
} // beast

View File

@@ -29,3 +29,4 @@
#include <test/basics/Slice_test.cpp>
#include <test/basics/StringUtilities_test.cpp>
#include <test/basics/TaggedCache_test.cpp>
#include <test/basics/tagged_integer_test.cpp>

View File

@@ -18,7 +18,6 @@
*/
//==============================================================================
#include <test/beast/beast_tagged_integer_test.cpp>
#include <test/beast/beast_weak_fn_test.cpp>
#include <test/beast/beast_Zero_test.cpp>
#include <test/beast/define_print.cpp>