From e703cc3db847688b4f6704aac1f10d012302ad00 Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 28 Dec 2018 12:46:03 -0500 Subject: [PATCH 01/51] Add more `name` tests --- tests/unit/name_tests.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 15b8290f17..a6ab9e2dce 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -5,10 +5,31 @@ using namespace eosio::native; EOSIO_TEST_BEGIN(name_test) //silence_output(true); - eosio_assert( eosio::name{"azaa34"}.value == "azaa34"_n.value, "eosio::name != azaa34" ); eosio_assert( eosio::name{0}.value == 0, "eosio::name != 0" ); + eosio_assert( eosio::name{"azaa34"}.value == "azaa34"_n.value, "eosio::name != azaa34" ); + eosio_assert( eosio::name{"aa11"}.value == "aa11"_n.value, "eosio::name != aa11" ); eosio_assert( eosio::name{"z11"}.value == "z11"_n.value, "eosio::name != z11" ); + + eosio_assert( eosio::name{"1"}.value == "1"_n.value ); + eosio_assert( eosio::name{"5"}.value == "5"_n.value ); + eosio_assert( eosio::name{"a"}.value == "a"_n.value ); + eosio_assert( eosio::name{"z"}.value == "z"_n.value ); + + eosio_assert( eosio::name{"abc"}.value == "abc"_n.value ); + eosio_assert( eosio::name{"123"}.value == "123"_n.value ); + + eosio_assert( eosio::name{"abc.123"}.value == "abc.123"_n.value ); + eosio_assert( eosio::name{"123.abc"}.value == "123.abc"_n.value ); + + eosio_assert( eosio::name{"12345abcdefgj"}.value == "12345abcdefgj"_n.value ); + eosio_assert( eosio::name{"ijklmnopqrstj"}.value == "ijklmnopqrstj"_n.value ); + eosio_assert( eosio::name{"vwxyz.12345aj"}.value == "vwxyz.12345aj"_n.value ); + + eosio_assert( eosio::name{"111111111111j"}.value == "111111111111j"_n.value ); + eosio_assert( eosio::name{"555555555555j"}.value == "555555555555j"_n.value ); + eosio_assert( eosio::name{"aaaaaaaaaaaaj"}.value == "555555555555j"_n.value ); + eosio_assert( eosio::name{"zzzzzzzzzzzzj"}.value == "zzzzzzzzzzzzj"_n.value ); auto testa = []() { eosio_assert( eosio::name{"bb"}.value == "aa"_n.value, "bb != aa" ); From 5d83272d8cc58a1e00050189ac66d9eeb815136b Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 2 Jan 2019 18:32:06 -0500 Subject: [PATCH 02/51] Start tests --- temp/TEST_NOTES | 175 ++++++++++++++++++++++++++++ temp/main.cpp | 224 ++++++++++++++++++++++++++++++++++++ tests/unit/name_tests.cpp | 214 ++++++++++++++++++++++++++-------- tests/unit/symbol_tests.cpp | 63 ++++++++++ 4 files changed, 625 insertions(+), 51 deletions(-) create mode 100644 temp/TEST_NOTES create mode 100644 temp/main.cpp create mode 100644 tests/unit/symbol_tests.cpp diff --git a/temp/TEST_NOTES b/temp/TEST_NOTES new file mode 100644 index 0000000000..56c82c3ffd --- /dev/null +++ b/temp/TEST_NOTES @@ -0,0 +1,175 @@ +[*] asset.hpp + dependencies + ************ + "serialize.hpp" + "print.hpp" + "system.hpp" + "symbol.hpp" + + + ************ + namespace eosio + --------------- + [*] asset + - constructors + [*] asset() + [*] asset(uint64_t, class symbol s) + - methods + [*] bool is_amount_within_range() + [*] bool is_valid() + [*] void set_amount(int64_t) + [*] std::string to_string() + [*] void print() + [*] operator- + [*] operator-= + [*] operator+= + [*] operator*= + [*] operator/= + [*] friend operator- + [*] friend operator+ + [*] friend operator* + [*] friend operator* + [*] friend operator/ + [*] friend operator/ + [*] friend operator== + [*] friend operator!= + [*] friend operator< + [*] friend operator<= + [*] friend operator> + [*] friend operator>= + - members + [*] uint64_t amount + [*] symbol symbol + [*] int64_t max_amount + [*] extended_asset + - constructors + [*] extended_asset() + [*] extended_asset(int64_t, extended_symbol) + [*] extended_asset(asset, name) + - methods + [*] extended_symbol get_extended_symbol() + [*] void print() + [*] operator- + [*] friend operator- + [*] friend operator+ + [*] friend operator+= + [*] friend operator-= + [*] friend operator< + [*] friend operator== + [*] friend operator!= + [*] friend operator<= + [*] friend operator>= + - members + [*] asset quantity + [*] name contract + --------------- +[*] binary_extension.hpp +[*] datastream.hpp +[*] eosio.hpp +[*] fixed_bytes.hpp +[*] name.hpp + dependencies + ************ + "system.hpp" + "serialize.hpp" + + + ************ + namespace eosio + --------------- + [*] name + - constructors + [✓] name() + [✓] name(uint64_t) + [✓] name(name::raw) + [✓] name(std::string_view) + - methods + [✓] uint8_t char_to_value(char) + [✓] uint8_t length() + [*] name suffix() + [*] operator raw() + [*] operator bool() + [*] char* write_as_string(char*, char*) + [*] std::string to_string() + [*] friend operator== + [*] friend operator!= + [*] friend operator< + - members + [*] uint64_t value + namespace detail + ---------------- + [*] to_const_char_arr + - members + [*] char value[] + ---------------- + ---------------- + [*] eosio::name operator"" _n +[*] print.hpp +[*] public_key.hpp +[*] serialize.hpp +[*] symbol.hpp + dependencies + ************ + "system.hpp" + "print.h" + "name.hpp" + "serialize.hpp" + + + + ************ + namespace eosio + --------------- + [*] symbol_code + - constructors + [*] symbol_code() + [*] symbol_code(uint64_t) + [*] symbol_code(std::string_view) + - methods + [*] bool is_valid() + [*] uint32_t length() + [*] uint64_t raw() + [*] operator bool() + [*] char* write_as_string(char*, char*) + [*] std::string to_string() + [*] friend operator== + [*] friend operator!= + [*] friend operator< + - members + [*] uint64_t value + [*] symbol + - constructors + [*] symbol() + [*] symbol(uint64_t) + [*] symbol(symbol_code, uint8_t) + [*] symbol(string_view, uint8_t) + - methods + [*] bool is_valid() + [*] uint8_t precision() + [*] symbol_code code() + [*] uint64_t raw() + [*] operator bool() + [*] void print(bool show_precision) + [*] friend operator== + [*] friend operator!= + [*] friend operator< + - members + [*] uint64_t value + [*] extended_symbol + - constructors + [*] extended_symbol() + [*] extended_symbol(symbol, name) + - methods + [*] symbol get_symbol() + [*] name get_contract() + [*] void print(bool show_precision) + [*] friend operator== + [*] friend operator!= + [*] friend operator< + - members + [*] symbol symbol + [*] name contract + --------------- +[*] system.hpp +[*] time.hpp +[*] varint.hpp diff --git a/temp/main.cpp b/temp/main.cpp new file mode 100644 index 0000000000..061e7e91e9 --- /dev/null +++ b/temp/main.cpp @@ -0,0 +1,224 @@ +// eosio-cpp -c main.cpp -fnative; ./a.out + +#include +#include + +#define NATIVE + +using namespace eosio; +using namespace eosio::native; + +int main() +{ + eosio_assert( name{}.value == 0ULL, "name != 0ULL" ); + + eosio_assert( name{0ULL}.value == 0ULL, "name != 0ULL" ); + eosio_assert( name{1ULL}.value == 1ULL, "name != 1ULL" ); + eosio_assert( name{18446744073709551615ULL}.value == 18446744073709551615ULL, "name != 18446744073709551615ULL" ); + + eosio_assert( name{name::raw{0ULL}}.value == 0ULL, "name != 0ULL" ); + eosio_assert( name{name::raw{1ULL}}.value == 1ULL, "name != 1ULL" ); + eosio_assert( name{name::raw{18446744073709551615ULL}}.value == 18446744073709551615ULL, "name != 18446744073709551615ULL" ); + + eosio_assert( name{"1"}.value == 576460752303423488ULL, "name != 1" ); + eosio_assert( name{"5"}.value == 2882303761517117440ULL, "name != 5" ); + eosio_assert( name{"a"}.value == 3458764513820540928ULL, "name != a" ); + eosio_assert( name{"z"}.value == 17870283321406128128ULL, "name != z" ); + + eosio_assert( name{"abc"}.value == 3589368903014285312ULL, "name != abc" ); + eosio_assert( name{"123"}.value == 614178399182651392ULL, "name != 123" ); + + eosio_assert( name{".abc"}.value == 112167778219196416ULL, "name != .abc" ); + eosio_assert( name{"123."}.value == 614178399182651392ULL, "name != 123." ); + + eosio_assert( name{"abc.123"}.value == 3589369488740450304ULL, "name != abc.123" ); + eosio_assert( name{"123.abc"}.value == 614181822271586304ULL, "name != 123.abc" ); + + eosio_assert( name{"12345abcdefgj"}.value == 614251623682315983ULL, "name != 12345abcdefgj" ); + eosio_assert( name{"hijklmnopqrsj"}.value == 7754926748989239183ULL, "name != hijklmnopqrsj" ); + eosio_assert( name{"tuvwxyz.1234j"}.value == 14895601873741973071ULL, "name != tuvwxyz.1234j" ); + + eosio_assert( name{"111111111111j"}.value == 595056260442243615ULL, "name != 111111111111j" ); + eosio_assert( name{"555555555555j"}.value == 2975281302211218015ULL, "name != 555555555555j" ); + eosio_assert( name{"aaaaaaaaaaaaj"}.value == 3570337562653461615ULL, "name != aaaaaaaaaaaaj" ); + eosio_assert( name{"zzzzzzzzzzzzj"}.value == 18446744073709551615ULL, "name != zzzzzzzzzzzzj" ); + + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ); + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ); + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ); + REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ); + + // ------------------------------------------------------------------------------------------------------------------------------ + + char c{'.'}; + uint8_t expected_value{0}; // Will increment to the expected correct value in the set [0,32) +#ifdef NATIVE + print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); + print_f("Value of expected_value: %", static_cast(expected_value) ); +#endif + eosio_assert( name::char_to_value(c) == expected_value, "name::char_to_value(c) != expected_value" ); + ++expected_value; + + for(c = '1'; c <= '5'; ++c ) { +#ifdef NATIVE + print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); + print_f("Value of expected_value: %", static_cast(expected_value) ); +#endif + eosio_assert( name::char_to_value(c) == expected_value, "name::char_to_value(c) != expected_value" ); + ++expected_value; + } + + for(c = 'a'; c <= 'z'; ++c ) { +#ifdef NATIVE + print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); + print_f("Value of expected_value: %", static_cast(expected_value) ); +#endif + eosio_assert( name::char_to_value(c) == expected_value, "name::char_to_value(c) != expected_value" ); + ++expected_value; + } + + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); + + + // ------------------------------------------------------------------------------------------------------------------------------ + + eosio_assert( name{""}.length() == 0, "name.length() != 0" ); + eosio_assert( name{"e"}.length() == 1, "name.length() != 1" ); + eosio_assert( name{"eo"}.length() == 2, "name.length() != 2" ); + eosio_assert( name{"eos"}.length() == 3, "name.length() != 3" ); + eosio_assert( name{"eosi"}.length() == 4, "name.length() != 4" ); + eosio_assert( name{"eosio"}.length() == 5, "name.length() != 5" ); + eosio_assert( name{"eosioa"}.length() == 6, "name.length() != 6" ); + eosio_assert( name{"eosioac"}.length() == 7, "name.length() != 7" ); + eosio_assert( name{"eosioacc"}.length() == 8, "name.length() != 8" ); + eosio_assert( name{"eosioacco"}.length() == 9, "name.length() != 9" ); + eosio_assert( name{"eosioaccou"}.length() == 10, "name.length() != 10" ); + eosio_assert( name{"eosioaccoun"}.length() == 11, "name.length() != 11" ); + eosio_assert( name{"eosioaccount"}.length() == 12, "name.length() != 12" ); + eosio_assert( name{"eosioaccountj"}.length() == 13, "name.length() != 13" ); + + REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ); + + // ------------------------------------------------------------------------------------------------------------------------------ + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{".eosioaccounj"}.suffix() ); // TODO: interesting print behavior; starts newline sometimes + print_f("Value of expected suffix: eosioaccounj" ); +#endif + eosio_assert( name{".eosioaccounj"}.suffix() == name{"eosioaccounj"}, "name.suffix() != eosioaccounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"e.osioaccounj"}.suffix() ); + print_f("Value of expected suffix: osioaccounj" ); +#endif + eosio_assert( name{"e.osioaccounj"}.suffix() == name{"osioaccounj"}, "name.suffix() != osioaccounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eo.sioaccounj"}.suffix() ); + print_f("Value of expected suffix: sioaccounj" ); +#endif + eosio_assert( name{"eo.sioaccounj"}.suffix() == name{"sioaccounj"}, "name.suffix() != sioaccounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eos.ioaccounj"}.suffix() ); + print_f("Value of expected suffix: ioaccounj" ); +#endif + eosio_assert( name{"eos.ioaccounj"}.suffix() == name{"ioaccounj"}, "name.suffix() != ioaccounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosi.oaccounj"}.suffix() ); + print_f("Value of expected suffix: oaccounj" ); +#endif + eosio_assert( name{"eosi.oaccounj"}.suffix() == name{"oaccounj"}, "name.suffix() != oaccounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosio.accounj"}.suffix() ); + print_f("Value of expected suffix: accounj" ); +#endif + eosio_assert( name{"eosio.accounj"}.suffix() == name{"accounj"}, "name.suffix() != accounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioa.ccounj"}.suffix() ); + print_f("Value of expected suffix: ccounj" ); +#endif + eosio_assert( name{"eosioa.ccounj"}.suffix() == name{"ccounj"}, "name.suffix() != ccounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioac.counj"}.suffix() ); + print_f("Value of expected suffix: counj" ); +#endif + eosio_assert( name{"eosioac.counj"}.suffix() == name{"counj"}, "name.suffix() != counj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioacc.ounj"}.suffix() ); + print_f("Value of expected suffix: ounj" ); +#endif + eosio_assert( name{"eosioacc.ounj"}.suffix() == name{"ounj"}, "name.suffix() != ounj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioacco.unj"}.suffix() ); + print_f("Value of expected suffix: unj" ); +#endif + eosio_assert( name{"eosioacco.unj"}.suffix() == name{"unj"}, "name.suffix() != unj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioaccou.nj"}.suffix() ); + print_f("Value of expected suffix: nj" ); +#endif + eosio_assert( name{"eosioaccou.nj"}.suffix() == name{"nj"}, "name.suffix() != nj" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioaccoun.j"}.suffix() ); + print_f("Value of expected suffix: j" ); +#endif + eosio_assert( name{"eosioaccoun.j"}.suffix() == name{"j"}, "name.suffix() != j" ); +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); + print_f("Value of expected suffix: " ); +#endif + eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); + + // ------------------------------------------------------------------------------------------------------------------------------ + + // eosio_assert( symbol_code{}.raw() == 0ULL, "symbol_code != 0ULL" ); + // eosio_assert( symbol_code{0ULL}.raw() == 0ULL, "symbol_code != 0ULL" ); + // eosio_assert( symbol_code{1ULL}.raw() == 1ULL, "symbol_code != 1ULL" ); + // eosio_assert( symbol_code{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol_code != 18446744073709551615ULL" ); + + // eosio_assert( symbol_code{"A"}.raw() == 65ULL, "symbol_code != 65ULL" ); + // eosio_assert( symbol_code{"Z"}.raw() == 90ULL, "symbol_code != 90ULL" ); + // eosio_assert( symbol_code{"AAAAAAA"}.raw() == 18367622009667905ULL, "symbol_code != 18367622009667905ULL" ); + // eosio_assert( symbol_code{"ZZZZZZZ"}.raw() == 25432092013386330ULL, "symbol_code != 25432092013386330ULL" ); + + // symbol_code s{"ZZZZZZZ"}; + // eosio::print_f("%\n",s.raw()); + + // eosio_assert( symbol{}.raw() == 0ULL, "symbol != 0ULL" ); + // eosio_assert( symbol{0ULL}.raw() == 0ULL, "symbol != 0ULL" ); + // eosio_assert( symbol{1ULL}.raw() == 1ULL, "symbol != 1ULL" ); + // eosio_assert( symbol{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol != 18446744073709551615ULL" ); + + // eosio_assert( symbol{1}.raw() == 576460752303423488ULL, "symbol != 1" ); + // eosio_assert( symbol{5}.raw() == 2882303761517117440ULL, "symbol != 5" ); + // eosio_assert( symbol{"a"}.raw() == 3458764513820540928ULL, "symbol != a" ); + // eosio_assert( symbol{"z"}.raw() == 17870283321406128128ULL, "symbol != z" ); + + // eosio_assert( symbol{"abc"}.raw() == 3589368903014285312ULL, "symbol != abc" ); + // eosio_assert( symbol{"123"}.raw() == 614178399182651392ULL, "symbol != 123" ); + + // eosio_assert( symbol{".abc"}.raw() == 112167778219196416ULL, "symbol != .abc" ); + // eosio_assert( symbol{"123."}.raw() == 614178399182651392ULL, "symbol != 123." ); + + // eosio_assert( symbol{"abc.123"}.raw() == 3589369488740450304ULL, "symbol != abc.123" ); + // eosio_assert( symbol{"123.abc"}.raw() == 614181822271586304ULL, "symbol != 123.abc" ); + + // eosio_assert( symbol{"12345abcdefgj"}.raw() == 614251623682315983ULL, "symbol != 12345abcdefgj" ); + // eosio_assert( symbol{"hijklmnopqrsj"}.raw() == 7754926748989239183ULL, "symbol != hijklmnopqrsj" ); + // eosio_assert( symbol{"tuvwxyz.1234j"}.raw() == 14895601873741973071ULL, "symbol != tuvwxyz.1234j" ); + + // eosio_assert( symbol{"111111111111j"}.raw() == 595056260442243615ULL, "symbol != 111111111111j" ); + // eosio_assert( symbol{"555555555555j"}.raw() == 2975281302211218015ULL, "symbol != 555555555555j" ); + // eosio_assert( symbol{"aaaaaaaaaaaaj"}.raw() == 3570337562653461615ULL, "symbol != aaaaaaaaaaaaj" ); + // eosio_assert( symbol{"zzzzzzzzzzzzj"}.raw() == 18446744073709551615ULL, "symbol != zzzzzzzzzzzzj" ); + // eosio::symbol n(18446744073709551615ULL); + // eosio::print_f("%\n",n.raw()); + + // return 0; +} diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index a6ab9e2dce..5fc9d8a018 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,68 +1,180 @@ #include #include +using namespace eosio; using namespace eosio::native; -EOSIO_TEST_BEGIN(name_test) - //silence_output(true); - eosio_assert( eosio::name{0}.value == 0, "eosio::name != 0" ); - eosio_assert( eosio::name{"azaa34"}.value == "azaa34"_n.value, "eosio::name != azaa34" ); - - eosio_assert( eosio::name{"aa11"}.value == "aa11"_n.value, "eosio::name != aa11" ); - eosio_assert( eosio::name{"z11"}.value == "z11"_n.value, "eosio::name != z11" ); +// Defined in `eosio.cdt/libraries/eosiolib/name.hpp` +EOSIO_TEST_BEGIN(name_type_constructors_test) + eosio_assert( name{}.value == 0ULL, "name != 0ULL" ); - eosio_assert( eosio::name{"1"}.value == "1"_n.value ); - eosio_assert( eosio::name{"5"}.value == "5"_n.value ); - eosio_assert( eosio::name{"a"}.value == "a"_n.value ); - eosio_assert( eosio::name{"z"}.value == "z"_n.value ); - - eosio_assert( eosio::name{"abc"}.value == "abc"_n.value ); - eosio_assert( eosio::name{"123"}.value == "123"_n.value ); - - eosio_assert( eosio::name{"abc.123"}.value == "abc.123"_n.value ); - eosio_assert( eosio::name{"123.abc"}.value == "123.abc"_n.value ); + eosio_assert( name{0ULL}.value == 0ULL, "name != 0ULL" ); + eosio_assert( name{1ULL}.value == 1ULL, "name != 1ULL" ); + eosio_assert( name{18446744073709551615ULL}.value == 18446744073709551615ULL, "name != 18446744073709551615ULL" ); + + eosio_assert( name{"1"}.value == 576460752303423488ULL, "name != 1" ); + eosio_assert( name{"5"}.value == 2882303761517117440ULL, "name != 5" ); + eosio_assert( name{"a"}.value == 3458764513820540928ULL, "name != a" ); + eosio_assert( name{"z"}.value == 17870283321406128128ULL, "name != z" ); - eosio_assert( eosio::name{"12345abcdefgj"}.value == "12345abcdefgj"_n.value ); - eosio_assert( eosio::name{"ijklmnopqrstj"}.value == "ijklmnopqrstj"_n.value ); - eosio_assert( eosio::name{"vwxyz.12345aj"}.value == "vwxyz.12345aj"_n.value ); + eosio_assert( name{"abc"}.value == 3589368903014285312ULL, "name != abc" ); + eosio_assert( name{"123"}.value == 614178399182651392ULL, "name != 123" ); - eosio_assert( eosio::name{"111111111111j"}.value == "111111111111j"_n.value ); - eosio_assert( eosio::name{"555555555555j"}.value == "555555555555j"_n.value ); - eosio_assert( eosio::name{"aaaaaaaaaaaaj"}.value == "555555555555j"_n.value ); - eosio_assert( eosio::name{"zzzzzzzzzzzzj"}.value == "zzzzzzzzzzzzj"_n.value ); + eosio_assert( name{".abc"}.value == 112167778219196416ULL, "name != .abc" ); + eosio_assert( name{"123."}.value == 614178399182651392ULL, "name != 123." ); - auto testa = []() { - eosio_assert( eosio::name{"bb"}.value == "aa"_n.value, "bb != aa" ); - }; - REQUIRE_ASSERT("bb != aa", testa); - REQUIRE_ASSERT("character is not in allowed character set for names", - ([]() { - eosio::name{"!"}.value; - })); + eosio_assert( name{"abc.123"}.value == 3589369488740450304ULL, "name != abc.123" ); + eosio_assert( name{"123.abc"}.value == 614181822271586304ULL, "name != 123.abc" ); + + eosio_assert( name{"12345abcdefgj"}.value == 614251623682315983ULL, "name != 12345abcdefgj" ); + eosio_assert( name{"hijklmnopqrsj"}.value == 7754926748989239183ULL, "name != hijklmnopqrsj" ); + eosio_assert( name{"tuvwxyz.1234j"}.value == 14895601873741973071ULL, "name != tuvwxyz.1234j" ); + + eosio_assert( name{"111111111111j"}.value == 595056260442243615ULL, "name != 111111111111j" ); + eosio_assert( name{"555555555555j"}.value == 2975281302211218015ULL, "name != 555555555555j" ); + eosio_assert( name{"aaaaaaaaaaaaj"}.value == 3570337562653461615ULL, "name != aaaaaaaaaaaaj" ); + eosio_assert( name{"zzzzzzzzzzzzj"}.value == 18446744073709551615ULL, "name != zzzzzzzzzzzzj" ); + + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"}.value;}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"}.value;}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"}.value;}) ); + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"}.value;}) ); + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"}.value;}) ); + REQUIRE_ASSERT( "the length must not be longer than 13 characters in an `eosio::name`", ([]() {name{"12345abcdefghj"}.value;}) ); + silence_output(false); EOSIO_TEST_END -EOSIO_TEST_BEGIN(is_account_test) - intrinsics::set_intrinsic([](uint64_t a) { - if (a == 3) - return true; - if (a == 4) - return true; - return false; - }); - - CHECK_ASSERT("is not an account", ([]() { - eosio_assert(is_account(5), "is not an account"); - })); - CHECK_EQUAL(is_account(3), true); - CHECK_EQUAL(is_account(4), true); - CHECK_EQUAL(is_account(6), true); - REQUIRE_EQUAL(is_account(7), true); - +EOSIO_TEST_BEGIN(name_type_methods_test) EOSIO_TEST_END +// // Intrinsics defined in `eos/libraries/chain/wasm_interface.cpp` that involve the `name` type +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(set_resource_limits_intrinsic_test) +// intrinsics::set_intrinsic([](name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(get_resource_limits_intrinsic_test) +// intrinsics::set_intrinsic([](name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(set_proposed_producers_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(is_privileged_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(set_privileged_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(get_active_producers_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(check_permission_authorization_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(get_permission_last_used_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(get_account_creation_time_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(get_permission_last_used_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(require_authorization_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(has_authorization_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(require_recipient_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(is_account_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // Defined in `eos/libraries/chain/wasm_interface.cpp` +// EOSIO_TEST_BEGIN(send_deferred_intrinsic_test) +// intrinsics::set_intrinsic([](uint64_t a) { + +// }); +// EOSIO_TEST_END + +// // EOSIO_TEST_BEGIN(is_account_test) +// // intrinsics::set_intrinsic([](uint64_t a) { +// // if (a == 3) +// // return true; +// // if (a == 4) +// // return true; +// // return false; +// // }); + +// // CHECK_ASSERT("is not an account", ([]() { +// // eosio_assert(is_account(5), "is not an account"); +// // })); +// // CHECK_EQUAL(is_account(3), true); +// // CHECK_EQUAL(is_account(4), true); +// // CHECK_EQUAL(is_account(6), true); +// // REQUIRE_EQUAL(is_account(7), true); + +// // EOSIO_TEST_END + int main(int argc, char** argv) { - EOSIO_TEST(name_test); - EOSIO_TEST(is_account_test); + //EOSIO_TEST(name_type_test); + // EOSIO_TEST(is_account_test); return has_failed(); } diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp new file mode 100644 index 0000000000..49fe19ce58 --- /dev/null +++ b/tests/unit/symbol_tests.cpp @@ -0,0 +1,63 @@ +#include +#include + +using namespace eosio::native; + +// Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` +EOSIO_TEST_BEGIN(symbol_type_test) + eosio_assert( symbol_code{}.raw() == 0ULL, "symbol_code != 0ULL" ); + eosio_assert( symbol_code{0ULL}.raw() == 0ULL, "symbol_code != 0ULL" ); + eosio_assert( symbol_code{1ULL}.raw() == 1ULL, "symbol_code != 1ULL" ); + eosio_assert( symbol_code{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol_code != 18446744073709551615ULL" ); + + eosio_assert( symbol_code{"A"}.raw() == 65ULL, "symbol_code != 65ULL" ); + eosio_assert( symbol_code{"Z"}.raw() == 90ULL, "symbol_code != 90ULL" ); + eosio_assert( symbol_code{"AAAAAAA"}.raw() == 18367622009667905ULL, "symbol_code != 18367622009667905ULL" ); + eosio_assert( symbol_code{"ZZZZZZZ"}.raw() == 25432092013386330ULL, "symbol_code != 25432092013386330ULL" ); + + eosio_assert( symbol{0ULL}.value == 0ULL, "symbol != 0ULL" ); + eosio_assert( symbol{1ULL}.value == 1ULL, "symbol != 1ULL" ); + eosio_assert( symbol{18446744073709551615ULL}.value == 18446744073709551615ULL, "symbol != 18446744073709551615ULL" ); + + eosio_assert( symbol{"1"}.value == 576460752303423488ULL, "symbol != 1" ); + eosio_assert( symbol{"5"}.value == 2882303761517117440ULL, "symbol != 5" ); + eosio_assert( symbol{"a"}.value == 3458764513820540928ULL, "symbol != a" ); + eosio_assert( symbol{"z"}.value == 17870283321406128128ULL, "symbol != z" ); + + eosio_assert( symbol{"abc"}.value == 3589368903014285312ULL, "symbol != abc" ); + eosio_assert( symbol{"123"}.value == 614178399182651392ULL, "symbol != 123" ); + + eosio_assert( symbol{".abc"}.value == 112167778219196416ULL, "symbol != .abc" ); + eosio_assert( symbol{"123."}.value == 614178399182651392ULL, "symbol != 123." ); + + eosio_assert( symbol{"abc.123"}.value == 3589369488740450304ULL, "symbol != abc.123" ); + eosio_assert( symbol{"123.abc"}.value == 614181822271586304ULL, "symbol != 123.abc" ); + + eosio_assert( symbol{"12345abcdefgj"}.value == 614251623682315983ULL, "symbol != 12345abcdefgj" ); + eosio_assert( symbol{"hijklmnopqrsj"}.value == 7754926748989239183ULL, "symbol != hijklmnopqrsj" ); + eosio_assert( symbol{"tuvwxyz.1234j"}.value == 14895601873741973071ULL, "symbol != tuvwxyz.1234j" ); + + eosio_assert( symbol{"111111111111j"}.value == 595056260442243615ULL, "symbol != 111111111111j" ); + eosio_assert( symbol{"555555555555j"}.value == 2975281302211218015ULL, "symbol != 555555555555j" ); + eosio_assert( symbol{"aaaaaaaaaaaaj"}.value == 3570337562653461615ULL, "symbol != aaaaaaaaaaaaj" ); + eosio_assert( symbol{"zzzzzzzzzzzzj"}.value == 18446744073709551615ULL, "symbol != zzzzzzzzzzzzj" ); + + REQUIRE_ASSERT( "character is not in allowed character set for symbols", ([]() {symbol{"-1"}.value;}) ); + + REQUIRE_ASSERT( "character is not in allowed character set for symbols", ([]() {symbol{"0"}.value;}) ); + + REQUIRE_ASSERT( "character is not in allowed character set for symbols", ([]() {symbol{"6"}.value;}) ); + + REQUIRE_ASSERT( "thirteenth character in symbol cannot be a letter that comes after j", ([]() {symbol{"111111111111k"}.value;}) ); + + REQUIRE_ASSERT( "thirteenth character in symbol cannot be a letter that comes after j", ([]() {symbol{"zzzzzzzzzzzzk"}.value;}) ); + + REQUIRE_ASSERT( "the length must not be longer than 13 characters in an `eosio::symbol`", ([]() {symbol{"12345abcdefghj"}.value;}) ); + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char** argv) { + EOSIO_TEST(symbol_type_test); + return has_failed(); +} From cd1def0d68186a42f46858b1a1a1c07196558805 Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 3 Jan 2019 18:29:02 -0500 Subject: [PATCH 03/51] Continuation of `name` type tests --- temp/main.cpp | 231 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 214 insertions(+), 17 deletions(-) diff --git a/temp/main.cpp b/temp/main.cpp index 061e7e91e9..9ef870b181 100644 --- a/temp/main.cpp +++ b/temp/main.cpp @@ -1,4 +1,5 @@ // eosio-cpp -c main.cpp -fnative; ./a.out +// TODO: better error messages #include #include @@ -29,7 +30,10 @@ int main() eosio_assert( name{"123"}.value == 614178399182651392ULL, "name != 123" ); eosio_assert( name{".abc"}.value == 112167778219196416ULL, "name != .abc" ); + eosio_assert( name{".........abc"}.value == 102016ULL, "name != .........abc" ); eosio_assert( name{"123."}.value == 614178399182651392ULL, "name != 123." ); + eosio_assert( name{"123........."}.value == 614178399182651392ULL, "name != 123........." ); + eosio_assert( name{".a.b.c.1.2.3."}.value == 108209673814966320ULL, "name != .a.b.c.1.2.3." ); eosio_assert( name{"abc.123"}.value == 3589369488740450304ULL, "name != abc.123" ); eosio_assert( name{"123.abc"}.value == 614181822271586304ULL, "name != 123.abc" ); @@ -111,69 +115,262 @@ int main() #ifdef NATIVE print_f("Value of suffix: %\n", name{".eosioaccounj"}.suffix() ); // TODO: interesting print behavior; starts newline sometimes - print_f("Value of expected suffix: eosioaccounj" ); + print_f("Value of expected suffix: eosioaccounj\n" ); #endif + eosio_assert( name{".eosioaccounj"}.suffix() == name{"eosioaccounj"}, "name.suffix() != eosioaccounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"e.osioaccounj"}.suffix() ); - print_f("Value of expected suffix: osioaccounj" ); + print_f("Value of expected suffix: osioaccounj\n" ); #endif + eosio_assert( name{"e.osioaccounj"}.suffix() == name{"osioaccounj"}, "name.suffix() != osioaccounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eo.sioaccounj"}.suffix() ); - print_f("Value of expected suffix: sioaccounj" ); + print_f("Value of expected suffix: sioaccounj\n" ); #endif + eosio_assert( name{"eo.sioaccounj"}.suffix() == name{"sioaccounj"}, "name.suffix() != sioaccounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eos.ioaccounj"}.suffix() ); - print_f("Value of expected suffix: ioaccounj" ); + print_f("Value of expected suffix: ioaccounj\n" ); #endif + eosio_assert( name{"eos.ioaccounj"}.suffix() == name{"ioaccounj"}, "name.suffix() != ioaccounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosi.oaccounj"}.suffix() ); - print_f("Value of expected suffix: oaccounj" ); + print_f("Value of expected suffix: oaccounj\n" ); #endif + eosio_assert( name{"eosi.oaccounj"}.suffix() == name{"oaccounj"}, "name.suffix() != oaccounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosio.accounj"}.suffix() ); - print_f("Value of expected suffix: accounj" ); + print_f("Value of expected suffix: accounj\n" ); #endif + eosio_assert( name{"eosio.accounj"}.suffix() == name{"accounj"}, "name.suffix() != accounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosioa.ccounj"}.suffix() ); - print_f("Value of expected suffix: ccounj" ); + print_f("Value of expected suffix: ccounj\n" ); #endif + eosio_assert( name{"eosioa.ccounj"}.suffix() == name{"ccounj"}, "name.suffix() != ccounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosioac.counj"}.suffix() ); - print_f("Value of expected suffix: counj" ); + print_f("Value of expected suffix: counj\n" ); #endif + eosio_assert( name{"eosioac.counj"}.suffix() == name{"counj"}, "name.suffix() != counj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosioacc.ounj"}.suffix() ); - print_f("Value of expected suffix: ounj" ); + print_f("Value of expected suffix: ounj\n" ); #endif + eosio_assert( name{"eosioacc.ounj"}.suffix() == name{"ounj"}, "name.suffix() != ounj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosioacco.unj"}.suffix() ); - print_f("Value of expected suffix: unj" ); + print_f("Value of expected suffix: unj\n" ); #endif + eosio_assert( name{"eosioacco.unj"}.suffix() == name{"unj"}, "name.suffix() != unj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosioaccou.nj"}.suffix() ); - print_f("Value of expected suffix: nj" ); + print_f("Value of expected suffix: nj\n" ); #endif + eosio_assert( name{"eosioaccou.nj"}.suffix() == name{"nj"}, "name.suffix() != nj" ); + #ifdef NATIVE print_f("Value of suffix: %\n", name{"eosioaccoun.j"}.suffix() ); - print_f("Value of expected suffix: j" ); + print_f("Value of expected suffix: j\n" ); #endif + eosio_assert( name{"eosioaccoun.j"}.suffix() == name{"j"}, "name.suffix() != j" ); -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); - print_f("Value of expected suffix: " ); -#endif - eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); + +// #ifdef NATIVE +// print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); +// print_f("Value of expected suffix: \n" ); +// #endif +// eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); + + // ------------------------------------------------------------------------------------------------------------------------------ + + eosio_assert( static_cast(name::raw{name{"1"}}) == 576460752303423488ULL, "name != 1" ); + eosio_assert( static_cast(name::raw{name{"5"}}) == 2882303761517117440ULL, "name != 5" ); + eosio_assert( static_cast(name::raw{name{"a"}}) == 3458764513820540928ULL, "name != a" ); + eosio_assert( static_cast(name::raw{name{"z"}}) == 17870283321406128128ULL, "name != z" ); + + eosio_assert( static_cast(name::raw{name{"abc"}}) == 3589368903014285312ULL, "name != abc" ); + eosio_assert( static_cast(name::raw{name{"123"}}) == 614178399182651392ULL, "name != 123" ); + + eosio_assert( static_cast(name::raw{name{".abc"}}) == 112167778219196416ULL, "name != .abc" ); + eosio_assert( static_cast(name::raw{name{".........abc"}}) == 102016ULL, "name != .........abc" ); + eosio_assert( static_cast(name::raw{name{"123."}}) == 614178399182651392ULL, "name != 123." ); + eosio_assert( static_cast(name::raw{name{"123........."}}) == 614178399182651392ULL, "name != 123........." ); + eosio_assert( static_cast(name::raw{name{".a.b.c.1.2.3."}}) == 108209673814966320ULL, "name != .a.b.c.1.2.3." ); + + eosio_assert( static_cast(name::raw{name{"abc.123"}}) == 3589369488740450304ULL, "name != abc.123" ); + eosio_assert( static_cast(name::raw{name{"123.abc"}}) == 614181822271586304ULL, "name != 123.abc" ); + + eosio_assert( static_cast(name::raw{name{"12345abcdefgj"}}) == 614251623682315983ULL, "name != 12345abcdefgj" ); + eosio_assert( static_cast(name::raw{name{"hijklmnopqrsj"}}) == 7754926748989239183ULL, "name != hijklmnopqrsj" ); + eosio_assert( static_cast(name::raw{name{"tuvwxyz.1234j"}}) == 14895601873741973071ULL, "name != tuvwxyz.1234j" ); + + eosio_assert( static_cast(name::raw{name{"111111111111j"}}) == 595056260442243615ULL, "name != 111111111111j" ); + eosio_assert( static_cast(name::raw{name{"555555555555j"}}) == 2975281302211218015ULL, "name != 555555555555j" ); + eosio_assert( static_cast(name::raw{name{"aaaaaaaaaaaaj"}}) == 3570337562653461615ULL, "name != aaaaaaaaaaaaj" ); + eosio_assert( static_cast(name::raw{name{"zzzzzzzzzzzzj"}}) == 18446744073709551615ULL, "name != zzzzzzzzzzzzj" ); + + // ------------------------------------------------------------------------------------------------------------------------------ + + eosio_assert( static_cast(name{"1"}) == true, "name != true" ); + eosio_assert( static_cast(name{"5"}) == true, "name != true" ); + eosio_assert( static_cast(name{"a"}) == true, "name != true" ); + eosio_assert( static_cast(name{"z"}) == true, "name != true" ); + + eosio_assert( static_cast(name{"abc"}) == true, "name != true" ); + eosio_assert( static_cast(name{"123"}) == true, "name != true" ); + + eosio_assert( static_cast(name{".abc"}) == true, "name != true" ); + eosio_assert( static_cast(name{".........abc"}) == true, "name != true" ); + eosio_assert( static_cast(name{"123."}) == true, "name != true" ); + eosio_assert( static_cast(name{"123........."}) == true, "name != true" ); + eosio_assert( static_cast(name{".a.b.c.1.2.3."}) == true, "name != true" ); + + eosio_assert( static_cast(name{"abc.123"}) == true, "name != true" ); + eosio_assert( static_cast(name{"123.abc"}) == true, "name != true" ); + + eosio_assert( static_cast(name{"12345abcdefgj"}) == true, "name != true" ); + eosio_assert( static_cast(name{"hijklmnopqrsj"}) == true, "name != true" ); + eosio_assert( static_cast(name{"tuvwxyz.1234j"}) == true, "name != true" ); + + eosio_assert( static_cast(name{"111111111111j"}) == true, "name != true" ); + eosio_assert( static_cast(name{"555555555555j"}) == true, "name != true" ); + eosio_assert( static_cast(name{"aaaaaaaaaaaaj"}) == true, "name != true" ); + eosio_assert( static_cast(name{"zzzzzzzzzzzzj"}) == true, "name != true" ); + + eosio_assert( static_cast(name{0}) == false, "name != false" ); + + // ------------------------------------------------------------------------------------------------------------------------------ + // print_f("%", ); + + char buffer[13]{}; + + std::string test_str{"1"}; + name{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 1" ); + name{test_str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 5" ); + name{test_str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != a" ); + name{test_str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != z" ); + + name{test_str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != abc" ); + name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123" ); + + name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != .abc" ); + name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != .........abc" ); + name{test_str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123." ); + name{test_str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123........." ); + name{test_str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != .a.b.c.1.2.3." ); + + name{test_str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != abc.123" ); + name{test_str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123.abc" ); + + name{test_str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 12345abcdefgj" ); + name{test_str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != hijklmnopqrsj" ); + name{test_str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != tuvwxyz.1234j" ); + + name{test_str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 111111111111j" ); + name{test_str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 555555555555j" ); + name{test_str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != aaaaaaaaaaaaj" ); + name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != zzzzzzzzzzzzj" ); + + // char buffer[13]{}; + + // name{"1"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "1") == 0, "name != 1" ); + // name{"5"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "5") == 0, "name != 5" ); + // name{"a"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "a") == 0, "name != a" ); + // name{"z"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "z") == 0, "name != z" ); + + // name{"abc"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "abc") == 0, "name != abc" ); + // name{"123"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "123") == 0, "name != 123" ); + + // name{".abc"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, ".abc") == 0, "name != .abc" ); + // name{"123."}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "123.") == 0, "name != 123." ); + + // name{"abc.123"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "abc.123") == 0, "name != abc.123" ); + // name{"123.abc"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "123.abc") == 0, "name != 123.abc" ); + + // name{"12345abcdefgj"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "12345abcdefgj") == 0, "name != 12345abcdefgj" ); + // name{"hijklmnopqrsj"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "hijklmnopqrsj") == 0, "name != hijklmnopqrsj" ); + // name{"tuvwxyz.1234j"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "tuvwxyz.1234j") == 0, "name != tuvwxyz.1234j" ); + + // name{"111111111111j"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "111111111111j") == 0, "name != 111111111111j" ); + // name{"555555555555j"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "555555555555j") == 0, "name != 555555555555j" ); + // name{"aaaaaaaaaaaaj"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "aaaaaaaaaaaaj") == 0, "name != aaaaaaaaaaaaj" ); + // name{"zzzzzzzzzzzzj"}.write_as_string(buffer, buffer + sizeof(buffer)); + // print_f( "%\n", buffer ); + // eosio_assert( strcmp(buffer, "zzzzzzzzzzzzj") == 0, "name != zzzzzzzzzzzzj" ); // ------------------------------------------------------------------------------------------------------------------------------ From 3c16d7546363e01a2078b36c47eb779655fed28e Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 4 Jan 2019 17:58:29 -0500 Subject: [PATCH 04/51] Continuation of `name` type tests --- temp/main.cpp | 411 +-------------------------- tests/unit/name_tests.cpp | 533 +++++++++++++++++++++++++++++++++--- tests/unit/symbol_tests.cpp | 43 +++ 3 files changed, 533 insertions(+), 454 deletions(-) diff --git a/temp/main.cpp b/temp/main.cpp index 9ef870b181..9bf7631e35 100644 --- a/temp/main.cpp +++ b/temp/main.cpp @@ -1,421 +1,14 @@ -// eosio-cpp -c main.cpp -fnative; ./a.out -// TODO: better error messages +// eosio-cpp main.cpp -w -fnative; ./a.out #include #include -#define NATIVE - using namespace eosio; using namespace eosio::native; int main() { - eosio_assert( name{}.value == 0ULL, "name != 0ULL" ); - - eosio_assert( name{0ULL}.value == 0ULL, "name != 0ULL" ); - eosio_assert( name{1ULL}.value == 1ULL, "name != 1ULL" ); - eosio_assert( name{18446744073709551615ULL}.value == 18446744073709551615ULL, "name != 18446744073709551615ULL" ); - - eosio_assert( name{name::raw{0ULL}}.value == 0ULL, "name != 0ULL" ); - eosio_assert( name{name::raw{1ULL}}.value == 1ULL, "name != 1ULL" ); - eosio_assert( name{name::raw{18446744073709551615ULL}}.value == 18446744073709551615ULL, "name != 18446744073709551615ULL" ); - - eosio_assert( name{"1"}.value == 576460752303423488ULL, "name != 1" ); - eosio_assert( name{"5"}.value == 2882303761517117440ULL, "name != 5" ); - eosio_assert( name{"a"}.value == 3458764513820540928ULL, "name != a" ); - eosio_assert( name{"z"}.value == 17870283321406128128ULL, "name != z" ); - - eosio_assert( name{"abc"}.value == 3589368903014285312ULL, "name != abc" ); - eosio_assert( name{"123"}.value == 614178399182651392ULL, "name != 123" ); - - eosio_assert( name{".abc"}.value == 112167778219196416ULL, "name != .abc" ); - eosio_assert( name{".........abc"}.value == 102016ULL, "name != .........abc" ); - eosio_assert( name{"123."}.value == 614178399182651392ULL, "name != 123." ); - eosio_assert( name{"123........."}.value == 614178399182651392ULL, "name != 123........." ); - eosio_assert( name{".a.b.c.1.2.3."}.value == 108209673814966320ULL, "name != .a.b.c.1.2.3." ); - - eosio_assert( name{"abc.123"}.value == 3589369488740450304ULL, "name != abc.123" ); - eosio_assert( name{"123.abc"}.value == 614181822271586304ULL, "name != 123.abc" ); - - eosio_assert( name{"12345abcdefgj"}.value == 614251623682315983ULL, "name != 12345abcdefgj" ); - eosio_assert( name{"hijklmnopqrsj"}.value == 7754926748989239183ULL, "name != hijklmnopqrsj" ); - eosio_assert( name{"tuvwxyz.1234j"}.value == 14895601873741973071ULL, "name != tuvwxyz.1234j" ); - - eosio_assert( name{"111111111111j"}.value == 595056260442243615ULL, "name != 111111111111j" ); - eosio_assert( name{"555555555555j"}.value == 2975281302211218015ULL, "name != 555555555555j" ); - eosio_assert( name{"aaaaaaaaaaaaj"}.value == 3570337562653461615ULL, "name != aaaaaaaaaaaaj" ); - eosio_assert( name{"zzzzzzzzzzzzj"}.value == 18446744073709551615ULL, "name != zzzzzzzzzzzzj" ); - - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ); - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ); - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ); - REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ); - - // ------------------------------------------------------------------------------------------------------------------------------ - - char c{'.'}; - uint8_t expected_value{0}; // Will increment to the expected correct value in the set [0,32) -#ifdef NATIVE - print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); - print_f("Value of expected_value: %", static_cast(expected_value) ); -#endif - eosio_assert( name::char_to_value(c) == expected_value, "name::char_to_value(c) != expected_value" ); - ++expected_value; - - for(c = '1'; c <= '5'; ++c ) { -#ifdef NATIVE - print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); - print_f("Value of expected_value: %", static_cast(expected_value) ); -#endif - eosio_assert( name::char_to_value(c) == expected_value, "name::char_to_value(c) != expected_value" ); - ++expected_value; - } - - for(c = 'a'; c <= 'z'; ++c ) { -#ifdef NATIVE - print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); - print_f("Value of expected_value: %", static_cast(expected_value) ); -#endif - eosio_assert( name::char_to_value(c) == expected_value, "name::char_to_value(c) != expected_value" ); - ++expected_value; - } - - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); - - - // ------------------------------------------------------------------------------------------------------------------------------ - - eosio_assert( name{""}.length() == 0, "name.length() != 0" ); - eosio_assert( name{"e"}.length() == 1, "name.length() != 1" ); - eosio_assert( name{"eo"}.length() == 2, "name.length() != 2" ); - eosio_assert( name{"eos"}.length() == 3, "name.length() != 3" ); - eosio_assert( name{"eosi"}.length() == 4, "name.length() != 4" ); - eosio_assert( name{"eosio"}.length() == 5, "name.length() != 5" ); - eosio_assert( name{"eosioa"}.length() == 6, "name.length() != 6" ); - eosio_assert( name{"eosioac"}.length() == 7, "name.length() != 7" ); - eosio_assert( name{"eosioacc"}.length() == 8, "name.length() != 8" ); - eosio_assert( name{"eosioacco"}.length() == 9, "name.length() != 9" ); - eosio_assert( name{"eosioaccou"}.length() == 10, "name.length() != 10" ); - eosio_assert( name{"eosioaccoun"}.length() == 11, "name.length() != 11" ); - eosio_assert( name{"eosioaccount"}.length() == 12, "name.length() != 12" ); - eosio_assert( name{"eosioaccountj"}.length() == 13, "name.length() != 13" ); - - REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ); - - // ------------------------------------------------------------------------------------------------------------------------------ - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{".eosioaccounj"}.suffix() ); // TODO: interesting print behavior; starts newline sometimes - print_f("Value of expected suffix: eosioaccounj\n" ); -#endif - - eosio_assert( name{".eosioaccounj"}.suffix() == name{"eosioaccounj"}, "name.suffix() != eosioaccounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"e.osioaccounj"}.suffix() ); - print_f("Value of expected suffix: osioaccounj\n" ); -#endif - - eosio_assert( name{"e.osioaccounj"}.suffix() == name{"osioaccounj"}, "name.suffix() != osioaccounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eo.sioaccounj"}.suffix() ); - print_f("Value of expected suffix: sioaccounj\n" ); -#endif - - eosio_assert( name{"eo.sioaccounj"}.suffix() == name{"sioaccounj"}, "name.suffix() != sioaccounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eos.ioaccounj"}.suffix() ); - print_f("Value of expected suffix: ioaccounj\n" ); -#endif - - eosio_assert( name{"eos.ioaccounj"}.suffix() == name{"ioaccounj"}, "name.suffix() != ioaccounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosi.oaccounj"}.suffix() ); - print_f("Value of expected suffix: oaccounj\n" ); -#endif - - eosio_assert( name{"eosi.oaccounj"}.suffix() == name{"oaccounj"}, "name.suffix() != oaccounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosio.accounj"}.suffix() ); - print_f("Value of expected suffix: accounj\n" ); -#endif - - eosio_assert( name{"eosio.accounj"}.suffix() == name{"accounj"}, "name.suffix() != accounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosioa.ccounj"}.suffix() ); - print_f("Value of expected suffix: ccounj\n" ); -#endif - - eosio_assert( name{"eosioa.ccounj"}.suffix() == name{"ccounj"}, "name.suffix() != ccounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosioac.counj"}.suffix() ); - print_f("Value of expected suffix: counj\n" ); -#endif - - eosio_assert( name{"eosioac.counj"}.suffix() == name{"counj"}, "name.suffix() != counj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosioacc.ounj"}.suffix() ); - print_f("Value of expected suffix: ounj\n" ); -#endif - - eosio_assert( name{"eosioacc.ounj"}.suffix() == name{"ounj"}, "name.suffix() != ounj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosioacco.unj"}.suffix() ); - print_f("Value of expected suffix: unj\n" ); -#endif - - eosio_assert( name{"eosioacco.unj"}.suffix() == name{"unj"}, "name.suffix() != unj" ); - -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosioaccou.nj"}.suffix() ); - print_f("Value of expected suffix: nj\n" ); -#endif - - eosio_assert( name{"eosioaccou.nj"}.suffix() == name{"nj"}, "name.suffix() != nj" ); -#ifdef NATIVE - print_f("Value of suffix: %\n", name{"eosioaccoun.j"}.suffix() ); - print_f("Value of expected suffix: j\n" ); -#endif - - eosio_assert( name{"eosioaccoun.j"}.suffix() == name{"j"}, "name.suffix() != j" ); - -// #ifdef NATIVE -// print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); -// print_f("Value of expected suffix: \n" ); -// #endif -// eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); - - // ------------------------------------------------------------------------------------------------------------------------------ - - eosio_assert( static_cast(name::raw{name{"1"}}) == 576460752303423488ULL, "name != 1" ); - eosio_assert( static_cast(name::raw{name{"5"}}) == 2882303761517117440ULL, "name != 5" ); - eosio_assert( static_cast(name::raw{name{"a"}}) == 3458764513820540928ULL, "name != a" ); - eosio_assert( static_cast(name::raw{name{"z"}}) == 17870283321406128128ULL, "name != z" ); - - eosio_assert( static_cast(name::raw{name{"abc"}}) == 3589368903014285312ULL, "name != abc" ); - eosio_assert( static_cast(name::raw{name{"123"}}) == 614178399182651392ULL, "name != 123" ); - - eosio_assert( static_cast(name::raw{name{".abc"}}) == 112167778219196416ULL, "name != .abc" ); - eosio_assert( static_cast(name::raw{name{".........abc"}}) == 102016ULL, "name != .........abc" ); - eosio_assert( static_cast(name::raw{name{"123."}}) == 614178399182651392ULL, "name != 123." ); - eosio_assert( static_cast(name::raw{name{"123........."}}) == 614178399182651392ULL, "name != 123........." ); - eosio_assert( static_cast(name::raw{name{".a.b.c.1.2.3."}}) == 108209673814966320ULL, "name != .a.b.c.1.2.3." ); - - eosio_assert( static_cast(name::raw{name{"abc.123"}}) == 3589369488740450304ULL, "name != abc.123" ); - eosio_assert( static_cast(name::raw{name{"123.abc"}}) == 614181822271586304ULL, "name != 123.abc" ); - - eosio_assert( static_cast(name::raw{name{"12345abcdefgj"}}) == 614251623682315983ULL, "name != 12345abcdefgj" ); - eosio_assert( static_cast(name::raw{name{"hijklmnopqrsj"}}) == 7754926748989239183ULL, "name != hijklmnopqrsj" ); - eosio_assert( static_cast(name::raw{name{"tuvwxyz.1234j"}}) == 14895601873741973071ULL, "name != tuvwxyz.1234j" ); - - eosio_assert( static_cast(name::raw{name{"111111111111j"}}) == 595056260442243615ULL, "name != 111111111111j" ); - eosio_assert( static_cast(name::raw{name{"555555555555j"}}) == 2975281302211218015ULL, "name != 555555555555j" ); - eosio_assert( static_cast(name::raw{name{"aaaaaaaaaaaaj"}}) == 3570337562653461615ULL, "name != aaaaaaaaaaaaj" ); - eosio_assert( static_cast(name::raw{name{"zzzzzzzzzzzzj"}}) == 18446744073709551615ULL, "name != zzzzzzzzzzzzj" ); - - // ------------------------------------------------------------------------------------------------------------------------------ - - eosio_assert( static_cast(name{"1"}) == true, "name != true" ); - eosio_assert( static_cast(name{"5"}) == true, "name != true" ); - eosio_assert( static_cast(name{"a"}) == true, "name != true" ); - eosio_assert( static_cast(name{"z"}) == true, "name != true" ); - - eosio_assert( static_cast(name{"abc"}) == true, "name != true" ); - eosio_assert( static_cast(name{"123"}) == true, "name != true" ); - - eosio_assert( static_cast(name{".abc"}) == true, "name != true" ); - eosio_assert( static_cast(name{".........abc"}) == true, "name != true" ); - eosio_assert( static_cast(name{"123."}) == true, "name != true" ); - eosio_assert( static_cast(name{"123........."}) == true, "name != true" ); - eosio_assert( static_cast(name{".a.b.c.1.2.3."}) == true, "name != true" ); - - eosio_assert( static_cast(name{"abc.123"}) == true, "name != true" ); - eosio_assert( static_cast(name{"123.abc"}) == true, "name != true" ); - - eosio_assert( static_cast(name{"12345abcdefgj"}) == true, "name != true" ); - eosio_assert( static_cast(name{"hijklmnopqrsj"}) == true, "name != true" ); - eosio_assert( static_cast(name{"tuvwxyz.1234j"}) == true, "name != true" ); - - eosio_assert( static_cast(name{"111111111111j"}) == true, "name != true" ); - eosio_assert( static_cast(name{"555555555555j"}) == true, "name != true" ); - eosio_assert( static_cast(name{"aaaaaaaaaaaaj"}) == true, "name != true" ); - eosio_assert( static_cast(name{"zzzzzzzzzzzzj"}) == true, "name != true" ); - - eosio_assert( static_cast(name{0}) == false, "name != false" ); - - // ------------------------------------------------------------------------------------------------------------------------------ - // print_f("%", ); - - char buffer[13]{}; - - std::string test_str{"1"}; - name{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 1" ); - name{test_str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 5" ); - name{test_str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != a" ); - name{test_str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != z" ); - - name{test_str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != abc" ); - name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123" ); - - name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != .abc" ); - name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != .........abc" ); - name{test_str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123." ); - name{test_str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123........." ); - name{test_str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != .a.b.c.1.2.3." ); - - name{test_str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != abc.123" ); - name{test_str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 123.abc" ); - - name{test_str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 12345abcdefgj" ); - name{test_str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != hijklmnopqrsj" ); - name{test_str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != tuvwxyz.1234j" ); - - name{test_str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 111111111111j" ); - name{test_str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != 555555555555j" ); - name{test_str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != aaaaaaaaaaaaj" ); - name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - eosio_assert( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())) == 0, "name != zzzzzzzzzzzzj" ); - - // char buffer[13]{}; - - // name{"1"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "1") == 0, "name != 1" ); - // name{"5"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "5") == 0, "name != 5" ); - // name{"a"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "a") == 0, "name != a" ); - // name{"z"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "z") == 0, "name != z" ); - - // name{"abc"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "abc") == 0, "name != abc" ); - // name{"123"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "123") == 0, "name != 123" ); - - // name{".abc"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, ".abc") == 0, "name != .abc" ); - // name{"123."}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "123.") == 0, "name != 123." ); - - // name{"abc.123"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "abc.123") == 0, "name != abc.123" ); - // name{"123.abc"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "123.abc") == 0, "name != 123.abc" ); - - // name{"12345abcdefgj"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "12345abcdefgj") == 0, "name != 12345abcdefgj" ); - // name{"hijklmnopqrsj"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "hijklmnopqrsj") == 0, "name != hijklmnopqrsj" ); - // name{"tuvwxyz.1234j"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "tuvwxyz.1234j") == 0, "name != tuvwxyz.1234j" ); - - // name{"111111111111j"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "111111111111j") == 0, "name != 111111111111j" ); - // name{"555555555555j"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "555555555555j") == 0, "name != 555555555555j" ); - // name{"aaaaaaaaaaaaj"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "aaaaaaaaaaaaj") == 0, "name != aaaaaaaaaaaaj" ); - // name{"zzzzzzzzzzzzj"}.write_as_string(buffer, buffer + sizeof(buffer)); - // print_f( "%\n", buffer ); - // eosio_assert( strcmp(buffer, "zzzzzzzzzzzzj") == 0, "name != zzzzzzzzzzzzj" ); - - // ------------------------------------------------------------------------------------------------------------------------------ - - // eosio_assert( symbol_code{}.raw() == 0ULL, "symbol_code != 0ULL" ); - // eosio_assert( symbol_code{0ULL}.raw() == 0ULL, "symbol_code != 0ULL" ); - // eosio_assert( symbol_code{1ULL}.raw() == 1ULL, "symbol_code != 1ULL" ); - // eosio_assert( symbol_code{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol_code != 18446744073709551615ULL" ); - - // eosio_assert( symbol_code{"A"}.raw() == 65ULL, "symbol_code != 65ULL" ); - // eosio_assert( symbol_code{"Z"}.raw() == 90ULL, "symbol_code != 90ULL" ); - // eosio_assert( symbol_code{"AAAAAAA"}.raw() == 18367622009667905ULL, "symbol_code != 18367622009667905ULL" ); - // eosio_assert( symbol_code{"ZZZZZZZ"}.raw() == 25432092013386330ULL, "symbol_code != 25432092013386330ULL" ); - - // symbol_code s{"ZZZZZZZ"}; - // eosio::print_f("%\n",s.raw()); - - // eosio_assert( symbol{}.raw() == 0ULL, "symbol != 0ULL" ); - // eosio_assert( symbol{0ULL}.raw() == 0ULL, "symbol != 0ULL" ); - // eosio_assert( symbol{1ULL}.raw() == 1ULL, "symbol != 1ULL" ); - // eosio_assert( symbol{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol != 18446744073709551615ULL" ); - - // eosio_assert( symbol{1}.raw() == 576460752303423488ULL, "symbol != 1" ); - // eosio_assert( symbol{5}.raw() == 2882303761517117440ULL, "symbol != 5" ); - // eosio_assert( symbol{"a"}.raw() == 3458764513820540928ULL, "symbol != a" ); - // eosio_assert( symbol{"z"}.raw() == 17870283321406128128ULL, "symbol != z" ); - - // eosio_assert( symbol{"abc"}.raw() == 3589368903014285312ULL, "symbol != abc" ); - // eosio_assert( symbol{"123"}.raw() == 614178399182651392ULL, "symbol != 123" ); - - // eosio_assert( symbol{".abc"}.raw() == 112167778219196416ULL, "symbol != .abc" ); - // eosio_assert( symbol{"123."}.raw() == 614178399182651392ULL, "symbol != 123." ); - - // eosio_assert( symbol{"abc.123"}.raw() == 3589369488740450304ULL, "symbol != abc.123" ); - // eosio_assert( symbol{"123.abc"}.raw() == 614181822271586304ULL, "symbol != 123.abc" ); - - // eosio_assert( symbol{"12345abcdefgj"}.raw() == 614251623682315983ULL, "symbol != 12345abcdefgj" ); - // eosio_assert( symbol{"hijklmnopqrsj"}.raw() == 7754926748989239183ULL, "symbol != hijklmnopqrsj" ); - // eosio_assert( symbol{"tuvwxyz.1234j"}.raw() == 14895601873741973071ULL, "symbol != tuvwxyz.1234j" ); - - // eosio_assert( symbol{"111111111111j"}.raw() == 595056260442243615ULL, "symbol != 111111111111j" ); - // eosio_assert( symbol{"555555555555j"}.raw() == 2975281302211218015ULL, "symbol != 555555555555j" ); - // eosio_assert( symbol{"aaaaaaaaaaaaj"}.raw() == 3570337562653461615ULL, "symbol != aaaaaaaaaaaaj" ); - // eosio_assert( symbol{"zzzzzzzzzzzzj"}.raw() == 18446744073709551615ULL, "symbol != zzzzzzzzzzzzj" ); - // eosio::symbol n(18446744073709551615ULL); - // eosio::print_f("%\n",n.raw()); - // return 0; + return 0; } diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 5fc9d8a018..6322717f54 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,46 +1,489 @@ #include #include +#define NATIVE + using namespace eosio; using namespace eosio::native; // Defined in `eosio.cdt/libraries/eosiolib/name.hpp` -EOSIO_TEST_BEGIN(name_type_constructors_test) - eosio_assert( name{}.value == 0ULL, "name != 0ULL" ); +EOSIO_TEST_BEGIN(name_type_test) + // ------------ + // constructors + + REQUIRE_EQUAL( name{}.value, 0ULL ); + + REQUIRE_EQUAL( name{0ULL}.value, 0ULL ); + REQUIRE_EQUAL( name{1ULL}.value, 1ULL ); + REQUIRE_EQUAL( name{18446744073709551615ULL}.value, 18446744073709551615ULL ); + + REQUIRE_EQUAL( name{name::raw{0ULL}}.value, 0ULL ); + REQUIRE_EQUAL( name{name::raw{1ULL}}.value, 1ULL ); + REQUIRE_EQUAL( name{name::raw{18446744073709551615ULL}}.value, 18446744073709551615ULL ); + + REQUIRE_EQUAL( name{"1"}.value, 576460752303423488ULL ); + REQUIRE_EQUAL( name{"5"}.value, 2882303761517117440ULL ); + REQUIRE_EQUAL( name{"a"}.value, 3458764513820540928ULL ); + REQUIRE_EQUAL( name{"z"}.value, 17870283321406128128ULL ); + + REQUIRE_EQUAL( name{"abc"}.value, 3589368903014285312ULL ); + REQUIRE_EQUAL( name{"123"}.value, 614178399182651392ULL ); + + REQUIRE_EQUAL( name{".abc"}.value, 112167778219196416ULL ); + REQUIRE_EQUAL( name{".........abc"}.value, 102016ULL ); + REQUIRE_EQUAL( name{"123."}.value, 614178399182651392ULL ); + REQUIRE_EQUAL( name{"123........."}.value, 614178399182651392ULL ); + REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ); + + REQUIRE_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ); + REQUIRE_EQUAL( name{"123.abc"}.value, 614181822271586304ULL ); + + REQUIRE_EQUAL( name{"12345abcdefgj"}.value, 614251623682315983ULL ); + REQUIRE_EQUAL( name{"hijklmnopqrsj"}.value, 7754926748989239183ULL ); + REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.value, 14895601873741973071ULL ); + + REQUIRE_EQUAL( name{"111111111111j"}.value, 595056260442243615ULL ); + REQUIRE_EQUAL( name{"555555555555j"}.value, 2975281302211218015ULL ); + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.value, 3570337562653461615ULL ); + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.value, 18446744073709551615ULL ); + + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ); + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ); + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ); + REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ); + + // --------------- + // `char_to_value` + + char c{'.'}; + uint8_t expected_value{0}; // Will increment to the expected correct value in the set [0,32) +#ifdef NATIVE + print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); + print_f("Value of expected_value: %", static_cast(expected_value) ); +#endif + REQUIRE_EQUAL( name::char_to_value(c), expected_value ); + ++expected_value; - eosio_assert( name{0ULL}.value == 0ULL, "name != 0ULL" ); - eosio_assert( name{1ULL}.value == 1ULL, "name != 1ULL" ); - eosio_assert( name{18446744073709551615ULL}.value == 18446744073709551615ULL, "name != 18446744073709551615ULL" ); + for(c = '1'; c <= '5'; ++c ) { +#ifdef NATIVE + print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); + print_f("Value of expected_value: %", static_cast(expected_value) ); +#endif + REQUIRE_EQUAL( name::char_to_value(c), expected_value ); + ++expected_value; + } - eosio_assert( name{"1"}.value == 576460752303423488ULL, "name != 1" ); - eosio_assert( name{"5"}.value == 2882303761517117440ULL, "name != 5" ); - eosio_assert( name{"a"}.value == 3458764513820540928ULL, "name != a" ); - eosio_assert( name{"z"}.value == 17870283321406128128ULL, "name != z" ); + for(c = 'a'; c <= 'z'; ++c ) { +#ifdef NATIVE + print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); + print_f("Value of expected_value: %", static_cast(expected_value) ); +#endif + REQUIRE_EQUAL( name::char_to_value(c), expected_value ); + ++expected_value; + } + + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); + - eosio_assert( name{"abc"}.value == 3589368903014285312ULL, "name != abc" ); - eosio_assert( name{"123"}.value == 614178399182651392ULL, "name != 123" ); + // ------ + // length - eosio_assert( name{".abc"}.value == 112167778219196416ULL, "name != .abc" ); - eosio_assert( name{"123."}.value == 614178399182651392ULL, "name != 123." ); + REQUIRE_EQUAL( name{""}.length(), 0 ); + REQUIRE_EQUAL( name{"e"}.length(), 1 ); + REQUIRE_EQUAL( name{"eo"}.length(), 2 ); + REQUIRE_EQUAL( name{"eos"}.length(), 3 ); + REQUIRE_EQUAL( name{"eosi"}.length(), 4 ); + REQUIRE_EQUAL( name{"eosio"}.length(), 5 ); + REQUIRE_EQUAL( name{"eosioa"}.length(), 6 ); + REQUIRE_EQUAL( name{"eosioac"}.length(), 7 ); + REQUIRE_EQUAL( name{"eosioacc"}.length(), 8 ); + REQUIRE_EQUAL( name{"eosioacco"}.length(), 9 ); + REQUIRE_EQUAL( name{"eosioaccou"}.length(), 10 ); + REQUIRE_EQUAL( name{"eosioaccoun"}.length(), 11 ); + REQUIRE_EQUAL( name{"eosioaccount"}.length(), 12 ); + REQUIRE_EQUAL( name{"eosioaccountj"}.length(), 13 ); + + REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ); + + // ------ + // suffix + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{".eosioaccounj"}.suffix() ); // TODO: interesting print behavior; starts newline sometimes + print_f("Value of expected suffix: eosioaccounj\n" ); +#endif + + REQUIRE_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"e.osioaccounj"}.suffix() ); + print_f("Value of expected suffix: osioaccounj\n" ); +#endif + + REQUIRE_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eo.sioaccounj"}.suffix() ); + print_f("Value of expected suffix: sioaccounj\n" ); +#endif + + REQUIRE_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eos.ioaccounj"}.suffix() ); + print_f("Value of expected suffix: ioaccounj\n" ); +#endif + + REQUIRE_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosi.oaccounj"}.suffix() ); + print_f("Value of expected suffix: oaccounj\n" ); +#endif - eosio_assert( name{"abc.123"}.value == 3589369488740450304ULL, "name != abc.123" ); - eosio_assert( name{"123.abc"}.value == 614181822271586304ULL, "name != 123.abc" ); + REQUIRE_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosio.accounj"}.suffix() ); + print_f("Value of expected suffix: accounj\n" ); +#endif + + REQUIRE_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioa.ccounj"}.suffix() ); + print_f("Value of expected suffix: ccounj\n" ); +#endif + + REQUIRE_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioac.counj"}.suffix() ); + print_f("Value of expected suffix: counj\n" ); +#endif + + REQUIRE_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioacc.ounj"}.suffix() ); + print_f("Value of expected suffix: ounj\n" ); +#endif + + REQUIRE_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioacco.unj"}.suffix() ); + print_f("Value of expected suffix: unj\n" ); +#endif + + REQUIRE_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioaccou.nj"}.suffix() ); + print_f("Value of expected suffix: nj\n" ); +#endif + + REQUIRE_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eosioaccoun.j"}.suffix() ); + print_f("Value of expected suffix: j\n" ); +#endif + + REQUIRE_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"e.o.s.i.o.a.c"}.suffix() ); + print_f("Value of expected suffix: c\n" ); +#endif + + REQUIRE_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ); + +#ifdef NATIVE + print_f("Value of suffix: %\n", name{"eos.ioa.cco"}.suffix() ); + print_f("Value of expected suffix: cco\n" ); +#endif + + REQUIRE_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ); + +// TODO: fix bug +// #ifdef NATIVE +// print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); +// print_f("Value of expected suffix: \n" ); +// #endif +// eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); + + // ------------ + // operator raw + + REQUIRE_EQUAL( static_cast(name::raw{name{"1"}}), 576460752303423488ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"5"}}), 2882303761517117440ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"a"}}), 3458764513820540928ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"z"}}), 17870283321406128128ULL ); + + REQUIRE_EQUAL( static_cast(name::raw{name{"abc"}}), 3589368903014285312ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"123"}}), 614178399182651392ULL ); + + REQUIRE_EQUAL( static_cast(name::raw{name{".abc"}}), 112167778219196416ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{".........abc"}}), 102016ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"123."}}), 614178399182651392ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"123........."}}), 614178399182651392ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{".a.b.c.1.2.3."}}), 108209673814966320ULL ); + + REQUIRE_EQUAL( static_cast(name::raw{name{"abc.123"}}), 3589369488740450304ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"123.abc"}}), 614181822271586304ULL ); + + REQUIRE_EQUAL( static_cast(name::raw{name{"12345abcdefgj"}}), 614251623682315983ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"hijklmnopqrsj"}}), 7754926748989239183ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"tuvwxyz.1234j"}}), 14895601873741973071ULL ); - eosio_assert( name{"12345abcdefgj"}.value == 614251623682315983ULL, "name != 12345abcdefgj" ); - eosio_assert( name{"hijklmnopqrsj"}.value == 7754926748989239183ULL, "name != hijklmnopqrsj" ); - eosio_assert( name{"tuvwxyz.1234j"}.value == 14895601873741973071ULL, "name != tuvwxyz.1234j" ); + REQUIRE_EQUAL( static_cast(name::raw{name{"111111111111j"}}), 595056260442243615ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"555555555555j"}}), 2975281302211218015ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"aaaaaaaaaaaaj"}}), 3570337562653461615ULL ); + REQUIRE_EQUAL( static_cast(name::raw{name{"zzzzzzzzzzzzj"}}), 18446744073709551615ULL ); + + // ------------- + // operator bool - eosio_assert( name{"111111111111j"}.value == 595056260442243615ULL, "name != 111111111111j" ); - eosio_assert( name{"555555555555j"}.value == 2975281302211218015ULL, "name != 555555555555j" ); - eosio_assert( name{"aaaaaaaaaaaaj"}.value == 3570337562653461615ULL, "name != aaaaaaaaaaaaj" ); - eosio_assert( name{"zzzzzzzzzzzzj"}.value == 18446744073709551615ULL, "name != zzzzzzzzzzzzj" ); + REQUIRE_EQUAL( static_cast(name{"1"}), true ); + REQUIRE_EQUAL( static_cast(name{"5"}), true ); + REQUIRE_EQUAL( static_cast(name{"a"}), true ); + REQUIRE_EQUAL( static_cast(name{"z"}), true ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"}.value;}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"}.value;}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"}.value;}) ); - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"}.value;}) ); - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"}.value;}) ); - REQUIRE_ASSERT( "the length must not be longer than 13 characters in an `eosio::name`", ([]() {name{"12345abcdefghj"}.value;}) ); + REQUIRE_EQUAL( static_cast(name{"abc"}), true ); + REQUIRE_EQUAL( static_cast(name{"123"}), true ); + + REQUIRE_EQUAL( static_cast(name{".abc"}), true ); + REQUIRE_EQUAL( static_cast(name{".........abc"}), true ); + REQUIRE_EQUAL( static_cast(name{"123."}), true ); + REQUIRE_EQUAL( static_cast(name{"123........."}), true ); + REQUIRE_EQUAL( static_cast(name{".a.b.c.1.2.3."}), true ); + + REQUIRE_EQUAL( static_cast(name{"abc.123"}), true ); + REQUIRE_EQUAL( static_cast(name{"123.abc"}), true ); + + REQUIRE_EQUAL( static_cast(name{"12345abcdefgj"}), true ); + REQUIRE_EQUAL( static_cast(name{"hijklmnopqrsj"}), true ); + REQUIRE_EQUAL( static_cast(name{"tuvwxyz.1234j"}), true ); + + REQUIRE_EQUAL( static_cast(name{"111111111111j"}), true ); + REQUIRE_EQUAL( static_cast(name{"555555555555j"}), true ); + REQUIRE_EQUAL( static_cast(name{"aaaaaaaaaaaaj"}), true ); + REQUIRE_EQUAL( static_cast(name{"zzzzzzzzzzzzj"}), true ); + + REQUIRE_EQUAL( static_cast(name{0}), false ); + + // --------------- + // write_as_string + + char buffer[13]{}; + + std::string test_str{"1"}; + name{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + + name{test_str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + + // TODO: fix bug + // Note that '.' characters at the end of a name do not get written to the buffer. + name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ); + name{test_str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ); + name{test_str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ); + + name{test_str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + + name{test_str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + + name{test_str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + + // --------- + // to_string + + REQUIRE_EQUAL( name{"1"}.to_string(), "1" ); + REQUIRE_EQUAL( name{"5"}.to_string(), "5" ); + REQUIRE_EQUAL( name{"a"}.to_string(), "a" ); + REQUIRE_EQUAL( name{"z"}.to_string(), "z" ); + + REQUIRE_EQUAL( name{"abc"}.to_string(), "abc" ); + REQUIRE_EQUAL( name{"123"}.to_string(), "123" ); + + // TODO: fix bug + REQUIRE_EQUAL( name{".abc"}.to_string(), ".abc" ); + REQUIRE_EQUAL( name{".........abc"}.to_string(), ".........abc" ); + REQUIRE_EQUAL( name{"123."}.to_string(), "123" ); + REQUIRE_EQUAL( name{"123........."}.to_string(), "123" ); + REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.to_string(), ".a.b.c.1.2.3" ); + + REQUIRE_EQUAL( name{"abc.123"}.to_string(), "abc.123" ); + REQUIRE_EQUAL( name{"123.abc"}.to_string(), "123.abc" ); + + REQUIRE_EQUAL( name{"12345abcdefgj"}.to_string(), "12345abcdefgj" ); + REQUIRE_EQUAL( name{"hijklmnopqrsj"}.to_string(), "hijklmnopqrsj" ); + REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.to_string(), "tuvwxyz.1234j" ); + + REQUIRE_EQUAL( name{"111111111111j"}.to_string(), "111111111111j" ); + REQUIRE_EQUAL( name{"555555555555j"}.to_string(), "555555555555j" ); + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ); + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ); + + // ---------- + // operator== + + REQUIRE_EQUAL( name{"1"} == name{"1"}, true ); + REQUIRE_EQUAL( name{"5"} == name{"5"}, true ); + REQUIRE_EQUAL( name{"a"} == name{"a"}, true ); + REQUIRE_EQUAL( name{"z"} == name{"z"}, true ); + + REQUIRE_EQUAL( name{"abc"} == name{"abc"}, true ); + REQUIRE_EQUAL( name{"123"} == name{"123"}, true ); + + // TODO: fix bug + REQUIRE_EQUAL( name{".abc"} == name{".abc"}, true ); + REQUIRE_EQUAL( name{".........abc"} == name{".........abc"}, true ); + REQUIRE_EQUAL( name{"123."} == name{"123"}, true ); + REQUIRE_EQUAL( name{"123........."} == name{"123"}, true ); + REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == name{".a.b.c.1.2.3"}, true ); + + REQUIRE_EQUAL( name{"abc.123"} == name{"abc.123"}, true ); + REQUIRE_EQUAL( name{"123.abc"} == name{"123.abc"}, true ); + + REQUIRE_EQUAL( name{"12345abcdefgj"} == name{"12345abcdefgj"}, true ); + REQUIRE_EQUAL( name{"hijklmnopqrsj"} == name{"hijklmnopqrsj"}, true ); + REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == name{"tuvwxyz.1234j"}, true ); + + REQUIRE_EQUAL( name{"111111111111j"} == name{"111111111111j"}, true ); + REQUIRE_EQUAL( name{"555555555555j"} == name{"555555555555j"}, true ); + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ); + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ); + + // ---------- + // operator!= + + REQUIRE_EQUAL( name{"1"} != name{0}, true ); + REQUIRE_EQUAL( name{"5"} != name{0}, true ); + REQUIRE_EQUAL( name{"a"} != name{0}, true ); + REQUIRE_EQUAL( name{"z"} != name{0}, true ); + + REQUIRE_EQUAL( name{"abc"} != name{0}, true ); + REQUIRE_EQUAL( name{"123"} != name{0}, true ); + + REQUIRE_EQUAL( name{".abc"} != name{0}, true ); + REQUIRE_EQUAL( name{".........abc"} != name{0}, true ); + REQUIRE_EQUAL( name{"123."} != name{0}, true ); + REQUIRE_EQUAL( name{"123........."} != name{0}, true ); + REQUIRE_EQUAL( name{".a.b.c.1.2.3."} != name{0}, true ); + + REQUIRE_EQUAL( name{"abc.123"} != name{0}, true ); + REQUIRE_EQUAL( name{"123.abc"} != name{0}, true ); + + REQUIRE_EQUAL( name{"12345abcdefgj"} != name{0}, true ); + REQUIRE_EQUAL( name{"hijklmnopqrsj"} != name{0}, true ); + REQUIRE_EQUAL( name{"tuvwxyz.1234j"} != name{0}, true ); + + REQUIRE_EQUAL( name{"111111111111j"} != name{0}, true ); + REQUIRE_EQUAL( name{"555555555555j"} != name{0}, true ); + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} != name{0}, true ); + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} != name{0}, true ); + + // --------- + // operator< + + REQUIRE_EQUAL( name{0} < name{"1"}, true ); + REQUIRE_EQUAL( name{0} < name{"5"}, true ); + REQUIRE_EQUAL( name{0} < name{"a"}, true ); + REQUIRE_EQUAL( name{0} < name{"z"}, true ); + + REQUIRE_EQUAL( name{0} < name{"abc"}, true ); + REQUIRE_EQUAL( name{0} < name{"123"}, true ); + + REQUIRE_EQUAL( name{0} < name{".abc"}, true ); + REQUIRE_EQUAL( name{0} < name{".........abc"}, true ); + REQUIRE_EQUAL( name{0} < name{"123."}, true ); + REQUIRE_EQUAL( name{0} < name{"123........."}, true ); + REQUIRE_EQUAL( name{0} < name{".a.b.c.1.2.3."}, true ); + + REQUIRE_EQUAL( name{0} < name{"abc.123"}, true ); + REQUIRE_EQUAL( name{0} < name{"123.abc"}, true ); + + REQUIRE_EQUAL( name{0} < name{"12345abcdefgj"}, true ); + REQUIRE_EQUAL( name{0} < name{"hijklmnopqrsj"}, true ); + REQUIRE_EQUAL( name{0} < name{"tuvwxyz.1234j"}, true ); + + REQUIRE_EQUAL( name{0} < name{"111111111111j"}, true ); + REQUIRE_EQUAL( name{0} < name{"555555555555j"}, true ); + REQUIRE_EQUAL( name{0} < name{"aaaaaaaaaaaaj"}, true ); + REQUIRE_EQUAL( name{0} < name{"zzzzzzzzzzzzj"}, true ); + + // ------------ + // operator""_n + + REQUIRE_EQUAL( name{0} == ""_n, true ); + + REQUIRE_EQUAL( name{"1"} == "1"_n, true ); + REQUIRE_EQUAL( name{"5"} == "5"_n, true ); + REQUIRE_EQUAL( name{"a"} == "a"_n, true ); + REQUIRE_EQUAL( name{"z"} == "z"_n, true ); + + REQUIRE_EQUAL( name{"abc"} == "abc"_n, true ); + REQUIRE_EQUAL( name{"123"} == "123"_n, true ); + + // TODO: fix bug + REQUIRE_EQUAL( name{".abc"} == ".abc"_n, true ); + REQUIRE_EQUAL( name{".........abc"} == ".........abc"_n, true ); + REQUIRE_EQUAL( name{"123."} == "123."_n, true ); + REQUIRE_EQUAL( name{"123........."} == "123........."_n, true ); + REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ); + + REQUIRE_EQUAL( name{"abc.123"} == "abc.123"_n, true ); + REQUIRE_EQUAL( name{"123.abc"} == "123.abc"_n, true ); + + REQUIRE_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ); + REQUIRE_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ); + REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ); + + REQUIRE_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ); + REQUIRE_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ); + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ); + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ); silence_output(false); EOSIO_TEST_END @@ -154,27 +597,27 @@ EOSIO_TEST_END // }); // EOSIO_TEST_END -// // EOSIO_TEST_BEGIN(is_account_test) -// // intrinsics::set_intrinsic([](uint64_t a) { -// // if (a == 3) -// // return true; -// // if (a == 4) -// // return true; -// // return false; -// // }); +// EOSIO_TEST_BEGIN(is_account_test) +// intrinsics::set_intrinsic([](uint64_t a) { +// if (a == 3) +// return true; +// if (a == 4) +// return true; +// return false; +// }); -// // CHECK_ASSERT("is not an account", ([]() { -// // eosio_assert(is_account(5), "is not an account"); -// // })); -// // CHECK_EQUAL(is_account(3), true); -// // CHECK_EQUAL(is_account(4), true); -// // CHECK_EQUAL(is_account(6), true); -// // REQUIRE_EQUAL(is_account(7), true); +// CHECK_ASSERT("is not an account", ([]() { +// eosio_assert(is_account(5), "is not an account"); +// })); +// CHECK_EQUAL(is_account(3), true); +// CHECK_EQUAL(is_account(4), true); +// CHECK_EQUAL(is_account(6), true); +// REQUIRE_EQUAL(is_account(7), true); -// // EOSIO_TEST_END +// EOSIO_TEST_END int main(int argc, char** argv) { - //EOSIO_TEST(name_type_test); + EOSIO_TEST(name_type_test); // EOSIO_TEST(is_account_test); return has_failed(); } diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 49fe19ce58..569426fdba 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -61,3 +61,46 @@ int main(int argc, char** argv) { EOSIO_TEST(symbol_type_test); return has_failed(); } + +// eosio_assert( symbol_code{}.raw() == 0ULL, "symbol_code != 0ULL" ); +// eosio_assert( symbol_code{0ULL}.raw() == 0ULL, "symbol_code != 0ULL" ); +// eosio_assert( symbol_code{1ULL}.raw() == 1ULL, "symbol_code != 1ULL" ); +// eosio_assert( symbol_code{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol_code != 18446744073709551615ULL" ); + +// eosio_assert( symbol_code{"A"}.raw() == 65ULL, "symbol_code != 65ULL" ); +// eosio_assert( symbol_code{"Z"}.raw() == 90ULL, "symbol_code != 90ULL" ); +// eosio_assert( symbol_code{"AAAAAAA"}.raw() == 18367622009667905ULL, "symbol_code != 18367622009667905ULL" ); +// eosio_assert( symbol_code{"ZZZZZZZ"}.raw() == 25432092013386330ULL, "symbol_code != 25432092013386330ULL" ); + +// symbol_code s{"ZZZZZZZ"}; +// eosio::print_f("%\n",s.raw()); + +// eosio_assert( symbol{}.raw() == 0ULL, "symbol != 0ULL" ); +// eosio_assert( symbol{0ULL}.raw() == 0ULL, "symbol != 0ULL" ); +// eosio_assert( symbol{1ULL}.raw() == 1ULL, "symbol != 1ULL" ); +// eosio_assert( symbol{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol != 18446744073709551615ULL" ); + +// eosio_assert( symbol{1}.raw() == 576460752303423488ULL, "symbol != 1" ); +// eosio_assert( symbol{5}.raw() == 2882303761517117440ULL, "symbol != 5" ); +// eosio_assert( symbol{"a"}.raw() == 3458764513820540928ULL, "symbol != a" ); +// eosio_assert( symbol{"z"}.raw() == 17870283321406128128ULL, "symbol != z" ); + +// eosio_assert( symbol{"abc"}.raw() == 3589368903014285312ULL, "symbol != abc" ); +// eosio_assert( symbol{"123"}.raw() == 614178399182651392ULL, "symbol != 123" ); + +// eosio_assert( symbol{".abc"}.raw() == 112167778219196416ULL, "symbol != .abc" ); +// eosio_assert( symbol{"123."}.raw() == 614178399182651392ULL, "symbol != 123." ); + +// eosio_assert( symbol{"abc.123"}.raw() == 3589369488740450304ULL, "symbol != abc.123" ); +// eosio_assert( symbol{"123.abc"}.raw() == 614181822271586304ULL, "symbol != 123.abc" ); + +// eosio_assert( symbol{"12345abcdefgj"}.raw() == 614251623682315983ULL, "symbol != 12345abcdefgj" ); +// eosio_assert( symbol{"hijklmnopqrsj"}.raw() == 7754926748989239183ULL, "symbol != hijklmnopqrsj" ); +// eosio_assert( symbol{"tuvwxyz.1234j"}.raw() == 14895601873741973071ULL, "symbol != tuvwxyz.1234j" ); + +// eosio_assert( symbol{"111111111111j"}.raw() == 595056260442243615ULL, "symbol != 111111111111j" ); +// eosio_assert( symbol{"555555555555j"}.raw() == 2975281302211218015ULL, "symbol != 555555555555j" ); +// eosio_assert( symbol{"aaaaaaaaaaaaj"}.raw() == 3570337562653461615ULL, "symbol != aaaaaaaaaaaaj" ); +// eosio_assert( symbol{"zzzzzzzzzzzzj"}.raw() == 18446744073709551615ULL, "symbol != zzzzzzzzzzzzj" ); +// eosio::symbol n(18446744073709551615ULL); +// eosio::print_f("%\n",n.raw()); From c0703b0f518756ba425ebf0a4cacf8ff0521b136 Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 7 Jan 2019 18:24:12 -0500 Subject: [PATCH 05/51] Start `symbol` type tests --- libraries/eosiolib/name.hpp | 12 +- tests/unit/name_tests.cpp | 105 +++++------- tests/unit/symbol_tests.cpp | 313 ++++++++++++++++++++++++++---------- 3 files changed, 273 insertions(+), 157 deletions(-) diff --git a/libraries/eosiolib/name.hpp b/libraries/eosiolib/name.hpp index 1a09c75e8e..17ecae7f12 100644 --- a/libraries/eosiolib/name.hpp +++ b/libraries/eosiolib/name.hpp @@ -132,6 +132,9 @@ namespace eosio { * Returns the suffix of the %name */ constexpr name suffix()const { + // if((value >> 56) & 0x1F) + // return name{""}; + uint32_t remaining_bits_after_last_actual_dot = 0; uint32_t tmp = 0; for( int32_t remaining_bits = 59; remaining_bits >= 4; remaining_bits -= 5 ) { // Note: remaining_bits must remain signed integer @@ -192,14 +195,17 @@ namespace eosio { if( (begin + 13) < begin || (begin + 13) > end ) return begin; auto v = value; - for( auto i = 0; i < 13; ++i, v <<= 5 ) { - if( v == 0 ) return begin; + for( auto i = 0; i < 13; ++i, v <<= 5 ) { + if( v == 0 ) { + *begin = '\0'; + return begin; + } auto indx = (v & mask) >> (i == 12 ? 60 : 59); *begin = charmap[indx]; ++begin; } - + return begin; } diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 6322717f54..0ed0f4a8f7 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,7 +1,7 @@ #include #include -#define NATIVE +#define NATIVE_NAME using namespace eosio; using namespace eosio::native; @@ -10,7 +10,6 @@ using namespace eosio::native; EOSIO_TEST_BEGIN(name_type_test) // ------------ // constructors - REQUIRE_EQUAL( name{}.value, 0ULL ); REQUIRE_EQUAL( name{0ULL}.value, 0ULL ); @@ -56,10 +55,9 @@ EOSIO_TEST_BEGIN(name_type_test) // --------------- // `char_to_value` - char c{'.'}; uint8_t expected_value{0}; // Will increment to the expected correct value in the set [0,32) -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); print_f("Value of expected_value: %", static_cast(expected_value) ); #endif @@ -67,7 +65,7 @@ EOSIO_TEST_BEGIN(name_type_test) ++expected_value; for(c = '1'; c <= '5'; ++c ) { -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); print_f("Value of expected_value: %", static_cast(expected_value) ); #endif @@ -76,7 +74,7 @@ EOSIO_TEST_BEGIN(name_type_test) } for(c = 'a'; c <= 'z'; ++c ) { -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); print_f("Value of expected_value: %", static_cast(expected_value) ); #endif @@ -95,7 +93,6 @@ EOSIO_TEST_BEGIN(name_type_test) // ------ // length - REQUIRE_EQUAL( name{""}.length(), 0 ); REQUIRE_EQUAL( name{"e"}.length(), 1 ); REQUIRE_EQUAL( name{"eo"}.length(), 2 ); @@ -115,115 +112,114 @@ EOSIO_TEST_BEGIN(name_type_test) // ------ // suffix - -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{".eosioaccounj"}.suffix() ); // TODO: interesting print behavior; starts newline sometimes print_f("Value of expected suffix: eosioaccounj\n" ); #endif REQUIRE_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"e.osioaccounj"}.suffix() ); print_f("Value of expected suffix: osioaccounj\n" ); #endif REQUIRE_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eo.sioaccounj"}.suffix() ); print_f("Value of expected suffix: sioaccounj\n" ); #endif REQUIRE_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eos.ioaccounj"}.suffix() ); print_f("Value of expected suffix: ioaccounj\n" ); #endif REQUIRE_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosi.oaccounj"}.suffix() ); print_f("Value of expected suffix: oaccounj\n" ); #endif REQUIRE_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosio.accounj"}.suffix() ); print_f("Value of expected suffix: accounj\n" ); #endif REQUIRE_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosioa.ccounj"}.suffix() ); print_f("Value of expected suffix: ccounj\n" ); #endif REQUIRE_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosioac.counj"}.suffix() ); print_f("Value of expected suffix: counj\n" ); #endif REQUIRE_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosioacc.ounj"}.suffix() ); print_f("Value of expected suffix: ounj\n" ); #endif REQUIRE_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosioacco.unj"}.suffix() ); print_f("Value of expected suffix: unj\n" ); #endif REQUIRE_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosioaccou.nj"}.suffix() ); print_f("Value of expected suffix: nj\n" ); #endif REQUIRE_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eosioaccoun.j"}.suffix() ); print_f("Value of expected suffix: j\n" ); #endif REQUIRE_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ); -#ifdef NATIVE +// Note that this case is ignored because '.' characters at the end of a name are ignored +// Although, it is my goal to make the functions perfect mirrors of eachother evenutally (01/07/2019) +// #ifdef NATIVE_NAME +// print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); +// print_f("Value of expected suffix: \n" ); +// #endif +// eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); + +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"e.o.s.i.o.a.c"}.suffix() ); print_f("Value of expected suffix: c\n" ); #endif REQUIRE_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ); -#ifdef NATIVE +#ifdef NATIVE_NAME print_f("Value of suffix: %\n", name{"eos.ioa.cco"}.suffix() ); print_f("Value of expected suffix: cco\n" ); #endif REQUIRE_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ); -// TODO: fix bug -// #ifdef NATIVE -// print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); -// print_f("Value of expected suffix: \n" ); -// #endif -// eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); - // ------------ // operator raw - REQUIRE_EQUAL( static_cast(name::raw{name{"1"}}), 576460752303423488ULL ); REQUIRE_EQUAL( static_cast(name::raw{name{"5"}}), 2882303761517117440ULL ); REQUIRE_EQUAL( static_cast(name::raw{name{"a"}}), 3458764513820540928ULL ); @@ -252,38 +248,19 @@ EOSIO_TEST_BEGIN(name_type_test) // ------------- // operator bool + // Note that I must be explicit about calling the operator because it is defined as `explicit` + REQUIRE_EQUAL( name{0}.operator bool(), false ); + REQUIRE_EQUAL( name{1}.operator bool(), true ); + REQUIRE_EQUAL( !name{0}, true ); + REQUIRE_EQUAL( !name{1}, false ); - REQUIRE_EQUAL( static_cast(name{"1"}), true ); - REQUIRE_EQUAL( static_cast(name{"5"}), true ); - REQUIRE_EQUAL( static_cast(name{"a"}), true ); - REQUIRE_EQUAL( static_cast(name{"z"}), true ); - - REQUIRE_EQUAL( static_cast(name{"abc"}), true ); - REQUIRE_EQUAL( static_cast(name{"123"}), true ); - - REQUIRE_EQUAL( static_cast(name{".abc"}), true ); - REQUIRE_EQUAL( static_cast(name{".........abc"}), true ); - REQUIRE_EQUAL( static_cast(name{"123."}), true ); - REQUIRE_EQUAL( static_cast(name{"123........."}), true ); - REQUIRE_EQUAL( static_cast(name{".a.b.c.1.2.3."}), true ); - - REQUIRE_EQUAL( static_cast(name{"abc.123"}), true ); - REQUIRE_EQUAL( static_cast(name{"123.abc"}), true ); - - REQUIRE_EQUAL( static_cast(name{"12345abcdefgj"}), true ); - REQUIRE_EQUAL( static_cast(name{"hijklmnopqrsj"}), true ); - REQUIRE_EQUAL( static_cast(name{"tuvwxyz.1234j"}), true ); - - REQUIRE_EQUAL( static_cast(name{"111111111111j"}), true ); - REQUIRE_EQUAL( static_cast(name{"555555555555j"}), true ); - REQUIRE_EQUAL( static_cast(name{"aaaaaaaaaaaaj"}), true ); - REQUIRE_EQUAL( static_cast(name{"zzzzzzzzzzzzj"}), true ); - - REQUIRE_EQUAL( static_cast(name{0}), false ); + REQUIRE_EQUAL( name{""}.operator bool(), false ); + REQUIRE_EQUAL( name{"1"}.operator bool(), true ); + REQUIRE_EQUAL( !name{""}, true ); + REQUIRE_EQUAL( !name{"1"}, false ); // --------------- // write_as_string - char buffer[13]{}; std::string test_str{"1"}; @@ -301,8 +278,7 @@ EOSIO_TEST_BEGIN(name_type_test) name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); - // TODO: fix bug - // Note that '.' characters at the end of a name do not get written to the buffer. + // Note that any '.' characters at the end of a name are ignored name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); @@ -337,7 +313,6 @@ EOSIO_TEST_BEGIN(name_type_test) // --------- // to_string - REQUIRE_EQUAL( name{"1"}.to_string(), "1" ); REQUIRE_EQUAL( name{"5"}.to_string(), "5" ); REQUIRE_EQUAL( name{"a"}.to_string(), "a" ); @@ -346,7 +321,6 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( name{"abc"}.to_string(), "abc" ); REQUIRE_EQUAL( name{"123"}.to_string(), "123" ); - // TODO: fix bug REQUIRE_EQUAL( name{".abc"}.to_string(), ".abc" ); REQUIRE_EQUAL( name{".........abc"}.to_string(), ".........abc" ); REQUIRE_EQUAL( name{"123."}.to_string(), "123" ); @@ -367,7 +341,6 @@ EOSIO_TEST_BEGIN(name_type_test) // ---------- // operator== - REQUIRE_EQUAL( name{"1"} == name{"1"}, true ); REQUIRE_EQUAL( name{"5"} == name{"5"}, true ); REQUIRE_EQUAL( name{"a"} == name{"a"}, true ); @@ -376,7 +349,6 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( name{"abc"} == name{"abc"}, true ); REQUIRE_EQUAL( name{"123"} == name{"123"}, true ); - // TODO: fix bug REQUIRE_EQUAL( name{".abc"} == name{".abc"}, true ); REQUIRE_EQUAL( name{".........abc"} == name{".........abc"}, true ); REQUIRE_EQUAL( name{"123."} == name{"123"}, true ); @@ -397,7 +369,6 @@ EOSIO_TEST_BEGIN(name_type_test) // ---------- // operator!= - REQUIRE_EQUAL( name{"1"} != name{0}, true ); REQUIRE_EQUAL( name{"5"} != name{0}, true ); REQUIRE_EQUAL( name{"a"} != name{0}, true ); @@ -426,7 +397,6 @@ EOSIO_TEST_BEGIN(name_type_test) // --------- // operator< - REQUIRE_EQUAL( name{0} < name{"1"}, true ); REQUIRE_EQUAL( name{0} < name{"5"}, true ); REQUIRE_EQUAL( name{0} < name{"a"}, true ); @@ -455,7 +425,6 @@ EOSIO_TEST_BEGIN(name_type_test) // ------------ // operator""_n - REQUIRE_EQUAL( name{0} == ""_n, true ); REQUIRE_EQUAL( name{"1"} == "1"_n, true ); @@ -466,7 +435,6 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( name{"abc"} == "abc"_n, true ); REQUIRE_EQUAL( name{"123"} == "123"_n, true ); - // TODO: fix bug REQUIRE_EQUAL( name{".abc"} == ".abc"_n, true ); REQUIRE_EQUAL( name{".........abc"} == ".........abc"_n, true ); REQUIRE_EQUAL( name{"123."} == "123."_n, true ); @@ -618,6 +586,5 @@ EOSIO_TEST_END int main(int argc, char** argv) { EOSIO_TEST(name_type_test); - // EOSIO_TEST(is_account_test); return has_failed(); } diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 569426fdba..3c6497c273 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -1,106 +1,249 @@ #include #include +// #define NATIVE_NAME +#define NATIVE_SYMBOL + +using namespace eosio; using namespace eosio::native; +// Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` +EOSIO_TEST_BEGIN(symbol_code_type_test) + // ---------------- + // constructors/raw + REQUIRE_EQUAL( symbol_code{}.raw(), 0ULL ); + + REQUIRE_EQUAL( symbol_code{0ULL}.raw(), 0ULL ); + REQUIRE_EQUAL( symbol_code{1ULL}.raw(), 1ULL ); + REQUIRE_EQUAL( symbol_code{18446744073709551615ULL}.raw(), 18446744073709551615ULL ); + + REQUIRE_EQUAL( symbol_code{"A"}.raw(), 65ULL ); + REQUIRE_EQUAL( symbol_code{"Z"}.raw(), 90ULL ); + REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ); + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.raw(), 25432092013386330ULL ); + + REQUIRE_ASSERT( "string is too long to be a valid symbol_code", ([]() {symbol_code{"ABCDEFGH"};}) ); + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"a"};}) ); + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"z"};}) ); + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ); + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ); + + // -------- + // is_valid + REQUIRE_EQUAL( symbol_code{65ULL}.is_valid(), true ); // "A" + REQUIRE_EQUAL( symbol_code{90ULL}.is_valid(), true ); // "Z" + REQUIRE_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ); // "AAAAAAA" + REQUIRE_EQUAL( symbol_code{25432092013386330ULL}.is_valid(), true ); // "ZZZZZZZ" + + REQUIRE_EQUAL( symbol_code{64ULL}.is_valid(), false ); + REQUIRE_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ); + + // ------ + // length + REQUIRE_EQUAL( symbol_code{""}.length(), 0 ); + REQUIRE_EQUAL( symbol_code{"S"}.length(), 1 ); + REQUIRE_EQUAL( symbol_code{"SY"}.length(), 2 ); + REQUIRE_EQUAL( symbol_code{"SYM"}.length(), 3 ); + REQUIRE_EQUAL( symbol_code{"SYMB"}.length(), 4 ); + REQUIRE_EQUAL( symbol_code{"SYMBO"}.length(), 5 ); + REQUIRE_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ); + REQUIRE_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ); + + // ------------- + // operator bool + // Note that I must be explicit about calling the operator because it is defined as `explicit` + REQUIRE_EQUAL( symbol_code{0}.operator bool(), false ); + REQUIRE_EQUAL( symbol_code{1}.operator bool(), true ); + REQUIRE_EQUAL( !symbol_code{0}, true ); + REQUIRE_EQUAL( !symbol_code{1}, false ); + + REQUIRE_EQUAL( symbol_code{""}.operator bool(), false ); + REQUIRE_EQUAL( symbol_code{"SYMBOL"}.operator bool(), true ); + REQUIRE_EQUAL( !symbol_code{""}, true ); + REQUIRE_EQUAL( !symbol_code{"SYMBOL"}, false ); + + // --------------- + // write_as_string + // TODO: rename when moved + char buffer1[7]{}; + + // TODO: rename when moved + std::string test_str1{"A"}; + symbol_code{test_str1}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); +print_f("%\n",test_str1); +print_f("%\n",buffer1); + REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); + symbol_code{test_str1 = "Z"}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); +print_f("%\n",test_str1); +print_f("%\n",buffer1); + REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); + symbol_code{test_str1 = "AAAAAAA"}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); +print_f("%\n",test_str1); +print_f("%\n",buffer1); + REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); + symbol_code{test_str1 = "ZZZZZZZ"}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); +print_f("%\n",test_str1); +print_f("%\n",buffer1); + REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); + + // --------- + // to_string + REQUIRE_EQUAL( symbol_code{"A"}.to_string(), "A" ); + REQUIRE_EQUAL( symbol_code{"Z"}.to_string(), "Z" ); + REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ); + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ); + + // ---------- + // operator== + REQUIRE_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ); + REQUIRE_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ); + REQUIRE_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ); + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ); + + // ---------- + // operator!= + REQUIRE_EQUAL( symbol_code{"A"} != symbol_code{0}, true ); + REQUIRE_EQUAL( symbol_code{"Z"} != symbol_code{0}, true ); + REQUIRE_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{0}, true ); + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{0}, true ); + + // --------- + // operator< + REQUIRE_EQUAL( symbol_code{0} < symbol_code{"A"}, true ); + REQUIRE_EQUAL( symbol_code{0} < symbol_code{"Z"}, true ); + REQUIRE_EQUAL( symbol_code{0} < symbol_code{"AAAAAAA"}, true ); + REQUIRE_EQUAL( symbol_code{0} < symbol_code{"ZZZZZZZ"}, true ); +EOSIO_TEST_END + // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_type_test) - eosio_assert( symbol_code{}.raw() == 0ULL, "symbol_code != 0ULL" ); - eosio_assert( symbol_code{0ULL}.raw() == 0ULL, "symbol_code != 0ULL" ); - eosio_assert( symbol_code{1ULL}.raw() == 1ULL, "symbol_code != 1ULL" ); - eosio_assert( symbol_code{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol_code != 18446744073709551615ULL" ); - - eosio_assert( symbol_code{"A"}.raw() == 65ULL, "symbol_code != 65ULL" ); - eosio_assert( symbol_code{"Z"}.raw() == 90ULL, "symbol_code != 90ULL" ); - eosio_assert( symbol_code{"AAAAAAA"}.raw() == 18367622009667905ULL, "symbol_code != 18367622009667905ULL" ); - eosio_assert( symbol_code{"ZZZZZZZ"}.raw() == 25432092013386330ULL, "symbol_code != 25432092013386330ULL" ); - - eosio_assert( symbol{0ULL}.value == 0ULL, "symbol != 0ULL" ); - eosio_assert( symbol{1ULL}.value == 1ULL, "symbol != 1ULL" ); - eosio_assert( symbol{18446744073709551615ULL}.value == 18446744073709551615ULL, "symbol != 18446744073709551615ULL" ); - - eosio_assert( symbol{"1"}.value == 576460752303423488ULL, "symbol != 1" ); - eosio_assert( symbol{"5"}.value == 2882303761517117440ULL, "symbol != 5" ); - eosio_assert( symbol{"a"}.value == 3458764513820540928ULL, "symbol != a" ); - eosio_assert( symbol{"z"}.value == 17870283321406128128ULL, "symbol != z" ); - - eosio_assert( symbol{"abc"}.value == 3589368903014285312ULL, "symbol != abc" ); - eosio_assert( symbol{"123"}.value == 614178399182651392ULL, "symbol != 123" ); - - eosio_assert( symbol{".abc"}.value == 112167778219196416ULL, "symbol != .abc" ); - eosio_assert( symbol{"123."}.value == 614178399182651392ULL, "symbol != 123." ); + // ------------ + // constructors/raw + REQUIRE_EQUAL( symbol{}.raw(), 0ULL ); + + REQUIRE_EQUAL( symbol{0ULL}.raw(), 0ULL ); + REQUIRE_EQUAL( symbol{1ULL}.raw(), 1ULL ); + REQUIRE_EQUAL( symbol{18446744073709551615ULL}.raw(), 18446744073709551615ULL ); + + // Note that unless constructed with `initializer_list`, precision does not check for wrap-around + REQUIRE_EQUAL( (symbol{"A", 0}.raw()), 16640ULL ); + REQUIRE_EQUAL( (symbol{"Z", 0}.raw()), 23040ULL ); + REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.raw()), 4702111234474983680ULL ); + REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.raw()), 6510615555426900480ULL ); + + REQUIRE_EQUAL( (symbol{symbol_code{"A"}, 0}.raw()), 16640ULL ); + REQUIRE_EQUAL( (symbol{symbol_code{"Z"}, 0}.raw()), 23040ULL ); + REQUIRE_EQUAL( (symbol{symbol_code{"AAAAAAA"}, 0}.raw()), 4702111234474983680ULL ); + REQUIRE_EQUAL( (symbol{symbol_code{"ZZZZZZZ"}, 0}.raw()), 6510615555426900480ULL ); + +print_f("%",symbol{"A", 0}.raw()); +print_f("%",symbol{"Z", 0}.raw()); +print_f("%",symbol{"AAAAAAA", 0}.raw()); +print_f("%",symbol{"ZZZZZZZ", 0}.raw()); +print_f("%",symbol{"ZZZZZZZ", 255}.raw()); + + // -------- + // is_valid + REQUIRE_EQUAL( symbol{16640ULL}.is_valid(), true ); // "A", precision: 0 + REQUIRE_EQUAL( symbol{23040ULL}.is_valid(), true ); // "Z", precision: 0 + REQUIRE_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ); // "AAAAAAA", precision: 0 + REQUIRE_EQUAL( symbol{6510615555426900480ULL}.is_valid(), true ); // "ZZZZZZZ", precision: 0 + + REQUIRE_EQUAL( symbol{16639ULL}.is_valid(), false ); + REQUIRE_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ); + + // --------- + // precision + REQUIRE_EQUAL( (symbol{"A", 0}.precision()), 0 ); + REQUIRE_EQUAL( (symbol{"Z", 0}.precision()), 0 ); + REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.precision()), 0 ); + REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.precision()), 0 ); + + REQUIRE_EQUAL( (symbol{"A", 255}.precision()), 255 ); + REQUIRE_EQUAL( (symbol{"Z", 255}.precision()), 255 ); + REQUIRE_EQUAL( (symbol{"AAAAAAA", 255}.precision()), 255 ); + REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 255}.precision()), 255 ); + + // ---- + // code + REQUIRE_EQUAL( (symbol{"A", 0}.code()), symbol_code{"A"} ); + REQUIRE_EQUAL( (symbol{"Z", 0}.code()), symbol_code{"Z"} ); + REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.code()), symbol_code{"AAAAAAA"} ); + REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.code()), symbol_code{"ZZZZZZZ"} ); + + // ------------- + // operator bool + // Note that I must be explicit about calling the operator because it is defined as `explicit` + REQUIRE_EQUAL( symbol{0}.operator bool(), false ); + REQUIRE_EQUAL( symbol{1}.operator bool(), true ); + REQUIRE_EQUAL( !symbol{0}, true ); + REQUIRE_EQUAL( !symbol{1}, false ); + + REQUIRE_EQUAL( (symbol{"", 0}.operator bool()), false ); + REQUIRE_EQUAL( (symbol{"SYMBOL", 0}.operator bool()), true ); + REQUIRE_EQUAL( (!symbol{"", 0}), true ); + REQUIRE_EQUAL( (!symbol{"SYMBOL", 0}), false ); + + // ----- + // print - eosio_assert( symbol{"abc.123"}.value == 3589369488740450304ULL, "symbol != abc.123" ); - eosio_assert( symbol{"123.abc"}.value == 614181822271586304ULL, "symbol != 123.abc" ); - - eosio_assert( symbol{"12345abcdefgj"}.value == 614251623682315983ULL, "symbol != 12345abcdefgj" ); - eosio_assert( symbol{"hijklmnopqrsj"}.value == 7754926748989239183ULL, "symbol != hijklmnopqrsj" ); - eosio_assert( symbol{"tuvwxyz.1234j"}.value == 14895601873741973071ULL, "symbol != tuvwxyz.1234j" ); - - eosio_assert( symbol{"111111111111j"}.value == 595056260442243615ULL, "symbol != 111111111111j" ); - eosio_assert( symbol{"555555555555j"}.value == 2975281302211218015ULL, "symbol != 555555555555j" ); - eosio_assert( symbol{"aaaaaaaaaaaaj"}.value == 3570337562653461615ULL, "symbol != aaaaaaaaaaaaj" ); - eosio_assert( symbol{"zzzzzzzzzzzzj"}.value == 18446744073709551615ULL, "symbol != zzzzzzzzzzzzj" ); - REQUIRE_ASSERT( "character is not in allowed character set for symbols", ([]() {symbol{"-1"}.value;}) ); + // ---------- + // operator== + REQUIRE_EQUAL( (symbol{"A", 0} == symbol{"A", 0}), true ); + REQUIRE_EQUAL( (symbol{"Z", 0} == symbol{"Z", 0}), true ); + REQUIRE_EQUAL( (symbol{"AAAAAAA", 0} == symbol{"AAAAAAA", 0}), true ); + REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0} == symbol{"ZZZZZZZ", 0}), true ); + + // ---------- + // operator!= + REQUIRE_EQUAL( (symbol{"A",0} != symbol{0}), true ); + REQUIRE_EQUAL( (symbol{"Z",0} != symbol{0}), true ); + REQUIRE_EQUAL( (symbol{"AAAAAAA",0} != symbol{0}), true ); + REQUIRE_EQUAL( (symbol{"ZZZZZZZ",0} != symbol{0}), true ); + + // --------- + // operator< + REQUIRE_EQUAL( (symbol{0} < symbol{"A",0}), true ); + REQUIRE_EQUAL( (symbol{0} < symbol{"Z",0}), true ); + REQUIRE_EQUAL( (symbol{0} < symbol{"AAAAAAA",0}), true ); + REQUIRE_EQUAL( (symbol{0} < symbol{"ZZZZZZZ",0}), true ); +EOSIO_TEST_END - REQUIRE_ASSERT( "character is not in allowed character set for symbols", ([]() {symbol{"0"}.value;}) ); +// Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` +EOSIO_TEST_BEGIN(extended_symbol_type_test) + // ------------ + // constructors + + + // ---------- + // get_symbol + - REQUIRE_ASSERT( "character is not in allowed character set for symbols", ([]() {symbol{"6"}.value;}) ); + // ------------ + // get_contract + - REQUIRE_ASSERT( "thirteenth character in symbol cannot be a letter that comes after j", ([]() {symbol{"111111111111k"}.value;}) ); + // ----- + // print + - REQUIRE_ASSERT( "thirteenth character in symbol cannot be a letter that comes after j", ([]() {symbol{"zzzzzzzzzzzzk"}.value;}) ); + // ---------- + // operator== + - REQUIRE_ASSERT( "the length must not be longer than 13 characters in an `eosio::symbol`", ([]() {symbol{"12345abcdefghj"}.value;}) ); + // ---------- + // operator!= + - silence_output(false); + // --------- + // operator< + EOSIO_TEST_END int main(int argc, char** argv) { + // EOSIO_TEST(symbol_code_type_test); EOSIO_TEST(symbol_type_test); + // EOSIO_TEST(extended_symbol_type_test); return has_failed(); } - -// eosio_assert( symbol_code{}.raw() == 0ULL, "symbol_code != 0ULL" ); -// eosio_assert( symbol_code{0ULL}.raw() == 0ULL, "symbol_code != 0ULL" ); -// eosio_assert( symbol_code{1ULL}.raw() == 1ULL, "symbol_code != 1ULL" ); -// eosio_assert( symbol_code{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol_code != 18446744073709551615ULL" ); - -// eosio_assert( symbol_code{"A"}.raw() == 65ULL, "symbol_code != 65ULL" ); -// eosio_assert( symbol_code{"Z"}.raw() == 90ULL, "symbol_code != 90ULL" ); -// eosio_assert( symbol_code{"AAAAAAA"}.raw() == 18367622009667905ULL, "symbol_code != 18367622009667905ULL" ); -// eosio_assert( symbol_code{"ZZZZZZZ"}.raw() == 25432092013386330ULL, "symbol_code != 25432092013386330ULL" ); - -// symbol_code s{"ZZZZZZZ"}; -// eosio::print_f("%\n",s.raw()); - -// eosio_assert( symbol{}.raw() == 0ULL, "symbol != 0ULL" ); -// eosio_assert( symbol{0ULL}.raw() == 0ULL, "symbol != 0ULL" ); -// eosio_assert( symbol{1ULL}.raw() == 1ULL, "symbol != 1ULL" ); -// eosio_assert( symbol{18446744073709551615ULL}.raw() == 18446744073709551615ULL, "symbol != 18446744073709551615ULL" ); - -// eosio_assert( symbol{1}.raw() == 576460752303423488ULL, "symbol != 1" ); -// eosio_assert( symbol{5}.raw() == 2882303761517117440ULL, "symbol != 5" ); -// eosio_assert( symbol{"a"}.raw() == 3458764513820540928ULL, "symbol != a" ); -// eosio_assert( symbol{"z"}.raw() == 17870283321406128128ULL, "symbol != z" ); - -// eosio_assert( symbol{"abc"}.raw() == 3589368903014285312ULL, "symbol != abc" ); -// eosio_assert( symbol{"123"}.raw() == 614178399182651392ULL, "symbol != 123" ); - -// eosio_assert( symbol{".abc"}.raw() == 112167778219196416ULL, "symbol != .abc" ); -// eosio_assert( symbol{"123."}.raw() == 614178399182651392ULL, "symbol != 123." ); - -// eosio_assert( symbol{"abc.123"}.raw() == 3589369488740450304ULL, "symbol != abc.123" ); -// eosio_assert( symbol{"123.abc"}.raw() == 614181822271586304ULL, "symbol != 123.abc" ); - -// eosio_assert( symbol{"12345abcdefgj"}.raw() == 614251623682315983ULL, "symbol != 12345abcdefgj" ); -// eosio_assert( symbol{"hijklmnopqrsj"}.raw() == 7754926748989239183ULL, "symbol != hijklmnopqrsj" ); -// eosio_assert( symbol{"tuvwxyz.1234j"}.raw() == 14895601873741973071ULL, "symbol != tuvwxyz.1234j" ); - -// eosio_assert( symbol{"111111111111j"}.raw() == 595056260442243615ULL, "symbol != 111111111111j" ); -// eosio_assert( symbol{"555555555555j"}.raw() == 2975281302211218015ULL, "symbol != 555555555555j" ); -// eosio_assert( symbol{"aaaaaaaaaaaaj"}.raw() == 3570337562653461615ULL, "symbol != aaaaaaaaaaaaj" ); -// eosio_assert( symbol{"zzzzzzzzzzzzj"}.raw() == 18446744073709551615ULL, "symbol != zzzzzzzzzzzzj" ); -// eosio::symbol n(18446744073709551615ULL); -// eosio::print_f("%\n",n.raw()); From a536473915656cbc2f0f6963aee6189f7424ab51 Mon Sep 17 00:00:00 2001 From: johndebord Date: Tue, 8 Jan 2019 18:04:14 -0500 Subject: [PATCH 06/51] Start `asset` type tests --- tests/unit/asset_tests.cpp | 153 ++++++++++++++++++++++++++++++++++++ tests/unit/name_tests.cpp | 6 +- tests/unit/symbol_tests.cpp | 116 ++++++++++++++++++--------- 3 files changed, 233 insertions(+), 42 deletions(-) create mode 100644 tests/unit/asset_tests.cpp diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp new file mode 100644 index 0000000000..772a2332c8 --- /dev/null +++ b/tests/unit/asset_tests.cpp @@ -0,0 +1,153 @@ +#include +#include + +#include + +#define NATIVE_NAME +#define NATIVE_SYMBOL +#define NATIVE_ASSET + +using namespace eosio; +using namespace eosio::native; + +// Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` +EOSIO_TEST_BEGIN(asset_type_test) + // -------------------------------------------- + // constructors/is_amount_within_range/is_valid + REQUIRE_EQUAL( asset{}.amount, 0ULL ); + REQUIRE_EQUAL( asset{}.symbol.raw(), 0ULL ); + + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.amount), 0 ); + REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL",0}}.amount), 4611686018427387903LL ); + REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL",0}}.amount), -4611686018427387903LL ); + + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"A",0}}.symbol.raw()), 16640ULL ); // "A", precision: 0 + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"Z",0}}.symbol.raw()), 23040ULL ); // "Z", precision: 0 + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"AAAAAAA",0}}.symbol.raw()), 4702111234474983680ULL ); // "AAAAAAA", precision: 0 + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"ZZZZZZZ",0}}.symbol.raw()), 6510615555426900480ULL ); // "ZZZZZZZ", precision: 0 + + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{int64_t{}, symbol{16639ULL}};}) ); + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{int64_t{}, symbol{6510615555426900736ULL}};}) ); + + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{int64_t{4611686018427387904LL}, symbol{}};}) ); + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{int64_t{-4611686018427387904LL}, symbol{}};}) ); + + // ---------- + // set_amount + // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(0LL)), 0LL); + // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(4611686018427387903LL)), 4611686018427387903LL); + // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(-4611686018427387903LL)), -4611686018427387903LL); + + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( + []() {asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(4611686018427387904LL);}) ); + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( + []() {asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(-4611686018427387904LL);}) ); + + // --------- + // to_string + asset{int64_t{0}, symbol{"SYMBOLL",0}}.print(); +print_f("\n"); + asset{int64_t{10}, symbol{"A",1}}.print(); +print_f("\n"); + asset{int64_t{10}, symbol{"Z",1}}.print(); +print_f("\n"); + asset{int64_t{10}, symbol{"AAAAAAA",1}}.print(); +print_f("\n"); + asset{int64_t{10}, symbol{"ZZZZZZZ",1}}.print(); +print_f("\n"); + + // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"A",0}}.symbol.raw()), 16640ULL ); // "A", precision: 0 + // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"Z",0}}.symbol.raw()), 23040ULL ); // "Z", precision: 0 + // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"AAAAAAA",0}}.symbol.raw()), 4702111234474983680ULL ); // "AAAAAAA", precision: 0 + // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"ZZZZZZZ",0}}.symbol.raw()), 6510615555426900480ULL ); // "ZZZZZZZ", precision: 0 + + // ----- + // print + + // --------- + // operator- + + // ---------- + // operator-= + + // ---------- + // operator+= + + // ---------- + // operator*= + + // ---------- + // operator/= + + // --------- + // operator- + + // --------- + // operator+ + + // --------- + // operator* + + // --------- + // operator* + + // --------- + // operator/ + + // --------- + // operator/ + + // ---------- + // operator== + + // ---------- + // operator!= + + // --------- + // operator< + + // ---------- + // operator<= + + // --------- + // operator> + + // ---------- + // operator>= +EOSIO_TEST_END + +// Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` +EOSIO_TEST_BEGIN(extended_asset_type_test) + // constructors + + // get_extended_symbol + + // print + + // operator- + + // operator- + + // operator+ + + // operator+= + + // operator-= + + // operator< + + // operator== + + // operator!= + + // operator<= + + // operator>= + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char** argv) { + EOSIO_TEST(asset_type_test); + return has_failed(); +} diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 0ed0f4a8f7..4631b33b08 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -2,6 +2,8 @@ #include #define NATIVE_NAME +#define NATIVE_SYMBOL +#define NATIVE_ASSET using namespace eosio; using namespace eosio::native; @@ -456,9 +458,6 @@ EOSIO_TEST_BEGIN(name_type_test) silence_output(false); EOSIO_TEST_END -EOSIO_TEST_BEGIN(name_type_methods_test) -EOSIO_TEST_END - // // Intrinsics defined in `eos/libraries/chain/wasm_interface.cpp` that involve the `name` type // // Defined in `eos/libraries/chain/wasm_interface.cpp` // EOSIO_TEST_BEGIN(set_resource_limits_intrinsic_test) @@ -581,7 +580,6 @@ EOSIO_TEST_END // CHECK_EQUAL(is_account(4), true); // CHECK_EQUAL(is_account(6), true); // REQUIRE_EQUAL(is_account(7), true); - // EOSIO_TEST_END int main(int argc, char** argv) { diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 3c6497c273..0bff5621e2 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -1,8 +1,9 @@ #include #include -// #define NATIVE_NAME +#define NATIVE_NAME #define NATIVE_SYMBOL +#define NATIVE_ASSET using namespace eosio; using namespace eosio::native; @@ -64,27 +65,25 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) // --------------- // write_as_string - // TODO: rename when moved - char buffer1[7]{}; - - // TODO: rename when moved - std::string test_str1{"A"}; - symbol_code{test_str1}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); -print_f("%\n",test_str1); -print_f("%\n",buffer1); - REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); - symbol_code{test_str1 = "Z"}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); -print_f("%\n",test_str1); -print_f("%\n",buffer1); - REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); - symbol_code{test_str1 = "AAAAAAA"}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); -print_f("%\n",test_str1); -print_f("%\n",buffer1); - REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); - symbol_code{test_str1 = "ZZZZZZZ"}.write_as_string( buffer1, buffer1 + sizeof(buffer1) ); -print_f("%\n",test_str1); -print_f("%\n",buffer1); - REQUIRE_EQUAL( memcmp(test_str1.c_str(), buffer1, strlen(test_str1.c_str())), 0 ); + char buffer[7]{}; + + std::string test_str{"A"}; + symbol_code{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); +print_f("%\n",test_str); +print_f("%\n",buffer); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + symbol_code{test_str = "Z"}.write_as_string( buffer, buffer + sizeof(buffer) ); +print_f("%\n",test_str); +print_f("%\n",buffer); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + symbol_code{test_str = "AAAAAAA"}.write_as_string( buffer, buffer + sizeof(buffer) ); +print_f("%\n",test_str); +print_f("%\n",buffer); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + symbol_code{test_str = "ZZZZZZZ"}.write_as_string( buffer, buffer + sizeof(buffer) ); +print_f("%\n",test_str); +print_f("%\n",buffer); + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); // --------- // to_string @@ -186,7 +185,17 @@ print_f("%",symbol{"ZZZZZZZ", 255}.raw()); // ----- // print - +// symbol{"A", 0}.print(true); +// symbol{"AAAAAAA", 255}.print(true); + // REQUIRE_PRINT( "0,A", [](){symbol{"A", 0}.print(true);} ); + // REQUIRE_PRINT( "0,Z", [](){symbol{"Z", 0}.print(true);} ); + // REQUIRE_PRINT( "255,AAAAAAA", [](){symbol{"AAAAAAA", 255}.print(true);} ); + // REQUIRE_PRINT( "255,ZZZZZZZ", [](){symbol{"ZZZZZZZ", 255}.print(true);} ); + + // REQUIRE_PRINT( ",A", [](){symbol{"A", 0}.print(false);} ); + // REQUIRE_PRINT( ",Z", [](){symbol{"Z", 0}.print(false);} ); + // REQUIRE_PRINT( ",AAAAAAA", [](){symbol{"AAAAAAA", 255}.print(false);} ); + // REQUIRE_PRINT( ",ZZZZZZZ", [](){symbol{"ZZZZZZZ", 255}.print(false);} ); // ---------- // operator== @@ -213,37 +222,68 @@ EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(extended_symbol_type_test) // ------------ - // constructors - - - // ---------- - // get_symbol - - - // ------------ - // get_contract + // constructors/get_symbol/get_contract + REQUIRE_EQUAL( (extended_symbol{symbol{}, name{}}.get_symbol().raw()), 0ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{}, name{}}.get_contract().value), 0ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"1"}}.get_symbol().raw()), 16640ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"5"}}.get_symbol().code().raw()), 65ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"a"}}.get_symbol().raw()), 23040ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"z"}}.get_symbol().code().raw()), 90ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"1"}}.get_contract().value), 576460752303423488ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"5"}}.get_contract().value), 2882303761517117440ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"a"}}.get_contract().value), 3458764513820540928ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"z"}}.get_contract().value), 17870283321406128128ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"111111111111j"}}.get_symbol().raw()), 4702111234474983680ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"555555555555j"}}.get_symbol().code().raw()), 18367622009667905ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"aaaaaaaaaaaaj"}}.get_symbol().raw()), 6510615555426900480ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"zzzzzzzzzzzzj"}}.get_symbol().code().raw()), 25432092013386330ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"111111111111j"}}.get_contract().value), 595056260442243615ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"555555555555j"}}.get_contract().value), 2975281302211218015ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"aaaaaaaaaaaaj"}}.get_contract().value), 3570337562653461615ULL ); + REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"zzzzzzzzzzzzj"}}.get_contract().value), 18446744073709551615ULL ); // ----- // print - +// extended_symbol{symbol{"A", 0}, name{"1"}}.print(true); +// extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true); + // REQUIRE_PRINT( "0@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(true);} ); + // REQUIRE_PRINT( "0@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(true);} ); + // REQUIRE_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); + // REQUIRE_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); + + // REQUIRE_PRINT( "@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); + // REQUIRE_PRINT( "@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); + // REQUIRE_PRINT( "@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); + // REQUIRE_PRINT( "@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); // ---------- // operator== - + REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{0}} == extended_symbol{symbol{"A", 0}, name{}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{0}} == extended_symbol{symbol{"Z", 0}, name{}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{0}} == extended_symbol{symbol{"AAAAAAA", 0}, name{}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}} == extended_symbol{symbol{"ZZZZZZZ", 0}, name{}}), true ); // ---------- // operator!= - + REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); // --------- // operator< - + REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"A", 0}, name{0}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"Z", 0}, name{0}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"AAAAAAA", 0}, name{0}}), true ); + REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}}), true ); + + silence_output(false); EOSIO_TEST_END int main(int argc, char** argv) { - // EOSIO_TEST(symbol_code_type_test); + EOSIO_TEST(symbol_code_type_test); EOSIO_TEST(symbol_type_test); - // EOSIO_TEST(extended_symbol_type_test); + EOSIO_TEST(extended_symbol_type_test); return has_failed(); } From dfa7b9d37aac05c6cadc693670d828f200315de1 Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 9 Jan 2019 18:20:15 -0500 Subject: [PATCH 07/51] Continuation of `asset` type tests --- tests/unit/asset_tests.cpp | 72 ++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 772a2332c8..5eda3280e4 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -10,6 +10,11 @@ using namespace eosio; using namespace eosio::native; +// For incremented the `precision` in any test relating to `precision` +// The `precision` field in the `symbol` type must be constructed via constexpr, +// therefore a constexpr function is needed to dynamically test `precision` +constexpr uint8_t increment_precision( uint8_t precision ) { return ++precision; } + // Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) // -------------------------------------------- @@ -34,9 +39,11 @@ EOSIO_TEST_BEGIN(asset_type_test) // ---------- // set_amount - // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(0LL)), 0LL); - // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(4611686018427387903LL)), 4611686018427387903LL); - // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(-4611686018427387903LL)), -4611686018427387903LL); + asset a{int64_t{0}, symbol{"SYMBOLL", 1}}; + REQUIRE_EQUAL( (a.set_amount(0), a.amount), 0); + REQUIRE_EQUAL( (a.set_amount(1LL), a.amount), 1LL); + REQUIRE_EQUAL( (a.set_amount(4611686018427387903LL), a.amount), 4611686018427387903LL); + REQUIRE_EQUAL( (a.set_amount(-4611686018427387903LL), a.amount), -4611686018427387903LL); REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( []() {asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(4611686018427387904LL);}) ); @@ -45,24 +52,53 @@ EOSIO_TEST_BEGIN(asset_type_test) // --------- // to_string - asset{int64_t{0}, symbol{"SYMBOLL",0}}.print(); -print_f("\n"); - asset{int64_t{10}, symbol{"A",1}}.print(); -print_f("\n"); - asset{int64_t{10}, symbol{"Z",1}}.print(); -print_f("\n"); - asset{int64_t{10}, symbol{"AAAAAAA",1}}.print(); -print_f("\n"); - asset{int64_t{10}, symbol{"ZZZZZZZ",1}}.print(); -print_f("\n"); - - // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"A",0}}.symbol.raw()), 16640ULL ); // "A", precision: 0 - // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"Z",0}}.symbol.raw()), 23040ULL ); // "Z", precision: 0 - // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"AAAAAAA",0}}.symbol.raw()), 4702111234474983680ULL ); // "AAAAAAA", precision: 0 - // REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"ZZZZZZZ",0}}.symbol.raw()), 6510615555426900480ULL ); // "ZZZZZZZ", precision: 0 + // Note that printing an `asset` is limited to a precision of 63 + // This will trigger an error: + // `asset{int64_t{1LL}, symbol{"SYMBOLL", 64}}.print();` + // output: "Floating point exception: ..." + + // This produces weird behavior when the `precision` is 0: + // `asset{int64_t{1LL}, symbol{"SYMBOLL", precision}}.print();` + // output: "1. SYMBOLL" + + // Note that `assets` with negative amounts with 0 `precision` print have the odd behavior of printing two '-' characters: + REQUIRE_EQUAL( (asset{int64_t{0LL}, symbol{"SYMBOLL", 0}}.to_string()), + "0. SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{0LL}, symbol{"SYMBOLL", 63}}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); + + REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.to_string()), + "1. SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}.to_string()), + "--1. SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}.to_string()), + "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ); + + REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.to_string()), + "4611686018427387903. SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.to_string()), + "--4611686018427387903. SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}.to_string()), + "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}.to_string()), + "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); + + // How are these `assets` constructed? Isn't `uint8_t precision` supposed to be constexpr when constructing a symbol? + for( uint8_t precision{0}; precision < 64; ++precision ) { + REQUIRE_EQUAL( (asset{int64_t{0LL}, symbol{"SYMBOLL", precision}}.to_string()), + (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ); // ----- // print +// extended_symbol{symbol{"A", 0}, name{"1"}}.print(true); +// extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true); + // REQUIRE_PRINT( "0@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(true);} ); + // REQUIRE_PRINT( "0@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(true);} ); + // REQUIRE_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); + // REQUIRE_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); + // --------- // operator- From 09c82222b523802ed23fecb1dc817455837582b5 Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 11 Jan 2019 15:42:55 -0500 Subject: [PATCH 08/51] Continuation of `asset` type tests --- libraries/eosiolib/name.hpp | 157 +++++++------- libraries/eosiolib/symbol.hpp | 4 +- tests/unit/asset_tests.cpp | 392 +++++++++++++++++++++++++++------- tests/unit/name_tests.cpp | 158 +++----------- tests/unit/symbol_tests.cpp | 122 +++++------ 5 files changed, 481 insertions(+), 352 deletions(-) diff --git a/libraries/eosiolib/name.hpp b/libraries/eosiolib/name.hpp index 17ecae7f12..1f79161d54 100644 --- a/libraries/eosiolib/name.hpp +++ b/libraries/eosiolib/name.hpp @@ -30,10 +30,12 @@ namespace eosio { /** * Construct a new name * - * @brief Construct a new name object defaulting to a value of 0 + * @brief Construct a new name object with a default value of 0 * */ - constexpr name() : value(0) {} + constexpr name() + : value{} + {} /** * Construct a new name given a unit64_t value @@ -42,50 +44,49 @@ namespace eosio { * @param v - The unit64_t value * */ - constexpr explicit name( uint64_t v ) - :value(v) + constexpr explicit name( uint64_t val ) + : value{val} {} /** - * Construct a new name given a scoped enumerated type of raw (uint64_t). + * Construct a new name with a scoped enumerated type of raw uint64_t. * - * @brief Construct a new name object initialising value with r + * @brief Construct a new name object, initialising value with r * @param r - The raw value which is a scoped enumerated type of unit64_t * */ constexpr explicit name( name::raw r ) - :value(static_cast(r)) + : value(static_cast(r)) {} /** * Construct a new name given an string. * - * @brief Construct a new name object initialising value with str - * @param str - The string value which validated then converted to unit64_t + * @brief Construct a new name object, initialising value with str + * @param str - The string value which validates and converts to a unit64_t * */ constexpr explicit name( std::string_view str ) - :value(0) + : value{0} { - if( str.size() > 13 ) { + if( 13 < str.size() ) eosio::check( false, "string is too long to be a valid name" ); - } - if( str.empty() ) { + if( str.empty() ) return; - } - auto n = std::min( (uint32_t)str.size(), (uint32_t)12u ); - for( decltype(n) i = 0; i < n; ++i ) { + uint8_t length = (12 < str.size()) ? 12 : str.size(); + + for( decltype(length) i = 0; i < length; ++i ) { value <<= 5; value |= char_to_value( str[i] ); } - value <<= ( 4 + 5*(12 - n) ); + value <<= ( 4 + 5*(12 - length) ); + if( str.size() == 13 ) { - uint64_t v = char_to_value( str[12] ); - if( v > 0x0Full ) { + uint64_t val = char_to_value( str[12] ); + if( 0x0FULL < val ) eosio::check(false, "thirteenth character in name cannot be a letter that comes after j"); - } - value |= v; + value |= val; } } @@ -95,16 +96,20 @@ namespace eosio { * @param c - Character to be converted * @return constexpr char - Converted value */ - static constexpr uint8_t char_to_value( char c ) { - if( c == '.') - return 0; - else if( c >= '1' && c <= '5' ) - return (c - '1') + 1; - else if( c >= 'a' && c <= 'z' ) - return (c - 'a') + 6; - else - eosio::check( false, "character is not in allowed character set for names" ); - + static constexpr uint8_t char_to_value( char ch ) { + switch(ch) { + case '.': + return 0; + break; + case '1' ... '5': + return (ch - '1') + 1; + break; + case 'a' ... 'z': + return (ch - 'a') + 6; + break; + default: + eosio::check( false, "character is not in allowed character set for names" ); + } return 0; // control flow will never reach here; just added to suppress warning } @@ -112,54 +117,46 @@ namespace eosio { * Returns the length of the %name */ constexpr uint8_t length()const { - constexpr uint64_t mask = 0xF800000000000000ull; - if( value == 0 ) return 0; - - uint8_t l = 0; - uint8_t i = 0; - for( auto v = value; i < 13; ++i, v <<= 5 ) { - if( (v & mask) > 0 ) { - l = i; - } - } - - return l + 1; + + constexpr uint64_t mask{0xF800000000000000ULL}; + + uint64_t val{value}; + uint8_t length{}; + for( uint8_t i{}; i < 13; val <<= 5, ++i ) + if( 0 < (val & mask) ) + length = i; + return length + 1; } /** * Returns the suffix of the %name */ constexpr name suffix()const { - // if((value >> 56) & 0x1F) - // return name{""}; - - uint32_t remaining_bits_after_last_actual_dot = 0; - uint32_t tmp = 0; - for( int32_t remaining_bits = 59; remaining_bits >= 4; remaining_bits -= 5 ) { // Note: remaining_bits must remain signed integer - // Get characters one-by-one in name in order from left to right (not including the 13th character) - auto c = (value >> remaining_bits) & 0x1Full; - if( !c ) { // if this character is a dot - tmp = static_cast(remaining_bits); - } else { // if this character is not a dot + uint32_t remaining_bits_after_last_actual_dot{}; + uint32_t tmp{}; + for( int32_t remaining_bits{59}; 4 <= remaining_bits; remaining_bits -= 5 ) { // Remaining_bits must remain signed integer + // Get characters in name, one-by-one, in order from left to right (not including the 13th character) + uint8_t ch = (value >> remaining_bits) & 0x1FULL; + if( ch ) // if this character is not a dot remaining_bits_after_last_actual_dot = tmp; - } + else // if this character is a dot + tmp = static_cast(remaining_bits); } - uint64_t thirteenth_character = value & 0x0Full; - if( thirteenth_character ) { // if 13th character is not a dot + uint64_t thirteenth_character{value & 0x0FULL}; + if( thirteenth_character ) // if 13th character is not a dot remaining_bits_after_last_actual_dot = tmp; - } if( remaining_bits_after_last_actual_dot == 0 ) // there is no actual dot in the %name other than potentially leading dots return name{value}; - // At this point remaining_bits_after_last_actual_dot has to be within the range of 4 to 59 (and restricted to increments of 5). + // At this point remaining_bits_after_last_actual_dot has to be within the range of 4 to 59 (and restricted to increments of 5) - // Mask for remaining bits corresponding to characters after last actual dot, except for 4 least significant bits (corresponds to 13th character). - uint64_t mask = (1ull << remaining_bits_after_last_actual_dot) - 16; - uint32_t shift = 64 - remaining_bits_after_last_actual_dot; + // Mask for remaining bits corresponding to characters after last actual dot, except for 4 least significant bits (corresponds to 13th character) + uint64_t mask{(1ULL << remaining_bits_after_last_actual_dot) - 16}; + uint32_t shift{64 - remaining_bits_after_last_actual_dot}; return name{ ((value & mask) << shift) + (thirteenth_character << (shift-1)) }; } @@ -174,54 +171,50 @@ namespace eosio { /** * Explicit cast to bool of the uint64_t value of the name * - * @return Returns true if the name is set to the default value of 0 else true. + * @return Returns true if the name is set to the default value of 0 else true */ constexpr explicit operator bool()const { return value != 0; } /** - * Writes the %name as a string to the provided char buffer + * Writes the %name as a string to char buffer * * @pre Appropriate Size Precondition: (begin + 13) <= end and (begin + 13) does not overflow * @pre Valid Memory Region Precondition: The range [begin, end) must be a valid range of memory to write to. * @param begin - The start of the char buffer * @param end - Just past the end of the char buffer * @return char* - Just past the end of the last character written (returns begin if the Appropriate Size Precondition is not satisfied) - * @post If the Appropriate Size Precondition is satisfied, the range [begin, returned pointer) contains the string representation of the %name. + * @post If the Appropriate Size Precondition is satisfied, the range [begin, returned pointer) contains the string representation of the %name */ char* write_as_string( char* begin, char* end )const { static const char* charmap = ".12345abcdefghijklmnopqrstuvwxyz"; - constexpr uint64_t mask = 0xF800000000000000ull; + constexpr uint64_t mask = 0xF800000000000000ULL; - if( (begin + 13) < begin || (begin + 13) > end ) return begin; + if( (begin + 13) < begin || end < (begin + 13) ) return begin; - auto v = value; - for( auto i = 0; i < 13; ++i, v <<= 5 ) { - if( v == 0 ) { - *begin = '\0'; - return begin; - } + uint64_t val{value}; + for( uint8_t i{}; i < 13; ++i, val <<= 5 ) { + if( val == 0 ) return begin; - auto indx = (v & mask) >> (i == 12 ? 60 : 59); - *begin = charmap[indx]; + uint8_t index = (val & mask) >> ((i == 12) ? 60 : 59); + *begin = charmap[index]; ++begin; } - return begin; } /** * Returns the name as a string. * - * @brief Returns the name value as a string by calling write_as_string() and returning the buffer produced by write_as_string() + * @brief Returns the name value as a string by calling write_as_string(); returning the buffer produced by write_as_string() */ std::string to_string()const { char buffer[13]; - auto end = write_as_string( buffer, buffer + sizeof(buffer) ); + char* end{write_as_string( buffer, buffer + sizeof(buffer) )}; return {buffer, end}; } /** - * Equivalency operator. Returns true if a == b (are the same) + * Equivalency operator. Returns true if a == b * * @return boolean - true if both provided %name values are the same */ @@ -230,7 +223,7 @@ namespace eosio { } /** - * Inverted equivalency operator. Returns true if a != b (are different) + * Inverted equivalency operator. Returns true if a != b * * @return boolean - true if both provided %name values are not the same */ @@ -241,14 +234,14 @@ namespace eosio { /** * Less than operator. Returns true if a < b. * - * @return boolean - true if %name `a` is less than `b` + * @return boolean - true if %name a is less than b */ friend constexpr bool operator < ( const name& a, const name& b ) { return a.value < b.value; } - uint64_t value = 0; + uint64_t value{}; EOSLIB_SERIALIZE( name, (value) ) }; diff --git a/libraries/eosiolib/symbol.hpp b/libraries/eosiolib/symbol.hpp index 4711014920..f45a2ad610 100644 --- a/libraries/eosiolib/symbol.hpp +++ b/libraries/eosiolib/symbol.hpp @@ -35,7 +35,9 @@ namespace eosio { * @brief Construct a new symbol_code object defaulting to a value of 0 * */ - constexpr symbol_code() : value(0) {} + constexpr symbol_code() + : value{} + {} /** * Construct a new symbol_code given a scoped enumerated type of raw (uint64_t). diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 5eda3280e4..787a0f9596 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -3,28 +3,29 @@ #include -#define NATIVE_NAME -#define NATIVE_SYMBOL -#define NATIVE_ASSET - using namespace eosio; using namespace eosio::native; -// For incremented the `precision` in any test relating to `precision` +// For incrementing the `precision` in any test relating to `precision` // The `precision` field in the `symbol` type must be constructed via constexpr, // therefore a constexpr function is needed to dynamically test `precision` constexpr uint8_t increment_precision( uint8_t precision ) { return ++precision; } // Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) - // -------------------------------------------- - // constructors/is_amount_within_range/is_valid + silence_output(true); + + // -------------------------------------------------------------------- + // constructors/bool is_amount_within_range()const/bool is_valid()const + + /// constexpr asset() REQUIRE_EQUAL( asset{}.amount, 0ULL ); REQUIRE_EQUAL( asset{}.symbol.raw(), 0ULL ); + /// constexpr asset(int64_t, symbol) REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.amount), 0 ); - REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL",0}}.amount), 4611686018427387903LL ); - REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL",0}}.amount), -4611686018427387903LL ); + REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), 4611686018427387903LL ); + REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), -4611686018427387903LL ); REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"A",0}}.symbol.raw()), 16640ULL ); // "A", precision: 0 REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"Z",0}}.symbol.raw()), 23040ULL ); // "Z", precision: 0 @@ -37,8 +38,8 @@ EOSIO_TEST_BEGIN(asset_type_test) REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{int64_t{4611686018427387904LL}, symbol{}};}) ); REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{int64_t{-4611686018427387904LL}, symbol{}};}) ); - // ---------- - // set_amount + // ------------------------ + // void set_amount(int64_t) asset a{int64_t{0}, symbol{"SYMBOLL", 1}}; REQUIRE_EQUAL( (a.set_amount(0), a.amount), 0); REQUIRE_EQUAL( (a.set_amount(1LL), a.amount), 1LL); @@ -50,8 +51,8 @@ EOSIO_TEST_BEGIN(asset_type_test) REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( []() {asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(-4611686018427387904LL);}) ); - // --------- - // to_string + // --------------------------------------- + // std::to_string()const/void print()const // Note that printing an `asset` is limited to a precision of 63 // This will trigger an error: // `asset{int64_t{1LL}, symbol{"SYMBOLL", 64}}.print();` @@ -62,9 +63,13 @@ EOSIO_TEST_BEGIN(asset_type_test) // output: "1. SYMBOLL" // Note that `assets` with negative amounts with 0 `precision` print have the odd behavior of printing two '-' characters: - REQUIRE_EQUAL( (asset{int64_t{0LL}, symbol{"SYMBOLL", 0}}.to_string()), + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}}.to_string()), + "0. SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{-0}, symbol{"SYMBOLL", 0}}.to_string()), "0. SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{0LL}, symbol{"SYMBOLL", 63}}.to_string()), + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 63}}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); + REQUIRE_EQUAL( (asset{int64_t{-0}, symbol{"SYMBOLL", 63}}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.to_string()), @@ -89,101 +94,322 @@ EOSIO_TEST_BEGIN(asset_type_test) for( uint8_t precision{0}; precision < 64; ++precision ) { REQUIRE_EQUAL( (asset{int64_t{0LL}, symbol{"SYMBOLL", precision}}.to_string()), (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ); + } + + // ---------------------- + // asset operator-()const + REQUIRE_EQUAL( (-asset{int64_t{0}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{-0}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{0}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 63}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{-0}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 63}}.amount) ); + + REQUIRE_EQUAL( (-asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}.amount) ); + + REQUIRE_EQUAL( (-asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), + (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), + (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount), + (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount) ); + REQUIRE_EQUAL( (-asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount), + (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount) ); + + // ------------------------------------------------------------------------------------------ + // inline friend asset& operator+(const asset&, const asset&)/asset& operator+=(const asset&) + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} += asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), + (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + + REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} += asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}} ), + (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + + REQUIRE_ASSERT( "attempt to add asset with different symbol", ( + []() {asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} += asset{int64_t{1LL}, symbol{"LLOBMYS", 0}};}) ); + REQUIRE_ASSERT( "addition underflow", ( + []() {asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} += -asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + REQUIRE_ASSERT( "addition overflow", ( + []() {asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} += asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + + // ------------------------------------------------------------------------------------------ + // inline friend asset& operator-(const asset&, const asset&)/asset& operator-=(const asset&) + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} -= asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), + (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + + REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} -= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), + (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + + REQUIRE_ASSERT( "attempt to subtract asset with different symbol", ( + []() {asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} -= asset{int64_t{1LL}, symbol{"LLOBMYS", 0}};}) ); + REQUIRE_ASSERT( "subtraction underflow", ( + []() {asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} -= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + REQUIRE_ASSERT( "subtraction overflow", ( + []() {asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} -= -asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + + // ----------------------------------------------------------------------- + // friend asset operator*(const asset&, int64_t)/asset operator*=(int64_t) + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} *= 0 ), + (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + + REQUIRE_EQUAL( (asset{int64_t{2}, symbol{"SYMBOLL", 0}} *= 1LL ), + (asset{int64_t{2}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (asset{int64_t{2}, symbol{"SYMBOLL", 0}} *= -1LL ), + (asset{int64_t{-2}, symbol{"SYMBOLL", 0}}) ); + + REQUIRE_ASSERT( "multiplication overflow", ( + []() {asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} *= 2LL;}) ); + REQUIRE_ASSERT( "multiplication underflow", ( + []() {asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} *= 2LL;}) ); + + // // ------------------------------------------------------------------------ + // // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) + // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), + // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); + // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), + // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); + // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), + // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); + // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), + // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); + + REQUIRE_ASSERT( "divide by zero", ( []() {asset{int64_t{1}, symbol{"SYMBOLL", 0}} /= 0;}) ); +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Why is this not throwing?? +// And why is my addition test throwing?? + // REQUIRE_ASSERT( "signed division overflow", ( + // []() {asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}} /= -1LL;}) ); + + // REQUIRE_ASSERT( "divide by zero", ( + // []() {asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}} / + // asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}};}) ); + // REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( + // []() {asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}} / + // asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}}}) ); +/////////////////////////////////////////////////////////////////////////////////////////////////// + + // -------------------------------------------------- + // friend bool operator==(const asset&, const asset&) + REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} == asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), true ); + + REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} == + asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); + + REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} == + asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); + + REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( + []() {asset{int64_t{0}, symbol{"SYMBOLL", 0}} == asset{int64_t{0}, symbol{"LLOBMYS", 0}};}) ); + + // --------------------------------------------------- + // friend bool operator!=( const asset&, const asset&) + REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} != + -asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); + + REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} != + -asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); + + // ------------------------------------------------- + // friend bool operator<(const asset&, const asset&) + REQUIRE_EQUAL( ( asset{int64_t{0}, symbol{"SYMBOLL", 0}} < asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); + + // -------------------------------------------------- + // friend bool operator<=(const asset&, const asset&) + REQUIRE_EQUAL( ( asset{int64_t{0}, symbol{"SYMBOLL", 0}} <= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} <= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); + + // ------------------------------------------------- + // friend bool operator>(const asset&, const asset&) + REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} > asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), true ); + + // -------------------------------------------------- + // friend bool operator>=( const asset&, const asset&) + REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} >= asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} >= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); - // ----- - // print -// extended_symbol{symbol{"A", 0}, name{"1"}}.print(true); -// extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true); - // REQUIRE_PRINT( "0@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(true);} ); - // REQUIRE_PRINT( "0@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(true);} ); - // REQUIRE_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); - // REQUIRE_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); - - - // --------- - // operator- - - // ---------- - // operator-= - - // ---------- - // operator+= - - // ---------- - // operator*= - - // ---------- - // operator/= - - // --------- - // operator- - - // --------- - // operator+ - - // --------- - // operator* - - // --------- - // operator* + silence_output(false); +EOSIO_TEST_END - // --------- - // operator/ +// Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` +EOSIO_TEST_BEGIN(extended_asset_type_test) + silence_output(true); - // --------- - // operator/ + // ------------ + // constructors - // ---------- - // operator== + /// extended_asset() + REQUIRE_EQUAL( extended_asset{}.quantity, asset{} ); + REQUIRE_EQUAL( extended_asset{}.contract, name{} ); + + /// extended_asset(asset, name) + REQUIRE_EQUAL( (extended_asset{int64_t{0}, extended_symbol{symbol{"SYMBOLL", 0}, name{"eosioaccountj"}}}.quantity), + (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (extended_asset{int64_t{0}, extended_symbol{symbol{"SYMBOLL", 0}, name{"eosioaccountj"}}}.contract), + name{"eosioaccountj"}); + + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}.quantity), + (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}.contract), + (name{"eosioaccountj"}) ); + + // ------------------------------------------ + // extended_symbol get_extended_symbol()const + REQUIRE_EQUAL( (extended_asset{asset{}, name{}}.get_extended_symbol()), extended_symbol{} ); + + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.get_extended_symbol()), + (extended_symbol{symbol{"A", 0}, name{"1"}}) ); + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.get_extended_symbol()), + (extended_symbol{symbol{"A", 0}, name{"5"}}) ); + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.get_extended_symbol()), + (extended_symbol{symbol{"Z", 0}, name{"a"}}) ); + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.get_extended_symbol()), + (extended_symbol{symbol{"Z", 0}, name{"z"}}) ); + + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.get_extended_symbol()), + (extended_symbol{symbol{"AAAAAAA", 0}, name{"111111111111j"}}) ); + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.get_extended_symbol()), + (extended_symbol{symbol{"AAAAAAA", 0}, name{"555555555555j"}}) ); + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.get_extended_symbol()), + (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"aaaaaaaaaaaaj"}}) ); + REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.get_extended_symbol()), + (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"zzzzzzzzzzzzj"}}) ); + + + // Fix this printing functionality + // Note that if there is no precision, there will be odd output: + // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` + // output: "0. A@1" + // ----------------- + // void print()const +/////////////////////////////////////////////////////////////////////////////////////////////////// +// How is this not the correct print result?? +// print_f("0. A@1"); +// print_f("\n");; +// extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print(); +// print_f("\n");; +// REQUIRE_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ); +// REQUIRE_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ); +// REQUIRE_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ); +// REQUIRE_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ); + +// REQUIRE_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ); +// REQUIRE_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ); +// REQUIRE_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ); +// REQUIRE_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ); +/////////////////////////////////////////////////////////////////////////////////////////////////// + + // ------------------------------- + // extended_asset operator-()const + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), + (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), + (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), + (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), + (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), + (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), + (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), + (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), + (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), + (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), + (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), + (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), + (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + - // ---------- - // operator!= + // ----------------------------------------------------------------------------- + // friend extended_asset operator+(const extended_asset&, const extended_asset&) + extended_asset asset0{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}; + extended_asset asset1{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}; + extended_asset asset2{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}; - // --------- - // operator< + extended_asset asset3{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}; + extended_asset asset4{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"jtnuoccaoisoe"}}; - // ---------- - // operator<= + REQUIRE_EQUAL( (asset0 + asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); + REQUIRE_EQUAL( (asset1 + asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - // --------- - // operator> + REQUIRE_ASSERT( "type mismatch", ( + [&]() {asset3 + asset4;}) ); - // ---------- - // operator>= -EOSIO_TEST_END + // ------------------------------------------------------------------------- + // friend extended_asset& operator+=(extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (asset0 += asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); + REQUIRE_EQUAL( (asset1 += asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); -// Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` -EOSIO_TEST_BEGIN(extended_asset_type_test) - // constructors + REQUIRE_ASSERT( "type mismatch", ( + [&]() {asset3 += asset4;}) ); - // get_extended_symbol + // -------------------------------------------------------------------------- + // friend extended_asset& operator-=( extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (asset0 - asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); + REQUIRE_EQUAL( (asset1 - asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - // print + REQUIRE_ASSERT( "type mismatch", ( + [&]() {asset3 - asset4;}) ); - // operator- + // ---------------------------------------------------------------------------- + //friend extended_asset operator-(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (asset0 -= asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); + REQUIRE_EQUAL( (asset1 -= asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - // operator- + REQUIRE_ASSERT( "type mismatch", ( + [&]() {asset3 -= asset4;}) ); - // operator+ + // -------------------------------------------------------------------- + // friend bool operator==(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (asset0 == asset0), true ); + REQUIRE_EQUAL( (asset1 == asset1), true ); - // operator+= +/////////////////////////////////////////////////////////////////////////////////////////////////// +// Why is this not throwing?? + // REQUIRE_ASSERT( "type mismatch", ( + // [&]() {asset3 == asset4;}) ); +/////////////////////////////////////////////////////////////////////////////////////////////////// - // operator-= + // -------------------------------------------------------------------- + // friend bool operator!=(const extended_asset&, const extended_asset&) +// print_f("%", asset3 != asset4); +// print_f("\n"); + REQUIRE_EQUAL( asset3 != asset4, true ); + // REQUIRE_EQUAL( (asset1 != asset2), false ); + // REQUIRE_EQUAL( (asset2 != asset3), false ); - // operator< + // REQUIRE_ASSERT( "type mismatch", ( + // [&]() {asset0 != asset1;}) ); - // operator== + // -------------------------------------------------------------------- + // friend bool operator<( const extended_asset&, const extended_asset&) + // ??? + REQUIRE_EQUAL( asset0 < asset1, true ); - // operator!= + // REQUIRE_ASSERT( "type mismatch", ( + // []() {extended_asset{asset{}, name{}};}) ); - // operator<= + // -------------------------------------------------------------------- + // friend bool operator<=(const extended_asset&, const extended_asset&) + // REQUIRE_EQUAL( asset0 <= asset1, true ); - // operator>= + // -------------------------------------------------------------------- + // friend bool operator>=(const extended_asset&, const extended_asset&) + // REQUIRE_EQUAL( asset1 > asset0, true ); silence_output(false); EOSIO_TEST_END int main(int argc, char** argv) { EOSIO_TEST(asset_type_test); + EOSIO_TEST(extended_asset_type_test); return has_failed(); } diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 4631b33b08..e51e2dd33a 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,27 +1,33 @@ #include #include -#define NATIVE_NAME -#define NATIVE_SYMBOL -#define NATIVE_ASSET - using namespace eosio; using namespace eosio::native; +const uint64_t u64MAX = std::numeric_limits::max(); // 18446744073709551615 +const uint64_t u64MIN = std::numeric_limits::min(); // 0 + // Defined in `eosio.cdt/libraries/eosiolib/name.hpp` EOSIO_TEST_BEGIN(name_type_test) + silence_output(true); + // ------------ // constructors + + /// constexpr name() REQUIRE_EQUAL( name{}.value, 0ULL ); + /// constexpr explicit name(uint64_t) REQUIRE_EQUAL( name{0ULL}.value, 0ULL ); REQUIRE_EQUAL( name{1ULL}.value, 1ULL ); - REQUIRE_EQUAL( name{18446744073709551615ULL}.value, 18446744073709551615ULL ); + REQUIRE_EQUAL( name{u64MAX}.value, u64MAX ); + /// constexpr explicit name(name::raw) REQUIRE_EQUAL( name{name::raw{0ULL}}.value, 0ULL ); REQUIRE_EQUAL( name{name::raw{1ULL}}.value, 1ULL ); - REQUIRE_EQUAL( name{name::raw{18446744073709551615ULL}}.value, 18446744073709551615ULL ); + REQUIRE_EQUAL( name{name::raw{u64MAX}}.value, u64MAX ); + /// constexpr explicit name(std::string_view) REQUIRE_EQUAL( name{"1"}.value, 576460752303423488ULL ); REQUIRE_EQUAL( name{"5"}.value, 2882303761517117440ULL ); REQUIRE_EQUAL( name{"a"}.value, 3458764513820540928ULL ); @@ -55,31 +61,19 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ); REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ); - // --------------- - // `char_to_value` + // -------------------------------------------- + // static constexpr uint8_t char_to_value(char) char c{'.'}; uint8_t expected_value{0}; // Will increment to the expected correct value in the set [0,32) -#ifdef NATIVE_NAME - print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); - print_f("Value of expected_value: %", static_cast(expected_value) ); -#endif REQUIRE_EQUAL( name::char_to_value(c), expected_value ); ++expected_value; for(c = '1'; c <= '5'; ++c ) { -#ifdef NATIVE_NAME - print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); - print_f("Value of expected_value: %", static_cast(expected_value) ); -#endif REQUIRE_EQUAL( name::char_to_value(c), expected_value ); ++expected_value; } for(c = 'a'; c <= 'z'; ++c ) { -#ifdef NATIVE_NAME - print_f("Value of name::char_to_value(%): %", c, static_cast(name::char_to_value(c)) ); - print_f("Value of expected_value: %", static_cast(expected_value) ); -#endif REQUIRE_EQUAL( name::char_to_value(c), expected_value ); ++expected_value; } @@ -93,8 +87,8 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); - // ------ - // length + // ------------------------------- + // constexpr uint8_t length()cosnt REQUIRE_EQUAL( name{""}.length(), 0 ); REQUIRE_EQUAL( name{"e"}.length(), 1 ); REQUIRE_EQUAL( name{"eo"}.length(), 2 ); @@ -112,90 +106,19 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ); - // ------ - // suffix -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{".eosioaccounj"}.suffix() ); // TODO: interesting print behavior; starts newline sometimes - print_f("Value of expected suffix: eosioaccounj\n" ); -#endif - + // ---------------------------- + // constexpr name suffix()const REQUIRE_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"e.osioaccounj"}.suffix() ); - print_f("Value of expected suffix: osioaccounj\n" ); -#endif - REQUIRE_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eo.sioaccounj"}.suffix() ); - print_f("Value of expected suffix: sioaccounj\n" ); -#endif - REQUIRE_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eos.ioaccounj"}.suffix() ); - print_f("Value of expected suffix: ioaccounj\n" ); -#endif - REQUIRE_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosi.oaccounj"}.suffix() ); - print_f("Value of expected suffix: oaccounj\n" ); -#endif - REQUIRE_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosio.accounj"}.suffix() ); - print_f("Value of expected suffix: accounj\n" ); -#endif - REQUIRE_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosioa.ccounj"}.suffix() ); - print_f("Value of expected suffix: ccounj\n" ); -#endif - REQUIRE_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosioac.counj"}.suffix() ); - print_f("Value of expected suffix: counj\n" ); -#endif - REQUIRE_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosioacc.ounj"}.suffix() ); - print_f("Value of expected suffix: ounj\n" ); -#endif - REQUIRE_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosioacco.unj"}.suffix() ); - print_f("Value of expected suffix: unj\n" ); -#endif - REQUIRE_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosioaccou.nj"}.suffix() ); - print_f("Value of expected suffix: nj\n" ); -#endif - REQUIRE_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eosioaccoun.j"}.suffix() ); - print_f("Value of expected suffix: j\n" ); -#endif - REQUIRE_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ); // Note that this case is ignored because '.' characters at the end of a name are ignored @@ -206,22 +129,11 @@ EOSIO_TEST_BEGIN(name_type_test) // #endif // eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"e.o.s.i.o.a.c"}.suffix() ); - print_f("Value of expected suffix: c\n" ); -#endif - - REQUIRE_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ); - -#ifdef NATIVE_NAME - print_f("Value of suffix: %\n", name{"eos.ioa.cco"}.suffix() ); - print_f("Value of expected suffix: cco\n" ); -#endif - + REQUIRE_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ); REQUIRE_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ); - // ------------ - // operator raw + // ----------------------------- + // constexpr operator raw()const REQUIRE_EQUAL( static_cast(name::raw{name{"1"}}), 576460752303423488ULL ); REQUIRE_EQUAL( static_cast(name::raw{name{"5"}}), 2882303761517117440ULL ); REQUIRE_EQUAL( static_cast(name::raw{name{"a"}}), 3458764513820540928ULL ); @@ -248,8 +160,8 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( static_cast(name::raw{name{"aaaaaaaaaaaaj"}}), 3570337562653461615ULL ); REQUIRE_EQUAL( static_cast(name::raw{name{"zzzzzzzzzzzzj"}}), 18446744073709551615ULL ); - // ------------- - // operator bool + // --------------------------------------- + // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` REQUIRE_EQUAL( name{0}.operator bool(), false ); REQUIRE_EQUAL( name{1}.operator bool(), true ); @@ -261,8 +173,8 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( !name{""}, true ); REQUIRE_EQUAL( !name{"1"}, false ); - // --------------- - // write_as_string + // ---------------------------------------- + // char* write_as_string(char*, char*)const char buffer[13]{}; std::string test_str{"1"}; @@ -313,8 +225,8 @@ EOSIO_TEST_BEGIN(name_type_test) name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); - // --------- - // to_string + // ---------------------------- + // std::string to_string()const REQUIRE_EQUAL( name{"1"}.to_string(), "1" ); REQUIRE_EQUAL( name{"5"}.to_string(), "5" ); REQUIRE_EQUAL( name{"a"}.to_string(), "a" ); @@ -341,8 +253,8 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ); REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ); - // ---------- - // operator== + // ---------------------------------------------------------- + // friend constexpr bool operator==(const name&, const name&) REQUIRE_EQUAL( name{"1"} == name{"1"}, true ); REQUIRE_EQUAL( name{"5"} == name{"5"}, true ); REQUIRE_EQUAL( name{"a"} == name{"a"}, true ); @@ -369,8 +281,8 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ); REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ); - // ---------- - // operator!= + // ----------------------------------------------------------- + // friend constexpr bool operator!=(const name&, const name&) REQUIRE_EQUAL( name{"1"} != name{0}, true ); REQUIRE_EQUAL( name{"5"} != name{0}, true ); REQUIRE_EQUAL( name{"a"} != name{0}, true ); @@ -397,8 +309,8 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} != name{0}, true ); REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} != name{0}, true ); - // --------- - // operator< + // --------------------------------------------------------- + // friend constexpr bool operator<(const name&, const name&) REQUIRE_EQUAL( name{0} < name{"1"}, true ); REQUIRE_EQUAL( name{0} < name{"5"}, true ); REQUIRE_EQUAL( name{0} < name{"a"}, true ); @@ -425,8 +337,8 @@ EOSIO_TEST_BEGIN(name_type_test) REQUIRE_EQUAL( name{0} < name{"aaaaaaaaaaaaj"}, true ); REQUIRE_EQUAL( name{0} < name{"zzzzzzzzzzzzj"}, true ); - // ------------ - // operator""_n + // ------------------------------------ + // inline constexpr name operator""_n() REQUIRE_EQUAL( name{0} == ""_n, true ); REQUIRE_EQUAL( name{"1"} == "1"_n, true ); diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 0bff5621e2..8c98456871 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -1,23 +1,25 @@ #include #include -#define NATIVE_NAME -#define NATIVE_SYMBOL -#define NATIVE_ASSET - using namespace eosio; using namespace eosio::native; // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_code_type_test) - // ---------------- - // constructors/raw + silence_output(true); + + // ------------------------------------------ + // constructors/constexpr uint64_t raw()const + + /// constexpr symbol_code() REQUIRE_EQUAL( symbol_code{}.raw(), 0ULL ); + /// constexpr explicit symbol_code(uint64_t raw) REQUIRE_EQUAL( symbol_code{0ULL}.raw(), 0ULL ); REQUIRE_EQUAL( symbol_code{1ULL}.raw(), 1ULL ); REQUIRE_EQUAL( symbol_code{18446744073709551615ULL}.raw(), 18446744073709551615ULL ); + /// constexpr explicit symbol_code(std::string_view str) REQUIRE_EQUAL( symbol_code{"A"}.raw(), 65ULL ); REQUIRE_EQUAL( symbol_code{"Z"}.raw(), 90ULL ); REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ); @@ -29,8 +31,8 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ); REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ); - // -------- - // is_valid + // ------------------------------ + // constexpr bool is_valid()const REQUIRE_EQUAL( symbol_code{65ULL}.is_valid(), true ); // "A" REQUIRE_EQUAL( symbol_code{90ULL}.is_valid(), true ); // "Z" REQUIRE_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ); // "AAAAAAA" @@ -39,8 +41,8 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) REQUIRE_EQUAL( symbol_code{64ULL}.is_valid(), false ); REQUIRE_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ); - // ------ - // length + // -------------------------------- + // constexpr uint32_t length()const REQUIRE_EQUAL( symbol_code{""}.length(), 0 ); REQUIRE_EQUAL( symbol_code{"S"}.length(), 1 ); REQUIRE_EQUAL( symbol_code{"SY"}.length(), 2 ); @@ -50,8 +52,8 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) REQUIRE_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ); REQUIRE_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ); - // ------------- - // operator bool + // --------------------------------------- + // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` REQUIRE_EQUAL( symbol_code{0}.operator bool(), false ); REQUIRE_EQUAL( symbol_code{1}.operator bool(), true ); @@ -63,51 +65,43 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) REQUIRE_EQUAL( !symbol_code{""}, true ); REQUIRE_EQUAL( !symbol_code{"SYMBOL"}, false ); - // --------------- - // write_as_string + // ---------------------------------------- + // char* write_as_string(char*, char*)const char buffer[7]{}; std::string test_str{"A"}; symbol_code{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); -print_f("%\n",test_str); -print_f("%\n",buffer); REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); symbol_code{test_str = "Z"}.write_as_string( buffer, buffer + sizeof(buffer) ); -print_f("%\n",test_str); -print_f("%\n",buffer); REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); symbol_code{test_str = "AAAAAAA"}.write_as_string( buffer, buffer + sizeof(buffer) ); -print_f("%\n",test_str); -print_f("%\n",buffer); REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); symbol_code{test_str = "ZZZZZZZ"}.write_as_string( buffer, buffer + sizeof(buffer) ); -print_f("%\n",test_str); -print_f("%\n",buffer); REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); - // --------- - // to_string + // ---------------------------- + // std::string to_string()const REQUIRE_EQUAL( symbol_code{"A"}.to_string(), "A" ); REQUIRE_EQUAL( symbol_code{"Z"}.to_string(), "Z" ); REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ); REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ); - // ---------- - // operator== + // -------------------------------------------------------------- + // friend bool operator==(const symbol_code&, const symbol_code&) REQUIRE_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ); REQUIRE_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ); REQUIRE_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ); REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ); - // ---------- - // operator!= + // -------------------------------------------------------------- + // friend bool operator!=(const symbol_code&, const symbol_code&) REQUIRE_EQUAL( symbol_code{"A"} != symbol_code{0}, true ); REQUIRE_EQUAL( symbol_code{"Z"} != symbol_code{0}, true ); REQUIRE_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{0}, true ); REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{0}, true ); - // --------- - // operator< + // ------------------------------------------------------------- + // friend bool operator<(const symbol_code&, const symbol_code&) REQUIRE_EQUAL( symbol_code{0} < symbol_code{"A"}, true ); REQUIRE_EQUAL( symbol_code{0} < symbol_code{"Z"}, true ); REQUIRE_EQUAL( symbol_code{0} < symbol_code{"AAAAAAA"}, true ); @@ -116,33 +110,32 @@ EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_type_test) - // ------------ - // constructors/raw + // ------------------------------------------ + // constructors/constexpr uint64_t raw()const + + /// constexpr symbol() REQUIRE_EQUAL( symbol{}.raw(), 0ULL ); + /// constexpr explicit symbol(uint64_t) REQUIRE_EQUAL( symbol{0ULL}.raw(), 0ULL ); REQUIRE_EQUAL( symbol{1ULL}.raw(), 1ULL ); REQUIRE_EQUAL( symbol{18446744073709551615ULL}.raw(), 18446744073709551615ULL ); + /// constexpr symbol(std::string_view, uint8_t) // Note that unless constructed with `initializer_list`, precision does not check for wrap-around REQUIRE_EQUAL( (symbol{"A", 0}.raw()), 16640ULL ); REQUIRE_EQUAL( (symbol{"Z", 0}.raw()), 23040ULL ); REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.raw()), 4702111234474983680ULL ); REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.raw()), 6510615555426900480ULL ); + /// constexpr symbol(symbol_code, uint8_t) REQUIRE_EQUAL( (symbol{symbol_code{"A"}, 0}.raw()), 16640ULL ); REQUIRE_EQUAL( (symbol{symbol_code{"Z"}, 0}.raw()), 23040ULL ); REQUIRE_EQUAL( (symbol{symbol_code{"AAAAAAA"}, 0}.raw()), 4702111234474983680ULL ); REQUIRE_EQUAL( (symbol{symbol_code{"ZZZZZZZ"}, 0}.raw()), 6510615555426900480ULL ); -print_f("%",symbol{"A", 0}.raw()); -print_f("%",symbol{"Z", 0}.raw()); -print_f("%",symbol{"AAAAAAA", 0}.raw()); -print_f("%",symbol{"ZZZZZZZ", 0}.raw()); -print_f("%",symbol{"ZZZZZZZ", 255}.raw()); - - // -------- - // is_valid + // -------------------- + // bool is_valid()const REQUIRE_EQUAL( symbol{16640ULL}.is_valid(), true ); // "A", precision: 0 REQUIRE_EQUAL( symbol{23040ULL}.is_valid(), true ); // "Z", precision: 0 REQUIRE_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ); // "AAAAAAA", precision: 0 @@ -151,8 +144,8 @@ print_f("%",symbol{"ZZZZZZZ", 255}.raw()); REQUIRE_EQUAL( symbol{16639ULL}.is_valid(), false ); REQUIRE_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ); - // --------- - // precision + // ------------------------- + // uint64_t precision()const REQUIRE_EQUAL( (symbol{"A", 0}.precision()), 0 ); REQUIRE_EQUAL( (symbol{"Z", 0}.precision()), 0 ); REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.precision()), 0 ); @@ -163,15 +156,15 @@ print_f("%",symbol{"ZZZZZZZ", 255}.raw()); REQUIRE_EQUAL( (symbol{"AAAAAAA", 255}.precision()), 255 ); REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 255}.precision()), 255 ); - // ---- - // code + // ----------------------- + // symbol_code code()const REQUIRE_EQUAL( (symbol{"A", 0}.code()), symbol_code{"A"} ); REQUIRE_EQUAL( (symbol{"Z", 0}.code()), symbol_code{"Z"} ); REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.code()), symbol_code{"AAAAAAA"} ); REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.code()), symbol_code{"ZZZZZZZ"} ); - // ------------- - // operator bool + // --------------------------------------- + // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` REQUIRE_EQUAL( symbol{0}.operator bool(), false ); REQUIRE_EQUAL( symbol{1}.operator bool(), true ); @@ -183,8 +176,8 @@ print_f("%",symbol{"ZZZZZZZ", 255}.raw()); REQUIRE_EQUAL( (!symbol{"", 0}), true ); REQUIRE_EQUAL( (!symbol{"SYMBOL", 0}), false ); - // ----- - // print + // --------------------- + // void print(bool)const // symbol{"A", 0}.print(true); // symbol{"AAAAAAA", 255}.print(true); // REQUIRE_PRINT( "0,A", [](){symbol{"A", 0}.print(true);} ); @@ -197,22 +190,22 @@ print_f("%",symbol{"ZZZZZZZ", 255}.raw()); // REQUIRE_PRINT( ",AAAAAAA", [](){symbol{"AAAAAAA", 255}.print(false);} ); // REQUIRE_PRINT( ",ZZZZZZZ", [](){symbol{"ZZZZZZZ", 255}.print(false);} ); - // ---------- - // operator== + // -------------------------------------------------------------- + // friend constexpr bool operator==(const symbol&, const symbol&) REQUIRE_EQUAL( (symbol{"A", 0} == symbol{"A", 0}), true ); REQUIRE_EQUAL( (symbol{"Z", 0} == symbol{"Z", 0}), true ); REQUIRE_EQUAL( (symbol{"AAAAAAA", 0} == symbol{"AAAAAAA", 0}), true ); REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0} == symbol{"ZZZZZZZ", 0}), true ); - // ---------- - // operator!= + // -------------------------------------------------------------- + // friend constexpr bool operator!=(const symbol&, const symbol&) REQUIRE_EQUAL( (symbol{"A",0} != symbol{0}), true ); REQUIRE_EQUAL( (symbol{"Z",0} != symbol{0}), true ); REQUIRE_EQUAL( (symbol{"AAAAAAA",0} != symbol{0}), true ); REQUIRE_EQUAL( (symbol{"ZZZZZZZ",0} != symbol{0}), true ); - // --------- - // operator< + // -------------------------------------------------------------- + // friebnd constexpr bool operator<(const symbol&, const symbol&) REQUIRE_EQUAL( (symbol{0} < symbol{"A",0}), true ); REQUIRE_EQUAL( (symbol{0} < symbol{"Z",0}), true ); REQUIRE_EQUAL( (symbol{0} < symbol{"AAAAAAA",0}), true ); @@ -221,11 +214,14 @@ EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(extended_symbol_type_test) - // ------------ + // ------------------------------------ // constructors/get_symbol/get_contract + + /// constexpr extended_symbol() REQUIRE_EQUAL( (extended_symbol{symbol{}, name{}}.get_symbol().raw()), 0ULL ); REQUIRE_EQUAL( (extended_symbol{symbol{}, name{}}.get_contract().value), 0ULL ); + /// constexpr extended_symbol(symbol, name)/constexpr symbol get_symbol()const/constexpr name get_contract()const REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"1"}}.get_symbol().raw()), 16640ULL ); REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"5"}}.get_symbol().code().raw()), 65ULL ); REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"a"}}.get_symbol().raw()), 23040ULL ); @@ -243,8 +239,8 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"aaaaaaaaaaaaj"}}.get_contract().value), 3570337562653461615ULL ); REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"zzzzzzzzzzzzj"}}.get_contract().value), 18446744073709551615ULL ); - // ----- - // print + // --------------------- + // void print(bool)const // extended_symbol{symbol{"A", 0}, name{"1"}}.print(true); // extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true); // REQUIRE_PRINT( "0@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(true);} ); @@ -257,22 +253,22 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) // REQUIRE_PRINT( "@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); // REQUIRE_PRINT( "@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); - // ---------- - // operator== + // ------------------------------------------------------------------------------- + // friend constexpr bool operator==(const extended_symbol&, const extended_symbol&) REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{0}} == extended_symbol{symbol{"A", 0}, name{}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{0}} == extended_symbol{symbol{"Z", 0}, name{}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{0}} == extended_symbol{symbol{"AAAAAAA", 0}, name{}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}} == extended_symbol{symbol{"ZZZZZZZ", 0}, name{}}), true ); - // ---------- - // operator!= + // ------------------------------------------------------------------------------- + // friend constexpr bool operator!=(const extended_symbol&, const extended_symbol&) REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); - // --------- - // operator< + // ------------------------------------------------------------------------------- + // friend constexpr bool operator<(const extended_symbol&, const extended_symbol&) REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"A", 0}, name{0}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"Z", 0}, name{0}}), true ); REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"AAAAAAA", 0}, name{0}}), true ); From 86397cf73a81f61df41eb4386e3727323ceda93b Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 11 Jan 2019 18:20:20 -0500 Subject: [PATCH 09/51] Continuation of `asset` type tests --- libraries/eosiolib/name.hpp | 44 +++++++++++++++++++++++------- libraries/eosiolib/symbol.hpp | 50 +++++++++++++++++------------------ 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/libraries/eosiolib/name.hpp b/libraries/eosiolib/name.hpp index 1f79161d54..a4f7c1a6ba 100644 --- a/libraries/eosiolib/name.hpp +++ b/libraries/eosiolib/name.hpp @@ -4,8 +4,8 @@ */ #pragma once -#include "system.hpp" #include "serialize.hpp" +#include "system.hpp" #include #include @@ -40,8 +40,8 @@ namespace eosio { /** * Construct a new name given a unit64_t value * - * @brief Construct a new name object initialising value with v - * @param v - The unit64_t value + * @brief Construct a new name object initialising value with val + * @param val - The unit64_t value * */ constexpr explicit name( uint64_t val ) @@ -186,8 +186,8 @@ namespace eosio { * @post If the Appropriate Size Precondition is satisfied, the range [begin, returned pointer) contains the string representation of the %name */ char* write_as_string( char* begin, char* end )const { - static const char* charmap = ".12345abcdefghijklmnopqrstuvwxyz"; - constexpr uint64_t mask = 0xF800000000000000ULL; + static const char* charmap{".12345abcdefghijklmnopqrstuvwxyz"}; + constexpr uint64_t mask{0xF800000000000000ULL}; if( (begin + 13) < begin || end < (begin + 13) ) return begin; @@ -218,7 +218,7 @@ namespace eosio { * * @return boolean - true if both provided %name values are the same */ - friend constexpr bool operator == ( const name& a, const name& b ) { + friend constexpr bool operator==( const name& a, const name& b ) { return a.value == b.value; } @@ -227,20 +227,46 @@ namespace eosio { * * @return boolean - true if both provided %name values are not the same */ - friend constexpr bool operator != ( const name& a, const name& b ) { + friend constexpr bool operator!=( const name& a, const name& b ) { return a.value != b.value; } /** - * Less than operator. Returns true if a < b. + * Less than operator. Returns true if a < b * * @return boolean - true if %name a is less than b */ - friend constexpr bool operator < ( const name& a, const name& b ) { + friend constexpr bool operator<( const name& a, const name& b ) { return a.value < b.value; } + /** + * Less than or equal to operator. Returns true if a <= b + * + * @return boolean - true if %name a is less than or equal to b + */ + friend constexpr bool operator<=( const name& a, const name& b ) { + return a.value <= b.value; + } + + /** + * Greater than operator. Returns true if a > b + * + * @return boolean - true if %name a is greater than b + */ + friend constexpr bool operator>( const name& a, const name& b ) { + return a.value > b.value; + } + /** + * Greater than or equal to operator. Returns true if a >= b + * + * @return boolean - true if %name a is greater than or equal to b + */ + friend constexpr bool operator>=( const name& a, const name& b ) { + return a.value >= b.value; + } + uint64_t value{}; EOSLIB_SERIALIZE( name, (value) ) diff --git a/libraries/eosiolib/symbol.hpp b/libraries/eosiolib/symbol.hpp index f45a2ad610..9f61b53ac7 100644 --- a/libraries/eosiolib/symbol.hpp +++ b/libraries/eosiolib/symbol.hpp @@ -4,14 +4,14 @@ */ #pragma once -#include "system.hpp" -#include "print.h" #include "name.hpp" +#include "print.h" #include "serialize.hpp" +#include "system.hpp" -#include #include #include +#include namespace eosio { @@ -32,7 +32,7 @@ namespace eosio { /** * Default constructor, construct a new symbol_code * - * @brief Construct a new symbol_code object defaulting to a value of 0 + * @brief Construct a new symbol_code object with a default value of 0 * */ constexpr symbol_code() @@ -40,14 +40,14 @@ namespace eosio { {} /** - * Construct a new symbol_code given a scoped enumerated type of raw (uint64_t). + * Construct a new symbol_code with a scoped enumerated type of raw uint64_t. * * @brief Construct a new symbol_code object initialising value with raw * @param raw - The raw value which is a scoped enumerated type of unit64_t * */ constexpr explicit symbol_code( uint64_t raw ) - :value(raw) + : value{raw} {} /** @@ -58,15 +58,13 @@ namespace eosio { * */ constexpr explicit symbol_code( std::string_view str ) - :value(0) + : value{} { - if( str.size() > 7 ) { + if( 7 < str.size() ) eosio::check( false, "string is too long to be a valid symbol_code" ); - } for( auto itr = str.rbegin(); itr != str.rend(); ++itr ) { - if( *itr < 'A' || *itr > 'Z') { + if( *itr < 'A' || 'Z' < *itr ) eosio::check( false, "only uppercase letters allowed in symbol_code string" ); - } value <<= 8; value |= *itr; } @@ -77,16 +75,16 @@ namespace eosio { * @return true - if symbol is valid */ constexpr bool is_valid()const { - auto sym = value; - for ( int i=0; i < 7; i++ ) { - char c = (char)(sym & 0xFF); - if ( !('A' <= c && c <= 'Z') ) return false; + uint64_t sym{value}; + for ( int i{}; i < 7; ++i ) { + char ch = static_cast(sym & 0xFF); + if ( !('A' <= ch && ch <= 'Z') ) return false; sym >>= 8; if ( !(sym & 0xFF) ) { do { sym >>= 8; if ( (sym & 0xFF) ) return false; - i++; + ++i; } while( i < 7 ); } } @@ -99,13 +97,13 @@ namespace eosio { * @return length - character length of the provided symbol */ constexpr uint32_t length()const { - auto sym = value; - uint32_t len = 0; - while (sym & 0xFF && len <= 7) { - len++; + uint64_t sym{value}; + uint32_t length{}; + while (sym & 0xFF && length <= 7) { + ++length; sym >>= 8; } - return len; + return length; } /** @@ -135,15 +133,15 @@ namespace eosio { * @post If the Appropriate Size Precondition is satisfied, the range [begin, returned pointer) contains the string representation of the symbol_code. */ char* write_as_string( char* begin, char* end )const { - constexpr uint64_t mask = 0xFFull; + constexpr uint64_t mask = 0xFFULL; if( (begin + 7) < begin || (begin + 7) > end ) return begin; - auto v = value; - for( auto i = 0; i < 7; ++i, v >>= 8 ) { - if( v == 0 ) return begin; + auto val = value; + for( auto i = 0; i < 7; ++i, val >>= 8 ) { + if( val == 0 ) return begin; - *begin = static_cast(v & mask); + *begin = static_cast(val & mask); ++begin; } From 0b458b21f35a1380c350e8b0a487a195378061f1 Mon Sep 17 00:00:00 2001 From: johndebord Date: Tue, 15 Jan 2019 17:12:58 -0500 Subject: [PATCH 10/51] Start `microseconds`, `time_point`, and `time_point_sec` type tests --- libraries/eosiolib/name.hpp | 193 ++++++++++------------ libraries/eosiolib/symbol.hpp | 54 +++--- tests/unit/asset_tests.cpp | 2 +- tests/unit/name_tests.cpp | 126 +------------- tests/unit/symbol_tests.cpp | 10 +- tests/unit/time_tests.cpp | 300 ++++++++++++++++++++++++++++++++++ 6 files changed, 422 insertions(+), 263 deletions(-) create mode 100644 tests/unit/time_tests.cpp diff --git a/libraries/eosiolib/name.hpp b/libraries/eosiolib/name.hpp index a4f7c1a6ba..1a09c75e8e 100644 --- a/libraries/eosiolib/name.hpp +++ b/libraries/eosiolib/name.hpp @@ -4,8 +4,8 @@ */ #pragma once -#include "serialize.hpp" #include "system.hpp" +#include "serialize.hpp" #include #include @@ -30,63 +30,62 @@ namespace eosio { /** * Construct a new name * - * @brief Construct a new name object with a default value of 0 + * @brief Construct a new name object defaulting to a value of 0 * */ - constexpr name() - : value{} - {} + constexpr name() : value(0) {} /** * Construct a new name given a unit64_t value * - * @brief Construct a new name object initialising value with val - * @param val - The unit64_t value + * @brief Construct a new name object initialising value with v + * @param v - The unit64_t value * */ - constexpr explicit name( uint64_t val ) - : value{val} + constexpr explicit name( uint64_t v ) + :value(v) {} /** - * Construct a new name with a scoped enumerated type of raw uint64_t. + * Construct a new name given a scoped enumerated type of raw (uint64_t). * - * @brief Construct a new name object, initialising value with r + * @brief Construct a new name object initialising value with r * @param r - The raw value which is a scoped enumerated type of unit64_t * */ constexpr explicit name( name::raw r ) - : value(static_cast(r)) + :value(static_cast(r)) {} /** * Construct a new name given an string. * - * @brief Construct a new name object, initialising value with str - * @param str - The string value which validates and converts to a unit64_t + * @brief Construct a new name object initialising value with str + * @param str - The string value which validated then converted to unit64_t * */ constexpr explicit name( std::string_view str ) - : value{0} + :value(0) { - if( 13 < str.size() ) + if( str.size() > 13 ) { eosio::check( false, "string is too long to be a valid name" ); - if( str.empty() ) + } + if( str.empty() ) { return; + } - uint8_t length = (12 < str.size()) ? 12 : str.size(); - - for( decltype(length) i = 0; i < length; ++i ) { + auto n = std::min( (uint32_t)str.size(), (uint32_t)12u ); + for( decltype(n) i = 0; i < n; ++i ) { value <<= 5; value |= char_to_value( str[i] ); } - value <<= ( 4 + 5*(12 - length) ); - + value <<= ( 4 + 5*(12 - n) ); if( str.size() == 13 ) { - uint64_t val = char_to_value( str[12] ); - if( 0x0FULL < val ) + uint64_t v = char_to_value( str[12] ); + if( v > 0x0Full ) { eosio::check(false, "thirteenth character in name cannot be a letter that comes after j"); - value |= val; + } + value |= v; } } @@ -96,20 +95,16 @@ namespace eosio { * @param c - Character to be converted * @return constexpr char - Converted value */ - static constexpr uint8_t char_to_value( char ch ) { - switch(ch) { - case '.': - return 0; - break; - case '1' ... '5': - return (ch - '1') + 1; - break; - case 'a' ... 'z': - return (ch - 'a') + 6; - break; - default: - eosio::check( false, "character is not in allowed character set for names" ); - } + static constexpr uint8_t char_to_value( char c ) { + if( c == '.') + return 0; + else if( c >= '1' && c <= '5' ) + return (c - '1') + 1; + else if( c >= 'a' && c <= 'z' ) + return (c - 'a') + 6; + else + eosio::check( false, "character is not in allowed character set for names" ); + return 0; // control flow will never reach here; just added to suppress warning } @@ -117,46 +112,51 @@ namespace eosio { * Returns the length of the %name */ constexpr uint8_t length()const { + constexpr uint64_t mask = 0xF800000000000000ull; + if( value == 0 ) return 0; - - constexpr uint64_t mask{0xF800000000000000ULL}; - - uint64_t val{value}; - uint8_t length{}; - for( uint8_t i{}; i < 13; val <<= 5, ++i ) - if( 0 < (val & mask) ) - length = i; - return length + 1; + + uint8_t l = 0; + uint8_t i = 0; + for( auto v = value; i < 13; ++i, v <<= 5 ) { + if( (v & mask) > 0 ) { + l = i; + } + } + + return l + 1; } /** * Returns the suffix of the %name */ constexpr name suffix()const { - uint32_t remaining_bits_after_last_actual_dot{}; - uint32_t tmp{}; - for( int32_t remaining_bits{59}; 4 <= remaining_bits; remaining_bits -= 5 ) { // Remaining_bits must remain signed integer - // Get characters in name, one-by-one, in order from left to right (not including the 13th character) - uint8_t ch = (value >> remaining_bits) & 0x1FULL; - if( ch ) // if this character is not a dot - remaining_bits_after_last_actual_dot = tmp; - else // if this character is a dot + uint32_t remaining_bits_after_last_actual_dot = 0; + uint32_t tmp = 0; + for( int32_t remaining_bits = 59; remaining_bits >= 4; remaining_bits -= 5 ) { // Note: remaining_bits must remain signed integer + // Get characters one-by-one in name in order from left to right (not including the 13th character) + auto c = (value >> remaining_bits) & 0x1Full; + if( !c ) { // if this character is a dot tmp = static_cast(remaining_bits); + } else { // if this character is not a dot + remaining_bits_after_last_actual_dot = tmp; + } } - uint64_t thirteenth_character{value & 0x0FULL}; - if( thirteenth_character ) // if 13th character is not a dot + uint64_t thirteenth_character = value & 0x0Full; + if( thirteenth_character ) { // if 13th character is not a dot remaining_bits_after_last_actual_dot = tmp; + } if( remaining_bits_after_last_actual_dot == 0 ) // there is no actual dot in the %name other than potentially leading dots return name{value}; - // At this point remaining_bits_after_last_actual_dot has to be within the range of 4 to 59 (and restricted to increments of 5) + // At this point remaining_bits_after_last_actual_dot has to be within the range of 4 to 59 (and restricted to increments of 5). - // Mask for remaining bits corresponding to characters after last actual dot, except for 4 least significant bits (corresponds to 13th character) - uint64_t mask{(1ULL << remaining_bits_after_last_actual_dot) - 16}; - uint32_t shift{64 - remaining_bits_after_last_actual_dot}; + // Mask for remaining bits corresponding to characters after last actual dot, except for 4 least significant bits (corresponds to 13th character). + uint64_t mask = (1ull << remaining_bits_after_last_actual_dot) - 16; + uint32_t shift = 64 - remaining_bits_after_last_actual_dot; return name{ ((value & mask) << shift) + (thirteenth_character << (shift-1)) }; } @@ -171,103 +171,78 @@ namespace eosio { /** * Explicit cast to bool of the uint64_t value of the name * - * @return Returns true if the name is set to the default value of 0 else true + * @return Returns true if the name is set to the default value of 0 else true. */ constexpr explicit operator bool()const { return value != 0; } /** - * Writes the %name as a string to char buffer + * Writes the %name as a string to the provided char buffer * * @pre Appropriate Size Precondition: (begin + 13) <= end and (begin + 13) does not overflow * @pre Valid Memory Region Precondition: The range [begin, end) must be a valid range of memory to write to. * @param begin - The start of the char buffer * @param end - Just past the end of the char buffer * @return char* - Just past the end of the last character written (returns begin if the Appropriate Size Precondition is not satisfied) - * @post If the Appropriate Size Precondition is satisfied, the range [begin, returned pointer) contains the string representation of the %name + * @post If the Appropriate Size Precondition is satisfied, the range [begin, returned pointer) contains the string representation of the %name. */ char* write_as_string( char* begin, char* end )const { - static const char* charmap{".12345abcdefghijklmnopqrstuvwxyz"}; - constexpr uint64_t mask{0xF800000000000000ULL}; + static const char* charmap = ".12345abcdefghijklmnopqrstuvwxyz"; + constexpr uint64_t mask = 0xF800000000000000ull; - if( (begin + 13) < begin || end < (begin + 13) ) return begin; + if( (begin + 13) < begin || (begin + 13) > end ) return begin; - uint64_t val{value}; - for( uint8_t i{}; i < 13; ++i, val <<= 5 ) { - if( val == 0 ) return begin; + auto v = value; + for( auto i = 0; i < 13; ++i, v <<= 5 ) { + if( v == 0 ) return begin; - uint8_t index = (val & mask) >> ((i == 12) ? 60 : 59); - *begin = charmap[index]; + auto indx = (v & mask) >> (i == 12 ? 60 : 59); + *begin = charmap[indx]; ++begin; } + return begin; } /** * Returns the name as a string. * - * @brief Returns the name value as a string by calling write_as_string(); returning the buffer produced by write_as_string() + * @brief Returns the name value as a string by calling write_as_string() and returning the buffer produced by write_as_string() */ std::string to_string()const { char buffer[13]; - char* end{write_as_string( buffer, buffer + sizeof(buffer) )}; + auto end = write_as_string( buffer, buffer + sizeof(buffer) ); return {buffer, end}; } /** - * Equivalency operator. Returns true if a == b + * Equivalency operator. Returns true if a == b (are the same) * * @return boolean - true if both provided %name values are the same */ - friend constexpr bool operator==( const name& a, const name& b ) { + friend constexpr bool operator == ( const name& a, const name& b ) { return a.value == b.value; } /** - * Inverted equivalency operator. Returns true if a != b + * Inverted equivalency operator. Returns true if a != b (are different) * * @return boolean - true if both provided %name values are not the same */ - friend constexpr bool operator!=( const name& a, const name& b ) { + friend constexpr bool operator != ( const name& a, const name& b ) { return a.value != b.value; } /** - * Less than operator. Returns true if a < b + * Less than operator. Returns true if a < b. * - * @return boolean - true if %name a is less than b + * @return boolean - true if %name `a` is less than `b` */ - friend constexpr bool operator<( const name& a, const name& b ) { + friend constexpr bool operator < ( const name& a, const name& b ) { return a.value < b.value; } - /** - * Less than or equal to operator. Returns true if a <= b - * - * @return boolean - true if %name a is less than or equal to b - */ - friend constexpr bool operator<=( const name& a, const name& b ) { - return a.value <= b.value; - } - - /** - * Greater than operator. Returns true if a > b - * - * @return boolean - true if %name a is greater than b - */ - friend constexpr bool operator>( const name& a, const name& b ) { - return a.value > b.value; - } - /** - * Greater than or equal to operator. Returns true if a >= b - * - * @return boolean - true if %name a is greater than or equal to b - */ - friend constexpr bool operator>=( const name& a, const name& b ) { - return a.value >= b.value; - } - - uint64_t value{}; + uint64_t value = 0; EOSLIB_SERIALIZE( name, (value) ) }; diff --git a/libraries/eosiolib/symbol.hpp b/libraries/eosiolib/symbol.hpp index 9f61b53ac7..4711014920 100644 --- a/libraries/eosiolib/symbol.hpp +++ b/libraries/eosiolib/symbol.hpp @@ -4,14 +4,14 @@ */ #pragma once -#include "name.hpp" +#include "system.hpp" #include "print.h" +#include "name.hpp" #include "serialize.hpp" -#include "system.hpp" +#include #include #include -#include namespace eosio { @@ -32,22 +32,20 @@ namespace eosio { /** * Default constructor, construct a new symbol_code * - * @brief Construct a new symbol_code object with a default value of 0 + * @brief Construct a new symbol_code object defaulting to a value of 0 * */ - constexpr symbol_code() - : value{} - {} + constexpr symbol_code() : value(0) {} /** - * Construct a new symbol_code with a scoped enumerated type of raw uint64_t. + * Construct a new symbol_code given a scoped enumerated type of raw (uint64_t). * * @brief Construct a new symbol_code object initialising value with raw * @param raw - The raw value which is a scoped enumerated type of unit64_t * */ constexpr explicit symbol_code( uint64_t raw ) - : value{raw} + :value(raw) {} /** @@ -58,13 +56,15 @@ namespace eosio { * */ constexpr explicit symbol_code( std::string_view str ) - : value{} + :value(0) { - if( 7 < str.size() ) + if( str.size() > 7 ) { eosio::check( false, "string is too long to be a valid symbol_code" ); + } for( auto itr = str.rbegin(); itr != str.rend(); ++itr ) { - if( *itr < 'A' || 'Z' < *itr ) + if( *itr < 'A' || *itr > 'Z') { eosio::check( false, "only uppercase letters allowed in symbol_code string" ); + } value <<= 8; value |= *itr; } @@ -75,16 +75,16 @@ namespace eosio { * @return true - if symbol is valid */ constexpr bool is_valid()const { - uint64_t sym{value}; - for ( int i{}; i < 7; ++i ) { - char ch = static_cast(sym & 0xFF); - if ( !('A' <= ch && ch <= 'Z') ) return false; + auto sym = value; + for ( int i=0; i < 7; i++ ) { + char c = (char)(sym & 0xFF); + if ( !('A' <= c && c <= 'Z') ) return false; sym >>= 8; if ( !(sym & 0xFF) ) { do { sym >>= 8; if ( (sym & 0xFF) ) return false; - ++i; + i++; } while( i < 7 ); } } @@ -97,13 +97,13 @@ namespace eosio { * @return length - character length of the provided symbol */ constexpr uint32_t length()const { - uint64_t sym{value}; - uint32_t length{}; - while (sym & 0xFF && length <= 7) { - ++length; + auto sym = value; + uint32_t len = 0; + while (sym & 0xFF && len <= 7) { + len++; sym >>= 8; } - return length; + return len; } /** @@ -133,15 +133,15 @@ namespace eosio { * @post If the Appropriate Size Precondition is satisfied, the range [begin, returned pointer) contains the string representation of the symbol_code. */ char* write_as_string( char* begin, char* end )const { - constexpr uint64_t mask = 0xFFULL; + constexpr uint64_t mask = 0xFFull; if( (begin + 7) < begin || (begin + 7) > end ) return begin; - auto val = value; - for( auto i = 0; i < 7; ++i, val >>= 8 ) { - if( val == 0 ) return begin; + auto v = value; + for( auto i = 0; i < 7; ++i, v >>= 8 ) { + if( v == 0 ) return begin; - *begin = static_cast(val & mask); + *begin = static_cast(v & mask); ++begin; } diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 787a0f9596..5437d2a72f 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -408,7 +408,7 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) silence_output(false); EOSIO_TEST_END -int main(int argc, char** argv) { +int main(int argc, char* argv[]) { EOSIO_TEST(asset_type_test); EOSIO_TEST(extended_asset_type_test); return has_failed(); diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index e51e2dd33a..b9c5cfc15f 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -370,131 +370,7 @@ EOSIO_TEST_BEGIN(name_type_test) silence_output(false); EOSIO_TEST_END -// // Intrinsics defined in `eos/libraries/chain/wasm_interface.cpp` that involve the `name` type -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(set_resource_limits_intrinsic_test) -// intrinsics::set_intrinsic([](name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(get_resource_limits_intrinsic_test) -// intrinsics::set_intrinsic([](name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(set_proposed_producers_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(is_privileged_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(set_privileged_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(get_active_producers_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(check_permission_authorization_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(get_permission_last_used_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(get_account_creation_time_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(get_permission_last_used_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(require_authorization_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(has_authorization_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(require_recipient_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(is_account_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// // Defined in `eos/libraries/chain/wasm_interface.cpp` -// EOSIO_TEST_BEGIN(send_deferred_intrinsic_test) -// intrinsics::set_intrinsic([](uint64_t a) { - -// }); -// EOSIO_TEST_END - -// EOSIO_TEST_BEGIN(is_account_test) -// intrinsics::set_intrinsic([](uint64_t a) { -// if (a == 3) -// return true; -// if (a == 4) -// return true; -// return false; -// }); - -// CHECK_ASSERT("is not an account", ([]() { -// eosio_assert(is_account(5), "is not an account"); -// })); -// CHECK_EQUAL(is_account(3), true); -// CHECK_EQUAL(is_account(4), true); -// CHECK_EQUAL(is_account(6), true); -// REQUIRE_EQUAL(is_account(7), true); -// EOSIO_TEST_END - -int main(int argc, char** argv) { +int main(int argc, char* argv[]) { EOSIO_TEST(name_type_test); return has_failed(); } diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 8c98456871..c8d00e7bf4 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -106,10 +106,14 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) REQUIRE_EQUAL( symbol_code{0} < symbol_code{"Z"}, true ); REQUIRE_EQUAL( symbol_code{0} < symbol_code{"AAAAAAA"}, true ); REQUIRE_EQUAL( symbol_code{0} < symbol_code{"ZZZZZZZ"}, true ); + + silence_output(false); EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_type_test) + silence_output(true); + // ------------------------------------------ // constructors/constexpr uint64_t raw()const @@ -210,10 +214,14 @@ EOSIO_TEST_BEGIN(symbol_type_test) REQUIRE_EQUAL( (symbol{0} < symbol{"Z",0}), true ); REQUIRE_EQUAL( (symbol{0} < symbol{"AAAAAAA",0}), true ); REQUIRE_EQUAL( (symbol{0} < symbol{"ZZZZZZZ",0}), true ); + + silence_output(false); EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(extended_symbol_type_test) + silence_output(true); + // ------------------------------------ // constructors/get_symbol/get_contract @@ -277,7 +285,7 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) silence_output(false); EOSIO_TEST_END -int main(int argc, char** argv) { +int main(int argc, char* argv[]) { EOSIO_TEST(symbol_code_type_test); EOSIO_TEST(symbol_type_test); EOSIO_TEST(extended_symbol_type_test); diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp new file mode 100644 index 0000000000..c10ec69c7d --- /dev/null +++ b/tests/unit/time_tests.cpp @@ -0,0 +1,300 @@ +#include +#include + +#include + +using eosio::microseconds; +using eosio::time_point; +using eosio::time_point_sec; + +using namespace eosio::native; + +const uint32_t u32MAX = std::numeric_limits::max(); // 18446744073709551615 +const uint32_t u32MIN = std::numeric_limits::min(); // 0 + +const int64_t i64MAX = std::numeric_limits::max(); // 9223372036854775807 +const int64_t i64MIN = std::numeric_limits::min(); // -9223372036854775808 + +// This is interesting. `print_f` won't print the '-' character. +// `printf` won't print anything. +// `printf("%\n",i64MIN);` + +// Note that there are no invariants established to protect this type against overflow. +// For example: if you define a microsecond type from i64MAX and then check that value in +// Or if you check the `seconds` of a (u64MAX / 1000000 + 1), it will overflow. +// Potentially could be abused in a contract? + +// Defined in `eosio.cdt/libraries/eosiolib/time.hpp` +EOSIO_TEST_BEGIN(microseconds_type_test) + silence_output(false); + + // ------------ + // constructors + + // ----------------------------------------------- + /// explicit microseconds(uint64_t)/int64_t count() + REQUIRE_EQUAL( microseconds{}.count(), 0ULL ) + REQUIRE_EQUAL( microseconds{i64MAX}.count(), i64MAX ) + REQUIRE_EQUAL( microseconds{i64MIN}.count(), i64MIN ) + + // ----------------------------- + // static microseconds maximum() + REQUIRE_EQUAL( microseconds::maximum().count(), microseconds{0x7FFFFFFFFFFFFFFFLL}.count() ) + + // ------------------------------------------------------------------------ + // friend microseconds operator+(const microseconds&, const microseconds&) + // friend microseconds operator-(const microseconds&, const microseconds&) + REQUIRE_EQUAL( (microseconds{0LL} + microseconds{1LL}), microseconds{1LL} ) + REQUIRE_EQUAL( (microseconds{0LL} - microseconds{1LL}), microseconds{-1LL} ) + + // ------------------------------------- + // bool operator==(const microseconds&) + // bool operator!=(const microseconds&) + // bool operator<(const microseconds&) + // bool operator<=(const microseconds&) + // bool operator>(const microseconds&) + // bool operator>=(const microseconds&) + REQUIRE_EQUAL( (microseconds{1LL} == microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} != microseconds{0LL}), true ) + REQUIRE_EQUAL( (microseconds{0LL} < microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} <= microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} > microseconds{0LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} >= microseconds{1LL}), true ) + + // ---------------------------------------------- + // microseconds& operator+=(const microseconds&) + // microseconds& operator-=(const microseconds&) + REQUIRE_EQUAL( (microseconds{0LL} += microseconds{1LL}), microseconds{1LL} ) + REQUIRE_EQUAL( (microseconds{0LL} -= microseconds{1LL}), microseconds{-1LL} ) + + // -------------------- + // int64_t to_seconds() + REQUIRE_EQUAL( (microseconds{i64MAX}.to_seconds()), i64MAX / 1000000 ) + + // ------------------------------------ + // inline microseconds seconds(int64_t) + // inline microseconds milliseconds(int64_t) + // inline microseconds minutes(int64_t) + // inline microseconds hours(int64_t) + // inline microseconds days(int64_t) + REQUIRE_EQUAL( eosio::seconds(60LL), microseconds{60LL*1000000LL} ) + REQUIRE_EQUAL( eosio::milliseconds(60LL), microseconds{60LL*1000LL} ) + REQUIRE_EQUAL( eosio::minutes(60LL), microseconds{60LL*60LL*1000000LL} ) + REQUIRE_EQUAL( eosio::hours(60LL), microseconds{60LL*60LL*60LL*1000000LL} ) + REQUIRE_EQUAL( eosio::days(60LL), microseconds{24LL*60LL*60LL*60LL*1000000LL} ) + + silence_output(false); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(time_point_type_test) + silence_output(true); + + // ------------ + // constructors + microseconds ms0{0LL}; + microseconds ms1{1LL}; + microseconds ms2{-1LL}; + microseconds ms_max{i64MAX}; + microseconds ms_min{i64MIN}; + + /// explicit time_point(microseconds)/microseconds& time_since_epoch() + REQUIRE_EQUAL( time_point{ms0}.time_since_epoch(), ms0 ) + REQUIRE_EQUAL( time_point{ms1}.time_since_epoch(), ms1 ) + + // -------------------------- + // uint32_t sec_since_epoch() + REQUIRE_EQUAL( time_point{ms0}.sec_since_epoch(), 0 / 1000000 ) + REQUIRE_EQUAL( time_point{ms1}.sec_since_epoch(), 1 / 1000000 ) + REQUIRE_EQUAL( time_point{ms_max}.sec_since_epoch(), i64MAX / 1000000 ) + REQUIRE_EQUAL( time_point{ms_min}.sec_since_epoch(), i64MIN / 1000000 ) + + // ----------------------------------- + // bool operator==(const time_point&) + // bool operator!=(const time_point&) + // bool operator<(const time_point&) + // bool operator<=(const time_point&) + // bool operator>(const time_point&) + // bool operator>=(const time_point&) + REQUIRE_EQUAL( (time_point{ms0} == time_point{ms0}), true ) + REQUIRE_EQUAL( (time_point{ms1} != time_point{ms0}), true ) + REQUIRE_EQUAL( (time_point{ms0} < time_point{ms1}), true ) + REQUIRE_EQUAL( (time_point{ms1} <= time_point{ms1}), true ) + REQUIRE_EQUAL( (time_point{ms1} > time_point{ms0}), true ) + REQUIRE_EQUAL( (time_point{ms1} >= time_point{ms1}), true ) + + // -------------------------------------------- + // time_point& operator+=(const microseconds&) + // time_point& operator-=(const microseconds&) + REQUIRE_EQUAL( (time_point{ms0} += ms1), time_point{ms1} ) + REQUIRE_EQUAL( (time_point{ms0} -= ms1), time_point{ms2} ) + + // ------------------------------------------ + // time_point operator+(const microseconds&) + // time_point operator+(const time_point&) + // time_point operator-(const microseconds&) + // microseconds operator-(const time_point&) + REQUIRE_EQUAL( (time_point{ms0} + ms1), time_point{ms1} ) + REQUIRE_EQUAL( (time_point{ms0} + time_point{ms1}), time_point{ms1} ) + REQUIRE_EQUAL( (time_point{ms0} - ms1), time_point{ms2} ) + REQUIRE_EQUAL( (time_point{ms0} - time_point{ms1}), ms2 ) + + silence_output(false); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(time_point_sec_type_test) + silence_output(false); + + // ------------ + // constructors + microseconds ms0{0LL}; + microseconds ms1{1LL}; + microseconds ms2{-1LL}; + microseconds ms_max{i64MAX}; + microseconds ms_min{i64MIN}; + + time_point tp0{ms0}; + time_point tp1{ms1}; + time_point tp2{ms2}; + time_point tp_max{ms_max}; + time_point tp_min{ms_min}; + + /// time_point_sec()/uint32_t sec_since_epoch()const + REQUIRE_EQUAL( time_point_sec{}.sec_since_epoch(), 0 ) + + /// explicit time_point_sec(uint32_t) + REQUIRE_EQUAL( time_point_sec{u32MAX}.sec_since_epoch(), u32MAX ) + REQUIRE_EQUAL( time_point_sec{u32MAX + 1}.sec_since_epoch(), 0 ) + + /// time_point_sec(const time_point&) + REQUIRE_EQUAL( time_point_sec{tp0}.sec_since_epoch(), 0LL / 1000000LL ) + REQUIRE_EQUAL( time_point_sec{tp1}.sec_since_epoch(), 1LL / 1000000LL ) + REQUIRE_EQUAL( time_point_sec{tp2}.sec_since_epoch(), -1LL / 1000000LL ) + REQUIRE_EQUAL( time_point_sec{tp_max}.sec_since_epoch(), i64MAX / 1000000LL ) + REQUIRE_EQUAL( time_point_sec{tp_min}.sec_since_epoch(), i64MIN / 1000000LL ) + + // ------------------------------- + // static time_point_sec maximum() + REQUIRE_EQUAL( time_point_sec{}.maximum().utc_seconds, 0xFFFFFFFF ) + REQUIRE_EQUAL( time_point_sec{}.maximum().utc_seconds != 1, true ) + + // --------------------------- + // static time_point_sec min() + REQUIRE_EQUAL( time_point_sec{}.min().utc_seconds, 0 ) + REQUIRE_EQUAL( time_point_sec{}.min().utc_seconds != 1, true ) + + // Millisecond data is lost when using these data types in certain ways. + // For Example: + // microseconds mms0{1}; + // eosio::print_f("_count: %\n",mms0._count); + // time_point ttp0{mms0}; + // eosio::print_f("_count: %\n",ttp0.elapsed._count); + // time_point_sec ttps0{ttp0}; + // eosio::print_f("utc_seconds: %\n",ttps0.utc_seconds); + // ttp0 = ttps0; + // eosio::print_f("_count: %\n",ttp0.elapsed._count); + // + // Also may have found a `print_f` bug/ + // To replicate: + // eosio::print_f("%", time_point{time_point_sec{tp_max}}.elapsed.count()); + // eosio::print_f("%", 9223372036854775807 / 1000000); + // -------------------------- + // operator time_point()const + REQUIRE_EQUAL( (time_point{time_point_sec{tp0}}.elapsed.count()), 0 ) + REQUIRE_EQUAL( (time_point{time_point_sec{tp1}}.elapsed.count()), 0 ) + REQUIRE_EQUAL( (time_point{time_point_sec{tp2}}.elapsed.count()), 0 ) + REQUIRE_EQUAL( (time_point{time_point_sec{tp_max}}.elapsed._count / 1000000), ms_max.count() / 1000000 ) + REQUIRE_EQUAL( (time_point{time_point_sec{tp_min}}.elapsed._count / 1000000), ms_min.count() / 1000000 ) + + // ------------------------------------------- + // time_point_sec operator=(const time_point&) + + // -------------------------------------------------------------------- + // friend bool operator==(const time_point_sec&, const time_point_sec&) + // friend bool operator!=(const time_point_sec&, const time_point_sec&) + // friend bool operator<(const time_point_sec&, const time_point_sec&) + // friend bool operator<=(const time_point_sec&, const time_point_sec&) + // friend bool operator>(const time_point_sec&, const time_point_sec&) + // friend bool operator>=(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp1} != time_point{tp0}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp1}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp1} <= time_point{tp1}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp1} > time_point{tp0}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp1} >= time_point{tp1}), true ) + + + // ------------------------------------ + // time_point_sec& operator+=(uint32_t) + // time_point_sec& operator+=(microseconds) + // time_point_sec& operator+=(time_point_sec) + // time_point_sec& operator-=(uint32_t) + // time_point_sec& operator-=(microseconds) + // time_point_sec& operator-=(time_point_sec) + // time_point_sec operator+(uint32_t)const + // time_point_sec operator-(uint32_t)const + + + // ----------------------------------------------------------------------- + // friend time_point operator+(const time_point_sec&, const microseconds&) + // friend time_point operator-(const time_point_sec&, const microseconds&) + // friend microseconds operator-(const time_point_sec&, const time_point_sec&) + // friend microseconds operator-(const time_point&, const time_point_sec&) + + + silence_output(false); +EOSIO_TEST_END + +EOSIO_TEST_BEGIN(block_timestamp_type_test) + silence_output(true); + + // ------------ + // constructors + + /// explicit block_timestamp(uint32_t) + + /// block_timestamp(const time_point&) + + /// block_timestamp(const time_point&) + + // -------------------------------- + // static block_timestamp maximum() + + // ---------------------------- + // static block_timestamp min() + + // ---------------------- + // block_timestamp next() + + // ------------------------------- + // time_point to_time_point()const + + // -------------------------- + // operator time_point()const + + // -------------------------------------- + // void set_time_point(const time_point&) + + // ------------------------------------------ + // void set_time_point(const time_point_sec&) + + // --------------------------------- + // void operator=(const time_point&) + + // --------------------------------------- + // bool operator==(const block_timestamp&) + // bool operator!=(const block_timestamp&) + // bool operator<(const block_timestamp&) + // bool operator<=(const block_timestamp&) + // bool operator>(const block_timestamp&) + // bool operator>=(const block_timestamp&) + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char* argv[]) { + // EOSIO_TEST(microseconds_type_test); + // EOSIO_TEST(time_point_type_test); + EOSIO_TEST(time_point_sec_type_test); + // EOSIO_TEST(block_timestamp_type_test); + return has_failed(); +} From 39845b08d4a774292c74e4d0f7fa9142e1e348a5 Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 17 Jan 2019 18:25:57 -0500 Subject: [PATCH 11/51] Refactor tests to be more succinct --- tests/unit/asset_tests.cpp | 159 ++++++------ tests/unit/eosio_tests.cpp | 19 ++ tests/unit/name_tests.cpp | 505 ++++++++++++++++++------------------ tests/unit/symbol_tests.cpp | 349 +++++++++++++------------ tests/unit/system_tests.cpp | 50 ++-- tests/unit/time_tests.cpp | 114 ++++++-- 6 files changed, 665 insertions(+), 531 deletions(-) create mode 100644 tests/unit/eosio_tests.cpp diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 5437d2a72f..4a27548aaa 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -1,10 +1,14 @@ -#include #include - #include -using namespace eosio; -using namespace eosio::native; +using eosio::name; +using eosio::symbol; +using eosio::extended_symbol; +using eosio::asset; +using eosio::extended_asset; + +const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL +const uint64_t u64min = std::numeric_limits::min(); // 0ULL // For incrementing the `precision` in any test relating to `precision` // The `precision` field in the `symbol` type must be constructed via constexpr, @@ -13,7 +17,14 @@ constexpr uint8_t increment_precision( uint8_t precision ) { return ++precision; // Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) - silence_output(true); + silence_output(false); + + symbol s0{"A", 0}; + symbol s1{"Z", 0}; + symbol s2{"AAAAAAA", 0}; + symbol s3{"ZZZZZZZ", 0}; + symbol s4{"SYMBOLL", 0}; + symbol s5{"SYMBOLL", 63}; // -------------------------------------------------------------------- // constructors/bool is_amount_within_range()const/bool is_valid()const @@ -23,33 +34,33 @@ EOSIO_TEST_BEGIN(asset_type_test) REQUIRE_EQUAL( asset{}.symbol.raw(), 0ULL ); /// constexpr asset(int64_t, symbol) - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL",0}}.amount), 0 ); - REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), 4611686018427387903LL ); - REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), -4611686018427387903LL ); + REQUIRE_EQUAL( (asset{0LL, s4}.amount), 0LL ); + REQUIRE_EQUAL( (asset{4611686018427387903LL, s4}.amount), 4611686018427387903LL ); + REQUIRE_EQUAL( (asset{-4611686018427387903LL, s4}.amount), -4611686018427387903LL ); - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"A",0}}.symbol.raw()), 16640ULL ); // "A", precision: 0 - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"Z",0}}.symbol.raw()), 23040ULL ); // "Z", precision: 0 - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"AAAAAAA",0}}.symbol.raw()), 4702111234474983680ULL ); // "AAAAAAA", precision: 0 - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"ZZZZZZZ",0}}.symbol.raw()), 6510615555426900480ULL ); // "ZZZZZZZ", precision: 0 + REQUIRE_EQUAL( (asset{0LL, s0}.symbol.raw()), 16640ULL ); // "A", precision: 0 + REQUIRE_EQUAL( (asset{0LL, s1}.symbol.raw()), 23040ULL ); // "Z", precision: 0 + REQUIRE_EQUAL( (asset{0LL, s2}.symbol.raw()), 4702111234474983680ULL ); // "AAAAAAA", precision: 0 + REQUIRE_EQUAL( (asset{0LL, s3}.symbol.raw()), 6510615555426900480ULL ); // "ZZZZZZZ", precision: 0 - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{int64_t{}, symbol{16639ULL}};}) ); - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{int64_t{}, symbol{6510615555426900736ULL}};}) ); + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{16639ULL}};}) ); + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{6510615555426900736ULL}};}) ); - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{int64_t{4611686018427387904LL}, symbol{}};}) ); - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{int64_t{-4611686018427387904LL}, symbol{}};}) ); + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ 4611686018427387904LL, {}};}) ); + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{-4611686018427387904LL, {}};}) ); // ------------------------ // void set_amount(int64_t) - asset a{int64_t{0}, symbol{"SYMBOLL", 1}}; - REQUIRE_EQUAL( (a.set_amount(0), a.amount), 0); + asset a{0LL, s4}; + REQUIRE_EQUAL( (a.set_amount(0LL), a.amount), 0LL); REQUIRE_EQUAL( (a.set_amount(1LL), a.amount), 1LL); REQUIRE_EQUAL( (a.set_amount(4611686018427387903LL), a.amount), 4611686018427387903LL); REQUIRE_EQUAL( (a.set_amount(-4611686018427387903LL), a.amount), -4611686018427387903LL); REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( - []() {asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(4611686018427387904LL);}) ); + [&]() {asset{0LL, s4}.set_amount(4611686018427387904LL);}) ); REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( - []() {asset{int64_t{0}, symbol{"SYMBOLL",0}}.set_amount(-4611686018427387904LL);}) ); + [&]() {asset{0LL, s4}.set_amount(-4611686018427387904LL);}) ); // --------------------------------------- // std::to_string()const/void print()const @@ -63,104 +74,104 @@ EOSIO_TEST_BEGIN(asset_type_test) // output: "1. SYMBOLL" // Note that `assets` with negative amounts with 0 `precision` print have the odd behavior of printing two '-' characters: - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}}.to_string()), + REQUIRE_EQUAL( (asset{0LL, s4}.to_string()), "0. SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{-0}, symbol{"SYMBOLL", 0}}.to_string()), + REQUIRE_EQUAL( (asset{-0LL}, s4}.to_string()), "0. SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 63}}.to_string()), + REQUIRE_EQUAL( (asset{0LL, s5}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{-0}, symbol{"SYMBOLL", 63}}.to_string()), + REQUIRE_EQUAL( (asset{-0LL, s5}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.to_string()), + REQUIRE_EQUAL( (asset{1LL, s4}.to_string()), "1. SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}.to_string()), + REQUIRE_EQUAL( (asset{-1LL, s4}.to_string()), "--1. SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}.to_string()), + REQUIRE_EQUAL( (asset{1LL, s5}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}.to_string()), + REQUIRE_EQUAL( (asset{-1LL, s5}.to_string()), "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.to_string()), + REQUIRE_EQUAL( (asset{4611686018427387903LL, s4}.to_string()), "4611686018427387903. SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.to_string()), + REQUIRE_EQUAL( (asset{-4611686018427387903LL, s4}.to_string()), "--4611686018427387903. SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}.to_string()), + REQUIRE_EQUAL( (asset{4611686018427387903LL, s5}.to_string()), "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); - REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}.to_string()), + REQUIRE_EQUAL( (asset{-4611686018427387903LL, s5}.to_string()), "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); // How are these `assets` constructed? Isn't `uint8_t precision` supposed to be constexpr when constructing a symbol? for( uint8_t precision{0}; precision < 64; ++precision ) { - REQUIRE_EQUAL( (asset{int64_t{0LL}, symbol{"SYMBOLL", precision}}.to_string()), + REQUIRE_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ); } // ---------------------- // asset operator-()const - REQUIRE_EQUAL( (-asset{int64_t{0}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{-0}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{0}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 63}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{-0}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{0}, symbol{"SYMBOLL", 63}}.amount) ); - - REQUIRE_EQUAL( (-asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}.amount), (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}.amount), (asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}.amount) ); - - REQUIRE_EQUAL( (-asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), - (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount), - (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount), - (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount) ); - REQUIRE_EQUAL( (-asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount), - (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}.amount) ); + REQUIRE_EQUAL( (-asset{ 0LL}, s4}.amount), (asset{0LL, s4}.amount) ); + REQUIRE_EQUAL( (-asset{-0LL}, s4}.amount), (asset{0LL, s4}.amount) ); + REQUIRE_EQUAL( (-asset{ 0LL}, s5}.amount), (asset{0LL, s5}.amount) ); + REQUIRE_EQUAL( (-asset{-0LL}, s5}.amount), (asset{0LL, s5}.amount) ); + + REQUIRE_EQUAL( (-asset{ 1LL, s4}.amount), (asset{-1LL}, s4}.amount) ); + REQUIRE_EQUAL( (-asset{-1LL, s4}.amount), (asset{ 1LL}, s4}.amount) ); + REQUIRE_EQUAL( (-asset{ 1LL, s5}.amount), (asset{-1LL}, s5}.amount) ); + REQUIRE_EQUAL( (-asset{-1LL, s5}.amount), (asset{ 1LL}, s5}.amount) ); + + REQUIRE_EQUAL( (-asset{ 4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount), + ( asset{-4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{-4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount), + ( asset{ 4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{ 4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount), + ( asset{-4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount) ); + REQUIRE_EQUAL( (-asset{-4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount), + ( asset{ 4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount) ); // ------------------------------------------------------------------------------------------ // inline friend asset& operator+(const asset&, const asset&)/asset& operator+=(const asset&) - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} += asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), - (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (asset{0LL, s4} += asset{0LL, s4} ), + (asset{0LL, s4}) ); - REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} += asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}} ), - (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (asset{1LL, s4} += asset{-1LL, s4} ), + (asset{0LL, s4}) ); REQUIRE_ASSERT( "attempt to add asset with different symbol", ( - []() {asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} += asset{int64_t{1LL}, symbol{"LLOBMYS", 0}};}) ); + []() {asset{1LL}, s4} += asset{1LL, symbol{"LLOBMYS", 0}};}) ); REQUIRE_ASSERT( "addition underflow", ( - []() {asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} += -asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + []() {asset{-4611686018427387903LL}, s4} += -asset{1LL, s4};}) ); REQUIRE_ASSERT( "addition overflow", ( - []() {asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} += asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + []() {asset{ 4611686018427387903LL}, s4} += asset{1LL, s4};}) ); // ------------------------------------------------------------------------------------------ // inline friend asset& operator-(const asset&, const asset&)/asset& operator-=(const asset&) - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} -= asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), - (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (asset{0LL, s4} -= asset{0LL, s4} ), + (asset{0LL, s4}) ); - REQUIRE_EQUAL( (asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} -= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), - (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (asset{1LL, s4} -= asset{1LL, s4} ), + (asset{0LL, s4}) ); REQUIRE_ASSERT( "attempt to subtract asset with different symbol", ( - []() {asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} -= asset{int64_t{1LL}, symbol{"LLOBMYS", 0}};}) ); + []() {asset{1LL, s4} -= asset{1LL, symbol{"LLOBMYS", 0}};}) ); REQUIRE_ASSERT( "subtraction underflow", ( - []() {asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} -= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + []() {asset{-4611686018427387903LL}, s4} -= asset{1LL, s4};}) ); REQUIRE_ASSERT( "subtraction overflow", ( - []() {asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} -= -asset{int64_t{1LL}, symbol{"SYMBOLL", 0}};}) ); + []() {asset{ 4611686018427387903LL}, s4} -= -asset{1LL, s4};}) ); // ----------------------------------------------------------------------- // friend asset operator*(const asset&, int64_t)/asset operator*=(int64_t) - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} *= 0 ), - (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (asset{ 0LL}, s4} *= 0 ), + (asset{ 0LL}, s4}) ); - REQUIRE_EQUAL( (asset{int64_t{2}, symbol{"SYMBOLL", 0}} *= 1LL ), - (asset{int64_t{2}, symbol{"SYMBOLL", 0}}) ); - REQUIRE_EQUAL( (asset{int64_t{2}, symbol{"SYMBOLL", 0}} *= -1LL ), - (asset{int64_t{-2}, symbol{"SYMBOLL", 0}}) ); + REQUIRE_EQUAL( (asset{ 2LL}, s4} *= 1LL ), + (asset{ 2LL}, s4}) ); + REQUIRE_EQUAL( (asset{ 2LL}, s4} *= -1LL ), + (asset{-2LL}, s4}) ); REQUIRE_ASSERT( "multiplication overflow", ( - []() {asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} *= 2LL;}) ); + []() {asset{ 4611686018427387903LL}, s4} *= 2LL;}) ); REQUIRE_ASSERT( "multiplication underflow", ( - []() {asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} *= 2LL;}) ); + []() {asset{-4611686018427387903LL}, s4} *= 2LL;}) ); // // ------------------------------------------------------------------------ // // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) @@ -173,7 +184,7 @@ EOSIO_TEST_BEGIN(asset_type_test) // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); - REQUIRE_ASSERT( "divide by zero", ( []() {asset{int64_t{1}, symbol{"SYMBOLL", 0}} /= 0;}) ); + //REQUIRE_ASSERT( "divide by zero", ( []() {asset{int64_t{1}, symbol{"SYMBOLL", 0}} /= 0;}) ); /////////////////////////////////////////////////////////////////////////////////////////////////// // Why is this not throwing?? // And why is my addition test throwing?? diff --git a/tests/unit/eosio_tests.cpp b/tests/unit/eosio_tests.cpp new file mode 100644 index 0000000000..803b792816 --- /dev/null +++ b/tests/unit/eosio_tests.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace eosio; +using namespace eosio::native; + +// Defined in `eosio.cdt/libraries/eosiolib/eosio.hpp` +EOSIO_TEST_BEGIN(eosio_test) + silence_output(false); + + + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char* argv[]) { + EOSIO_TEST(eosio_test); + return has_failed(); +} diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index b9c5cfc15f..6f53449bb8 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,11 +1,10 @@ -#include #include +#include -using namespace eosio; -using namespace eosio::native; +using eosio::name; -const uint64_t u64MAX = std::numeric_limits::max(); // 18446744073709551615 -const uint64_t u64MIN = std::numeric_limits::min(); // 0 +const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL +const uint64_t u64min = std::numeric_limits::min(); // 0ULL // Defined in `eosio.cdt/libraries/eosiolib/name.hpp` EOSIO_TEST_BEGIN(name_type_test) @@ -15,163 +14,161 @@ EOSIO_TEST_BEGIN(name_type_test) // constructors /// constexpr name() - REQUIRE_EQUAL( name{}.value, 0ULL ); + CHECK_EQUAL( name{}.value, 0ULL ) /// constexpr explicit name(uint64_t) - REQUIRE_EQUAL( name{0ULL}.value, 0ULL ); - REQUIRE_EQUAL( name{1ULL}.value, 1ULL ); - REQUIRE_EQUAL( name{u64MAX}.value, u64MAX ); + CHECK_EQUAL( name{0ULL}.value, 0ULL ) + CHECK_EQUAL( name{1ULL}.value, 1ULL ) + CHECK_EQUAL( name{u64max}.value, u64max ) /// constexpr explicit name(name::raw) - REQUIRE_EQUAL( name{name::raw{0ULL}}.value, 0ULL ); - REQUIRE_EQUAL( name{name::raw{1ULL}}.value, 1ULL ); - REQUIRE_EQUAL( name{name::raw{u64MAX}}.value, u64MAX ); + CHECK_EQUAL( name{name::raw{0ULL}}.value, 0ULL ) + CHECK_EQUAL( name{name::raw{1ULL}}.value, 1ULL ) + CHECK_EQUAL( name{name::raw{u64max}}.value, u64max ) + // Note that these are the exact `uint64_t` value representations of the given string /// constexpr explicit name(std::string_view) - REQUIRE_EQUAL( name{"1"}.value, 576460752303423488ULL ); - REQUIRE_EQUAL( name{"5"}.value, 2882303761517117440ULL ); - REQUIRE_EQUAL( name{"a"}.value, 3458764513820540928ULL ); - REQUIRE_EQUAL( name{"z"}.value, 17870283321406128128ULL ); - - REQUIRE_EQUAL( name{"abc"}.value, 3589368903014285312ULL ); - REQUIRE_EQUAL( name{"123"}.value, 614178399182651392ULL ); - - REQUIRE_EQUAL( name{".abc"}.value, 112167778219196416ULL ); - REQUIRE_EQUAL( name{".........abc"}.value, 102016ULL ); - REQUIRE_EQUAL( name{"123."}.value, 614178399182651392ULL ); - REQUIRE_EQUAL( name{"123........."}.value, 614178399182651392ULL ); - REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ); + CHECK_EQUAL( name{"1"}.value, 576460752303423488ULL ) + CHECK_EQUAL( name{"5"}.value, 2882303761517117440ULL ) + CHECK_EQUAL( name{"a"}.value, 3458764513820540928ULL ) + CHECK_EQUAL( name{"z"}.value, 17870283321406128128ULL ) + + CHECK_EQUAL( name{"abc"}.value, 3589368903014285312ULL ) + CHECK_EQUAL( name{"123"}.value, 614178399182651392ULL ) + + CHECK_EQUAL( name{".abc"}.value, 112167778219196416ULL ) + CHECK_EQUAL( name{".........abc"}.value, 102016ULL ) + CHECK_EQUAL( name{"123."}.value, 614178399182651392ULL ) + CHECK_EQUAL( name{"123........."}.value, 614178399182651392ULL ) + CHECK_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ) - REQUIRE_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ); - REQUIRE_EQUAL( name{"123.abc"}.value, 614181822271586304ULL ); + CHECK_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ) + CHECK_EQUAL( name{"123.abc"}.value, 614181822271586304ULL ) - REQUIRE_EQUAL( name{"12345abcdefgj"}.value, 614251623682315983ULL ); - REQUIRE_EQUAL( name{"hijklmnopqrsj"}.value, 7754926748989239183ULL ); - REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.value, 14895601873741973071ULL ); + CHECK_EQUAL( name{"12345abcdefgj"}.value, 614251623682315983ULL ) + CHECK_EQUAL( name{"hijklmnopqrsj"}.value, 7754926748989239183ULL ) + CHECK_EQUAL( name{"tuvwxyz.1234j"}.value, 14895601873741973071ULL ) - REQUIRE_EQUAL( name{"111111111111j"}.value, 595056260442243615ULL ); - REQUIRE_EQUAL( name{"555555555555j"}.value, 2975281302211218015ULL ); - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.value, 3570337562653461615ULL ); - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.value, 18446744073709551615ULL ); + CHECK_EQUAL( name{"111111111111j"}.value, 595056260442243615ULL ) + CHECK_EQUAL( name{"555555555555j"}.value, 2975281302211218015ULL ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.value, 3570337562653461615ULL ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.value, u64max ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ); - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ); - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ); - REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ); + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ) + CHECK_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ) + CHECK_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ) + CHECK_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ) // -------------------------------------------- // static constexpr uint8_t char_to_value(char) char c{'.'}; uint8_t expected_value{0}; // Will increment to the expected correct value in the set [0,32) - REQUIRE_EQUAL( name::char_to_value(c), expected_value ); + CHECK_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; for(c = '1'; c <= '5'; ++c ) { - REQUIRE_EQUAL( name::char_to_value(c), expected_value ); + CHECK_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; } for(c = 'a'; c <= 'z'; ++c ) { - REQUIRE_EQUAL( name::char_to_value(c), expected_value ); + CHECK_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; } - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ); - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); - + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); // ------------------------------- // constexpr uint8_t length()cosnt - REQUIRE_EQUAL( name{""}.length(), 0 ); - REQUIRE_EQUAL( name{"e"}.length(), 1 ); - REQUIRE_EQUAL( name{"eo"}.length(), 2 ); - REQUIRE_EQUAL( name{"eos"}.length(), 3 ); - REQUIRE_EQUAL( name{"eosi"}.length(), 4 ); - REQUIRE_EQUAL( name{"eosio"}.length(), 5 ); - REQUIRE_EQUAL( name{"eosioa"}.length(), 6 ); - REQUIRE_EQUAL( name{"eosioac"}.length(), 7 ); - REQUIRE_EQUAL( name{"eosioacc"}.length(), 8 ); - REQUIRE_EQUAL( name{"eosioacco"}.length(), 9 ); - REQUIRE_EQUAL( name{"eosioaccou"}.length(), 10 ); - REQUIRE_EQUAL( name{"eosioaccoun"}.length(), 11 ); - REQUIRE_EQUAL( name{"eosioaccount"}.length(), 12 ); - REQUIRE_EQUAL( name{"eosioaccountj"}.length(), 13 ); - - REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ); + CHECK_EQUAL( name{""}.length(), 0 ) + CHECK_EQUAL( name{"e"}.length(), 1 ) + CHECK_EQUAL( name{"eo"}.length(), 2 ) + CHECK_EQUAL( name{"eos"}.length(), 3 ) + CHECK_EQUAL( name{"eosi"}.length(), 4 ) + CHECK_EQUAL( name{"eosio"}.length(), 5 ) + CHECK_EQUAL( name{"eosioa"}.length(), 6 ) + CHECK_EQUAL( name{"eosioac"}.length(), 7 ) + CHECK_EQUAL( name{"eosioacc"}.length(), 8 ) + CHECK_EQUAL( name{"eosioacco"}.length(), 9 ) + CHECK_EQUAL( name{"eosioaccou"}.length(), 10 ) + CHECK_EQUAL( name{"eosioaccoun"}.length(), 11 ) + CHECK_EQUAL( name{"eosioaccount"}.length(), 12 ) + CHECK_EQUAL( name{"eosioaccountj"}.length(), 13 ) + + CHECK_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ) // ---------------------------- // constexpr name suffix()const - REQUIRE_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ); - REQUIRE_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ); - REQUIRE_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ); - REQUIRE_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ); - REQUIRE_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ); - REQUIRE_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ); - REQUIRE_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ); - REQUIRE_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ); - REQUIRE_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ); - REQUIRE_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ); - REQUIRE_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ); - REQUIRE_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ); - -// Note that this case is ignored because '.' characters at the end of a name are ignored -// Although, it is my goal to make the functions perfect mirrors of eachother evenutally (01/07/2019) -// #ifdef NATIVE_NAME -// print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); -// print_f("Value of expected suffix: \n" ); -// #endif -// eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); - - REQUIRE_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ); - REQUIRE_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ); + CHECK_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ) + CHECK_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ) + CHECK_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ) + CHECK_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ) + CHECK_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ) + CHECK_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ) + CHECK_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ) + CHECK_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ) + CHECK_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ) + CHECK_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ) + CHECK_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ) + CHECK_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ) + + // Note that this case is ignored because '.' characters at the end of a name are ignored + // Make functions perfect mirrors of eachother (01/07/2019) + // print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); + // print_f("Value of expected suffix: \n" ); + // eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); + + CHECK_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ) + CHECK_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ) // ----------------------------- // constexpr operator raw()const - REQUIRE_EQUAL( static_cast(name::raw{name{"1"}}), 576460752303423488ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"5"}}), 2882303761517117440ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"a"}}), 3458764513820540928ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"z"}}), 17870283321406128128ULL ); - - REQUIRE_EQUAL( static_cast(name::raw{name{"abc"}}), 3589368903014285312ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"123"}}), 614178399182651392ULL ); - - REQUIRE_EQUAL( static_cast(name::raw{name{".abc"}}), 112167778219196416ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{".........abc"}}), 102016ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"123."}}), 614178399182651392ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"123........."}}), 614178399182651392ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{".a.b.c.1.2.3."}}), 108209673814966320ULL ); - - REQUIRE_EQUAL( static_cast(name::raw{name{"abc.123"}}), 3589369488740450304ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"123.abc"}}), 614181822271586304ULL ); - - REQUIRE_EQUAL( static_cast(name::raw{name{"12345abcdefgj"}}), 614251623682315983ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"hijklmnopqrsj"}}), 7754926748989239183ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"tuvwxyz.1234j"}}), 14895601873741973071ULL ); - - REQUIRE_EQUAL( static_cast(name::raw{name{"111111111111j"}}), 595056260442243615ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"555555555555j"}}), 2975281302211218015ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"aaaaaaaaaaaaj"}}), 3570337562653461615ULL ); - REQUIRE_EQUAL( static_cast(name::raw{name{"zzzzzzzzzzzzj"}}), 18446744073709551615ULL ); + CHECK_EQUAL( name{"1"}.operator name::raw(), static_cast(576460752303423488ULL) ) + CHECK_EQUAL( name{"5"}.operator name::raw(), static_cast(2882303761517117440ULL) ) + CHECK_EQUAL( name{"a"}.operator name::raw(), static_cast(3458764513820540928ULL) ) + CHECK_EQUAL( name{"z"}.operator name::raw(), static_cast(17870283321406128128ULL) ) + + CHECK_EQUAL( name{"abc"}.operator name::raw(), static_cast(3589368903014285312ULL) ) + CHECK_EQUAL( name{"123"}.operator name::raw(), static_cast(614178399182651392ULL) ) + + CHECK_EQUAL( name{".abc"}.operator name::raw(), static_cast(112167778219196416ULL) ) + CHECK_EQUAL( name{".........abc"}.operator name::raw(), static_cast(102016ULL) ) + CHECK_EQUAL( name{"123."}.operator name::raw(), static_cast(614178399182651392ULL) ) + CHECK_EQUAL( name{"123........."}.operator name::raw(), static_cast(614178399182651392ULL) ) + CHECK_EQUAL( name{".a.b.c.1.2.3."}.operator name::raw(), static_cast(108209673814966320ULL) ) + + CHECK_EQUAL( name{"abc.123"}.operator name::raw(), static_cast(3589369488740450304ULL) ) + CHECK_EQUAL( name{"123.abc"}.operator name::raw(), static_cast(614181822271586304ULL) ) + + CHECK_EQUAL( name{"12345abcdefgj"}.operator name::raw(), static_cast(614251623682315983ULL) ) + CHECK_EQUAL( name{"hijklmnopqrsj"}.operator name::raw(), static_cast(7754926748989239183ULL) ) + CHECK_EQUAL( name{"tuvwxyz.1234j"}.operator name::raw(), static_cast(14895601873741973071ULL) ) + + CHECK_EQUAL( name{"111111111111j"}.operator name::raw(), static_cast(595056260442243615ULL) ) + CHECK_EQUAL( name{"555555555555j"}.operator name::raw(), static_cast(2975281302211218015ULL) ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.operator name::raw(), static_cast(3570337562653461615ULL) ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.operator name::raw(), static_cast(u64max) ) // --------------------------------------- // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` - REQUIRE_EQUAL( name{0}.operator bool(), false ); - REQUIRE_EQUAL( name{1}.operator bool(), true ); - REQUIRE_EQUAL( !name{0}, true ); - REQUIRE_EQUAL( !name{1}, false ); + CHECK_EQUAL( name{0}.operator bool(), false ) + CHECK_EQUAL( name{1}.operator bool(), true ) + CHECK_EQUAL( !name{0}, true ) + CHECK_EQUAL( !name{1}, false ) - REQUIRE_EQUAL( name{""}.operator bool(), false ); - REQUIRE_EQUAL( name{"1"}.operator bool(), true ); - REQUIRE_EQUAL( !name{""}, true ); - REQUIRE_EQUAL( !name{"1"}, false ); + CHECK_EQUAL( name{""}.operator bool(), false ) + CHECK_EQUAL( name{"1"}.operator bool(), true ) + CHECK_EQUAL( !name{""}, true ) + CHECK_EQUAL( !name{"1"}, false ) // ---------------------------------------- // char* write_as_string(char*, char*)const @@ -179,193 +176,193 @@ EOSIO_TEST_BEGIN(name_type_test) std::string test_str{"1"}; name{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // Note that any '.' characters at the end of a name are ignored name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ); + CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) name{test_str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ); + CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) name{test_str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ); + CHECK_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ) name{test_str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // ---------------------------- // std::string to_string()const - REQUIRE_EQUAL( name{"1"}.to_string(), "1" ); - REQUIRE_EQUAL( name{"5"}.to_string(), "5" ); - REQUIRE_EQUAL( name{"a"}.to_string(), "a" ); - REQUIRE_EQUAL( name{"z"}.to_string(), "z" ); - - REQUIRE_EQUAL( name{"abc"}.to_string(), "abc" ); - REQUIRE_EQUAL( name{"123"}.to_string(), "123" ); - - REQUIRE_EQUAL( name{".abc"}.to_string(), ".abc" ); - REQUIRE_EQUAL( name{".........abc"}.to_string(), ".........abc" ); - REQUIRE_EQUAL( name{"123."}.to_string(), "123" ); - REQUIRE_EQUAL( name{"123........."}.to_string(), "123" ); - REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.to_string(), ".a.b.c.1.2.3" ); + CHECK_EQUAL( name{"1"}.to_string(), "1" ) + CHECK_EQUAL( name{"5"}.to_string(), "5" ) + CHECK_EQUAL( name{"a"}.to_string(), "a" ) + CHECK_EQUAL( name{"z"}.to_string(), "z" ) + + CHECK_EQUAL( name{"abc"}.to_string(), "abc" ) + CHECK_EQUAL( name{"123"}.to_string(), "123" ) + + CHECK_EQUAL( name{".abc"}.to_string(), ".abc" ) + CHECK_EQUAL( name{".........abc"}.to_string(), ".........abc" ) + CHECK_EQUAL( name{"123."}.to_string(), "123" ) + CHECK_EQUAL( name{"123........."}.to_string(), "123" ) + CHECK_EQUAL( name{".a.b.c.1.2.3."}.to_string(), ".a.b.c.1.2.3" ) - REQUIRE_EQUAL( name{"abc.123"}.to_string(), "abc.123" ); - REQUIRE_EQUAL( name{"123.abc"}.to_string(), "123.abc" ); + CHECK_EQUAL( name{"abc.123"}.to_string(), "abc.123" ) + CHECK_EQUAL( name{"123.abc"}.to_string(), "123.abc" ) - REQUIRE_EQUAL( name{"12345abcdefgj"}.to_string(), "12345abcdefgj" ); - REQUIRE_EQUAL( name{"hijklmnopqrsj"}.to_string(), "hijklmnopqrsj" ); - REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.to_string(), "tuvwxyz.1234j" ); + CHECK_EQUAL( name{"12345abcdefgj"}.to_string(), "12345abcdefgj" ) + CHECK_EQUAL( name{"hijklmnopqrsj"}.to_string(), "hijklmnopqrsj" ) + CHECK_EQUAL( name{"tuvwxyz.1234j"}.to_string(), "tuvwxyz.1234j" ) - REQUIRE_EQUAL( name{"111111111111j"}.to_string(), "111111111111j" ); - REQUIRE_EQUAL( name{"555555555555j"}.to_string(), "555555555555j" ); - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ); - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ); + CHECK_EQUAL( name{"111111111111j"}.to_string(), "111111111111j" ) + CHECK_EQUAL( name{"555555555555j"}.to_string(), "555555555555j" ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ) // ---------------------------------------------------------- // friend constexpr bool operator==(const name&, const name&) - REQUIRE_EQUAL( name{"1"} == name{"1"}, true ); - REQUIRE_EQUAL( name{"5"} == name{"5"}, true ); - REQUIRE_EQUAL( name{"a"} == name{"a"}, true ); - REQUIRE_EQUAL( name{"z"} == name{"z"}, true ); - - REQUIRE_EQUAL( name{"abc"} == name{"abc"}, true ); - REQUIRE_EQUAL( name{"123"} == name{"123"}, true ); - - REQUIRE_EQUAL( name{".abc"} == name{".abc"}, true ); - REQUIRE_EQUAL( name{".........abc"} == name{".........abc"}, true ); - REQUIRE_EQUAL( name{"123."} == name{"123"}, true ); - REQUIRE_EQUAL( name{"123........."} == name{"123"}, true ); - REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == name{".a.b.c.1.2.3"}, true ); + CHECK_EQUAL( name{"1"} == name{"1"}, true ) + CHECK_EQUAL( name{"5"} == name{"5"}, true ) + CHECK_EQUAL( name{"a"} == name{"a"}, true ) + CHECK_EQUAL( name{"z"} == name{"z"}, true ) + + CHECK_EQUAL( name{"abc"} == name{"abc"}, true ) + CHECK_EQUAL( name{"123"} == name{"123"}, true ) + + CHECK_EQUAL( name{".abc"} == name{".abc"}, true ) + CHECK_EQUAL( name{".........abc"} == name{".........abc"}, true ) + CHECK_EQUAL( name{"123."} == name{"123"}, true ) + CHECK_EQUAL( name{"123........."} == name{"123"}, true ) + CHECK_EQUAL( name{".a.b.c.1.2.3."} == name{".a.b.c.1.2.3"}, true ) - REQUIRE_EQUAL( name{"abc.123"} == name{"abc.123"}, true ); - REQUIRE_EQUAL( name{"123.abc"} == name{"123.abc"}, true ); + CHECK_EQUAL( name{"abc.123"} == name{"abc.123"}, true ) + CHECK_EQUAL( name{"123.abc"} == name{"123.abc"}, true ) - REQUIRE_EQUAL( name{"12345abcdefgj"} == name{"12345abcdefgj"}, true ); - REQUIRE_EQUAL( name{"hijklmnopqrsj"} == name{"hijklmnopqrsj"}, true ); - REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == name{"tuvwxyz.1234j"}, true ); + CHECK_EQUAL( name{"12345abcdefgj"} == name{"12345abcdefgj"}, true ) + CHECK_EQUAL( name{"hijklmnopqrsj"} == name{"hijklmnopqrsj"}, true ) + CHECK_EQUAL( name{"tuvwxyz.1234j"} == name{"tuvwxyz.1234j"}, true ) - REQUIRE_EQUAL( name{"111111111111j"} == name{"111111111111j"}, true ); - REQUIRE_EQUAL( name{"555555555555j"} == name{"555555555555j"}, true ); - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ); - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ); + CHECK_EQUAL( name{"111111111111j"} == name{"111111111111j"}, true ) + CHECK_EQUAL( name{"555555555555j"} == name{"555555555555j"}, true ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ) // ----------------------------------------------------------- // friend constexpr bool operator!=(const name&, const name&) - REQUIRE_EQUAL( name{"1"} != name{0}, true ); - REQUIRE_EQUAL( name{"5"} != name{0}, true ); - REQUIRE_EQUAL( name{"a"} != name{0}, true ); - REQUIRE_EQUAL( name{"z"} != name{0}, true ); - - REQUIRE_EQUAL( name{"abc"} != name{0}, true ); - REQUIRE_EQUAL( name{"123"} != name{0}, true ); - - REQUIRE_EQUAL( name{".abc"} != name{0}, true ); - REQUIRE_EQUAL( name{".........abc"} != name{0}, true ); - REQUIRE_EQUAL( name{"123."} != name{0}, true ); - REQUIRE_EQUAL( name{"123........."} != name{0}, true ); - REQUIRE_EQUAL( name{".a.b.c.1.2.3."} != name{0}, true ); + CHECK_EQUAL( name{"1"} != name{}, true ) + CHECK_EQUAL( name{"5"} != name{}, true ) + CHECK_EQUAL( name{"a"} != name{}, true ) + CHECK_EQUAL( name{"z"} != name{}, true ) + + CHECK_EQUAL( name{"abc"} != name{}, true ) + CHECK_EQUAL( name{"123"} != name{}, true ) + + CHECK_EQUAL( name{".abc"} != name{}, true ) + CHECK_EQUAL( name{".........abc"} != name{}, true ) + CHECK_EQUAL( name{"123."} != name{}, true ) + CHECK_EQUAL( name{"123........."} != name{}, true ) + CHECK_EQUAL( name{".a.b.c.1.2.3."} != name{}, true ) - REQUIRE_EQUAL( name{"abc.123"} != name{0}, true ); - REQUIRE_EQUAL( name{"123.abc"} != name{0}, true ); + CHECK_EQUAL( name{"abc.123"} != name{}, true ) + CHECK_EQUAL( name{"123.abc"} != name{}, true ) - REQUIRE_EQUAL( name{"12345abcdefgj"} != name{0}, true ); - REQUIRE_EQUAL( name{"hijklmnopqrsj"} != name{0}, true ); - REQUIRE_EQUAL( name{"tuvwxyz.1234j"} != name{0}, true ); + CHECK_EQUAL( name{"12345abcdefgj"} != name{}, true ) + CHECK_EQUAL( name{"hijklmnopqrsj"} != name{}, true ) + CHECK_EQUAL( name{"tuvwxyz.1234j"} != name{}, true ) - REQUIRE_EQUAL( name{"111111111111j"} != name{0}, true ); - REQUIRE_EQUAL( name{"555555555555j"} != name{0}, true ); - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} != name{0}, true ); - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} != name{0}, true ); + CHECK_EQUAL( name{"111111111111j"} != name{}, true ) + CHECK_EQUAL( name{"555555555555j"} != name{}, true ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"} != name{}, true ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"} != name{}, true ) // --------------------------------------------------------- // friend constexpr bool operator<(const name&, const name&) - REQUIRE_EQUAL( name{0} < name{"1"}, true ); - REQUIRE_EQUAL( name{0} < name{"5"}, true ); - REQUIRE_EQUAL( name{0} < name{"a"}, true ); - REQUIRE_EQUAL( name{0} < name{"z"}, true ); - - REQUIRE_EQUAL( name{0} < name{"abc"}, true ); - REQUIRE_EQUAL( name{0} < name{"123"}, true ); - - REQUIRE_EQUAL( name{0} < name{".abc"}, true ); - REQUIRE_EQUAL( name{0} < name{".........abc"}, true ); - REQUIRE_EQUAL( name{0} < name{"123."}, true ); - REQUIRE_EQUAL( name{0} < name{"123........."}, true ); - REQUIRE_EQUAL( name{0} < name{".a.b.c.1.2.3."}, true ); + CHECK_EQUAL( name{} < name{"1"}, true ) + CHECK_EQUAL( name{} < name{"5"}, true ) + CHECK_EQUAL( name{} < name{"a"}, true ) + CHECK_EQUAL( name{} < name{"z"}, true ) + + CHECK_EQUAL( name{} < name{"abc"}, true ) + CHECK_EQUAL( name{} < name{"123"}, true ) + + CHECK_EQUAL( name{} < name{".abc"}, true ) + CHECK_EQUAL( name{} < name{".........abc"}, true ) + CHECK_EQUAL( name{} < name{"123."}, true ) + CHECK_EQUAL( name{} < name{"123........."}, true ) + CHECK_EQUAL( name{} < name{".a.b.c.1.2.3."}, true ) - REQUIRE_EQUAL( name{0} < name{"abc.123"}, true ); - REQUIRE_EQUAL( name{0} < name{"123.abc"}, true ); + CHECK_EQUAL( name{} < name{"abc.123"}, true ) + CHECK_EQUAL( name{} < name{"123.abc"}, true ) - REQUIRE_EQUAL( name{0} < name{"12345abcdefgj"}, true ); - REQUIRE_EQUAL( name{0} < name{"hijklmnopqrsj"}, true ); - REQUIRE_EQUAL( name{0} < name{"tuvwxyz.1234j"}, true ); + CHECK_EQUAL( name{} < name{"12345abcdefgj"}, true ) + CHECK_EQUAL( name{} < name{"hijklmnopqrsj"}, true ) + CHECK_EQUAL( name{} < name{"tuvwxyz.1234j"}, true ) - REQUIRE_EQUAL( name{0} < name{"111111111111j"}, true ); - REQUIRE_EQUAL( name{0} < name{"555555555555j"}, true ); - REQUIRE_EQUAL( name{0} < name{"aaaaaaaaaaaaj"}, true ); - REQUIRE_EQUAL( name{0} < name{"zzzzzzzzzzzzj"}, true ); + CHECK_EQUAL( name{} < name{"111111111111j"}, true ) + CHECK_EQUAL( name{} < name{"555555555555j"}, true ) + CHECK_EQUAL( name{} < name{"aaaaaaaaaaaaj"}, true ) + CHECK_EQUAL( name{} < name{"zzzzzzzzzzzzj"}, true ) // ------------------------------------ // inline constexpr name operator""_n() - REQUIRE_EQUAL( name{0} == ""_n, true ); + CHECK_EQUAL( name{} == ""_n, true ) - REQUIRE_EQUAL( name{"1"} == "1"_n, true ); - REQUIRE_EQUAL( name{"5"} == "5"_n, true ); - REQUIRE_EQUAL( name{"a"} == "a"_n, true ); - REQUIRE_EQUAL( name{"z"} == "z"_n, true ); - - REQUIRE_EQUAL( name{"abc"} == "abc"_n, true ); - REQUIRE_EQUAL( name{"123"} == "123"_n, true ); - - REQUIRE_EQUAL( name{".abc"} == ".abc"_n, true ); - REQUIRE_EQUAL( name{".........abc"} == ".........abc"_n, true ); - REQUIRE_EQUAL( name{"123."} == "123."_n, true ); - REQUIRE_EQUAL( name{"123........."} == "123........."_n, true ); - REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ); + CHECK_EQUAL( name{"1"} == "1"_n, true ) + CHECK_EQUAL( name{"5"} == "5"_n, true ) + CHECK_EQUAL( name{"a"} == "a"_n, true ) + CHECK_EQUAL( name{"z"} == "z"_n, true ) + + CHECK_EQUAL( name{"abc"} == "abc"_n, true ) + CHECK_EQUAL( name{"123"} == "123"_n, true ) + + CHECK_EQUAL( name{".abc"} == ".abc"_n, true ) + CHECK_EQUAL( name{".........abc"} == ".........abc"_n, true ) + CHECK_EQUAL( name{"123."} == "123."_n, true ) + CHECK_EQUAL( name{"123........."} == "123........."_n, true ) + CHECK_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ) - REQUIRE_EQUAL( name{"abc.123"} == "abc.123"_n, true ); - REQUIRE_EQUAL( name{"123.abc"} == "123.abc"_n, true ); + CHECK_EQUAL( name{"abc.123"} == "abc.123"_n, true ) + CHECK_EQUAL( name{"123.abc"} == "123.abc"_n, true ) - REQUIRE_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ); - REQUIRE_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ); - REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ); + CHECK_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ) + CHECK_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ) + CHECK_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ) - REQUIRE_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ); - REQUIRE_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ); - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ); - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ); + CHECK_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ) + CHECK_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index c8d00e7bf4..eda4219a1c 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -1,8 +1,13 @@ -#include #include +#include -using namespace eosio; -using namespace eosio::native; +using eosio::name; +using eosio::symbol_code; +using eosio::symbol; +using eosio::extended_symbol; + +const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL +const uint64_t u64min = std::numeric_limits::min(); // 0ULL // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_code_type_test) @@ -12,58 +17,58 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) // constructors/constexpr uint64_t raw()const /// constexpr symbol_code() - REQUIRE_EQUAL( symbol_code{}.raw(), 0ULL ); + CHECK_EQUAL( symbol_code{}.raw(), 0ULL ) /// constexpr explicit symbol_code(uint64_t raw) - REQUIRE_EQUAL( symbol_code{0ULL}.raw(), 0ULL ); - REQUIRE_EQUAL( symbol_code{1ULL}.raw(), 1ULL ); - REQUIRE_EQUAL( symbol_code{18446744073709551615ULL}.raw(), 18446744073709551615ULL ); + CHECK_EQUAL( symbol_code{0ULL}.raw(), 0ULL ) + CHECK_EQUAL( symbol_code{1ULL}.raw(), 1ULL ) + CHECK_EQUAL( symbol_code{u64max}.raw(), u64max ) /// constexpr explicit symbol_code(std::string_view str) - REQUIRE_EQUAL( symbol_code{"A"}.raw(), 65ULL ); - REQUIRE_EQUAL( symbol_code{"Z"}.raw(), 90ULL ); - REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ); - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.raw(), 25432092013386330ULL ); + CHECK_EQUAL( symbol_code{"A"}.raw(), 65ULL ) + CHECK_EQUAL( symbol_code{"Z"}.raw(), 90ULL ) + CHECK_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"}.raw(), 25432092013386330ULL ) - REQUIRE_ASSERT( "string is too long to be a valid symbol_code", ([]() {symbol_code{"ABCDEFGH"};}) ); - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"a"};}) ); - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"z"};}) ); - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ); - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ); + CHECK_ASSERT( "string is too long to be a valid symbol_code", ([]() {symbol_code{"ABCDEFGH"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"a"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"z"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ) // ------------------------------ // constexpr bool is_valid()const - REQUIRE_EQUAL( symbol_code{65ULL}.is_valid(), true ); // "A" - REQUIRE_EQUAL( symbol_code{90ULL}.is_valid(), true ); // "Z" - REQUIRE_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ); // "AAAAAAA" - REQUIRE_EQUAL( symbol_code{25432092013386330ULL}.is_valid(), true ); // "ZZZZZZZ" + CHECK_EQUAL( symbol_code{65ULL}.is_valid(), true ) // "A" + CHECK_EQUAL( symbol_code{90ULL}.is_valid(), true ) // "Z" + CHECK_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ) // "AAAAAAA" + CHECK_EQUAL( symbol_code{25432092013386330ULL}.is_valid(), true ) // "ZZZZZZZ" - REQUIRE_EQUAL( symbol_code{64ULL}.is_valid(), false ); - REQUIRE_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ); + CHECK_EQUAL( symbol_code{64ULL}.is_valid(), false ) + CHECK_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ) // -------------------------------- // constexpr uint32_t length()const - REQUIRE_EQUAL( symbol_code{""}.length(), 0 ); - REQUIRE_EQUAL( symbol_code{"S"}.length(), 1 ); - REQUIRE_EQUAL( symbol_code{"SY"}.length(), 2 ); - REQUIRE_EQUAL( symbol_code{"SYM"}.length(), 3 ); - REQUIRE_EQUAL( symbol_code{"SYMB"}.length(), 4 ); - REQUIRE_EQUAL( symbol_code{"SYMBO"}.length(), 5 ); - REQUIRE_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ); - REQUIRE_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ); + CHECK_EQUAL( symbol_code{""}.length(), 0 ) + CHECK_EQUAL( symbol_code{"S"}.length(), 1 ) + CHECK_EQUAL( symbol_code{"SY"}.length(), 2 ) + CHECK_EQUAL( symbol_code{"SYM"}.length(), 3 ) + CHECK_EQUAL( symbol_code{"SYMB"}.length(), 4 ) + CHECK_EQUAL( symbol_code{"SYMBO"}.length(), 5 ) + CHECK_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ) + CHECK_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ) // --------------------------------------- // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` - REQUIRE_EQUAL( symbol_code{0}.operator bool(), false ); - REQUIRE_EQUAL( symbol_code{1}.operator bool(), true ); - REQUIRE_EQUAL( !symbol_code{0}, true ); - REQUIRE_EQUAL( !symbol_code{1}, false ); + CHECK_EQUAL( symbol_code{0ULL}.operator bool(), false ) + CHECK_EQUAL( symbol_code{1ULL}.operator bool(), true ) + CHECK_EQUAL( !symbol_code{0ULL}, true ) + CHECK_EQUAL( !symbol_code{1ULL}, false ) - REQUIRE_EQUAL( symbol_code{""}.operator bool(), false ); - REQUIRE_EQUAL( symbol_code{"SYMBOL"}.operator bool(), true ); - REQUIRE_EQUAL( !symbol_code{""}, true ); - REQUIRE_EQUAL( !symbol_code{"SYMBOL"}, false ); + CHECK_EQUAL( symbol_code{""}.operator bool(), false ) + CHECK_EQUAL( symbol_code{"SYMBOL"}.operator bool(), true ) + CHECK_EQUAL( !symbol_code{""}, true ) + CHECK_EQUAL( !symbol_code{"SYMBOL"}, false ) // ---------------------------------------- // char* write_as_string(char*, char*)const @@ -71,41 +76,41 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) std::string test_str{"A"}; symbol_code{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "Z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "AAAAAAA"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "ZZZZZZZ"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ); + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // ---------------------------- // std::string to_string()const - REQUIRE_EQUAL( symbol_code{"A"}.to_string(), "A" ); - REQUIRE_EQUAL( symbol_code{"Z"}.to_string(), "Z" ); - REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ); - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ); + CHECK_EQUAL( symbol_code{"A"}.to_string(), "A" ) + CHECK_EQUAL( symbol_code{"Z"}.to_string(), "Z" ) + CHECK_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ) // -------------------------------------------------------------- // friend bool operator==(const symbol_code&, const symbol_code&) - REQUIRE_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ); - REQUIRE_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ); - REQUIRE_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ); - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ); + CHECK_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ) + CHECK_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ) + CHECK_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ) // -------------------------------------------------------------- // friend bool operator!=(const symbol_code&, const symbol_code&) - REQUIRE_EQUAL( symbol_code{"A"} != symbol_code{0}, true ); - REQUIRE_EQUAL( symbol_code{"Z"} != symbol_code{0}, true ); - REQUIRE_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{0}, true ); - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{0}, true ); + CHECK_EQUAL( symbol_code{"A"} != symbol_code{}, true ) + CHECK_EQUAL( symbol_code{"Z"} != symbol_code{}, true ) + CHECK_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{}, true ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{}, true ) // ------------------------------------------------------------- // friend bool operator<(const symbol_code&, const symbol_code&) - REQUIRE_EQUAL( symbol_code{0} < symbol_code{"A"}, true ); - REQUIRE_EQUAL( symbol_code{0} < symbol_code{"Z"}, true ); - REQUIRE_EQUAL( symbol_code{0} < symbol_code{"AAAAAAA"}, true ); - REQUIRE_EQUAL( symbol_code{0} < symbol_code{"ZZZZZZZ"}, true ); + CHECK_EQUAL( symbol_code{} < symbol_code{"A"}, true ) + CHECK_EQUAL( symbol_code{} < symbol_code{"Z"}, true ) + CHECK_EQUAL( symbol_code{} < symbol_code{"AAAAAAA"}, true ) + CHECK_EQUAL( symbol_code{} < symbol_code{"ZZZZZZZ"}, true ) silence_output(false); EOSIO_TEST_END @@ -114,106 +119,111 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(symbol_type_test) silence_output(true); + symbol_code sc0{"A"}; + symbol_code sc1{"Z"}; + symbol_code sc2{"AAAAAAA"}; + symbol_code sc3{"ZZZZZZZ"}; + // ------------------------------------------ // constructors/constexpr uint64_t raw()const /// constexpr symbol() - REQUIRE_EQUAL( symbol{}.raw(), 0ULL ); + CHECK_EQUAL( symbol{}.raw(), 0ULL ) /// constexpr explicit symbol(uint64_t) - REQUIRE_EQUAL( symbol{0ULL}.raw(), 0ULL ); - REQUIRE_EQUAL( symbol{1ULL}.raw(), 1ULL ); - REQUIRE_EQUAL( symbol{18446744073709551615ULL}.raw(), 18446744073709551615ULL ); + CHECK_EQUAL( symbol{0ULL}.raw(), 0ULL ) + CHECK_EQUAL( symbol{1ULL}.raw(), 1ULL ) + CHECK_EQUAL( symbol{u64max}.raw(), u64max ) /// constexpr symbol(std::string_view, uint8_t) // Note that unless constructed with `initializer_list`, precision does not check for wrap-around - REQUIRE_EQUAL( (symbol{"A", 0}.raw()), 16640ULL ); - REQUIRE_EQUAL( (symbol{"Z", 0}.raw()), 23040ULL ); - REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.raw()), 4702111234474983680ULL ); - REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.raw()), 6510615555426900480ULL ); + CHECK_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) + CHECK_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) + CHECK_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) + CHECK_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) /// constexpr symbol(symbol_code, uint8_t) - REQUIRE_EQUAL( (symbol{symbol_code{"A"}, 0}.raw()), 16640ULL ); - REQUIRE_EQUAL( (symbol{symbol_code{"Z"}, 0}.raw()), 23040ULL ); - REQUIRE_EQUAL( (symbol{symbol_code{"AAAAAAA"}, 0}.raw()), 4702111234474983680ULL ); - REQUIRE_EQUAL( (symbol{symbol_code{"ZZZZZZZ"}, 0}.raw()), 6510615555426900480ULL ); + CHECK_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) + CHECK_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) + CHECK_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) + CHECK_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) // -------------------- // bool is_valid()const - REQUIRE_EQUAL( symbol{16640ULL}.is_valid(), true ); // "A", precision: 0 - REQUIRE_EQUAL( symbol{23040ULL}.is_valid(), true ); // "Z", precision: 0 - REQUIRE_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ); // "AAAAAAA", precision: 0 - REQUIRE_EQUAL( symbol{6510615555426900480ULL}.is_valid(), true ); // "ZZZZZZZ", precision: 0 + CHECK_EQUAL( symbol{16640ULL}.is_valid(), true ) // "A", precision: 0 + CHECK_EQUAL( symbol{23040ULL}.is_valid(), true ) // "Z", precision: 0 + CHECK_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ) // "AAAAAAA", precision: 0 + CHECK_EQUAL( symbol{6510615555426900480ULL}.is_valid(), true ) // "ZZZZZZZ", precision: 0 - REQUIRE_EQUAL( symbol{16639ULL}.is_valid(), false ); - REQUIRE_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ); + CHECK_EQUAL( symbol{16639ULL}.is_valid(), false ) + CHECK_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ) // ------------------------- - // uint64_t precision()const - REQUIRE_EQUAL( (symbol{"A", 0}.precision()), 0 ); - REQUIRE_EQUAL( (symbol{"Z", 0}.precision()), 0 ); - REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.precision()), 0 ); - REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.precision()), 0 ); + // uint8_t precision()const + CHECK_EQUAL( (symbol{sc0, 0}.precision()), 0 ) + CHECK_EQUAL( (symbol{sc1, 0}.precision()), 0 ) + CHECK_EQUAL( (symbol{sc2, 0}.precision()), 0 ) + CHECK_EQUAL( (symbol{sc3, 0}.precision()), 0 ) - REQUIRE_EQUAL( (symbol{"A", 255}.precision()), 255 ); - REQUIRE_EQUAL( (symbol{"Z", 255}.precision()), 255 ); - REQUIRE_EQUAL( (symbol{"AAAAAAA", 255}.precision()), 255 ); - REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 255}.precision()), 255 ); + CHECK_EQUAL( (symbol{sc0, 255}.precision()), 255 ) + CHECK_EQUAL( (symbol{sc1, 255}.precision()), 255 ) + CHECK_EQUAL( (symbol{sc2, 255}.precision()), 255 ) + CHECK_EQUAL( (symbol{sc3, 255}.precision()), 255 ) // ----------------------- // symbol_code code()const - REQUIRE_EQUAL( (symbol{"A", 0}.code()), symbol_code{"A"} ); - REQUIRE_EQUAL( (symbol{"Z", 0}.code()), symbol_code{"Z"} ); - REQUIRE_EQUAL( (symbol{"AAAAAAA", 0}.code()), symbol_code{"AAAAAAA"} ); - REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0}.code()), symbol_code{"ZZZZZZZ"} ); + CHECK_EQUAL( (symbol{sc0, 0}.code()), sc0 ) + CHECK_EQUAL( (symbol{sc1, 0}.code()), sc1 ) + CHECK_EQUAL( (symbol{sc2, 0}.code()), sc2 ) + CHECK_EQUAL( (symbol{sc3, 0}.code()), sc3 ) // --------------------------------------- // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` - REQUIRE_EQUAL( symbol{0}.operator bool(), false ); - REQUIRE_EQUAL( symbol{1}.operator bool(), true ); - REQUIRE_EQUAL( !symbol{0}, true ); - REQUIRE_EQUAL( !symbol{1}, false ); + CHECK_EQUAL( symbol{0}.operator bool(), false ) + CHECK_EQUAL( symbol{1}.operator bool(), true ) + CHECK_EQUAL( !symbol{0}, true ) + CHECK_EQUAL( !symbol{1}, false ) - REQUIRE_EQUAL( (symbol{"", 0}.operator bool()), false ); - REQUIRE_EQUAL( (symbol{"SYMBOL", 0}.operator bool()), true ); - REQUIRE_EQUAL( (!symbol{"", 0}), true ); - REQUIRE_EQUAL( (!symbol{"SYMBOL", 0}), false ); + CHECK_EQUAL( (symbol{"", 0}.operator bool()), false ) + CHECK_EQUAL( (symbol{"SYMBOLL", 0}.operator bool()), true ) + CHECK_EQUAL( (!symbol{"", 0}), true ) + CHECK_EQUAL( (!symbol{"SYMBOLL", 0}), false ) // --------------------- // void print(bool)const -// symbol{"A", 0}.print(true); -// symbol{"AAAAAAA", 255}.print(true); - // REQUIRE_PRINT( "0,A", [](){symbol{"A", 0}.print(true);} ); - // REQUIRE_PRINT( "0,Z", [](){symbol{"Z", 0}.print(true);} ); - // REQUIRE_PRINT( "255,AAAAAAA", [](){symbol{"AAAAAAA", 255}.print(true);} ); - // REQUIRE_PRINT( "255,ZZZZZZZ", [](){symbol{"ZZZZZZZ", 255}.print(true);} ); - - // REQUIRE_PRINT( ",A", [](){symbol{"A", 0}.print(false);} ); - // REQUIRE_PRINT( ",Z", [](){symbol{"Z", 0}.print(false);} ); - // REQUIRE_PRINT( ",AAAAAAA", [](){symbol{"AAAAAAA", 255}.print(false);} ); - // REQUIRE_PRINT( ",ZZZZZZZ", [](){symbol{"ZZZZZZZ", 255}.print(false);} ); + // symbol{"A", 0}.print(true); + // symbol{"AAAAAAA", 255}.print(true); + // CHECK_PRINT( "0,A", [&](){symbol{"A", 0}.print(true);} ); + // CHECK_PRINT( "0,Z", [&](){symbol{"Z", 0}.print(true);} ); + // CHECK_PRINT( "255,AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(true);} ); + // CHECK_PRINT( "255,ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(true);} ); + + // CHECK_PRINT( ",A", [&](){symbol{"A", 0}.print(false);} ); + // CHECK_PRINT( ",Z", [&](){symbol{"Z", 0}.print(false);} ); + // CHECK_PRINT( ",AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(false);} ); + // CHECK_PRINT( ",ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(false);} ); // -------------------------------------------------------------- // friend constexpr bool operator==(const symbol&, const symbol&) - REQUIRE_EQUAL( (symbol{"A", 0} == symbol{"A", 0}), true ); - REQUIRE_EQUAL( (symbol{"Z", 0} == symbol{"Z", 0}), true ); - REQUIRE_EQUAL( (symbol{"AAAAAAA", 0} == symbol{"AAAAAAA", 0}), true ); - REQUIRE_EQUAL( (symbol{"ZZZZZZZ", 0} == symbol{"ZZZZZZZ", 0}), true ); + CHECK_EQUAL( (symbol{sc0, 0} == symbol{sc0, 0}), true ) + CHECK_EQUAL( (symbol{sc1, 0} == symbol{sc1, 0}), true ) + CHECK_EQUAL( (symbol{sc2, 0} == symbol{sc2, 0}), true ) + CHECK_EQUAL( (symbol{sc3, 0} == symbol{sc3, 0}), true ) // -------------------------------------------------------------- // friend constexpr bool operator!=(const symbol&, const symbol&) - REQUIRE_EQUAL( (symbol{"A",0} != symbol{0}), true ); - REQUIRE_EQUAL( (symbol{"Z",0} != symbol{0}), true ); - REQUIRE_EQUAL( (symbol{"AAAAAAA",0} != symbol{0}), true ); - REQUIRE_EQUAL( (symbol{"ZZZZZZZ",0} != symbol{0}), true ); + CHECK_EQUAL( (symbol{sc0, 0} != symbol{0}), true ) + CHECK_EQUAL( (symbol{sc1, 0} != symbol{0}), true ) + CHECK_EQUAL( (symbol{sc2, 0} != symbol{0}), true ) + CHECK_EQUAL( (symbol{sc3, 0} != symbol{0}), true ) // -------------------------------------------------------------- // friebnd constexpr bool operator<(const symbol&, const symbol&) - REQUIRE_EQUAL( (symbol{0} < symbol{"A",0}), true ); - REQUIRE_EQUAL( (symbol{0} < symbol{"Z",0}), true ); - REQUIRE_EQUAL( (symbol{0} < symbol{"AAAAAAA",0}), true ); - REQUIRE_EQUAL( (symbol{0} < symbol{"ZZZZZZZ",0}), true ); + CHECK_EQUAL( (symbol{} < symbol{sc0, 0}), true ) + CHECK_EQUAL( (symbol{} < symbol{sc1, 0}), true ) + CHECK_EQUAL( (symbol{} < symbol{sc2, 0}), true ) + CHECK_EQUAL( (symbol{} < symbol{sc3, 0}), true ) silence_output(false); EOSIO_TEST_END @@ -222,65 +232,80 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(extended_symbol_type_test) silence_output(true); + name n0{"1"}; + name n1{"5"}; + name n2{"a"}; + name n3{"z"}; + name n4{"111111111111j"}; + name n5{"555555555555j"}; + name n6{"aaaaaaaaaaaaj"}; + name n7{"zzzzzzzzzzzzj"}; + + symbol s0{"A", 0}; + symbol s1{"Z", 0}; + symbol s2{"AAAAAAA", 0}; + symbol s3{"ZZZZZZZ", 0}; + // ------------------------------------ // constructors/get_symbol/get_contract /// constexpr extended_symbol() - REQUIRE_EQUAL( (extended_symbol{symbol{}, name{}}.get_symbol().raw()), 0ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{}, name{}}.get_contract().value), 0ULL ); + CHECK_EQUAL( (extended_symbol{{}, {}}.get_symbol().raw()), 0ULL ) + CHECK_EQUAL( (extended_symbol{{}, {}}.get_contract().value), 0ULL ) /// constexpr extended_symbol(symbol, name)/constexpr symbol get_symbol()const/constexpr name get_contract()const - REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"1"}}.get_symbol().raw()), 16640ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"5"}}.get_symbol().code().raw()), 65ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"a"}}.get_symbol().raw()), 23040ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"z"}}.get_symbol().code().raw()), 90ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"1"}}.get_contract().value), 576460752303423488ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{"5"}}.get_contract().value), 2882303761517117440ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"a"}}.get_contract().value), 3458764513820540928ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{"z"}}.get_contract().value), 17870283321406128128ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"111111111111j"}}.get_symbol().raw()), 4702111234474983680ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"555555555555j"}}.get_symbol().code().raw()), 18367622009667905ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"aaaaaaaaaaaaj"}}.get_symbol().raw()), 6510615555426900480ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"zzzzzzzzzzzzj"}}.get_symbol().code().raw()), 25432092013386330ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"111111111111j"}}.get_contract().value), 595056260442243615ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{"555555555555j"}}.get_contract().value), 2975281302211218015ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"aaaaaaaaaaaaj"}}.get_contract().value), 3570337562653461615ULL ); - REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"zzzzzzzzzzzzj"}}.get_contract().value), 18446744073709551615ULL ); - + CHECK_EQUAL( (extended_symbol{s0, n0}.get_symbol().raw()), 16640ULL ) + CHECK_EQUAL( (extended_symbol{s0, n1}.get_symbol().code().raw()), 65ULL ) + CHECK_EQUAL( (extended_symbol{s1, n2}.get_symbol().raw()), 23040ULL ) + CHECK_EQUAL( (extended_symbol{s1, n3}.get_symbol().code().raw()), 90ULL ) + CHECK_EQUAL( (extended_symbol{s0, n0}.get_contract().value), 576460752303423488ULL ) + CHECK_EQUAL( (extended_symbol{s0, n1}.get_contract().value), 2882303761517117440ULL ) + CHECK_EQUAL( (extended_symbol{s1, n2}.get_contract().value), 3458764513820540928ULL ) + CHECK_EQUAL( (extended_symbol{s1, n3}.get_contract().value), 17870283321406128128ULL ) + CHECK_EQUAL( (extended_symbol{s2, n4}.get_symbol().raw()), 4702111234474983680ULL ) + CHECK_EQUAL( (extended_symbol{s2, n5}.get_symbol().code().raw()), 18367622009667905ULL ) + CHECK_EQUAL( (extended_symbol{s3, n6}.get_symbol().raw()), 6510615555426900480ULL ) + CHECK_EQUAL( (extended_symbol{s3, n7}.get_symbol().code().raw()), 25432092013386330ULL ) + CHECK_EQUAL( (extended_symbol{s2, n4}.get_contract().value), 595056260442243615ULL ) + CHECK_EQUAL( (extended_symbol{s2, n5}.get_contract().value), 2975281302211218015ULL ) + CHECK_EQUAL( (extended_symbol{s3, n6}.get_contract().value), 3570337562653461615ULL ) + CHECK_EQUAL( (extended_symbol{s3, n7}.get_contract().value), u64max ) + + // Must fix the behavior of `print` in order for these tests to work // --------------------- // void print(bool)const -// extended_symbol{symbol{"A", 0}, name{"1"}}.print(true); -// extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true); - // REQUIRE_PRINT( "0@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(true);} ); - // REQUIRE_PRINT( "0@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(true);} ); - // REQUIRE_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); - // REQUIRE_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); - - // REQUIRE_PRINT( "@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); - // REQUIRE_PRINT( "@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); - // REQUIRE_PRINT( "@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); - // REQUIRE_PRINT( "@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); + // extended_symbol{s0, n0}.print(true); + // extended_symbol{s0, n0}.print(true); + // CHECK_PRINT( "0@576460752303423488", [](){extended_symbol{s0, n0}.print(true);} ); + // CHECK_PRINT( "0@2882303761517117440", [](){extended_symbol{s1, n1}.print(true);} ); + // CHECK_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); + // CHECK_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); + + // CHECK_PRINT( "@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); + // CHECK_PRINT( "@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); + // CHECK_PRINT( "@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); + // CHECK_PRINT( "@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); // ------------------------------------------------------------------------------- // friend constexpr bool operator==(const extended_symbol&, const extended_symbol&) - REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{0}} == extended_symbol{symbol{"A", 0}, name{}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{0}} == extended_symbol{symbol{"Z", 0}, name{}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{0}} == extended_symbol{symbol{"AAAAAAA", 0}, name{}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}} == extended_symbol{symbol{"ZZZZZZZ", 0}, name{}}), true ); + CHECK_EQUAL( (extended_symbol{s0, n0} == extended_symbol{s0, n0}), true ) + CHECK_EQUAL( (extended_symbol{s1, n3} == extended_symbol{s1, n3}), true ) + CHECK_EQUAL( (extended_symbol{s2, n4} == extended_symbol{s2, n4}), true ) + CHECK_EQUAL( (extended_symbol{s3, n7} == extended_symbol{s3, n7}), true ) // ------------------------------------------------------------------------------- // friend constexpr bool operator!=(const extended_symbol&, const extended_symbol&) - REQUIRE_EQUAL( (extended_symbol{symbol{"A", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{"Z", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{"AAAAAAA", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}} != extended_symbol{symbol{0}, name{0}}), true ); + CHECK_EQUAL( (extended_symbol{} != extended_symbol{s0, {}}), true ) + CHECK_EQUAL( (extended_symbol{s0, {}} != extended_symbol{s1, {}}), true ) + CHECK_EQUAL( (extended_symbol{s1, {}} != extended_symbol{s2, {}}), true ) + CHECK_EQUAL( (extended_symbol{s2, {}} != extended_symbol{s3, {}}), true ) // ------------------------------------------------------------------------------- // friend constexpr bool operator<(const extended_symbol&, const extended_symbol&) - REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"A", 0}, name{0}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"Z", 0}, name{0}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"AAAAAAA", 0}, name{0}}), true ); - REQUIRE_EQUAL( (extended_symbol{symbol{0}, name{0}} < extended_symbol{symbol{"ZZZZZZZ", 0}, name{0}}), true ); + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s0, {}}), true ) + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s1, {}}), true ) + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s2, {}}), true ) + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s3, {}}), true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/system_tests.cpp b/tests/unit/system_tests.cpp index a842444487..81c3091feb 100644 --- a/tests/unit/system_tests.cpp +++ b/tests/unit/system_tests.cpp @@ -1,29 +1,39 @@ #include #include +using eosio::check; +using std::string; + using namespace eosio::native; +// Defined in `eosio.cdt/libraries/eosiolib/system.hpp` EOSIO_TEST_BEGIN(system_test) - silence_output(true); - auto test_check1 = [](bool b) { eosio::check(b, "asserted"); }; - test_check1(true); - CHECK_ASSERT("asserted", test_check1, false); - - auto test_check2 = [](bool b) { std::string s="asserted"; eosio::check(b, s); }; - test_check2(true); - REQUIRE_ASSERT("asserted", test_check2, false); - - auto test_check3 = [](bool b) { eosio::check(b, "asserted with more text", 8); }; - test_check3(true); - REQUIRE_ASSERT("asserted", test_check3, false); - - auto test_check4 = [](bool b) { std::string s="asserted with more text"; eosio::check(b, s, 8); }; - test_check4(true); - REQUIRE_ASSERT("asserted", test_check4, false); - - auto test_check5 = [](bool b) { eosio::check(b, 13); }; - test_check5(true); - REQUIRE_ASSERT("13", test_check5, false); + silence_output(false); + + // inline void check(bool, const char*) + REQUIRE_ASSERT( "asserted", []() { const char* str{"asserted"}; check(false, str);} ); + + // inline void check(bool, const std::string&) + REQUIRE_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, str);} ); + + // inline void check(bool, const std::string&&) + REQUIRE_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, std::move(str));} ); + + // inline void check(bool, const char*, size_t) + REQUIRE_ASSERT( "assert", []() { const char* str{"asserted"}; check(false, str, 6);} ); + + // inline void check(bool, const std::string&, size_t) + REQUIRE_ASSERT( "assert", []() { const string str{"asserted"}; check(false, str, 6);} ); + + // Note: this function will not work when give `0` as the `uint64_t` value. + // Maybe some invariants should be established as to what is acceptable and what isn't. + // REQUIRE_ASSERT("0", []() { check(false, 0);} ); + // inline void check(bool, uint64_t) + REQUIRE_ASSERT("1", []() { check(false, 1);} ); + REQUIRE_ASSERT("100", []() { check(false, 100);} ); + REQUIRE_ASSERT("18446744073709551615", []() { check(false, 18446744073709551615ULL);} ); + REQUIRE_ASSERT("18446744073709551615", []() { check(false, -1ULL);} ); + silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index c10ec69c7d..ef7939c1b7 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -6,6 +6,7 @@ using eosio::microseconds; using eosio::time_point; using eosio::time_point_sec; +using eosio::block_timestamp; using namespace eosio::native; @@ -202,11 +203,14 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) REQUIRE_EQUAL( (time_point{time_point_sec{tp0}}.elapsed.count()), 0 ) REQUIRE_EQUAL( (time_point{time_point_sec{tp1}}.elapsed.count()), 0 ) REQUIRE_EQUAL( (time_point{time_point_sec{tp2}}.elapsed.count()), 0 ) - REQUIRE_EQUAL( (time_point{time_point_sec{tp_max}}.elapsed._count / 1000000), ms_max.count() / 1000000 ) - REQUIRE_EQUAL( (time_point{time_point_sec{tp_min}}.elapsed._count / 1000000), ms_min.count() / 1000000 ) + REQUIRE_EQUAL( (time_point{time_point_sec{tp_max}}.elapsed.count() / 1000000), ms_max.count() / 1000000 ) + REQUIRE_EQUAL( (time_point{time_point_sec{tp_min}}.elapsed.count() / 1000000), ms_min.count() / 1000000 ) // ------------------------------------------- // time_point_sec operator=(const time_point&) + REQUIRE_EQUAL( (time_point_sec{} = time_point{tp0}), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{} = time_point{tp1}), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{} = time_point{tp_max}), time_point_sec{tp_max} ) // -------------------------------------------------------------------- // friend bool operator==(const time_point_sec&, const time_point_sec&) @@ -215,12 +219,12 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) // friend bool operator<=(const time_point_sec&, const time_point_sec&) // friend bool operator>(const time_point_sec&, const time_point_sec&) // friend bool operator>=(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp1} != time_point{tp0}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp1}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp1} <= time_point{tp1}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp1} > time_point{tp0}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp1} >= time_point{tp1}), true ) + REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) + REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) + REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) + REQUIRE_EQUAL( (time_point_sec{tp_max} <= time_point{tp_max}), true ) + REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) + REQUIRE_EQUAL( (time_point_sec{tp_max} >= time_point{tp_max}), true ) // ------------------------------------ @@ -232,47 +236,109 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) // time_point_sec& operator-=(time_point_sec) // time_point_sec operator+(uint32_t)const // time_point_sec operator-(uint32_t)const - + REQUIRE_EQUAL( (time_point_sec{} += i64MAX), time_point_sec{i64MAX} ) + REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64MAX}), time_point_sec{tp_max} ) + REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64MAX}), time_point_sec{i64MAX} ) + REQUIRE_EQUAL( (time_point_sec{i64MAX} -= i64MAX), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{i64MAX} -= microseconds{i64MAX}), time_point_sec{i64MAX - (i64MAX / 1000000LL)} ) + REQUIRE_EQUAL( (time_point_sec{i64MAX} -= time_point_sec{i64MAX}), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{} + i64MAX), time_point_sec{i64MAX} ) + REQUIRE_EQUAL( (time_point_sec{i64MAX} - i64MAX), time_point_sec{} ) // ----------------------------------------------------------------------- // friend time_point operator+(const time_point_sec&, const microseconds&) // friend time_point operator-(const time_point_sec&, const microseconds&) // friend microseconds operator-(const time_point_sec&, const time_point_sec&) // friend microseconds operator-(const time_point&, const time_point_sec&) - + // REQUIRE_EQUAL( (time_point_sec{} + microseconds{}), time_point{} ) + // REQUIRE_EQUAL( (time_point_sec{i64MAX} - microseconds{i64MAX}), time_point{} ) + // REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) + // REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) silence_output(false); EOSIO_TEST_END EOSIO_TEST_BEGIN(block_timestamp_type_test) - silence_output(true); + silence_output(false); + + microseconds ms0{0LL}; + microseconds ms1{1LL}; + microseconds ms2{-1LL}; + microseconds ms_max{i64MAX}; + microseconds ms_min{i64MIN}; + + time_point tp0{ms0}; + time_point tp1{ms1}; + time_point tp2{ms2}; + time_point tp_max{ms_max}; + time_point tp_min{ms_min}; + + time_point_sec tps0{tp0}; + time_point_sec tps1{tp1}; + time_point_sec tps2{tp2}; + time_point_sec tps_max{tp_max}; + time_point_sec tps_min{tp_min}; + +// eosio::print_f("%",block_timestamp{tp0}.slot); +// eosio::print_f("%",block_timestamp{0}.slot); // ------------ // constructors /// explicit block_timestamp(uint32_t) - + REQUIRE_EQUAL( block_timestamp{}.slot == block_timestamp{0}.slot, true ) + + // NOTE: No invariant established. `block_timestamp` gets wonky with certain inputs. + // Steps to reproduce: + // microseconds ms0{0LL}; + // time_point tp0{ms0}; + // block_timestamp{tp0}; + // eosio::print_f("%", block_timestamp.slot); // output: 18446744071816182016 + // Reason for erros: + // set_time_point(time_point{microseconds{0LL}}); + // int64_t micro_since_epoch = t.time_since_epoch().count(); // 0 + // int64_t msec_since_epoch = micro_since_epoch / 1000; // 0 / 1000 = 0 + // slot = uint32_t(( msec_since_epoch - block_timestamp_epoch ) / int64_t(block_interval_ms)); // 0 - 946684800000ll / 500 /// block_timestamp(const time_point&) + // REQUIRE_EQUAL( block_timestamp{tp0}.slot == block_timestamp{0}.slot, true ) - /// block_timestamp(const time_point&) + /// block_timestamp(const time_point_sec&) + // REQUIRE_EQUAL( block_timestamp{tps0}.slot == block_timestamp{0}.slot, true ) // -------------------------------- // static block_timestamp maximum() + REQUIRE_EQUAL( block_timestamp{}.maximum(), block_timestamp{0xFFFF} ) // ---------------------------- // static block_timestamp min() + REQUIRE_EQUAL( block_timestamp{}.min(), block_timestamp{0} ) // ---------------------- // block_timestamp next() - - // ------------------------------- - // time_point to_time_point()const - - // -------------------------- - // operator time_point()const + REQUIRE_EQUAL( (block_timestamp{}.next()), block_timestamp{1} ) + + // NOTE: This function will never throw unless the value lands on `0` + // For example: + // block_timestamp{std::numeric_limits::max(); + // REQUIRE_ASSERT( "block timestamp overflow", ([]() {block_timestamp{std::numeric_limits::max()-1}.next();}) ); + + // ---------------------------------------------------------- + // time_point to_time_point()const/operator time_point()const + block_timestamp bt0{tps0}; + block_timestamp bt1{tps1}; + block_timestamp bt2{tps2}; + block_timestamp bt_max{tps_max}; + block_timestamp bt_min{tps_min}; + + REQUIRE_EQUAL( (bt0.to_time_point()), static_cast(tps0) ) + REQUIRE_EQUAL( (bt1.to_time_point()), static_cast(tps1) ) + REQUIRE_EQUAL( (bt2.to_time_point()), static_cast(tps2) ) + REQUIRE_EQUAL( (bt_max.to_time_point()), static_cast(tps_max) ) + REQUIRE_EQUAL( (bt_min.to_time_point()), static_cast(tps_min) ) // -------------------------------------- // void set_time_point(const time_point&) + // REQUIRE_EQUAL( bt0.set_time_point{tp0}, ) // ------------------------------------------ // void set_time_point(const time_point_sec&) @@ -287,6 +353,12 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) // bool operator<=(const block_timestamp&) // bool operator>(const block_timestamp&) // bool operator>=(const block_timestamp&) + REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) + REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) + // REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) + REQUIRE_EQUAL( bt_max <= block_timestamp{tps_max}, true ) + // REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) + REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) silence_output(false); EOSIO_TEST_END @@ -294,7 +366,7 @@ EOSIO_TEST_END int main(int argc, char* argv[]) { // EOSIO_TEST(microseconds_type_test); // EOSIO_TEST(time_point_type_test); - EOSIO_TEST(time_point_sec_type_test); - // EOSIO_TEST(block_timestamp_type_test); + // EOSIO_TEST(time_point_sec_type_test); + EOSIO_TEST(block_timestamp_type_test); return has_failed(); } From e65b747549587fedaebcbf79ba1b75af0bd630db Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 18 Jan 2019 18:43:30 -0500 Subject: [PATCH 12/51] Continue refactor of tests for clarity and templates for future tests --- temp/TEST_NOTES | 175 ----------- temp/TODO | 331 +++++++++++++++++++++ temp/main.cpp | 14 - tests/unit/asset_tests.cpp | 574 ++++++++++++++++++++---------------- tests/unit/name_tests.cpp | 6 +- tests/unit/symbol_tests.cpp | 10 +- tests/unit/time_tests.cpp | 4 +- 7 files changed, 658 insertions(+), 456 deletions(-) delete mode 100644 temp/TEST_NOTES create mode 100644 temp/TODO delete mode 100644 temp/main.cpp diff --git a/temp/TEST_NOTES b/temp/TEST_NOTES deleted file mode 100644 index 56c82c3ffd..0000000000 --- a/temp/TEST_NOTES +++ /dev/null @@ -1,175 +0,0 @@ -[*] asset.hpp - dependencies - ************ - "serialize.hpp" - "print.hpp" - "system.hpp" - "symbol.hpp" - - - ************ - namespace eosio - --------------- - [*] asset - - constructors - [*] asset() - [*] asset(uint64_t, class symbol s) - - methods - [*] bool is_amount_within_range() - [*] bool is_valid() - [*] void set_amount(int64_t) - [*] std::string to_string() - [*] void print() - [*] operator- - [*] operator-= - [*] operator+= - [*] operator*= - [*] operator/= - [*] friend operator- - [*] friend operator+ - [*] friend operator* - [*] friend operator* - [*] friend operator/ - [*] friend operator/ - [*] friend operator== - [*] friend operator!= - [*] friend operator< - [*] friend operator<= - [*] friend operator> - [*] friend operator>= - - members - [*] uint64_t amount - [*] symbol symbol - [*] int64_t max_amount - [*] extended_asset - - constructors - [*] extended_asset() - [*] extended_asset(int64_t, extended_symbol) - [*] extended_asset(asset, name) - - methods - [*] extended_symbol get_extended_symbol() - [*] void print() - [*] operator- - [*] friend operator- - [*] friend operator+ - [*] friend operator+= - [*] friend operator-= - [*] friend operator< - [*] friend operator== - [*] friend operator!= - [*] friend operator<= - [*] friend operator>= - - members - [*] asset quantity - [*] name contract - --------------- -[*] binary_extension.hpp -[*] datastream.hpp -[*] eosio.hpp -[*] fixed_bytes.hpp -[*] name.hpp - dependencies - ************ - "system.hpp" - "serialize.hpp" - - - ************ - namespace eosio - --------------- - [*] name - - constructors - [✓] name() - [✓] name(uint64_t) - [✓] name(name::raw) - [✓] name(std::string_view) - - methods - [✓] uint8_t char_to_value(char) - [✓] uint8_t length() - [*] name suffix() - [*] operator raw() - [*] operator bool() - [*] char* write_as_string(char*, char*) - [*] std::string to_string() - [*] friend operator== - [*] friend operator!= - [*] friend operator< - - members - [*] uint64_t value - namespace detail - ---------------- - [*] to_const_char_arr - - members - [*] char value[] - ---------------- - ---------------- - [*] eosio::name operator"" _n -[*] print.hpp -[*] public_key.hpp -[*] serialize.hpp -[*] symbol.hpp - dependencies - ************ - "system.hpp" - "print.h" - "name.hpp" - "serialize.hpp" - - - - ************ - namespace eosio - --------------- - [*] symbol_code - - constructors - [*] symbol_code() - [*] symbol_code(uint64_t) - [*] symbol_code(std::string_view) - - methods - [*] bool is_valid() - [*] uint32_t length() - [*] uint64_t raw() - [*] operator bool() - [*] char* write_as_string(char*, char*) - [*] std::string to_string() - [*] friend operator== - [*] friend operator!= - [*] friend operator< - - members - [*] uint64_t value - [*] symbol - - constructors - [*] symbol() - [*] symbol(uint64_t) - [*] symbol(symbol_code, uint8_t) - [*] symbol(string_view, uint8_t) - - methods - [*] bool is_valid() - [*] uint8_t precision() - [*] symbol_code code() - [*] uint64_t raw() - [*] operator bool() - [*] void print(bool show_precision) - [*] friend operator== - [*] friend operator!= - [*] friend operator< - - members - [*] uint64_t value - [*] extended_symbol - - constructors - [*] extended_symbol() - [*] extended_symbol(symbol, name) - - methods - [*] symbol get_symbol() - [*] name get_contract() - [*] void print(bool show_precision) - [*] friend operator== - [*] friend operator!= - [*] friend operator< - - members - [*] symbol symbol - [*] name contract - --------------- -[*] system.hpp -[*] time.hpp -[*] varint.hpp diff --git a/temp/TODO b/temp/TODO new file mode 100644 index 0000000000..14c69a42ed --- /dev/null +++ b/temp/TODO @@ -0,0 +1,331 @@ +[✓] asset.hpp + dependencies + ************ + "serialize.hpp" + "print.hpp" + "system.hpp" + "symbol.hpp" + + + ************ + namespace eosio + --------------- + + [✓] asset + - constructors + [✓] asset() + [✓] asset(uint64_t, class symbol s) + - methods + [✓] bool is_amount_within_range() + [✓] bool is_valid() + [✓] void set_amount(int64_t) + [✓] std::string to_string() + [✓] void print() + [✓] operator- + [✓] operator-= + [✓] operator+= + [✓] operator*= + [✓] operator/= + [✓] friend operator- + [✓] friend operator+ + [✓] friend operator* + [✓] friend operator* + [✓] friend operator/ + [✓] friend operator/ + [✓] friend operator== + [✓] friend operator!= + [✓] friend operator< + [✓] friend operator<= + [✓] friend operator> + [✓] friend operator>= + - members + [✓] uint64_t amount + [✓] symbol symbol + [✓] int64_t max_amount + + [✓] extended_asset + - constructors + [✓] extended_asset() + [✓] extended_asset(int64_t, extended_symbol) + [✓] extended_asset(asset, name) + - methods + [✓] extended_symbol get_extended_symbol() + [✓] void print() + [✓] operator- + [✓] friend operator- + [✓] friend operator+ + [✓] friend operator+= + [✓] friend operator-= + [✓] friend operator< + [✓] friend operator== + [✓] friend operator!= + [✓] friend operator<= + [✓] friend operator>= + - members + [✓] asset quantity + [✓] name contract + --------------- + +[*] binary_extension.hpp +[*] datastream.hpp + +[✓] eosio.hpp + dependencies + ************ + "action.hpp" + "print.hpp" + "multi_index.hpp" + "dispatcher.hpp" + "contract.hpp" + ************ + namespace eosio + --------------- + - macros + [✓] #define CONTRACT + [✓] #define ACTION + [✓] #define TABLE + --------------- + +[*] fixed_bytes.hpp + +[✓] name.hpp + dependencies + ************ + "system.hpp" + "serialize.hpp" + + + ************ + namespace eosio + --------------- + + [✓] name + - constructors + [✓] name() + [✓] name(uint64_t) + [✓] name(name::raw) + [✓] name(std::string_view) + - methods + [✓] uint8_t char_to_value(char) + [✓] uint8_t length() + [✓] name suffix() + [✓] operator raw() + [✓] operator bool() + [✓] char* write_as_string(char*, char*) + [✓] std::string to_string() + [✓] friend operator== + [✓] friend operator!= + [✓] friend operator< + - members + [✓] uint64_t value + namespace detail + ---------------- + [✓] to_const_char_arr + - members + [✓] char value[] + ---------------- + ---------------- + [✓] eosio::name operator"" _n + +[*] print.hpp +[*] public_key.hpp +[*] serialize.hpp + +[✓] symbol.hpp + dependencies + ************ + "system.hpp" + "print.h" + "name.hpp" + "serialize.hpp" + + + + ************ + namespace eosio + --------------- + + [✓] symbol_code + - constructors + [✓] symbol_code() + [✓] symbol_code(uint64_t) + [✓] symbol_code(std::string_view) + - methods + [✓] bool is_valid() + [✓] uint32_t length() + [✓] uint64_t raw() + [✓] operator bool() + [✓] char* write_as_string(char*, char*) + [✓] std::string to_string() + [✓] friend operator== + [✓] friend operator!= + [✓] friend operator< + - members + [✓] uint64_t value + + [✓] symbol + - constructors + [✓] symbol() + [✓] symbol(uint64_t) + [✓] symbol(symbol_code, uint8_t) + [✓] symbol(string_view, uint8_t) + - methods + [✓] bool is_valid() + [✓] uint8_t precision() + [✓] symbol_code code() + [✓] uint64_t raw() + [✓] operator bool() + [✓] void print(bool show_precision) + [✓] friend operator== + [✓] friend operator!= + [✓] friend operator< + - members + [✓] uint64_t value + + [✓] extended_symbol + - constructors + [✓] extended_symbol() + [✓] extended_symbol(symbol, name) + - methods + [✓] symbol get_symbol() + [✓] name get_contract() + [✓] void print(bool show_precision) + [✓] friend operator== + [✓] friend operator!= + [✓] friend operator< + - members + [✓] symbol symbol + [✓] name contract + --------------- + +[✓] system.hpp + dependencies + ************ + "system.h" + + ************ + namespace eosio + --------------- + - methods + [✓] inline void check(bool pred, const char* msg) + [✓] inline void check(bool pred, const std::string& msg) + [✓] inline void check(bool pred, const std::string&& msg) + [✓] inline void check(bool pred, const char* msg, size_t n) + [✓] inline void check(bool pred, const std::string& msg, size_t n) + [✓] inline void check(bool pred, uint64_t code) + --------------- + +[✓] time.hpp + dependencies + ************ + "eosio.hpp" + + + ************ + namespace eosio + --------------- + [✓] microseconds + - constructors + [✓] microseconds(uint64_t) + - methods + [✓] static microseconds maximum() + [✓] friend microseconds operator+(const microseconds&, const microseconds&) + [✓] friend microseconds operator-(const microseconds&, const microseconds&) + [✓] bool operator==(const microseconds&) + [✓] bool operator!=(const microseconds&) + [✓] friend bool operator>(const microseconds&, const microseconds&) + [✓] friend bool operator>=(const microseconds&, const microseconds&) + [✓] friend bool operator<(const microseconds&, const microseconds&) + [✓] friend bool operator<=(const microseconds&, const microseconds&) + [✓] microseconds& operator+=(const microseconds&) + [✓] microseconds& operator-=(const microseconds&) + [✓] int64_t count() + [✓] int64_t to_seconds() + - members + [✓] count + + [✓] inline microseconds seconds(uint64_t) + [✓] inline microseconds milliseconds(uint64_t) + [✓] inline microseconds minutes(uint64_t) + [✓] inline microseconds hours(uint64_t) + [✓] inline microseconds days(uint64_t) + + [✓] time_point + - constructors + [✓] explicit time_point(microseconds) + - methods + [✓] const microseconds& time_since_epoch()const + [✓] uint32_t sec_since_epoch()const + [✓] bool operator>(const time_point&) + [✓] bool operator>=(const time_point&) + [✓] bool operator<(const time_point&) + [✓] bool operator<=(const time_point&) + [✓] bool operator==(const time_point&) + [✓] bool operator!=(const time_point&) + [✓] time_point& operator+=(const microseconds&) + [✓] time_point& operator-=(const microseconds&) + [✓] time_point operator+(const microseconds&) + [✓] time_point operator+(const time_point&) + [✓] time_point operator-(const microseconds&) + [✓] time_point operator-(const time_point&) + - members + [✓] microseconds elapsed + + [✓] time_point_sec + - constructors + [✓] time_point_sec() + [✓] explicit time_point_sec(uint32_t) + [✓] time_point_sec(const time_point&) + - methods + [✓] static time_point_sec maximum() + [✓] static time_point_sec min() + [✓] operator time_point()const + [✓] uint32_t sec_since_epoch()const + [✓] time_point_sec operator=(const eosio::time_point&) + [✓] friend bool operator<(const time_point_sec&, const time_point_sec&) + [✓] friend bool operator>(const time_point_sec&, const time_point_sec&) + [✓] friend bool operator<=(const time_point_sec&, const time_point_sec&) + [✓] friend bool operator>=(const time_point_sec&, const time_point_sec&) + [✓] friend bool operator==(const time_point_sec&, const time_point_sec&) + [✓] friend bool operator!=(const time_point_sec&, const time_point_sec&) + [✓] time_point_sec& operator+=(uint32_t) + [✓] time_point_sec& operator+=(microseconds) + [✓] time_point_sec& operator+=(time_point_sec) + [✓] time_point_sec& operator-=(uint32_t) + [✓] time_point_sec& operator-=(microseconds) + [✓] time_point_sec& operator-=(time_point_sec) + [✓] friend bool operator+(uint32_t) + [✓] friend bool operator-(uint32_t) + [✓] friend time_point operator+(const time_point_sec&, const microseconds&) + [✓] friend time_point operator-(const time_point_sec&, const microseconds&) + [✓] friend time_point operator-(const time_point_sec&, const microseconds&) + [✓] friend time_point operator-(const time_point_sec&, const time_point_sec&) + - members + [✓] uint32_t utc_seconds + + [✓] block_timestamp + - constructors + [✓] explicit block_timestamp(uint32_t) + [✓] block_timestamp(const time_point&) + [✓] block_timestamp(const time_point_sec&) + - methods + [✓] static block_timestamp maximum() + [✓] static block_timestamp min() + [✓] block_timestamp next()const + [✓] time_point to_time_point()const + [✓] operator time_point()const + [✓] void operator=(const time_point&) + [✓] bool operator>(const block_timestamp&) + [✓] bool operator>=(const block_timestamp&) + [✓] bool operator<(const block_timestamp&) + [✓] bool operator<=(const block_timestamp&) + [✓] bool operator==(const block_timestamp&) + [✓] bool operator!=(const block_timestamp&) + [✓] void set_time_point(const time_point&) + [✓] void set_time_point(const time_point_sec&) + - members + [✓] static constexpr int32_t block_interval_ms + [✓] static constexpr int32_t block_timestamp_epoch + [✓] uint32_t slot + --------------- +[*] varint.hpp diff --git a/temp/main.cpp b/temp/main.cpp deleted file mode 100644 index 9bf7631e35..0000000000 --- a/temp/main.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// eosio-cpp main.cpp -w -fnative; ./a.out - -#include -#include - -using namespace eosio; -using namespace eosio::native; - -int main() -{ - - - return 0; -} diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 4a27548aaa..57ac7f2813 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -7,8 +7,15 @@ using eosio::extended_symbol; using eosio::asset; using eosio::extended_asset; -const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL -const uint64_t u64min = std::numeric_limits::min(); // 0ULL +const int64_t i64_min{std::numeric_limits::min()}; // -9223372036854775808LL +const int64_t i64_max{std::numeric_limits::max()}; // 9223372036854775807LL + +const uint64_t u64_min{std::numeric_limits::min()}; // 0ULL +const uint64_t u64_max{std::numeric_limits::max()}; // 18446744073709551615ULL + +const int64_t asset_mask{(1LL << 62) - 1}; +const int64_t asset_min{-asset_mask}; // -4611686018427387903 +const int64_t asset_max{ asset_mask}; // 4611686018427387903 // For incrementing the `precision` in any test relating to `precision` // The `precision` field in the `symbol` type must be constructed via constexpr, @@ -35,8 +42,8 @@ EOSIO_TEST_BEGIN(asset_type_test) /// constexpr asset(int64_t, symbol) REQUIRE_EQUAL( (asset{0LL, s4}.amount), 0LL ); - REQUIRE_EQUAL( (asset{4611686018427387903LL, s4}.amount), 4611686018427387903LL ); - REQUIRE_EQUAL( (asset{-4611686018427387903LL, s4}.amount), -4611686018427387903LL ); + REQUIRE_EQUAL( (asset{asset_min, s4}.amount), asset_min ); + REQUIRE_EQUAL( (asset{asset_max, s4}.amount), asset_max ); REQUIRE_EQUAL( (asset{0LL, s0}.symbol.raw()), 16640ULL ); // "A", precision: 0 REQUIRE_EQUAL( (asset{0LL, s1}.symbol.raw()), 23040ULL ); // "Z", precision: 0 @@ -46,21 +53,26 @@ EOSIO_TEST_BEGIN(asset_type_test) REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{16639ULL}};}) ); REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{6510615555426900736ULL}};}) ); - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ 4611686018427387904LL, {}};}) ); - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{-4611686018427387904LL, {}};}) ); + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_min - 1, {}};}) ); + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_max + 1, {}};}) ); // ------------------------ // void set_amount(int64_t) asset a{0LL, s4}; REQUIRE_EQUAL( (a.set_amount(0LL), a.amount), 0LL); REQUIRE_EQUAL( (a.set_amount(1LL), a.amount), 1LL); - REQUIRE_EQUAL( (a.set_amount(4611686018427387903LL), a.amount), 4611686018427387903LL); - REQUIRE_EQUAL( (a.set_amount(-4611686018427387903LL), a.amount), -4611686018427387903LL); + REQUIRE_EQUAL( (a.set_amount(asset_min), a.amount), asset_min); + REQUIRE_EQUAL( (a.set_amount(asset_max), a.amount), asset_max); + + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( [&]() { + a.set_amount(asset_min - 1); + }) + ); - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( - [&]() {asset{0LL, s4}.set_amount(4611686018427387904LL);}) ); - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( - [&]() {asset{0LL, s4}.set_amount(-4611686018427387904LL);}) ); + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( [&]() { + a.set_amount(asset_max + 1); + }) + ); // --------------------------------------- // std::to_string()const/void print()const @@ -76,7 +88,7 @@ EOSIO_TEST_BEGIN(asset_type_test) // Note that `assets` with negative amounts with 0 `precision` print have the odd behavior of printing two '-' characters: REQUIRE_EQUAL( (asset{0LL, s4}.to_string()), "0. SYMBOLL" ); - REQUIRE_EQUAL( (asset{-0LL}, s4}.to_string()), + REQUIRE_EQUAL( (asset{-0LL, s4}.to_string()), "0. SYMBOLL" ); REQUIRE_EQUAL( (asset{0LL, s5}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); @@ -92,15 +104,15 @@ EOSIO_TEST_BEGIN(asset_type_test) REQUIRE_EQUAL( (asset{-1LL, s5}.to_string()), "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ); - REQUIRE_EQUAL( (asset{4611686018427387903LL, s4}.to_string()), - "4611686018427387903. SYMBOLL" ); - REQUIRE_EQUAL( (asset{-4611686018427387903LL, s4}.to_string()), + REQUIRE_EQUAL( (asset{asset_min, s4}.to_string()), "--4611686018427387903. SYMBOLL" ); - REQUIRE_EQUAL( (asset{4611686018427387903LL, s5}.to_string()), - "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); - REQUIRE_EQUAL( (asset{-4611686018427387903LL, s5}.to_string()), + REQUIRE_EQUAL( (asset{asset_max, s4}.to_string()), + "4611686018427387903. SYMBOLL" ); + REQUIRE_EQUAL( (asset{asset_min, s5}.to_string()), "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); - + REQUIRE_EQUAL( (asset{asset_max, s5}.to_string()), + "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); + // How are these `assets` constructed? Isn't `uint8_t precision` supposed to be constexpr when constructing a symbol? for( uint8_t precision{0}; precision < 64; ++precision ) { REQUIRE_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), @@ -109,24 +121,27 @@ EOSIO_TEST_BEGIN(asset_type_test) // ---------------------- // asset operator-()const - REQUIRE_EQUAL( (-asset{ 0LL}, s4}.amount), (asset{0LL, s4}.amount) ); - REQUIRE_EQUAL( (-asset{-0LL}, s4}.amount), (asset{0LL, s4}.amount) ); - REQUIRE_EQUAL( (-asset{ 0LL}, s5}.amount), (asset{0LL, s5}.amount) ); - REQUIRE_EQUAL( (-asset{-0LL}, s5}.amount), (asset{0LL, s5}.amount) ); - - REQUIRE_EQUAL( (-asset{ 1LL, s4}.amount), (asset{-1LL}, s4}.amount) ); - REQUIRE_EQUAL( (-asset{-1LL, s4}.amount), (asset{ 1LL}, s4}.amount) ); - REQUIRE_EQUAL( (-asset{ 1LL, s5}.amount), (asset{-1LL}, s5}.amount) ); - REQUIRE_EQUAL( (-asset{-1LL, s5}.amount), (asset{ 1LL}, s5}.amount) ); - - REQUIRE_EQUAL( (-asset{ 4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount), - ( asset{-4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{-4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount), - ( asset{ 4611686018427387903LL, symbol{"SYMBOLL", 0}}.amount) ); - REQUIRE_EQUAL( (-asset{ 4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount), - ( asset{-4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount) ); - REQUIRE_EQUAL( (-asset{-4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount), - ( asset{ 4611686018427387903LL, symbol{"SYMBOLL", 63}}.amount) ); + REQUIRE_EQUAL( (-asset{ 0LL, s4}.amount), (asset{0LL, s4}.amount) ); + REQUIRE_EQUAL( (-asset{-0LL, s4}.amount), (asset{0LL, s4}.amount) ); + REQUIRE_EQUAL( (-asset{ 0LL, s5}.amount), (asset{0LL, s5}.amount) ); + REQUIRE_EQUAL( (-asset{-0LL, s5}.amount), (asset{0LL, s5}.amount) ); + + REQUIRE_EQUAL( (-asset{ 1LL, s4}.amount), (asset{-1LL, s4}.amount) ); + REQUIRE_EQUAL( (-asset{-1LL, s4}.amount), (asset{ 1LL, s4}.amount) ); + REQUIRE_EQUAL( (-asset{ 1LL, s5}.amount), (asset{-1LL, s5}.amount) ); + REQUIRE_EQUAL( (-asset{-1LL, s5}.amount), (asset{ 1LL, s5}.amount) ); + + REQUIRE_EQUAL( (-asset{asset_min, symbol{"SYMBOLL", 0}}.amount), + ( asset{asset_max, symbol{"SYMBOLL", 0}}.amount) ); + + REQUIRE_EQUAL( (-asset{asset_max, symbol{"SYMBOLL", 0}}.amount), + ( asset{asset_min, symbol{"SYMBOLL", 0}}.amount) ); + + REQUIRE_EQUAL( (-asset{asset_min, symbol{"SYMBOLL", 63}}.amount), + ( asset{asset_max, symbol{"SYMBOLL", 63}}.amount) ); + + REQUIRE_EQUAL( (-asset{asset_max, symbol{"SYMBOLL", 63}}.amount), + ( asset{asset_min, symbol{"SYMBOLL", 63}}.amount) ); // ------------------------------------------------------------------------------------------ // inline friend asset& operator+(const asset&, const asset&)/asset& operator+=(const asset&) @@ -136,114 +151,162 @@ EOSIO_TEST_BEGIN(asset_type_test) REQUIRE_EQUAL( (asset{1LL, s4} += asset{-1LL, s4} ), (asset{0LL, s4}) ); - REQUIRE_ASSERT( "attempt to add asset with different symbol", ( - []() {asset{1LL}, s4} += asset{1LL, symbol{"LLOBMYS", 0}};}) ); - REQUIRE_ASSERT( "addition underflow", ( - []() {asset{-4611686018427387903LL}, s4} += -asset{1LL, s4};}) ); - REQUIRE_ASSERT( "addition overflow", ( - []() {asset{ 4611686018427387903LL}, s4} += asset{1LL, s4};}) ); + REQUIRE_ASSERT( "attempt to add asset with different symbol", ( [&]() { + asset{1LL, s4} += asset{1LL, symbol{"LLOBMYS", 0}}; + }) + ); + + REQUIRE_ASSERT( "addition underflow", ( [&]() { + asset{asset_min, s4} += -asset{1LL, s4}; + }) + ); + + REQUIRE_ASSERT( "addition overflow", ( [&]() { + asset{asset_max, s4} += asset{1LL, s4}; + }) + ); // ------------------------------------------------------------------------------------------ // inline friend asset& operator-(const asset&, const asset&)/asset& operator-=(const asset&) - REQUIRE_EQUAL( (asset{0LL, s4} -= asset{0LL, s4} ), - (asset{0LL, s4}) ); + REQUIRE_EQUAL( (asset{0LL, s4} -= asset{0LL, s4} ), (asset{0LL, s4}) ); + REQUIRE_EQUAL( (asset{1LL, s4} -= asset{1LL, s4} ), (asset{0LL, s4}) ); - REQUIRE_EQUAL( (asset{1LL, s4} -= asset{1LL, s4} ), - (asset{0LL, s4}) ); + REQUIRE_ASSERT( "attempt to subtract asset with different symbol", ( [&]() { + asset{1LL, s4} -= asset{1LL, symbol{"LLOBMYS", 0}}; + }) + ); - REQUIRE_ASSERT( "attempt to subtract asset with different symbol", ( - []() {asset{1LL, s4} -= asset{1LL, symbol{"LLOBMYS", 0}};}) ); - REQUIRE_ASSERT( "subtraction underflow", ( - []() {asset{-4611686018427387903LL}, s4} -= asset{1LL, s4};}) ); - REQUIRE_ASSERT( "subtraction overflow", ( - []() {asset{ 4611686018427387903LL}, s4} -= -asset{1LL, s4};}) ); + REQUIRE_ASSERT( "subtraction underflow", ( [&]() { + asset{asset_min, s4} -= asset{1LL, s4}; + }) + ); + + REQUIRE_ASSERT( "subtraction overflow", ( [&]() { + asset{asset_max, s4} -= -asset{1LL, s4}; + }) + ); // ----------------------------------------------------------------------- // friend asset operator*(const asset&, int64_t)/asset operator*=(int64_t) - REQUIRE_EQUAL( (asset{ 0LL}, s4} *= 0 ), - (asset{ 0LL}, s4}) ); - - REQUIRE_EQUAL( (asset{ 2LL}, s4} *= 1LL ), - (asset{ 2LL}, s4}) ); - REQUIRE_EQUAL( (asset{ 2LL}, s4} *= -1LL ), - (asset{-2LL}, s4}) ); - - REQUIRE_ASSERT( "multiplication overflow", ( - []() {asset{ 4611686018427387903LL}, s4} *= 2LL;}) ); - REQUIRE_ASSERT( "multiplication underflow", ( - []() {asset{-4611686018427387903LL}, s4} *= 2LL;}) ); - - // // ------------------------------------------------------------------------ - // // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) - // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), - // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); - // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), - // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); - // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), - // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); - // REQUIRE_EQUAL( (asset{int64_t{}, symbol{"SYMBOLL", 0}} /= asset{int64_t{}, symbol{"SYMBOLL", 0}} ), - // (asset{int64_t{}, symbol{"SYMBOLL", 0}}) ); - - //REQUIRE_ASSERT( "divide by zero", ( []() {asset{int64_t{1}, symbol{"SYMBOLL", 0}} /= 0;}) ); -/////////////////////////////////////////////////////////////////////////////////////////////////// -// Why is this not throwing?? -// And why is my addition test throwing?? - // REQUIRE_ASSERT( "signed division overflow", ( - // []() {asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}} /= -1LL;}) ); - - // REQUIRE_ASSERT( "divide by zero", ( - // []() {asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}} / - // asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}};}) ); - // REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( - // []() {asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}} / - // asset{int64_t{std::numeric_limits::min()}, symbol{"SYMBOLL", 0}}}) ); -/////////////////////////////////////////////////////////////////////////////////////////////////// + REQUIRE_EQUAL( (asset{ 0LL, s4} *= 0 ), (asset{ 0LL, s4}) ); + REQUIRE_EQUAL( (asset{ 2LL, s4} *= 1LL ), (asset{ 2LL, s4}) ); + REQUIRE_EQUAL( (asset{ 2LL, s4} *= -1LL ), (asset{-2LL, s4}) ); + + REQUIRE_ASSERT( "multiplication underflow", ( [&]() { + asset{asset_min, s4} *= 2LL; + }) + ); + + REQUIRE_ASSERT( "multiplication overflow", ( [&]() { + asset{ asset_max, s4} *= 2LL; + }) + ); + + // ----------------------------------------------------------------------------------------------------------------------------- + // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) + REQUIRE_EQUAL( (asset{ 0LL, s4} / asset{ 1LL, s4}.amount ), (asset{ 0LL, s4}) ); + REQUIRE_EQUAL( (asset{ 1LL, s4} / asset{ 1LL, s4}.amount ), (asset{ 1LL, s4}) ); + REQUIRE_EQUAL( (asset{ 4LL, s4} / asset{ 2LL, s4}.amount ), (asset{ 2LL, s4}) ); + REQUIRE_EQUAL( (asset{-4LL, s4} / asset{ 2LL, s4}.amount ), (asset{-2LL, s4}) ); + REQUIRE_EQUAL( (asset{-4LL, s4} / asset{-2LL, s4}.amount ), (asset{ 2LL, s4}) ); + + REQUIRE_EQUAL( (asset{ 0LL, s4} /= asset{ 1LL, s4}.amount ), (asset{ 0LL, s4}) ); + REQUIRE_EQUAL( (asset{ 1LL, s4} /= asset{ 1LL, s4}.amount ), (asset{ 1LL, s4}) ); + REQUIRE_EQUAL( (asset{ 4LL, s4} /= asset{ 2LL, s4}.amount ), (asset{ 2LL, s4}) ); + REQUIRE_EQUAL( (asset{-4LL, s4} /= asset{ 2LL, s4}.amount ), (asset{-2LL, s4}) ); + REQUIRE_EQUAL( (asset{-4LL, s4} /= asset{-2LL, s4}.amount ), (asset{ 2LL, s4}) ); + + REQUIRE_EQUAL( (asset{ 0LL, s4} / asset{ 1LL, s4} ), (asset{ 0LL, s4}.amount) ); + REQUIRE_EQUAL( (asset{ 1LL, s4} / asset{ 1LL, s4} ), (asset{ 1LL, s4}.amount) ); + REQUIRE_EQUAL( (asset{ 4LL, s4} / asset{ 2LL, s4} ), (asset{ 2LL, s4}.amount) ); + REQUIRE_EQUAL( (asset{-4LL, s4} / asset{ 2LL, s4} ), (asset{-2LL, s4}.amount) ); + REQUIRE_EQUAL( (asset{-4LL, s4} / asset{-2LL, s4} ), (asset{ 2LL, s4}.amount) ); + + REQUIRE_ASSERT( "divide by zero", ( [&]() {asset{1LL, s4} /= 0;}) ); + + // Note that there should be an invariant established here when adding or setting the `amount` + REQUIRE_ASSERT( "signed division overflow", ( [&]() { + asset a{}; + a.amount = std::numeric_limits::min(); + a /= -1LL; + }) + ); + + REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( [&]() { + asset{{1LL}, s0} / asset{{1LL}, s1}; + }) + ); // -------------------------------------------------- // friend bool operator==(const asset&, const asset&) - REQUIRE_EQUAL( (asset{int64_t{0}, symbol{"SYMBOLL", 0}} == asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), true ); - - REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} == - asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( (asset{{}, s4} == asset{{}, s4} ), true ); + REQUIRE_EQUAL( (asset{asset_min, s4} == asset{asset_min, s4} ), true ); + REQUIRE_EQUAL( (asset{asset_max, s4} == asset{asset_max, s4} ), true ); - REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} == - asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); - - REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( - []() {asset{int64_t{0}, symbol{"SYMBOLL", 0}} == asset{int64_t{0}, symbol{"LLOBMYS", 0}};}) ); + REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( []() { + asset{{}, symbol{"SYMBOLL", 0}} == asset{{}, symbol{"LLOBMYS", 0}}; + }) + ); // --------------------------------------------------- // friend bool operator!=( const asset&, const asset&) - REQUIRE_EQUAL( (asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} != - -asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); - - REQUIRE_EQUAL( (asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} != - -asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( (asset{asset_min, s4} != -asset{asset_min, s4} ), true ); + REQUIRE_EQUAL( (asset{asset_max, s4} != -asset{asset_max, s4} ), true ); // ------------------------------------------------- // friend bool operator<(const asset&, const asset&) - REQUIRE_EQUAL( ( asset{int64_t{0}, symbol{"SYMBOLL", 0}} < asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( (asset{{}, s4} < asset{1LL, s4} ), true ); // -------------------------------------------------- // friend bool operator<=(const asset&, const asset&) - REQUIRE_EQUAL( ( asset{int64_t{0}, symbol{"SYMBOLL", 0}} <= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); - REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} <= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( ( asset{{}, s4} <= asset{1LL, s4} ), true ); + REQUIRE_EQUAL( ( asset{1LL, s4} <= asset{1LL, s4} ), true ); // ------------------------------------------------- // friend bool operator>(const asset&, const asset&) - REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} > asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( ( asset{1LL, s4} > asset{{}, s4} ), true ); + REQUIRE_EQUAL( ( asset{{}, s4} > asset{1LL, s4} ), false ); // -------------------------------------------------- // friend bool operator>=( const asset&, const asset&) - REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} >= asset{int64_t{0}, symbol{"SYMBOLL", 0}} ), true ); - REQUIRE_EQUAL( ( asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} >= asset{int64_t{1LL}, symbol{"SYMBOLL", 0}} ), true ); + REQUIRE_EQUAL( ( asset{1LL, s4} >= asset{{}, s4} ), true ); + REQUIRE_EQUAL( ( asset{1LL, s4} >= asset{1LL, s4} ), true ); silence_output(false); EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(extended_asset_type_test) - silence_output(true); + silence_output(false); + + name n0{"1"}; + name n1{"5"}; + name n2{"a"}; + name n3{"z"}; + name n4{"111111111111j"}; + name n5{"555555555555j"}; + name n6{"aaaaaaaaaaaaj"}; + name n7{"zzzzzzzzzzzzj"}; + name n8{"eosioaccountj"}; + + symbol s0{"A", 0}; + symbol s1{"Z", 0}; + symbol s2{"AAAAAAA", 0}; + symbol s3{"ZZZZZZZ", 0}; + symbol s4{"SYMBOLL", 0}; + symbol s5{"SYMBOLL", 63}; + + extended_symbol es0{s4, n8}; + + asset a0{0LL, s0}; + asset a1{0LL, s0}; + asset a2{0LL, s1}; + asset a3{0LL, s1}; + asset a4{0LL, s2}; + asset a5{0LL, s2}; + asset a6{0LL, s3}; + asset a7{0LL, s3}; + asset a8{0LL, s4}; // ------------ // constructors @@ -252,175 +315,172 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) REQUIRE_EQUAL( extended_asset{}.quantity, asset{} ); REQUIRE_EQUAL( extended_asset{}.contract, name{} ); + /// extended_asset(int64_t, extended_symbol) + REQUIRE_EQUAL( (extended_asset{{}, es0}.quantity), (asset{{}, s4}) ); + REQUIRE_EQUAL( (extended_asset{{}, es0}.contract), n8); + /// extended_asset(asset, name) - REQUIRE_EQUAL( (extended_asset{int64_t{0}, extended_symbol{symbol{"SYMBOLL", 0}, name{"eosioaccountj"}}}.quantity), - (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); - REQUIRE_EQUAL( (extended_asset{int64_t{0}, extended_symbol{symbol{"SYMBOLL", 0}, name{"eosioaccountj"}}}.contract), - name{"eosioaccountj"}); - - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}.quantity), - (asset{int64_t{0}, symbol{"SYMBOLL", 0}}) ); - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}.contract), - (name{"eosioaccountj"}) ); - - // ------------------------------------------ - // extended_symbol get_extended_symbol()const - REQUIRE_EQUAL( (extended_asset{asset{}, name{}}.get_extended_symbol()), extended_symbol{} ); - - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.get_extended_symbol()), - (extended_symbol{symbol{"A", 0}, name{"1"}}) ); - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.get_extended_symbol()), - (extended_symbol{symbol{"A", 0}, name{"5"}}) ); - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.get_extended_symbol()), - (extended_symbol{symbol{"Z", 0}, name{"a"}}) ); - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.get_extended_symbol()), - (extended_symbol{symbol{"Z", 0}, name{"z"}}) ); - - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.get_extended_symbol()), - (extended_symbol{symbol{"AAAAAAA", 0}, name{"111111111111j"}}) ); - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.get_extended_symbol()), - (extended_symbol{symbol{"AAAAAAA", 0}, name{"555555555555j"}}) ); - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.get_extended_symbol()), - (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"aaaaaaaaaaaaj"}}) ); - REQUIRE_EQUAL( (extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.get_extended_symbol()), - (extended_symbol{symbol{"ZZZZZZZ", 0}, name{"zzzzzzzzzzzzj"}}) ); - - - // Fix this printing functionality - // Note that if there is no precision, there will be odd output: - // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` - // output: "0. A@1" - // ----------------- - // void print()const -/////////////////////////////////////////////////////////////////////////////////////////////////// -// How is this not the correct print result?? -// print_f("0. A@1"); -// print_f("\n");; -// extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print(); -// print_f("\n");; -// REQUIRE_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ); -// REQUIRE_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ); -// REQUIRE_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ); -// REQUIRE_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ); - -// REQUIRE_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ); -// REQUIRE_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ); -// REQUIRE_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ); -// REQUIRE_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ); -/////////////////////////////////////////////////////////////////////////////////////////////////// - - // ------------------------------- - // extended_asset operator-()const - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), - (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), - (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), - (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), - (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), - (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), - (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), - (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), - (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), - (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), - (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), - (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), - (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + REQUIRE_EQUAL( (extended_asset{a8, n8}.quantity), (asset{{}, s4}) ); + REQUIRE_EQUAL( (extended_asset{a8, n8}.contract), (n8) ); + + // // ------------------------------------------ + // // extended_symbol get_extended_symbol()const + // REQUIRE_EQUAL( (extended_asset{{}, {}}.get_extended_symbol()), extended_symbol{} ); + + // REQUIRE_EQUAL( (extended_asset{asset{{}, s0}, n0}.get_extended_symbol()), + // (extended_symbol{symbol{"A", 0}, name{"1"}}) ); + // REQUIRE_EQUAL( (extended_asset{asset{{}, s0}, n1}.get_extended_symbol()), + // (extended_symbol{symbol{"A", 0}, name{"5"}}) ); + // REQUIRE_EQUAL( (extended_asset{asset{{}, s1}, n2}.get_extended_symbol()), + // (extended_symbol{symbol{"Z", 0}, name{"a"}}) ); + // REQUIRE_EQUAL( (extended_asset{asset{{}, s1}, n3}.get_extended_symbol()), + // (extended_symbol{symbol{"Z", 0}, name{"z"}}) ); + + // REQUIRE_EQUAL( (extended_asset{asset{{}, s2}, n4}.get_extended_symbol()), + // (extended_symbol{s2, n4}) ); + // REQUIRE_EQUAL( (extended_asset{asset{{}, s2}, n5}.get_extended_symbol()), + // (extended_symbol{s2, n5}) ); + // REQUIRE_EQUAL( (extended_asset{asset{{}, symbol{"ZZZZZZZ", 0}}, n6}.get_extended_symbol()), + // (extended_symbol{symbol{"ZZZZZZZ", 0}, n6}) ); + // REQUIRE_EQUAL( (extended_asset{asset{{}, symbol{"ZZZZZZZ", 0}}, n7}.get_extended_symbol()), + // (extended_symbol{symbol{"ZZZZZZZ", 0}, n7}) ); + + +// // Fix this printing functionality +// // Note that if there is no precision, there will be odd output: +// // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` +// // output: "0. A@1" +// // ----------------- +// // void print()const +// /////////////////////////////////////////////////////////////////////////////////////////////////// +// // How is this not the correct print result?? +// // print_f("0. A@1"); +// // print_f("\n");; +// // extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print(); +// // print_f("\n");; +// // REQUIRE_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ); +// // REQUIRE_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ); +// // REQUIRE_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ); +// // REQUIRE_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ); + +// // REQUIRE_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ); +// // REQUIRE_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ); +// // REQUIRE_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ); +// // REQUIRE_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ); +// /////////////////////////////////////////////////////////////////////////////////////////////////// + +// // ------------------------------- +// // extended_asset operator-()const +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), +// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), +// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), +// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), +// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); + +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); +// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), +// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - // ----------------------------------------------------------------------------- - // friend extended_asset operator+(const extended_asset&, const extended_asset&) - extended_asset asset0{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}; - extended_asset asset1{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}; - extended_asset asset2{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}; +// // ----------------------------------------------------------------------------- +// // friend extended_asset operator+(const extended_asset&, const extended_asset&) +// extended_asset asset0{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}; +// extended_asset asset1{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}; +// extended_asset asset2{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}; - extended_asset asset3{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}; - extended_asset asset4{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"jtnuoccaoisoe"}}; +// extended_asset asset3{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}; +// extended_asset asset4{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"jtnuoccaoisoe"}}; - REQUIRE_EQUAL( (asset0 + asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_EQUAL( (asset1 + asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// REQUIRE_EQUAL( (asset0 + asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// REQUIRE_EQUAL( (asset1 + asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_ASSERT( "type mismatch", ( - [&]() {asset3 + asset4;}) ); +// REQUIRE_ASSERT( "type mismatch", ( +// [&]() {asset3 + asset4;}) ); - // ------------------------------------------------------------------------- - // friend extended_asset& operator+=(extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (asset0 += asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_EQUAL( (asset1 += asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// // ------------------------------------------------------------------------- +// // friend extended_asset& operator+=(extended_asset&, const extended_asset&) +// REQUIRE_EQUAL( (asset0 += asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// REQUIRE_EQUAL( (asset1 += asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_ASSERT( "type mismatch", ( - [&]() {asset3 += asset4;}) ); +// REQUIRE_ASSERT( "type mismatch", ( +// [&]() {asset3 += asset4;}) ); - // -------------------------------------------------------------------------- - // friend extended_asset& operator-=( extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (asset0 - asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_EQUAL( (asset1 - asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// // -------------------------------------------------------------------------- +// // friend extended_asset& operator-=( extended_asset&, const extended_asset&) +// REQUIRE_EQUAL( (asset0 - asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// REQUIRE_EQUAL( (asset1 - asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_ASSERT( "type mismatch", ( - [&]() {asset3 - asset4;}) ); +// REQUIRE_ASSERT( "type mismatch", ( +// [&]() {asset3 - asset4;}) ); - // ---------------------------------------------------------------------------- - //friend extended_asset operator-(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (asset0 -= asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_EQUAL( (asset1 -= asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// // ---------------------------------------------------------------------------- +// //friend extended_asset operator-(const extended_asset&, const extended_asset&) +// REQUIRE_EQUAL( (asset0 -= asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); +// REQUIRE_EQUAL( (asset1 -= asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - REQUIRE_ASSERT( "type mismatch", ( - [&]() {asset3 -= asset4;}) ); +// REQUIRE_ASSERT( "type mismatch", ( +// [&]() {asset3 -= asset4;}) ); - // -------------------------------------------------------------------- - // friend bool operator==(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (asset0 == asset0), true ); - REQUIRE_EQUAL( (asset1 == asset1), true ); +// // -------------------------------------------------------------------- +// // friend bool operator==(const extended_asset&, const extended_asset&) +// REQUIRE_EQUAL( (asset0 == asset0), true ); +// REQUIRE_EQUAL( (asset1 == asset1), true ); -/////////////////////////////////////////////////////////////////////////////////////////////////// -// Why is this not throwing?? - // REQUIRE_ASSERT( "type mismatch", ( - // [&]() {asset3 == asset4;}) ); -/////////////////////////////////////////////////////////////////////////////////////////////////// +// /////////////////////////////////////////////////////////////////////////////////////////////////// +// // Why is this not throwing?? +// // REQUIRE_ASSERT( "type mismatch", ( +// // [&]() {asset3 == asset4;}) ); +// /////////////////////////////////////////////////////////////////////////////////////////////////// - // -------------------------------------------------------------------- - // friend bool operator!=(const extended_asset&, const extended_asset&) -// print_f("%", asset3 != asset4); -// print_f("\n"); - REQUIRE_EQUAL( asset3 != asset4, true ); - // REQUIRE_EQUAL( (asset1 != asset2), false ); - // REQUIRE_EQUAL( (asset2 != asset3), false ); +// // -------------------------------------------------------------------- +// // friend bool operator!=(const extended_asset&, const extended_asset&) +// // print_f("%", asset3 != asset4); +// // print_f("\n"); +// REQUIRE_EQUAL( asset3 != asset4, true ); +// // REQUIRE_EQUAL( (asset1 != asset2), false ); +// // REQUIRE_EQUAL( (asset2 != asset3), false ); - // REQUIRE_ASSERT( "type mismatch", ( - // [&]() {asset0 != asset1;}) ); +// // REQUIRE_ASSERT( "type mismatch", ( +// // [&]() {asset0 != asset1;}) ); - // -------------------------------------------------------------------- - // friend bool operator<( const extended_asset&, const extended_asset&) - // ??? - REQUIRE_EQUAL( asset0 < asset1, true ); +// // -------------------------------------------------------------------- +// // friend bool operator<( const extended_asset&, const extended_asset&) +// // ??? +// REQUIRE_EQUAL( asset0 < asset1, true ); - // REQUIRE_ASSERT( "type mismatch", ( - // []() {extended_asset{asset{}, name{}};}) ); +// // REQUIRE_ASSERT( "type mismatch", ( +// // []() {extended_asset{asset{}, name{}};}) ); - // -------------------------------------------------------------------- - // friend bool operator<=(const extended_asset&, const extended_asset&) - // REQUIRE_EQUAL( asset0 <= asset1, true ); +// // -------------------------------------------------------------------- +// // friend bool operator<=(const extended_asset&, const extended_asset&) +// // REQUIRE_EQUAL( asset0 <= asset1, true ); - // -------------------------------------------------------------------- - // friend bool operator>=(const extended_asset&, const extended_asset&) - // REQUIRE_EQUAL( asset1 > asset0, true ); +// // -------------------------------------------------------------------- +// // friend bool operator>=(const extended_asset&, const extended_asset&) +// // REQUIRE_EQUAL( asset1 > asset0, true ); silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { - EOSIO_TEST(asset_type_test); + // EOSIO_TEST(asset_type_test); EOSIO_TEST(extended_asset_type_test); return has_failed(); } diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 6f53449bb8..397922ed0f 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -3,12 +3,12 @@ using eosio::name; -const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL const uint64_t u64min = std::numeric_limits::min(); // 0ULL +const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL // Defined in `eosio.cdt/libraries/eosiolib/name.hpp` EOSIO_TEST_BEGIN(name_type_test) - silence_output(true); + silence_output(false); // ------------ // constructors @@ -64,7 +64,7 @@ EOSIO_TEST_BEGIN(name_type_test) // -------------------------------------------- // static constexpr uint8_t char_to_value(char) char c{'.'}; - uint8_t expected_value{0}; // Will increment to the expected correct value in the set [0,32) + uint8_t expected_value{}; // Will increment to the expected correct value in the set [0,32) CHECK_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index eda4219a1c..706c078960 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -6,8 +6,8 @@ using eosio::symbol_code; using eosio::symbol; using eosio::extended_symbol; -const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL const uint64_t u64min = std::numeric_limits::min(); // 0ULL +const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL // Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_code_type_test) @@ -213,10 +213,10 @@ EOSIO_TEST_BEGIN(symbol_type_test) // -------------------------------------------------------------- // friend constexpr bool operator!=(const symbol&, const symbol&) - CHECK_EQUAL( (symbol{sc0, 0} != symbol{0}), true ) - CHECK_EQUAL( (symbol{sc1, 0} != symbol{0}), true ) - CHECK_EQUAL( (symbol{sc2, 0} != symbol{0}), true ) - CHECK_EQUAL( (symbol{sc3, 0} != symbol{0}), true ) + CHECK_EQUAL( (symbol{sc0, 0} != symbol{}), true ) + CHECK_EQUAL( (symbol{sc1, 0} != symbol{}), true ) + CHECK_EQUAL( (symbol{sc2, 0} != symbol{}), true ) + CHECK_EQUAL( (symbol{sc3, 0} != symbol{}), true ) // -------------------------------------------------------------- // friebnd constexpr bool operator<(const symbol&, const symbol&) diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index ef7939c1b7..333489517a 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -10,11 +10,11 @@ using eosio::block_timestamp; using namespace eosio::native; -const uint32_t u32MAX = std::numeric_limits::max(); // 18446744073709551615 const uint32_t u32MIN = std::numeric_limits::min(); // 0 +const uint32_t u32MAX = std::numeric_limits::max(); // 18446744073709551615 -const int64_t i64MAX = std::numeric_limits::max(); // 9223372036854775807 const int64_t i64MIN = std::numeric_limits::min(); // -9223372036854775808 +const int64_t i64MAX = std::numeric_limits::max(); // 9223372036854775807 // This is interesting. `print_f` won't print the '-' character. // `printf` won't print anything. From 897627f9b9c55d9efe58ff5ac280fec6d14f4996 Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 23 Jan 2019 15:42:46 -0500 Subject: [PATCH 13/51] Start varint and print tests --- tests/unit/asset_tests.cpp | 592 ++++++++++++++++-------------------- tests/unit/name_tests.cpp | 486 ++++++++++++++--------------- tests/unit/print_tests.cpp | 131 ++++++-- tests/unit/symbol_tests.cpp | 311 ++++++++++--------- tests/unit/time_tests.cpp | 140 +++++---- tests/unit/varint_tests.cpp | 144 +++++++++ 6 files changed, 986 insertions(+), 818 deletions(-) create mode 100644 tests/unit/varint_tests.cpp diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 57ac7f2813..2635004450 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -7,12 +7,6 @@ using eosio::extended_symbol; using eosio::asset; using eosio::extended_asset; -const int64_t i64_min{std::numeric_limits::min()}; // -9223372036854775808LL -const int64_t i64_max{std::numeric_limits::max()}; // 9223372036854775807LL - -const uint64_t u64_min{std::numeric_limits::min()}; // 0ULL -const uint64_t u64_max{std::numeric_limits::max()}; // 18446744073709551615ULL - const int64_t asset_mask{(1LL << 62) - 1}; const int64_t asset_min{-asset_mask}; // -4611686018427387903 const int64_t asset_max{ asset_mask}; // 4611686018427387903 @@ -24,55 +18,63 @@ constexpr uint8_t increment_precision( uint8_t precision ) { return ++precision; // Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) - silence_output(false); + silence_output(true); symbol s0{"A", 0}; symbol s1{"Z", 0}; symbol s2{"AAAAAAA", 0}; symbol s3{"ZZZZZZZ", 0}; - symbol s4{"SYMBOLL", 0}; - symbol s5{"SYMBOLL", 63}; + symbol sym_no_prec{"SYMBOLL", 0}; // Symbol with no precision + symbol sym_prec{"SYMBOLL", 63}; // Symbol with precision // -------------------------------------------------------------------- // constructors/bool is_amount_within_range()const/bool is_valid()const /// constexpr asset() - REQUIRE_EQUAL( asset{}.amount, 0ULL ); - REQUIRE_EQUAL( asset{}.symbol.raw(), 0ULL ); - + REQUIRE_EQUAL( asset{}.amount, 0ULL ) + REQUIRE_EQUAL( asset{}.symbol.raw(), 0ULL ) + /// constexpr asset(int64_t, symbol) - REQUIRE_EQUAL( (asset{0LL, s4}.amount), 0LL ); - REQUIRE_EQUAL( (asset{asset_min, s4}.amount), asset_min ); - REQUIRE_EQUAL( (asset{asset_max, s4}.amount), asset_max ); - - REQUIRE_EQUAL( (asset{0LL, s0}.symbol.raw()), 16640ULL ); // "A", precision: 0 - REQUIRE_EQUAL( (asset{0LL, s1}.symbol.raw()), 23040ULL ); // "Z", precision: 0 - REQUIRE_EQUAL( (asset{0LL, s2}.symbol.raw()), 4702111234474983680ULL ); // "AAAAAAA", precision: 0 - REQUIRE_EQUAL( (asset{0LL, s3}.symbol.raw()), 6510615555426900480ULL ); // "ZZZZZZZ", precision: 0 - - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{16639ULL}};}) ); - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{6510615555426900736ULL}};}) ); - - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_min - 1, {}};}) ); - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_max + 1, {}};}) ); + REQUIRE_EQUAL( (asset{0LL, s0}.amount), 0LL ) + REQUIRE_EQUAL( (asset{asset_min, s0}.amount), asset_min ) + REQUIRE_EQUAL( (asset{asset_max, s0}.amount), asset_max ) + + REQUIRE_EQUAL( (asset{0LL, s0}.symbol.raw()), 16640ULL ) // "A", precision: 0 + REQUIRE_EQUAL( (asset{0LL, s1}.symbol.raw()), 23040ULL ) // "Z", precision: 0 + REQUIRE_EQUAL( (asset{0LL, s2}.symbol.raw()), 4702111234474983680ULL ) // "AAAAAAA", precision: 0 + REQUIRE_EQUAL( (asset{0LL, s3}.symbol.raw()), 6510615555426900480ULL ) // "ZZZZZZZ", precision: 0 + + // There is an invariant established for `asset` that isn't enforced with `symbol` + // For example: + // valid code: + // `symbol{};` + // throws "invalid symbol name": + // `asset{{}, symbol{}};` + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{0LL}};}) ) + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{1LL}};}) ) + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{16639ULL}};}) ) + REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{6510615555426900736ULL}};}) ) + + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_min - 1, {}};}) ) + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_max + 1, {}};}) ) // ------------------------ // void set_amount(int64_t) - asset a{0LL, s4}; - REQUIRE_EQUAL( (a.set_amount(0LL), a.amount), 0LL); - REQUIRE_EQUAL( (a.set_amount(1LL), a.amount), 1LL); - REQUIRE_EQUAL( (a.set_amount(asset_min), a.amount), asset_min); - REQUIRE_EQUAL( (a.set_amount(asset_max), a.amount), asset_max); - - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( [&]() { + asset a{0LL, sym_no_prec}; + REQUIRE_EQUAL( (a.set_amount(0LL), a.amount), 0LL ) + REQUIRE_EQUAL( (a.set_amount(1LL), a.amount), 1LL ) + REQUIRE_EQUAL( (a.set_amount(asset_min), a.amount), asset_min ) + REQUIRE_EQUAL( (a.set_amount(asset_max), a.amount), asset_max ) + + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([&]() { a.set_amount(asset_min - 1); }) - ); + ) - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ( [&]() { + REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([&]() { a.set_amount(asset_max + 1); }) - ); + ) // --------------------------------------- // std::to_string()const/void print()const @@ -86,401 +88,331 @@ EOSIO_TEST_BEGIN(asset_type_test) // output: "1. SYMBOLL" // Note that `assets` with negative amounts with 0 `precision` print have the odd behavior of printing two '-' characters: - REQUIRE_EQUAL( (asset{0LL, s4}.to_string()), - "0. SYMBOLL" ); - REQUIRE_EQUAL( (asset{-0LL, s4}.to_string()), - "0. SYMBOLL" ); - REQUIRE_EQUAL( (asset{0LL, s5}.to_string()), - "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); - REQUIRE_EQUAL( (asset{-0LL, s5}.to_string()), - "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ); - - REQUIRE_EQUAL( (asset{1LL, s4}.to_string()), - "1. SYMBOLL" ); - REQUIRE_EQUAL( (asset{-1LL, s4}.to_string()), - "--1. SYMBOLL" ); - REQUIRE_EQUAL( (asset{1LL, s5}.to_string()), - "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ); - REQUIRE_EQUAL( (asset{-1LL, s5}.to_string()), - "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ); - - REQUIRE_EQUAL( (asset{asset_min, s4}.to_string()), - "--4611686018427387903. SYMBOLL" ); - REQUIRE_EQUAL( (asset{asset_max, s4}.to_string()), - "4611686018427387903. SYMBOLL" ); - REQUIRE_EQUAL( (asset{asset_min, s5}.to_string()), - "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); - REQUIRE_EQUAL( (asset{asset_max, s5}.to_string()), - "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ); + REQUIRE_EQUAL( (asset{0LL, sym_no_prec}.to_string()), + "0. SYMBOLL" ) + REQUIRE_EQUAL( (asset{-0LL, sym_no_prec}.to_string()), + "0. SYMBOLL" ) + REQUIRE_EQUAL( (asset{0LL, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) + REQUIRE_EQUAL( (asset{-0LL, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) + + REQUIRE_EQUAL( (asset{1LL, sym_no_prec}.to_string()), + "1. SYMBOLL" ) + REQUIRE_EQUAL( (asset{-1LL, sym_no_prec}.to_string()), + "--1. SYMBOLL" ) + REQUIRE_EQUAL( (asset{1LL, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) + REQUIRE_EQUAL( (asset{-1LL, sym_prec}.to_string()), + "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) + + REQUIRE_EQUAL( (asset{asset_min, sym_no_prec}.to_string()), + "--4611686018427387903. SYMBOLL" ) + REQUIRE_EQUAL( (asset{asset_max, sym_no_prec}.to_string()), + "4611686018427387903. SYMBOLL" ) + REQUIRE_EQUAL( (asset{asset_min, sym_prec}.to_string()), + "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) + REQUIRE_EQUAL( (asset{asset_max, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) // How are these `assets` constructed? Isn't `uint8_t precision` supposed to be constexpr when constructing a symbol? for( uint8_t precision{0}; precision < 64; ++precision ) { REQUIRE_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), - (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ); + (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ) } // ---------------------- // asset operator-()const - REQUIRE_EQUAL( (-asset{ 0LL, s4}.amount), (asset{0LL, s4}.amount) ); - REQUIRE_EQUAL( (-asset{-0LL, s4}.amount), (asset{0LL, s4}.amount) ); - REQUIRE_EQUAL( (-asset{ 0LL, s5}.amount), (asset{0LL, s5}.amount) ); - REQUIRE_EQUAL( (-asset{-0LL, s5}.amount), (asset{0LL, s5}.amount) ); - - REQUIRE_EQUAL( (-asset{ 1LL, s4}.amount), (asset{-1LL, s4}.amount) ); - REQUIRE_EQUAL( (-asset{-1LL, s4}.amount), (asset{ 1LL, s4}.amount) ); - REQUIRE_EQUAL( (-asset{ 1LL, s5}.amount), (asset{-1LL, s5}.amount) ); - REQUIRE_EQUAL( (-asset{-1LL, s5}.amount), (asset{ 1LL, s5}.amount) ); - - REQUIRE_EQUAL( (-asset{asset_min, symbol{"SYMBOLL", 0}}.amount), - ( asset{asset_max, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{ 0LL, sym_no_prec}.amount), (asset{0LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (-asset{-0LL, sym_no_prec}.amount), (asset{0LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (-asset{ 0LL, sym_prec}.amount), (asset{0LL, sym_prec}.amount) ) + REQUIRE_EQUAL( (-asset{-0LL, sym_prec}.amount), (asset{0LL, sym_prec}.amount) ) - REQUIRE_EQUAL( (-asset{asset_max, symbol{"SYMBOLL", 0}}.amount), - ( asset{asset_min, symbol{"SYMBOLL", 0}}.amount) ); + REQUIRE_EQUAL( (-asset{ 1LL, sym_no_prec}.amount), (asset{-1LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (-asset{-1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (-asset{ 1LL, sym_prec}.amount), (asset{-1LL, sym_prec}.amount) ) + REQUIRE_EQUAL( (-asset{-1LL, sym_prec}.amount), (asset{ 1LL, sym_prec}.amount) ) - REQUIRE_EQUAL( (-asset{asset_min, symbol{"SYMBOLL", 63}}.amount), - ( asset{asset_max, symbol{"SYMBOLL", 63}}.amount) ); - - REQUIRE_EQUAL( (-asset{asset_max, symbol{"SYMBOLL", 63}}.amount), - ( asset{asset_min, symbol{"SYMBOLL", 63}}.amount) ); + REQUIRE_EQUAL( (-asset{asset_min, sym_no_prec}.amount), (asset{asset_max, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (-asset{asset_max, sym_no_prec}.amount), (asset{asset_min, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (-asset{asset_min, sym_prec}.amount), (asset{asset_max, sym_prec}.amount) ) + REQUIRE_EQUAL( (-asset{asset_max, sym_prec}.amount), (asset{asset_min, sym_prec}.amount) ) // ------------------------------------------------------------------------------------------ // inline friend asset& operator+(const asset&, const asset&)/asset& operator+=(const asset&) - REQUIRE_EQUAL( (asset{0LL, s4} += asset{0LL, s4} ), - (asset{0LL, s4}) ); - - REQUIRE_EQUAL( (asset{1LL, s4} += asset{-1LL, s4} ), - (asset{0LL, s4}) ); + REQUIRE_EQUAL( (asset{0LL, sym_no_prec} += asset{ 0LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{1LL, sym_no_prec} += asset{-1LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) - REQUIRE_ASSERT( "attempt to add asset with different symbol", ( [&]() { - asset{1LL, s4} += asset{1LL, symbol{"LLOBMYS", 0}}; + REQUIRE_ASSERT( "attempt to add asset with different symbol", ([&]() { + asset{1LL, sym_no_prec} += asset{1LL, symbol{"LLOBMYS", 0}}; }) - ); + ) - REQUIRE_ASSERT( "addition underflow", ( [&]() { - asset{asset_min, s4} += -asset{1LL, s4}; + REQUIRE_ASSERT( "addition underflow", ([&]() { + asset{asset_min, sym_no_prec} += -asset{1LL, sym_no_prec}; }) - ); + ) - REQUIRE_ASSERT( "addition overflow", ( [&]() { - asset{asset_max, s4} += asset{1LL, s4}; + REQUIRE_ASSERT( "addition overflow", ([&]() { + asset{asset_max, sym_no_prec} += asset{1LL, sym_no_prec}; }) - ); + ) // ------------------------------------------------------------------------------------------ // inline friend asset& operator-(const asset&, const asset&)/asset& operator-=(const asset&) - REQUIRE_EQUAL( (asset{0LL, s4} -= asset{0LL, s4} ), (asset{0LL, s4}) ); - REQUIRE_EQUAL( (asset{1LL, s4} -= asset{1LL, s4} ), (asset{0LL, s4}) ); + REQUIRE_EQUAL( (asset{0LL, sym_no_prec} -= asset{0LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{1LL, sym_no_prec} -= asset{1LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) - REQUIRE_ASSERT( "attempt to subtract asset with different symbol", ( [&]() { - asset{1LL, s4} -= asset{1LL, symbol{"LLOBMYS", 0}}; + REQUIRE_ASSERT( "attempt to subtract asset with different symbol", ([&]() { + asset{1LL, sym_no_prec} -= asset{1LL, symbol{"LLOBMYS", 0}}; }) - ); + ) - REQUIRE_ASSERT( "subtraction underflow", ( [&]() { - asset{asset_min, s4} -= asset{1LL, s4}; + REQUIRE_ASSERT( "subtraction underflow", ([&]() { + asset{asset_min, sym_no_prec} -= asset{1LL, sym_no_prec}; }) - ); + ) - REQUIRE_ASSERT( "subtraction overflow", ( [&]() { - asset{asset_max, s4} -= -asset{1LL, s4}; + REQUIRE_ASSERT( "subtraction overflow", ([&]() { + asset{asset_max, sym_no_prec} -= -asset{1LL, sym_no_prec}; }) - ); + ) // ----------------------------------------------------------------------- // friend asset operator*(const asset&, int64_t)/asset operator*=(int64_t) - REQUIRE_EQUAL( (asset{ 0LL, s4} *= 0 ), (asset{ 0LL, s4}) ); - REQUIRE_EQUAL( (asset{ 2LL, s4} *= 1LL ), (asset{ 2LL, s4}) ); - REQUIRE_EQUAL( (asset{ 2LL, s4} *= -1LL ), (asset{-2LL, s4}) ); + REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} *= 0LL ), (asset{ 0LL, sym_no_prec}) ); + REQUIRE_EQUAL( (asset{ 2LL, sym_no_prec} *= 1LL ), (asset{ 2LL, sym_no_prec}) ); + REQUIRE_EQUAL( (asset{ 2LL, sym_no_prec} *= -1LL ), (asset{-2LL, sym_no_prec}) ); - REQUIRE_ASSERT( "multiplication underflow", ( [&]() { - asset{asset_min, s4} *= 2LL; + REQUIRE_ASSERT( "multiplication underflow", ([&]() { + asset{asset_min, sym_no_prec} *= 2LL; }) - ); + ) - REQUIRE_ASSERT( "multiplication overflow", ( [&]() { - asset{ asset_max, s4} *= 2LL; + REQUIRE_ASSERT( "multiplication overflow", ([&]() { + asset{ asset_max, sym_no_prec} *= 2LL; }) - ); + ) // ----------------------------------------------------------------------------------------------------------------------------- // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) - REQUIRE_EQUAL( (asset{ 0LL, s4} / asset{ 1LL, s4}.amount ), (asset{ 0LL, s4}) ); - REQUIRE_EQUAL( (asset{ 1LL, s4} / asset{ 1LL, s4}.amount ), (asset{ 1LL, s4}) ); - REQUIRE_EQUAL( (asset{ 4LL, s4} / asset{ 2LL, s4}.amount ), (asset{ 2LL, s4}) ); - REQUIRE_EQUAL( (asset{-4LL, s4} / asset{ 2LL, s4}.amount ), (asset{-2LL, s4}) ); - REQUIRE_EQUAL( (asset{-4LL, s4} / asset{-2LL, s4}.amount ), (asset{ 2LL, s4}) ); - - REQUIRE_EQUAL( (asset{ 0LL, s4} /= asset{ 1LL, s4}.amount ), (asset{ 0LL, s4}) ); - REQUIRE_EQUAL( (asset{ 1LL, s4} /= asset{ 1LL, s4}.amount ), (asset{ 1LL, s4}) ); - REQUIRE_EQUAL( (asset{ 4LL, s4} /= asset{ 2LL, s4}.amount ), (asset{ 2LL, s4}) ); - REQUIRE_EQUAL( (asset{-4LL, s4} /= asset{ 2LL, s4}.amount ), (asset{-2LL, s4}) ); - REQUIRE_EQUAL( (asset{-4LL, s4} /= asset{-2LL, s4}.amount ), (asset{ 2LL, s4}) ); - - REQUIRE_EQUAL( (asset{ 0LL, s4} / asset{ 1LL, s4} ), (asset{ 0LL, s4}.amount) ); - REQUIRE_EQUAL( (asset{ 1LL, s4} / asset{ 1LL, s4} ), (asset{ 1LL, s4}.amount) ); - REQUIRE_EQUAL( (asset{ 4LL, s4} / asset{ 2LL, s4} ), (asset{ 2LL, s4}.amount) ); - REQUIRE_EQUAL( (asset{-4LL, s4} / asset{ 2LL, s4} ), (asset{-2LL, s4}.amount) ); - REQUIRE_EQUAL( (asset{-4LL, s4} / asset{-2LL, s4} ), (asset{ 2LL, s4}.amount) ); - - REQUIRE_ASSERT( "divide by zero", ( [&]() {asset{1LL, s4} /= 0;}) ); - - // Note that there should be an invariant established here when adding or setting the `amount` - REQUIRE_ASSERT( "signed division overflow", ( [&]() { + REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + + REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{ 1LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{ 4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) + REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} /= asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + + REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 0LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 1LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{-2LL, sym_no_prec}.amount) ) + REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) + + REQUIRE_ASSERT( "divide by zero", ([&]() {asset{1LL, sym_no_prec} /= 0;}) ) + + // Note that there is no invariant established here when adding or setting the `amount` + REQUIRE_ASSERT( "signed division overflow", ([&]() { asset a{}; a.amount = std::numeric_limits::min(); a /= -1LL; }) - ); + ) - REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( [&]() { + REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ([&]() { asset{{1LL}, s0} / asset{{1LL}, s1}; }) - ); + ) // -------------------------------------------------- // friend bool operator==(const asset&, const asset&) - REQUIRE_EQUAL( (asset{{}, s4} == asset{{}, s4} ), true ); - REQUIRE_EQUAL( (asset{asset_min, s4} == asset{asset_min, s4} ), true ); - REQUIRE_EQUAL( (asset{asset_max, s4} == asset{asset_max, s4} ), true ); + REQUIRE_EQUAL( (asset{{}, sym_no_prec} == asset{{}, sym_no_prec} ), true ) + REQUIRE_EQUAL( (asset{asset_min, sym_no_prec} == asset{asset_min, sym_no_prec} ), true ) + REQUIRE_EQUAL( (asset{asset_max, sym_no_prec} == asset{asset_max, sym_no_prec} ), true ) REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( []() { asset{{}, symbol{"SYMBOLL", 0}} == asset{{}, symbol{"LLOBMYS", 0}}; }) - ); + ) // --------------------------------------------------- // friend bool operator!=( const asset&, const asset&) - REQUIRE_EQUAL( (asset{asset_min, s4} != -asset{asset_min, s4} ), true ); - REQUIRE_EQUAL( (asset{asset_max, s4} != -asset{asset_max, s4} ), true ); + REQUIRE_EQUAL( (asset{asset_min, sym_no_prec} != -asset{asset_min, sym_no_prec} ), true ) + REQUIRE_EQUAL( (asset{asset_max, sym_no_prec} != -asset{asset_max, sym_no_prec} ), true ) // ------------------------------------------------- // friend bool operator<(const asset&, const asset&) - REQUIRE_EQUAL( (asset{{}, s4} < asset{1LL, s4} ), true ); + REQUIRE_EQUAL( (asset{{}, sym_no_prec} < asset{1LL, sym_no_prec} ), true ) // -------------------------------------------------- // friend bool operator<=(const asset&, const asset&) - REQUIRE_EQUAL( ( asset{{}, s4} <= asset{1LL, s4} ), true ); - REQUIRE_EQUAL( ( asset{1LL, s4} <= asset{1LL, s4} ), true ); + REQUIRE_EQUAL( ( asset{{}, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) + REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) // ------------------------------------------------- // friend bool operator>(const asset&, const asset&) - REQUIRE_EQUAL( ( asset{1LL, s4} > asset{{}, s4} ), true ); - REQUIRE_EQUAL( ( asset{{}, s4} > asset{1LL, s4} ), false ); + REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} > asset{{}, sym_no_prec} ), true ) + REQUIRE_EQUAL( ( asset{{}, sym_no_prec} > asset{1LL, sym_no_prec} ), false ) // -------------------------------------------------- // friend bool operator>=( const asset&, const asset&) - REQUIRE_EQUAL( ( asset{1LL, s4} >= asset{{}, s4} ), true ); - REQUIRE_EQUAL( ( asset{1LL, s4} >= asset{1LL, s4} ), true ); + REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} >= asset{{}, sym_no_prec} ), true ) + REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} >= asset{1LL, sym_no_prec} ), true ) silence_output(false); EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(extended_asset_type_test) - silence_output(false); + silence_output(true); - name n0{"1"}; - name n1{"5"}; - name n2{"a"}; - name n3{"z"}; - name n4{"111111111111j"}; - name n5{"555555555555j"}; - name n6{"aaaaaaaaaaaaj"}; - name n7{"zzzzzzzzzzzzj"}; - name n8{"eosioaccountj"}; + symbol sym_no_prec{"SYMBOLL", 0}; + symbol sym_prec{"SYMBOLL", 63}; - symbol s0{"A", 0}; - symbol s1{"Z", 0}; - symbol s2{"AAAAAAA", 0}; - symbol s3{"ZZZZZZZ", 0}; - symbol s4{"SYMBOLL", 0}; - symbol s5{"SYMBOLL", 63}; - - extended_symbol es0{s4, n8}; + extended_symbol ex_sym_no_prec{sym_no_prec, name{"eosioaccountj"}}; + extended_symbol ex_sym_prec{sym_prec, name{"eosioaccountj"}}; - asset a0{0LL, s0}; - asset a1{0LL, s0}; - asset a2{0LL, s1}; - asset a3{0LL, s1}; - asset a4{0LL, s2}; - asset a5{0LL, s2}; - asset a6{0LL, s3}; - asset a7{0LL, s3}; - asset a8{0LL, s4}; + asset asset_no_prec{0LL, sym_no_prec}; + asset asset_prec{0LL, sym_prec}; // ------------ // constructors /// extended_asset() - REQUIRE_EQUAL( extended_asset{}.quantity, asset{} ); - REQUIRE_EQUAL( extended_asset{}.contract, name{} ); + REQUIRE_EQUAL( extended_asset{}.quantity, asset{} ) + REQUIRE_EQUAL( extended_asset{}.contract, name{} ) - /// extended_asset(int64_t, extended_symbol) - REQUIRE_EQUAL( (extended_asset{{}, es0}.quantity), (asset{{}, s4}) ); - REQUIRE_EQUAL( (extended_asset{{}, es0}.contract), n8); + /// extended_asset(int64_t, extended_symbol) + REQUIRE_EQUAL( (extended_asset{{}, ex_sym_no_prec}.quantity), (asset{0LL, sym_no_prec}) ) + REQUIRE_EQUAL( (extended_asset{{}, ex_sym_no_prec}.contract), (name{"eosioaccountj"}) ) /// extended_asset(asset, name) - REQUIRE_EQUAL( (extended_asset{a8, n8}.quantity), (asset{{}, s4}) ); - REQUIRE_EQUAL( (extended_asset{a8, n8}.contract), (n8) ); - - // // ------------------------------------------ - // // extended_symbol get_extended_symbol()const - // REQUIRE_EQUAL( (extended_asset{{}, {}}.get_extended_symbol()), extended_symbol{} ); - - // REQUIRE_EQUAL( (extended_asset{asset{{}, s0}, n0}.get_extended_symbol()), - // (extended_symbol{symbol{"A", 0}, name{"1"}}) ); - // REQUIRE_EQUAL( (extended_asset{asset{{}, s0}, n1}.get_extended_symbol()), - // (extended_symbol{symbol{"A", 0}, name{"5"}}) ); - // REQUIRE_EQUAL( (extended_asset{asset{{}, s1}, n2}.get_extended_symbol()), - // (extended_symbol{symbol{"Z", 0}, name{"a"}}) ); - // REQUIRE_EQUAL( (extended_asset{asset{{}, s1}, n3}.get_extended_symbol()), - // (extended_symbol{symbol{"Z", 0}, name{"z"}}) ); - - // REQUIRE_EQUAL( (extended_asset{asset{{}, s2}, n4}.get_extended_symbol()), - // (extended_symbol{s2, n4}) ); - // REQUIRE_EQUAL( (extended_asset{asset{{}, s2}, n5}.get_extended_symbol()), - // (extended_symbol{s2, n5}) ); - // REQUIRE_EQUAL( (extended_asset{asset{{}, symbol{"ZZZZZZZ", 0}}, n6}.get_extended_symbol()), - // (extended_symbol{symbol{"ZZZZZZZ", 0}, n6}) ); - // REQUIRE_EQUAL( (extended_asset{asset{{}, symbol{"ZZZZZZZ", 0}}, n7}.get_extended_symbol()), - // (extended_symbol{symbol{"ZZZZZZZ", 0}, n7}) ); - - -// // Fix this printing functionality -// // Note that if there is no precision, there will be odd output: -// // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` -// // output: "0. A@1" -// // ----------------- -// // void print()const -// /////////////////////////////////////////////////////////////////////////////////////////////////// -// // How is this not the correct print result?? -// // print_f("0. A@1"); -// // print_f("\n");; -// // extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print(); -// // print_f("\n");; -// // REQUIRE_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ); -// // REQUIRE_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ); -// // REQUIRE_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ); -// // REQUIRE_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ); - -// // REQUIRE_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ); -// // REQUIRE_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ); -// // REQUIRE_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ); -// // REQUIRE_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ); -// /////////////////////////////////////////////////////////////////////////////////////////////////// - -// // ------------------------------- -// // extended_asset operator-()const -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), -// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 0}}, name{}}.quantity), -// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), -// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{-0}, symbol{"SYMBOLL", 63}}, name{}}.quantity), -// (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-1LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 0}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); -// REQUIRE_EQUAL( (-extended_asset{asset{int64_t{4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity), -// (extended_asset{asset{int64_t{-4611686018427387903LL}, symbol{"SYMBOLL", 63}}, name{}}.quantity) ); - - -// // ----------------------------------------------------------------------------- -// // friend extended_asset operator+(const extended_asset&, const extended_asset&) -// extended_asset asset0{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}; -// extended_asset asset1{asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}, name{}}; -// extended_asset asset2{asset{int64_t{-1LL}, symbol{"SYMBOLL", 0}}, name{}}; - -// extended_asset asset3{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"eosioaccountj"}}; -// extended_asset asset4{asset{int64_t{}, symbol{"SYMBOLL", 0}}, name{"jtnuoccaoisoe"}}; - -// REQUIRE_EQUAL( (asset0 + asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); -// REQUIRE_EQUAL( (asset1 + asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - -// REQUIRE_ASSERT( "type mismatch", ( -// [&]() {asset3 + asset4;}) ); - -// // ------------------------------------------------------------------------- -// // friend extended_asset& operator+=(extended_asset&, const extended_asset&) -// REQUIRE_EQUAL( (asset0 += asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); -// REQUIRE_EQUAL( (asset1 += asset2), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); - -// REQUIRE_ASSERT( "type mismatch", ( -// [&]() {asset3 += asset4;}) ); + REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.quantity), (asset{ 0LL, sym_no_prec}) ) + REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.contract), (name{"eosioaccountj"}) ) + + // ------------------------------------------ + // extended_symbol get_extended_symbol()const + REQUIRE_EQUAL( (extended_asset{{}, ex_sym_no_prec}.get_extended_symbol()), (ex_sym_no_prec) ) + REQUIRE_EQUAL( (extended_asset{{}, ex_sym_prec}.get_extended_symbol()), (ex_sym_prec) ) + + // Note: uncomment once the appended '\n' is removed from the `print` function + // Fix this printing functionality + // Note that if there is no precision, there will be odd output: + // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` + // output: "0. A@1" + + // REQUIRE_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) + // REQUIRE_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) + // REQUIRE_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) + // REQUIRE_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ) + + // REQUIRE_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + // REQUIRE_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + // REQUIRE_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + // REQUIRE_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + + // ------------------------------- + // extended_asset operator-()const + REQUIRE_EQUAL( (-extended_asset{asset{ 0, sym_no_prec}, {}}.quantity), (extended_asset{asset_no_prec, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{-0, sym_no_prec}, {}}.quantity), (extended_asset{asset_no_prec, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{ 0, sym_prec}, {}}.quantity), (extended_asset{asset_prec, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{-0, sym_prec}, {}}.quantity), (extended_asset{asset_prec, {}}.quantity) ) + + REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_no_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_no_prec}, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_no_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_no_prec}, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_prec}, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_prec}, {}}.quantity) ) + + REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_no_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_no_prec}, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_no_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_no_prec}, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_prec}, {}}.quantity) ) + REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_prec}, {}}.quantity) ) + + // ----------------------------------------------------------------------------- + // friend extended_asset operator+(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (extended_asset{asset{0LL, sym_no_prec}, {}} + extended_asset{asset{ 0LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) + REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} + extended_asset{asset{-1LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) + + REQUIRE_ASSERT( "type mismatch", ([&]() { + extended_asset{asset_no_prec, name{"eosioaccountj"}} + extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}; + }) + ) -// // -------------------------------------------------------------------------- -// // friend extended_asset& operator-=( extended_asset&, const extended_asset&) -// REQUIRE_EQUAL( (asset0 - asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); -// REQUIRE_EQUAL( (asset1 - asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); + // ------------------------------------------------------------------------- + // friend extended_asset& operator+=(extended_asset&, const extended_asset&) + extended_asset temp{asset_no_prec, {}}; + REQUIRE_EQUAL( (temp += temp), (extended_asset{asset_no_prec, {}}) ) + temp = extended_asset{asset{1LL, sym_no_prec}, {}}; + REQUIRE_EQUAL( (temp += extended_asset{asset{-1LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) -// REQUIRE_ASSERT( "type mismatch", ( -// [&]() {asset3 - asset4;}) ); + REQUIRE_ASSERT( "type mismatch", ([&]() { + temp += extended_asset{asset_no_prec, name{"eosioaccountj"}}; + }) + ) -// // ---------------------------------------------------------------------------- -// //friend extended_asset operator-(const extended_asset&, const extended_asset&) -// REQUIRE_EQUAL( (asset0 -= asset0), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); -// REQUIRE_EQUAL( (asset1 -= asset1), (extended_asset{asset{int64_t{0}, symbol{"SYMBOLL", 0}}, name{}}) ); + // ----------------------------------------------------------------------------- + // friend extended_asset operator-(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (extended_asset{asset_no_prec, {}} - extended_asset{asset_no_prec, {}}), + (extended_asset{asset_no_prec, {}}) ) + REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} - extended_asset{asset{1LL, sym_no_prec}, {}}), + (extended_asset{asset{asset_no_prec}, {}}) ) -// REQUIRE_ASSERT( "type mismatch", ( -// [&]() {asset3 -= asset4;}) ); + REQUIRE_ASSERT( "type mismatch", ([&]() { + extended_asset{asset_no_prec, name{"eosioaccountj"}} - extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}; + }) + ) -// // -------------------------------------------------------------------- -// // friend bool operator==(const extended_asset&, const extended_asset&) -// REQUIRE_EQUAL( (asset0 == asset0), true ); -// REQUIRE_EQUAL( (asset1 == asset1), true ); + // -------------------------------------------------------------------------- + // friend extended_asset& operator-=(extended_asset&, const extended_asset&) + temp = extended_asset{asset_no_prec, {}}; + REQUIRE_EQUAL( (temp -= temp), (extended_asset{asset_no_prec, {}}) ) + temp = extended_asset{asset{1LL, sym_no_prec}, {}}; + REQUIRE_EQUAL( (temp -= temp), (extended_asset{asset_no_prec, {}}) ) -// /////////////////////////////////////////////////////////////////////////////////////////////////// -// // Why is this not throwing?? -// // REQUIRE_ASSERT( "type mismatch", ( -// // [&]() {asset3 == asset4;}) ); -// /////////////////////////////////////////////////////////////////////////////////////////////////// + REQUIRE_ASSERT( "type mismatch", ([&]() { + temp -= extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}; + }) + ) -// // -------------------------------------------------------------------- -// // friend bool operator!=(const extended_asset&, const extended_asset&) -// // print_f("%", asset3 != asset4); -// // print_f("\n"); -// REQUIRE_EQUAL( asset3 != asset4, true ); -// // REQUIRE_EQUAL( (asset1 != asset2), false ); -// // REQUIRE_EQUAL( (asset2 != asset3), false ); + // -------------------------------------------------------------------- + // friend bool operator==(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (extended_asset{asset_no_prec, {}} == extended_asset{asset_no_prec, {}}), true ) + REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} == extended_asset{asset{1LL, sym_no_prec}, {}}), true ) -// // REQUIRE_ASSERT( "type mismatch", ( -// // [&]() {asset0 != asset1;}) ); + // // Why is this not throwing?? + // REQUIRE_ASSERT( "type mismatch", ([&]() {asset1 == asset2;}) ); -// // -------------------------------------------------------------------- -// // friend bool operator<( const extended_asset&, const extended_asset&) -// // ??? -// REQUIRE_EQUAL( asset0 < asset1, true ); + // -------------------------------------------------------------------- + // friend bool operator!=(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}} != extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}), true ) + REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} != extended_asset{asset{-1LL, sym_no_prec}, {}}), true ) + REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} != extended_asset{asset{ 0LL, sym_no_prec}, name{"eosioaccountj"}}), true ) -// // REQUIRE_ASSERT( "type mismatch", ( -// // []() {extended_asset{asset{}, name{}};}) ); + // ------------------------------------------------------------------- + // friend bool operator<(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{}} < extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ) + REQUIRE_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} < extended_asset{{}, name{"eosioaccountj"}};}) ) -// // -------------------------------------------------------------------- -// // friend bool operator<=(const extended_asset&, const extended_asset&) -// // REQUIRE_EQUAL( asset0 <= asset1, true ); + // -------------------------------------------------------------------- + // friend bool operator<=(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{}} <= extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ); + REQUIRE_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} <= extended_asset{{}, name{"eosioaccountj"}};}) ) -// // -------------------------------------------------------------------- -// // friend bool operator>=(const extended_asset&, const extended_asset&) -// // REQUIRE_EQUAL( asset1 > asset0, true ); + // -------------------------------------------------------------------- + // friend bool operator>=(const extended_asset&, const extended_asset&) + REQUIRE_EQUAL( (extended_asset{asset{ 1LL, sym_no_prec}, {}} >= extended_asset{asset_no_prec, name{}}), true ); + REQUIRE_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} >= extended_asset{{}, name{"eosioaccountj"}};}) ) silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { - // EOSIO_TEST(asset_type_test); + EOSIO_TEST(asset_type_test); EOSIO_TEST(extended_asset_type_test); return has_failed(); } diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 397922ed0f..7f1f7343c9 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -8,167 +8,167 @@ const uint64_t u64max = std::numeric_limits::max(); // 184467440737095 // Defined in `eosio.cdt/libraries/eosiolib/name.hpp` EOSIO_TEST_BEGIN(name_type_test) - silence_output(false); + silence_output(true); // ------------ // constructors /// constexpr name() - CHECK_EQUAL( name{}.value, 0ULL ) + REQUIRE_EQUAL( name{}.value, 0ULL ) /// constexpr explicit name(uint64_t) - CHECK_EQUAL( name{0ULL}.value, 0ULL ) - CHECK_EQUAL( name{1ULL}.value, 1ULL ) - CHECK_EQUAL( name{u64max}.value, u64max ) + REQUIRE_EQUAL( name{0ULL}.value, 0ULL ) + REQUIRE_EQUAL( name{1ULL}.value, 1ULL ) + REQUIRE_EQUAL( name{u64max}.value, u64max ) /// constexpr explicit name(name::raw) - CHECK_EQUAL( name{name::raw{0ULL}}.value, 0ULL ) - CHECK_EQUAL( name{name::raw{1ULL}}.value, 1ULL ) - CHECK_EQUAL( name{name::raw{u64max}}.value, u64max ) + REQUIRE_EQUAL( name{name::raw{0ULL}}.value, 0ULL ) + REQUIRE_EQUAL( name{name::raw{1ULL}}.value, 1ULL ) + REQUIRE_EQUAL( name{name::raw{u64max}}.value, u64max ) // Note that these are the exact `uint64_t` value representations of the given string /// constexpr explicit name(std::string_view) - CHECK_EQUAL( name{"1"}.value, 576460752303423488ULL ) - CHECK_EQUAL( name{"5"}.value, 2882303761517117440ULL ) - CHECK_EQUAL( name{"a"}.value, 3458764513820540928ULL ) - CHECK_EQUAL( name{"z"}.value, 17870283321406128128ULL ) - - CHECK_EQUAL( name{"abc"}.value, 3589368903014285312ULL ) - CHECK_EQUAL( name{"123"}.value, 614178399182651392ULL ) - - CHECK_EQUAL( name{".abc"}.value, 112167778219196416ULL ) - CHECK_EQUAL( name{".........abc"}.value, 102016ULL ) - CHECK_EQUAL( name{"123."}.value, 614178399182651392ULL ) - CHECK_EQUAL( name{"123........."}.value, 614178399182651392ULL ) - CHECK_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ) + REQUIRE_EQUAL( name{"1"}.value, 576460752303423488ULL ) + REQUIRE_EQUAL( name{"5"}.value, 2882303761517117440ULL ) + REQUIRE_EQUAL( name{"a"}.value, 3458764513820540928ULL ) + REQUIRE_EQUAL( name{"z"}.value, 17870283321406128128ULL ) + + REQUIRE_EQUAL( name{"abc"}.value, 3589368903014285312ULL ) + REQUIRE_EQUAL( name{"123"}.value, 614178399182651392ULL ) + + REQUIRE_EQUAL( name{".abc"}.value, 112167778219196416ULL ) + REQUIRE_EQUAL( name{".........abc"}.value, 102016ULL ) + REQUIRE_EQUAL( name{"123."}.value, 614178399182651392ULL ) + REQUIRE_EQUAL( name{"123........."}.value, 614178399182651392ULL ) + REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ) - CHECK_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ) - CHECK_EQUAL( name{"123.abc"}.value, 614181822271586304ULL ) + REQUIRE_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ) + REQUIRE_EQUAL( name{"123.abc"}.value, 614181822271586304ULL ) - CHECK_EQUAL( name{"12345abcdefgj"}.value, 614251623682315983ULL ) - CHECK_EQUAL( name{"hijklmnopqrsj"}.value, 7754926748989239183ULL ) - CHECK_EQUAL( name{"tuvwxyz.1234j"}.value, 14895601873741973071ULL ) + REQUIRE_EQUAL( name{"12345abcdefgj"}.value, 614251623682315983ULL ) + REQUIRE_EQUAL( name{"hijklmnopqrsj"}.value, 7754926748989239183ULL ) + REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.value, 14895601873741973071ULL ) - CHECK_EQUAL( name{"111111111111j"}.value, 595056260442243615ULL ) - CHECK_EQUAL( name{"555555555555j"}.value, 2975281302211218015ULL ) - CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.value, 3570337562653461615ULL ) - CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.value, u64max ) + REQUIRE_EQUAL( name{"111111111111j"}.value, 595056260442243615ULL ) + REQUIRE_EQUAL( name{"555555555555j"}.value, 2975281302211218015ULL ) + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.value, 3570337562653461615ULL ) + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.value, u64max ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ) - CHECK_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ) - CHECK_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ) - CHECK_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ) + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ) + REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ) + REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ) // -------------------------------------------- // static constexpr uint8_t char_to_value(char) char c{'.'}; uint8_t expected_value{}; // Will increment to the expected correct value in the set [0,32) - CHECK_EQUAL( name::char_to_value(c), expected_value ) + REQUIRE_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; for(c = '1'; c <= '5'; ++c ) { - CHECK_EQUAL( name::char_to_value(c), expected_value ) + REQUIRE_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; } for(c = 'a'; c <= 'z'; ++c ) { - CHECK_EQUAL( name::char_to_value(c), expected_value ) + REQUIRE_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; } - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ) - CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ) + REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); // ------------------------------- // constexpr uint8_t length()cosnt - CHECK_EQUAL( name{""}.length(), 0 ) - CHECK_EQUAL( name{"e"}.length(), 1 ) - CHECK_EQUAL( name{"eo"}.length(), 2 ) - CHECK_EQUAL( name{"eos"}.length(), 3 ) - CHECK_EQUAL( name{"eosi"}.length(), 4 ) - CHECK_EQUAL( name{"eosio"}.length(), 5 ) - CHECK_EQUAL( name{"eosioa"}.length(), 6 ) - CHECK_EQUAL( name{"eosioac"}.length(), 7 ) - CHECK_EQUAL( name{"eosioacc"}.length(), 8 ) - CHECK_EQUAL( name{"eosioacco"}.length(), 9 ) - CHECK_EQUAL( name{"eosioaccou"}.length(), 10 ) - CHECK_EQUAL( name{"eosioaccoun"}.length(), 11 ) - CHECK_EQUAL( name{"eosioaccount"}.length(), 12 ) - CHECK_EQUAL( name{"eosioaccountj"}.length(), 13 ) - - CHECK_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ) + REQUIRE_EQUAL( name{""}.length(), 0 ) + REQUIRE_EQUAL( name{"e"}.length(), 1 ) + REQUIRE_EQUAL( name{"eo"}.length(), 2 ) + REQUIRE_EQUAL( name{"eos"}.length(), 3 ) + REQUIRE_EQUAL( name{"eosi"}.length(), 4 ) + REQUIRE_EQUAL( name{"eosio"}.length(), 5 ) + REQUIRE_EQUAL( name{"eosioa"}.length(), 6 ) + REQUIRE_EQUAL( name{"eosioac"}.length(), 7 ) + REQUIRE_EQUAL( name{"eosioacc"}.length(), 8 ) + REQUIRE_EQUAL( name{"eosioacco"}.length(), 9 ) + REQUIRE_EQUAL( name{"eosioaccou"}.length(), 10 ) + REQUIRE_EQUAL( name{"eosioaccoun"}.length(), 11 ) + REQUIRE_EQUAL( name{"eosioaccount"}.length(), 12 ) + REQUIRE_EQUAL( name{"eosioaccountj"}.length(), 13 ) + + REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ) // ---------------------------- // constexpr name suffix()const - CHECK_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ) - CHECK_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ) - CHECK_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ) - CHECK_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ) - CHECK_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ) - CHECK_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ) - CHECK_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ) - CHECK_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ) - CHECK_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ) - CHECK_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ) - CHECK_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ) - CHECK_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ) + REQUIRE_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ) + REQUIRE_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ) + REQUIRE_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ) + REQUIRE_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ) + REQUIRE_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ) + REQUIRE_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ) + REQUIRE_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ) + REQUIRE_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ) + REQUIRE_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ) + REQUIRE_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ) + REQUIRE_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ) + REQUIRE_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ) // Note that this case is ignored because '.' characters at the end of a name are ignored // Make functions perfect mirrors of eachother (01/07/2019) - // print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() ); - // print_f("Value of expected suffix: \n" ); - // eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" ); + // `print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() );` + // `print_f("Value of expected suffix: \n" );` + // `eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" );` - CHECK_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ) - CHECK_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ) + REQUIRE_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ) + REQUIRE_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ) // ----------------------------- // constexpr operator raw()const - CHECK_EQUAL( name{"1"}.operator name::raw(), static_cast(576460752303423488ULL) ) - CHECK_EQUAL( name{"5"}.operator name::raw(), static_cast(2882303761517117440ULL) ) - CHECK_EQUAL( name{"a"}.operator name::raw(), static_cast(3458764513820540928ULL) ) - CHECK_EQUAL( name{"z"}.operator name::raw(), static_cast(17870283321406128128ULL) ) - - CHECK_EQUAL( name{"abc"}.operator name::raw(), static_cast(3589368903014285312ULL) ) - CHECK_EQUAL( name{"123"}.operator name::raw(), static_cast(614178399182651392ULL) ) - - CHECK_EQUAL( name{".abc"}.operator name::raw(), static_cast(112167778219196416ULL) ) - CHECK_EQUAL( name{".........abc"}.operator name::raw(), static_cast(102016ULL) ) - CHECK_EQUAL( name{"123."}.operator name::raw(), static_cast(614178399182651392ULL) ) - CHECK_EQUAL( name{"123........."}.operator name::raw(), static_cast(614178399182651392ULL) ) - CHECK_EQUAL( name{".a.b.c.1.2.3."}.operator name::raw(), static_cast(108209673814966320ULL) ) - - CHECK_EQUAL( name{"abc.123"}.operator name::raw(), static_cast(3589369488740450304ULL) ) - CHECK_EQUAL( name{"123.abc"}.operator name::raw(), static_cast(614181822271586304ULL) ) - - CHECK_EQUAL( name{"12345abcdefgj"}.operator name::raw(), static_cast(614251623682315983ULL) ) - CHECK_EQUAL( name{"hijklmnopqrsj"}.operator name::raw(), static_cast(7754926748989239183ULL) ) - CHECK_EQUAL( name{"tuvwxyz.1234j"}.operator name::raw(), static_cast(14895601873741973071ULL) ) - - CHECK_EQUAL( name{"111111111111j"}.operator name::raw(), static_cast(595056260442243615ULL) ) - CHECK_EQUAL( name{"555555555555j"}.operator name::raw(), static_cast(2975281302211218015ULL) ) - CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.operator name::raw(), static_cast(3570337562653461615ULL) ) - CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.operator name::raw(), static_cast(u64max) ) + REQUIRE_EQUAL( name{"1"}.operator name::raw(), static_cast(576460752303423488ULL) ) + REQUIRE_EQUAL( name{"5"}.operator name::raw(), static_cast(2882303761517117440ULL) ) + REQUIRE_EQUAL( name{"a"}.operator name::raw(), static_cast(3458764513820540928ULL) ) + REQUIRE_EQUAL( name{"z"}.operator name::raw(), static_cast(17870283321406128128ULL) ) + + REQUIRE_EQUAL( name{"abc"}.operator name::raw(), static_cast(3589368903014285312ULL) ) + REQUIRE_EQUAL( name{"123"}.operator name::raw(), static_cast(614178399182651392ULL) ) + + REQUIRE_EQUAL( name{".abc"}.operator name::raw(), static_cast(112167778219196416ULL) ) + REQUIRE_EQUAL( name{".........abc"}.operator name::raw(), static_cast(102016ULL) ) + REQUIRE_EQUAL( name{"123."}.operator name::raw(), static_cast(614178399182651392ULL) ) + REQUIRE_EQUAL( name{"123........."}.operator name::raw(), static_cast(614178399182651392ULL) ) + REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.operator name::raw(), static_cast(108209673814966320ULL) ) + + REQUIRE_EQUAL( name{"abc.123"}.operator name::raw(), static_cast(3589369488740450304ULL) ) + REQUIRE_EQUAL( name{"123.abc"}.operator name::raw(), static_cast(614181822271586304ULL) ) + + REQUIRE_EQUAL( name{"12345abcdefgj"}.operator name::raw(), static_cast(614251623682315983ULL) ) + REQUIRE_EQUAL( name{"hijklmnopqrsj"}.operator name::raw(), static_cast(7754926748989239183ULL) ) + REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.operator name::raw(), static_cast(14895601873741973071ULL) ) + + REQUIRE_EQUAL( name{"111111111111j"}.operator name::raw(), static_cast(595056260442243615ULL) ) + REQUIRE_EQUAL( name{"555555555555j"}.operator name::raw(), static_cast(2975281302211218015ULL) ) + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.operator name::raw(), static_cast(3570337562653461615ULL) ) + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.operator name::raw(), static_cast(u64max) ) // --------------------------------------- // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` - CHECK_EQUAL( name{0}.operator bool(), false ) - CHECK_EQUAL( name{1}.operator bool(), true ) - CHECK_EQUAL( !name{0}, true ) - CHECK_EQUAL( !name{1}, false ) + REQUIRE_EQUAL( name{0}.operator bool(), false ) + REQUIRE_EQUAL( name{1}.operator bool(), true ) + REQUIRE_EQUAL( !name{0}.operator bool(), true ) + REQUIRE_EQUAL( !name{1}.operator bool(), false ) - CHECK_EQUAL( name{""}.operator bool(), false ) - CHECK_EQUAL( name{"1"}.operator bool(), true ) - CHECK_EQUAL( !name{""}, true ) - CHECK_EQUAL( !name{"1"}, false ) + REQUIRE_EQUAL( name{""}.operator bool(), false ) + REQUIRE_EQUAL( name{"1"}.operator bool(), true ) + REQUIRE_EQUAL( !name{""}.operator bool(), true ) + REQUIRE_EQUAL( !name{"1"}.operator bool(), false ) // ---------------------------------------- // char* write_as_string(char*, char*)const @@ -176,193 +176,193 @@ EOSIO_TEST_BEGIN(name_type_test) std::string test_str{"1"}; name{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // Note that any '.' characters at the end of a name are ignored name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) + REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ) name{test_str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) + REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ) name{test_str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ) + REQUIRE_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ) name{test_str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // ---------------------------- // std::string to_string()const - CHECK_EQUAL( name{"1"}.to_string(), "1" ) - CHECK_EQUAL( name{"5"}.to_string(), "5" ) - CHECK_EQUAL( name{"a"}.to_string(), "a" ) - CHECK_EQUAL( name{"z"}.to_string(), "z" ) - - CHECK_EQUAL( name{"abc"}.to_string(), "abc" ) - CHECK_EQUAL( name{"123"}.to_string(), "123" ) - - CHECK_EQUAL( name{".abc"}.to_string(), ".abc" ) - CHECK_EQUAL( name{".........abc"}.to_string(), ".........abc" ) - CHECK_EQUAL( name{"123."}.to_string(), "123" ) - CHECK_EQUAL( name{"123........."}.to_string(), "123" ) - CHECK_EQUAL( name{".a.b.c.1.2.3."}.to_string(), ".a.b.c.1.2.3" ) + REQUIRE_EQUAL( name{"1"}.to_string(), "1" ) + REQUIRE_EQUAL( name{"5"}.to_string(), "5" ) + REQUIRE_EQUAL( name{"a"}.to_string(), "a" ) + REQUIRE_EQUAL( name{"z"}.to_string(), "z" ) + + REQUIRE_EQUAL( name{"abc"}.to_string(), "abc" ) + REQUIRE_EQUAL( name{"123"}.to_string(), "123" ) + + REQUIRE_EQUAL( name{".abc"}.to_string(), ".abc" ) + REQUIRE_EQUAL( name{".........abc"}.to_string(), ".........abc" ) + REQUIRE_EQUAL( name{"123."}.to_string(), "123" ) + REQUIRE_EQUAL( name{"123........."}.to_string(), "123" ) + REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.to_string(), ".a.b.c.1.2.3" ) - CHECK_EQUAL( name{"abc.123"}.to_string(), "abc.123" ) - CHECK_EQUAL( name{"123.abc"}.to_string(), "123.abc" ) + REQUIRE_EQUAL( name{"abc.123"}.to_string(), "abc.123" ) + REQUIRE_EQUAL( name{"123.abc"}.to_string(), "123.abc" ) - CHECK_EQUAL( name{"12345abcdefgj"}.to_string(), "12345abcdefgj" ) - CHECK_EQUAL( name{"hijklmnopqrsj"}.to_string(), "hijklmnopqrsj" ) - CHECK_EQUAL( name{"tuvwxyz.1234j"}.to_string(), "tuvwxyz.1234j" ) + REQUIRE_EQUAL( name{"12345abcdefgj"}.to_string(), "12345abcdefgj" ) + REQUIRE_EQUAL( name{"hijklmnopqrsj"}.to_string(), "hijklmnopqrsj" ) + REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.to_string(), "tuvwxyz.1234j" ) - CHECK_EQUAL( name{"111111111111j"}.to_string(), "111111111111j" ) - CHECK_EQUAL( name{"555555555555j"}.to_string(), "555555555555j" ) - CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ) - CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ) + REQUIRE_EQUAL( name{"111111111111j"}.to_string(), "111111111111j" ) + REQUIRE_EQUAL( name{"555555555555j"}.to_string(), "555555555555j" ) + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ) + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ) // ---------------------------------------------------------- // friend constexpr bool operator==(const name&, const name&) - CHECK_EQUAL( name{"1"} == name{"1"}, true ) - CHECK_EQUAL( name{"5"} == name{"5"}, true ) - CHECK_EQUAL( name{"a"} == name{"a"}, true ) - CHECK_EQUAL( name{"z"} == name{"z"}, true ) - - CHECK_EQUAL( name{"abc"} == name{"abc"}, true ) - CHECK_EQUAL( name{"123"} == name{"123"}, true ) - - CHECK_EQUAL( name{".abc"} == name{".abc"}, true ) - CHECK_EQUAL( name{".........abc"} == name{".........abc"}, true ) - CHECK_EQUAL( name{"123."} == name{"123"}, true ) - CHECK_EQUAL( name{"123........."} == name{"123"}, true ) - CHECK_EQUAL( name{".a.b.c.1.2.3."} == name{".a.b.c.1.2.3"}, true ) + REQUIRE_EQUAL( name{"1"} == name{"1"}, true ) + REQUIRE_EQUAL( name{"5"} == name{"5"}, true ) + REQUIRE_EQUAL( name{"a"} == name{"a"}, true ) + REQUIRE_EQUAL( name{"z"} == name{"z"}, true ) + + REQUIRE_EQUAL( name{"abc"} == name{"abc"}, true ) + REQUIRE_EQUAL( name{"123"} == name{"123"}, true ) + + REQUIRE_EQUAL( name{".abc"} == name{".abc"}, true ) + REQUIRE_EQUAL( name{".........abc"} == name{".........abc"}, true ) + REQUIRE_EQUAL( name{"123."} == name{"123"}, true ) + REQUIRE_EQUAL( name{"123........."} == name{"123"}, true ) + REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == name{".a.b.c.1.2.3"}, true ) - CHECK_EQUAL( name{"abc.123"} == name{"abc.123"}, true ) - CHECK_EQUAL( name{"123.abc"} == name{"123.abc"}, true ) + REQUIRE_EQUAL( name{"abc.123"} == name{"abc.123"}, true ) + REQUIRE_EQUAL( name{"123.abc"} == name{"123.abc"}, true ) - CHECK_EQUAL( name{"12345abcdefgj"} == name{"12345abcdefgj"}, true ) - CHECK_EQUAL( name{"hijklmnopqrsj"} == name{"hijklmnopqrsj"}, true ) - CHECK_EQUAL( name{"tuvwxyz.1234j"} == name{"tuvwxyz.1234j"}, true ) + REQUIRE_EQUAL( name{"12345abcdefgj"} == name{"12345abcdefgj"}, true ) + REQUIRE_EQUAL( name{"hijklmnopqrsj"} == name{"hijklmnopqrsj"}, true ) + REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == name{"tuvwxyz.1234j"}, true ) - CHECK_EQUAL( name{"111111111111j"} == name{"111111111111j"}, true ) - CHECK_EQUAL( name{"555555555555j"} == name{"555555555555j"}, true ) - CHECK_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ) - CHECK_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ) + REQUIRE_EQUAL( name{"111111111111j"} == name{"111111111111j"}, true ) + REQUIRE_EQUAL( name{"555555555555j"} == name{"555555555555j"}, true ) + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ) + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ) // ----------------------------------------------------------- // friend constexpr bool operator!=(const name&, const name&) - CHECK_EQUAL( name{"1"} != name{}, true ) - CHECK_EQUAL( name{"5"} != name{}, true ) - CHECK_EQUAL( name{"a"} != name{}, true ) - CHECK_EQUAL( name{"z"} != name{}, true ) - - CHECK_EQUAL( name{"abc"} != name{}, true ) - CHECK_EQUAL( name{"123"} != name{}, true ) - - CHECK_EQUAL( name{".abc"} != name{}, true ) - CHECK_EQUAL( name{".........abc"} != name{}, true ) - CHECK_EQUAL( name{"123."} != name{}, true ) - CHECK_EQUAL( name{"123........."} != name{}, true ) - CHECK_EQUAL( name{".a.b.c.1.2.3."} != name{}, true ) + REQUIRE_EQUAL( name{"1"} != name{}, true ) + REQUIRE_EQUAL( name{"5"} != name{}, true ) + REQUIRE_EQUAL( name{"a"} != name{}, true ) + REQUIRE_EQUAL( name{"z"} != name{}, true ) + + REQUIRE_EQUAL( name{"abc"} != name{}, true ) + REQUIRE_EQUAL( name{"123"} != name{}, true ) + + REQUIRE_EQUAL( name{".abc"} != name{}, true ) + REQUIRE_EQUAL( name{".........abc"} != name{}, true ) + REQUIRE_EQUAL( name{"123."} != name{}, true ) + REQUIRE_EQUAL( name{"123........."} != name{}, true ) + REQUIRE_EQUAL( name{".a.b.c.1.2.3."} != name{}, true ) - CHECK_EQUAL( name{"abc.123"} != name{}, true ) - CHECK_EQUAL( name{"123.abc"} != name{}, true ) + REQUIRE_EQUAL( name{"abc.123"} != name{}, true ) + REQUIRE_EQUAL( name{"123.abc"} != name{}, true ) - CHECK_EQUAL( name{"12345abcdefgj"} != name{}, true ) - CHECK_EQUAL( name{"hijklmnopqrsj"} != name{}, true ) - CHECK_EQUAL( name{"tuvwxyz.1234j"} != name{}, true ) + REQUIRE_EQUAL( name{"12345abcdefgj"} != name{}, true ) + REQUIRE_EQUAL( name{"hijklmnopqrsj"} != name{}, true ) + REQUIRE_EQUAL( name{"tuvwxyz.1234j"} != name{}, true ) - CHECK_EQUAL( name{"111111111111j"} != name{}, true ) - CHECK_EQUAL( name{"555555555555j"} != name{}, true ) - CHECK_EQUAL( name{"aaaaaaaaaaaaj"} != name{}, true ) - CHECK_EQUAL( name{"zzzzzzzzzzzzj"} != name{}, true ) + REQUIRE_EQUAL( name{"111111111111j"} != name{}, true ) + REQUIRE_EQUAL( name{"555555555555j"} != name{}, true ) + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} != name{}, true ) + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} != name{}, true ) // --------------------------------------------------------- // friend constexpr bool operator<(const name&, const name&) - CHECK_EQUAL( name{} < name{"1"}, true ) - CHECK_EQUAL( name{} < name{"5"}, true ) - CHECK_EQUAL( name{} < name{"a"}, true ) - CHECK_EQUAL( name{} < name{"z"}, true ) - - CHECK_EQUAL( name{} < name{"abc"}, true ) - CHECK_EQUAL( name{} < name{"123"}, true ) - - CHECK_EQUAL( name{} < name{".abc"}, true ) - CHECK_EQUAL( name{} < name{".........abc"}, true ) - CHECK_EQUAL( name{} < name{"123."}, true ) - CHECK_EQUAL( name{} < name{"123........."}, true ) - CHECK_EQUAL( name{} < name{".a.b.c.1.2.3."}, true ) + REQUIRE_EQUAL( name{} < name{"1"}, true ) + REQUIRE_EQUAL( name{} < name{"5"}, true ) + REQUIRE_EQUAL( name{} < name{"a"}, true ) + REQUIRE_EQUAL( name{} < name{"z"}, true ) + + REQUIRE_EQUAL( name{} < name{"abc"}, true ) + REQUIRE_EQUAL( name{} < name{"123"}, true ) + + REQUIRE_EQUAL( name{} < name{".abc"}, true ) + REQUIRE_EQUAL( name{} < name{".........abc"}, true ) + REQUIRE_EQUAL( name{} < name{"123."}, true ) + REQUIRE_EQUAL( name{} < name{"123........."}, true ) + REQUIRE_EQUAL( name{} < name{".a.b.c.1.2.3."}, true ) - CHECK_EQUAL( name{} < name{"abc.123"}, true ) - CHECK_EQUAL( name{} < name{"123.abc"}, true ) + REQUIRE_EQUAL( name{} < name{"abc.123"}, true ) + REQUIRE_EQUAL( name{} < name{"123.abc"}, true ) - CHECK_EQUAL( name{} < name{"12345abcdefgj"}, true ) - CHECK_EQUAL( name{} < name{"hijklmnopqrsj"}, true ) - CHECK_EQUAL( name{} < name{"tuvwxyz.1234j"}, true ) + REQUIRE_EQUAL( name{} < name{"12345abcdefgj"}, true ) + REQUIRE_EQUAL( name{} < name{"hijklmnopqrsj"}, true ) + REQUIRE_EQUAL( name{} < name{"tuvwxyz.1234j"}, true ) - CHECK_EQUAL( name{} < name{"111111111111j"}, true ) - CHECK_EQUAL( name{} < name{"555555555555j"}, true ) - CHECK_EQUAL( name{} < name{"aaaaaaaaaaaaj"}, true ) - CHECK_EQUAL( name{} < name{"zzzzzzzzzzzzj"}, true ) + REQUIRE_EQUAL( name{} < name{"111111111111j"}, true ) + REQUIRE_EQUAL( name{} < name{"555555555555j"}, true ) + REQUIRE_EQUAL( name{} < name{"aaaaaaaaaaaaj"}, true ) + REQUIRE_EQUAL( name{} < name{"zzzzzzzzzzzzj"}, true ) // ------------------------------------ // inline constexpr name operator""_n() - CHECK_EQUAL( name{} == ""_n, true ) + REQUIRE_EQUAL( name{} == ""_n, true ) - CHECK_EQUAL( name{"1"} == "1"_n, true ) - CHECK_EQUAL( name{"5"} == "5"_n, true ) - CHECK_EQUAL( name{"a"} == "a"_n, true ) - CHECK_EQUAL( name{"z"} == "z"_n, true ) - - CHECK_EQUAL( name{"abc"} == "abc"_n, true ) - CHECK_EQUAL( name{"123"} == "123"_n, true ) - - CHECK_EQUAL( name{".abc"} == ".abc"_n, true ) - CHECK_EQUAL( name{".........abc"} == ".........abc"_n, true ) - CHECK_EQUAL( name{"123."} == "123."_n, true ) - CHECK_EQUAL( name{"123........."} == "123........."_n, true ) - CHECK_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ) + REQUIRE_EQUAL( name{"1"} == "1"_n, true ) + REQUIRE_EQUAL( name{"5"} == "5"_n, true ) + REQUIRE_EQUAL( name{"a"} == "a"_n, true ) + REQUIRE_EQUAL( name{"z"} == "z"_n, true ) + + REQUIRE_EQUAL( name{"abc"} == "abc"_n, true ) + REQUIRE_EQUAL( name{"123"} == "123"_n, true ) + + REQUIRE_EQUAL( name{".abc"} == ".abc"_n, true ) + REQUIRE_EQUAL( name{".........abc"} == ".........abc"_n, true ) + REQUIRE_EQUAL( name{"123."} == "123."_n, true ) + REQUIRE_EQUAL( name{"123........."} == "123........."_n, true ) + REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ) - CHECK_EQUAL( name{"abc.123"} == "abc.123"_n, true ) - CHECK_EQUAL( name{"123.abc"} == "123.abc"_n, true ) + REQUIRE_EQUAL( name{"abc.123"} == "abc.123"_n, true ) + REQUIRE_EQUAL( name{"123.abc"} == "123.abc"_n, true ) - CHECK_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ) - CHECK_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ) - CHECK_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ) + REQUIRE_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ) + REQUIRE_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ) + REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ) - CHECK_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ) - CHECK_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ) - CHECK_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ) - CHECK_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ) + REQUIRE_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ) + REQUIRE_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ) + REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ) + REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/print_tests.cpp b/tests/unit/print_tests.cpp index b69faf27ec..34ff0f5c42 100644 --- a/tests/unit/print_tests.cpp +++ b/tests/unit/print_tests.cpp @@ -1,30 +1,125 @@ #include #include +#include +#include +#include + using namespace eosio::native; +// EOSIO_TEST_BEGIN(print_test) +// silence_output(true); +// CHECK_PRINT("27", [](){ eosio::print((uint8_t)27); }); +// CHECK_PRINT("34", [](){ eosio::print((int)34); }); +// CHECK_PRINT([](std::string s){return s[0] == 'a';}, [](){ eosio::print((char)'a'); }); +// CHECK_PRINT([](std::string s){return s[0] == 'b';}, [](){ eosio::print((int8_t)'b'); }); +// CHECK_PRINT("202", [](){ eosio::print((unsigned int)202); }); +// CHECK_PRINT("-202", [](){ eosio::print((int)-202); }); +// CHECK_PRINT("707", [](){ eosio::print((unsigned long)707); }); +// CHECK_PRINT("-707", [](){ eosio::print((long)-707); }); +// CHECK_PRINT("909", [](){ eosio::print((unsigned long long)909); }); +// CHECK_PRINT("-909", [](){ eosio::print((long long)-909); }); +// CHECK_PRINT("404", [](){ eosio::print((uint32_t)404); }); +// CHECK_PRINT("-404", [](){ eosio::print((int32_t)-404); }); +// CHECK_PRINT("404000000", [](){ eosio::print((uint64_t)404000000); }); +// CHECK_PRINT("-404000000", [](){ eosio::print((int64_t)-404000000); }); +// CHECK_PRINT("0x0066000000000000", [](){ eosio::print((uint128_t)102); }); +// CHECK_PRINT("0xffffff9affffffffffffffffffffffff", [](){ eosio::print((int128_t)-102); }); +// silence_output(false); +// EOSIO_TEST_END + EOSIO_TEST_BEGIN(print_test) - silence_output(true); - CHECK_PRINT("27", [](){ eosio::print((uint8_t)27); }); - CHECK_PRINT("34", [](){ eosio::print((int)34); }); - CHECK_PRINT([](std::string s){return s[0] == 'a';}, [](){ eosio::print((char)'a'); }); - CHECK_PRINT([](std::string s){return s[0] == 'b';}, [](){ eosio::print((int8_t)'b'); }); - CHECK_PRINT("202", [](){ eosio::print((unsigned int)202); }); - CHECK_PRINT("-202", [](){ eosio::print((int)-202); }); - CHECK_PRINT("707", [](){ eosio::print((unsigned long)707); }); - CHECK_PRINT("-707", [](){ eosio::print((long)-707); }); - CHECK_PRINT("909", [](){ eosio::print((unsigned long long)909); }); - CHECK_PRINT("-909", [](){ eosio::print((long long)-909); }); - CHECK_PRINT("404", [](){ eosio::print((uint32_t)404); }); - CHECK_PRINT("-404", [](){ eosio::print((int32_t)-404); }); - CHECK_PRINT("404000000", [](){ eosio::print((uint64_t)404000000); }); - CHECK_PRINT("-404000000", [](){ eosio::print((int64_t)-404000000); }); - CHECK_PRINT("0x0066000000000000", [](){ eosio::print((uint128_t)102); }); - CHECK_PRINT("0xffffff9affffffffffffffffffffffff", [](){ eosio::print((int128_t)-102); }); + silence_output(false); + + // ------------------------------ + // inline void print(const char*) + std::string s{}; + char c{}; + CHECK_PRINT( "", [&](){eosio::print(s.c_str());} ); + + // Not understanding how this test isn't passing + // for(unsigned short i{}; i < 256; ++i) { + // c = i; + // s = static_cast(i); + // CHECK_PRINT( [&](std::string temp){return temp == std::string{c};}, [&](){eosio::print(s.c_str());} ); + + + // eosio::print(c); + // eosio::print("\n"); + // eosio::print(s); + // eosio::print("\n"); + // } + + // Not understanding how this test isn't passing + // Using 70 as an arbitrary string length + // for(unsigned short i{}; i < 70; ++i) { + // s = std::string(i, 'i'); + // CHECK_PRINT( [&](std::string temp){return temp == std::string(i, 'i');}, [&](){eosio::print(s.c_str());} ); + // } + + // ---------------------------------------------------------------------------------- + // template >::value && + // std::is_signed>::value, int> = 0> + // inline void print(T) + + // ---------------------------------------------------------------------------------- + // template >::value && + // std::is_signed>::value, int> = 0> + // inline void print(T) + + // ------------------------ + // inline void print(float) + float f{}; + for(float i{}; i < 1; f += 0.01) { + CHECK_PRINT( [&](std::string){return std::to_string{f};}, [&](){eosio::print(f);} ); + } + + // s = ; + // CHECK_PRINT( "", [&](){eosio::print(s.c_str());} ); + + // ------------------------- + // inline void print(double) + + // ------------------------------ + // inline void print(long double) + + // --------------------- + // template + // inline void print(const fixed_bytes&) + + // --------------------- + // template + // inline void print( fixed_bytes&) + + // ----------------------- + // inline void print(name) + + // ------------------------------------- + // inline void print(eosio::symbol_code) + + // ------------------------------------------------------------------------------------------ + // template>::value, int> = 0> + // inline void print(T&&) + + // -------------------------------- + // inline void print_f(const char*) + + // ----------------------------------------- + // template + // inline void print_f( const char*, Arg, Args...) + + // ---------------------------------------- + // template + // void print( Arg&&, Args&&...) + + // -------------------- + // template + // inline iostream& operator<<(iostream&, const T&) + silence_output(false); EOSIO_TEST_END -int main(int argc, char** argv) { +int main(int argc, char* argv[]) { EOSIO_TEST(print_test); return has_failed(); } diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 706c078960..e2dee3893e 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -17,58 +17,57 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) // constructors/constexpr uint64_t raw()const /// constexpr symbol_code() - CHECK_EQUAL( symbol_code{}.raw(), 0ULL ) + REQUIRE_EQUAL( symbol_code{}.raw(), 0ULL ) /// constexpr explicit symbol_code(uint64_t raw) - CHECK_EQUAL( symbol_code{0ULL}.raw(), 0ULL ) - CHECK_EQUAL( symbol_code{1ULL}.raw(), 1ULL ) - CHECK_EQUAL( symbol_code{u64max}.raw(), u64max ) + REQUIRE_EQUAL( symbol_code{0ULL}.raw(), 0ULL ) + REQUIRE_EQUAL( symbol_code{1ULL}.raw(), 1ULL ) + REQUIRE_EQUAL( symbol_code{u64max}.raw(), u64max ) /// constexpr explicit symbol_code(std::string_view str) - CHECK_EQUAL( symbol_code{"A"}.raw(), 65ULL ) - CHECK_EQUAL( symbol_code{"Z"}.raw(), 90ULL ) - CHECK_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ) - CHECK_EQUAL( symbol_code{"ZZZZZZZ"}.raw(), 25432092013386330ULL ) + REQUIRE_EQUAL( symbol_code{"A"}.raw(), 65ULL ) + REQUIRE_EQUAL( symbol_code{"Z"}.raw(), 90ULL ) + REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ) + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.raw(), 25432092013386330ULL ) - CHECK_ASSERT( "string is too long to be a valid symbol_code", ([]() {symbol_code{"ABCDEFGH"};}) ) - CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"a"};}) ) - CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"z"};}) ) - CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ) - CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ) + REQUIRE_ASSERT( "string is too long to be a valid symbol_code", ([]() {symbol_code{"ABCDEFGH"};}) ) + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"a"};}) ) + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"z"};}) ) + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ) + REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ) // ------------------------------ // constexpr bool is_valid()const - CHECK_EQUAL( symbol_code{65ULL}.is_valid(), true ) // "A" - CHECK_EQUAL( symbol_code{90ULL}.is_valid(), true ) // "Z" - CHECK_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ) // "AAAAAAA" - CHECK_EQUAL( symbol_code{25432092013386330ULL}.is_valid(), true ) // "ZZZZZZZ" + REQUIRE_EQUAL( symbol_code{65ULL}.is_valid(), true ) // "A" + REQUIRE_EQUAL( symbol_code{90ULL}.is_valid(), true ) // "Z" + REQUIRE_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ) // "AAAAAAA" + REQUIRE_EQUAL( symbol_code{25432092013386330ULL}.is_valid(), true ) // "ZZZZZZZ" - CHECK_EQUAL( symbol_code{64ULL}.is_valid(), false ) - CHECK_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ) + REQUIRE_EQUAL( symbol_code{64ULL}.is_valid(), false ) + REQUIRE_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ) // -------------------------------- // constexpr uint32_t length()const - CHECK_EQUAL( symbol_code{""}.length(), 0 ) - CHECK_EQUAL( symbol_code{"S"}.length(), 1 ) - CHECK_EQUAL( symbol_code{"SY"}.length(), 2 ) - CHECK_EQUAL( symbol_code{"SYM"}.length(), 3 ) - CHECK_EQUAL( symbol_code{"SYMB"}.length(), 4 ) - CHECK_EQUAL( symbol_code{"SYMBO"}.length(), 5 ) - CHECK_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ) - CHECK_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ) + REQUIRE_EQUAL( symbol_code{""}.length(), 0 ) + REQUIRE_EQUAL( symbol_code{"S"}.length(), 1 ) + REQUIRE_EQUAL( symbol_code{"SY"}.length(), 2 ) + REQUIRE_EQUAL( symbol_code{"SYM"}.length(), 3 ) + REQUIRE_EQUAL( symbol_code{"SYMB"}.length(), 4 ) + REQUIRE_EQUAL( symbol_code{"SYMBO"}.length(), 5 ) + REQUIRE_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ) + REQUIRE_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ) // --------------------------------------- // constexpr explicit operator bool()const - // Note that I must be explicit about calling the operator because it is defined as `explicit` - CHECK_EQUAL( symbol_code{0ULL}.operator bool(), false ) - CHECK_EQUAL( symbol_code{1ULL}.operator bool(), true ) - CHECK_EQUAL( !symbol_code{0ULL}, true ) - CHECK_EQUAL( !symbol_code{1ULL}, false ) + REQUIRE_EQUAL( symbol_code{0ULL}.operator bool(), false ) + REQUIRE_EQUAL( symbol_code{1ULL}.operator bool(), true ) + REQUIRE_EQUAL( !symbol_code{0ULL}.operator bool(), true ) + REQUIRE_EQUAL( !symbol_code{1ULL}.operator bool(), false ) - CHECK_EQUAL( symbol_code{""}.operator bool(), false ) - CHECK_EQUAL( symbol_code{"SYMBOL"}.operator bool(), true ) - CHECK_EQUAL( !symbol_code{""}, true ) - CHECK_EQUAL( !symbol_code{"SYMBOL"}, false ) + REQUIRE_EQUAL( symbol_code{""}.operator bool(), false ) + REQUIRE_EQUAL( symbol_code{"SYMBOL"}.operator bool(), true ) + REQUIRE_EQUAL( !symbol_code{""}.operator bool(), true ) + REQUIRE_EQUAL( !symbol_code{"SYMBOL"}.operator bool(), false ) // ---------------------------------------- // char* write_as_string(char*, char*)const @@ -76,41 +75,41 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) std::string test_str{"A"}; symbol_code{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "Z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "AAAAAAA"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "ZZZZZZZ"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // ---------------------------- // std::string to_string()const - CHECK_EQUAL( symbol_code{"A"}.to_string(), "A" ) - CHECK_EQUAL( symbol_code{"Z"}.to_string(), "Z" ) - CHECK_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ) - CHECK_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ) + REQUIRE_EQUAL( symbol_code{"A"}.to_string(), "A" ) + REQUIRE_EQUAL( symbol_code{"Z"}.to_string(), "Z" ) + REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ) + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ) // -------------------------------------------------------------- // friend bool operator==(const symbol_code&, const symbol_code&) - CHECK_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ) - CHECK_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ) - CHECK_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ) - CHECK_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ) + REQUIRE_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ) + REQUIRE_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ) + REQUIRE_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ) + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ) // -------------------------------------------------------------- // friend bool operator!=(const symbol_code&, const symbol_code&) - CHECK_EQUAL( symbol_code{"A"} != symbol_code{}, true ) - CHECK_EQUAL( symbol_code{"Z"} != symbol_code{}, true ) - CHECK_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{}, true ) - CHECK_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{}, true ) + REQUIRE_EQUAL( symbol_code{"A"} != symbol_code{}, true ) + REQUIRE_EQUAL( symbol_code{"Z"} != symbol_code{}, true ) + REQUIRE_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{}, true ) + REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{}, true ) // ------------------------------------------------------------- // friend bool operator<(const symbol_code&, const symbol_code&) - CHECK_EQUAL( symbol_code{} < symbol_code{"A"}, true ) - CHECK_EQUAL( symbol_code{} < symbol_code{"Z"}, true ) - CHECK_EQUAL( symbol_code{} < symbol_code{"AAAAAAA"}, true ) - CHECK_EQUAL( symbol_code{} < symbol_code{"ZZZZZZZ"}, true ) + REQUIRE_EQUAL( symbol_code{} < symbol_code{"A"}, true ) + REQUIRE_EQUAL( symbol_code{} < symbol_code{"Z"}, true ) + REQUIRE_EQUAL( symbol_code{} < symbol_code{"AAAAAAA"}, true ) + REQUIRE_EQUAL( symbol_code{} < symbol_code{"ZZZZZZZ"}, true ) silence_output(false); EOSIO_TEST_END @@ -128,102 +127,102 @@ EOSIO_TEST_BEGIN(symbol_type_test) // constructors/constexpr uint64_t raw()const /// constexpr symbol() - CHECK_EQUAL( symbol{}.raw(), 0ULL ) + REQUIRE_EQUAL( symbol{}.raw(), 0ULL ) /// constexpr explicit symbol(uint64_t) - CHECK_EQUAL( symbol{0ULL}.raw(), 0ULL ) - CHECK_EQUAL( symbol{1ULL}.raw(), 1ULL ) - CHECK_EQUAL( symbol{u64max}.raw(), u64max ) + REQUIRE_EQUAL( symbol{0ULL}.raw(), 0ULL ) + REQUIRE_EQUAL( symbol{1ULL}.raw(), 1ULL ) + REQUIRE_EQUAL( symbol{u64max}.raw(), u64max ) /// constexpr symbol(std::string_view, uint8_t) // Note that unless constructed with `initializer_list`, precision does not check for wrap-around - CHECK_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) - CHECK_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) - CHECK_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) - CHECK_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) + REQUIRE_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) + REQUIRE_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) + REQUIRE_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) + REQUIRE_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) /// constexpr symbol(symbol_code, uint8_t) - CHECK_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) - CHECK_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) - CHECK_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) - CHECK_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) + REQUIRE_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) + REQUIRE_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) + REQUIRE_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) + REQUIRE_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) // -------------------- // bool is_valid()const - CHECK_EQUAL( symbol{16640ULL}.is_valid(), true ) // "A", precision: 0 - CHECK_EQUAL( symbol{23040ULL}.is_valid(), true ) // "Z", precision: 0 - CHECK_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ) // "AAAAAAA", precision: 0 - CHECK_EQUAL( symbol{6510615555426900480ULL}.is_valid(), true ) // "ZZZZZZZ", precision: 0 + REQUIRE_EQUAL( symbol{16640ULL}.is_valid(), true ) // "A", precision: 0 + REQUIRE_EQUAL( symbol{23040ULL}.is_valid(), true ) // "Z", precision: 0 + REQUIRE_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ) // "AAAAAAA", precision: 0 + REQUIRE_EQUAL( symbol{6510615555426900480ULL}.is_valid(), true ) // "ZZZZZZZ", precision: 0 - CHECK_EQUAL( symbol{16639ULL}.is_valid(), false ) - CHECK_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ) + REQUIRE_EQUAL( symbol{16639ULL}.is_valid(), false ) + REQUIRE_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ) // ------------------------- // uint8_t precision()const - CHECK_EQUAL( (symbol{sc0, 0}.precision()), 0 ) - CHECK_EQUAL( (symbol{sc1, 0}.precision()), 0 ) - CHECK_EQUAL( (symbol{sc2, 0}.precision()), 0 ) - CHECK_EQUAL( (symbol{sc3, 0}.precision()), 0 ) + REQUIRE_EQUAL( (symbol{sc0, 0}.precision()), 0 ) + REQUIRE_EQUAL( (symbol{sc1, 0}.precision()), 0 ) + REQUIRE_EQUAL( (symbol{sc2, 0}.precision()), 0 ) + REQUIRE_EQUAL( (symbol{sc3, 0}.precision()), 0 ) - CHECK_EQUAL( (symbol{sc0, 255}.precision()), 255 ) - CHECK_EQUAL( (symbol{sc1, 255}.precision()), 255 ) - CHECK_EQUAL( (symbol{sc2, 255}.precision()), 255 ) - CHECK_EQUAL( (symbol{sc3, 255}.precision()), 255 ) + REQUIRE_EQUAL( (symbol{sc0, 255}.precision()), 255 ) + REQUIRE_EQUAL( (symbol{sc1, 255}.precision()), 255 ) + REQUIRE_EQUAL( (symbol{sc2, 255}.precision()), 255 ) + REQUIRE_EQUAL( (symbol{sc3, 255}.precision()), 255 ) // ----------------------- // symbol_code code()const - CHECK_EQUAL( (symbol{sc0, 0}.code()), sc0 ) - CHECK_EQUAL( (symbol{sc1, 0}.code()), sc1 ) - CHECK_EQUAL( (symbol{sc2, 0}.code()), sc2 ) - CHECK_EQUAL( (symbol{sc3, 0}.code()), sc3 ) + REQUIRE_EQUAL( (symbol{sc0, 0}.code()), sc0 ) + REQUIRE_EQUAL( (symbol{sc1, 0}.code()), sc1 ) + REQUIRE_EQUAL( (symbol{sc2, 0}.code()), sc2 ) + REQUIRE_EQUAL( (symbol{sc3, 0}.code()), sc3 ) // --------------------------------------- // constexpr explicit operator bool()const - // Note that I must be explicit about calling the operator because it is defined as `explicit` - CHECK_EQUAL( symbol{0}.operator bool(), false ) - CHECK_EQUAL( symbol{1}.operator bool(), true ) - CHECK_EQUAL( !symbol{0}, true ) - CHECK_EQUAL( !symbol{1}, false ) + REQUIRE_EQUAL( symbol{0}.operator bool(), false ) + REQUIRE_EQUAL( symbol{1}.operator bool(), true ) + REQUIRE_EQUAL( !symbol{0}.operator bool(), true ) + REQUIRE_EQUAL( !symbol{1}.operator bool(), false ) - CHECK_EQUAL( (symbol{"", 0}.operator bool()), false ) - CHECK_EQUAL( (symbol{"SYMBOLL", 0}.operator bool()), true ) - CHECK_EQUAL( (!symbol{"", 0}), true ) - CHECK_EQUAL( (!symbol{"SYMBOLL", 0}), false ) + REQUIRE_EQUAL( (symbol{"", 0}.operator bool()), false ) + REQUIRE_EQUAL( (symbol{"SYMBOLL", 0}.operator bool()), true ) + REQUIRE_EQUAL( (!symbol{"", 0}.operator bool()), true ) + REQUIRE_EQUAL( (!symbol{"SYMBOLL", 0}.operator bool()), false ) + // Note: uncomment once the appended '\n' is removed from the `print` function // --------------------- // void print(bool)const // symbol{"A", 0}.print(true); // symbol{"AAAAAAA", 255}.print(true); - // CHECK_PRINT( "0,A", [&](){symbol{"A", 0}.print(true);} ); - // CHECK_PRINT( "0,Z", [&](){symbol{"Z", 0}.print(true);} ); - // CHECK_PRINT( "255,AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(true);} ); - // CHECK_PRINT( "255,ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(true);} ); + // REQUIRE_PRINT( "0,A", [&](){symbol{"A", 0}.print(true);} ); + // REQUIRE_PRINT( "0,Z", [&](){symbol{"Z", 0}.print(true);} ); + // REQUIRE_PRINT( "255,AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(true);} ); + // REQUIRE_PRINT( "255,ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(true);} ); - // CHECK_PRINT( ",A", [&](){symbol{"A", 0}.print(false);} ); - // CHECK_PRINT( ",Z", [&](){symbol{"Z", 0}.print(false);} ); - // CHECK_PRINT( ",AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(false);} ); - // CHECK_PRINT( ",ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(false);} ); + // REQUIRE_PRINT( ",A", [&](){symbol{"A", 0}.print(false);} ); + // REQUIRE_PRINT( ",Z", [&](){symbol{"Z", 0}.print(false);} ); + // REQUIRE_PRINT( ",AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(false);} ); + // REQUIRE_PRINT( ",ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(false);} ); // -------------------------------------------------------------- // friend constexpr bool operator==(const symbol&, const symbol&) - CHECK_EQUAL( (symbol{sc0, 0} == symbol{sc0, 0}), true ) - CHECK_EQUAL( (symbol{sc1, 0} == symbol{sc1, 0}), true ) - CHECK_EQUAL( (symbol{sc2, 0} == symbol{sc2, 0}), true ) - CHECK_EQUAL( (symbol{sc3, 0} == symbol{sc3, 0}), true ) + REQUIRE_EQUAL( (symbol{sc0, 0} == symbol{sc0, 0}), true ) + REQUIRE_EQUAL( (symbol{sc1, 0} == symbol{sc1, 0}), true ) + REQUIRE_EQUAL( (symbol{sc2, 0} == symbol{sc2, 0}), true ) + REQUIRE_EQUAL( (symbol{sc3, 0} == symbol{sc3, 0}), true ) // -------------------------------------------------------------- // friend constexpr bool operator!=(const symbol&, const symbol&) - CHECK_EQUAL( (symbol{sc0, 0} != symbol{}), true ) - CHECK_EQUAL( (symbol{sc1, 0} != symbol{}), true ) - CHECK_EQUAL( (symbol{sc2, 0} != symbol{}), true ) - CHECK_EQUAL( (symbol{sc3, 0} != symbol{}), true ) + REQUIRE_EQUAL( (symbol{sc0, 0} != symbol{}), true ) + REQUIRE_EQUAL( (symbol{sc1, 0} != symbol{}), true ) + REQUIRE_EQUAL( (symbol{sc2, 0} != symbol{}), true ) + REQUIRE_EQUAL( (symbol{sc3, 0} != symbol{}), true ) // -------------------------------------------------------------- - // friebnd constexpr bool operator<(const symbol&, const symbol&) - CHECK_EQUAL( (symbol{} < symbol{sc0, 0}), true ) - CHECK_EQUAL( (symbol{} < symbol{sc1, 0}), true ) - CHECK_EQUAL( (symbol{} < symbol{sc2, 0}), true ) - CHECK_EQUAL( (symbol{} < symbol{sc3, 0}), true ) + // friend constexpr bool operator<(const symbol&, const symbol&) + REQUIRE_EQUAL( (symbol{} < symbol{sc0, 0}), true ) + REQUIRE_EQUAL( (symbol{} < symbol{sc1, 0}), true ) + REQUIRE_EQUAL( (symbol{} < symbol{sc2, 0}), true ) + REQUIRE_EQUAL( (symbol{} < symbol{sc3, 0}), true ) silence_output(false); EOSIO_TEST_END @@ -250,62 +249,62 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) // constructors/get_symbol/get_contract /// constexpr extended_symbol() - CHECK_EQUAL( (extended_symbol{{}, {}}.get_symbol().raw()), 0ULL ) - CHECK_EQUAL( (extended_symbol{{}, {}}.get_contract().value), 0ULL ) + REQUIRE_EQUAL( (extended_symbol{{}, {}}.get_symbol().raw()), 0ULL ) + REQUIRE_EQUAL( (extended_symbol{{}, {}}.get_contract().value), 0ULL ) /// constexpr extended_symbol(symbol, name)/constexpr symbol get_symbol()const/constexpr name get_contract()const - CHECK_EQUAL( (extended_symbol{s0, n0}.get_symbol().raw()), 16640ULL ) - CHECK_EQUAL( (extended_symbol{s0, n1}.get_symbol().code().raw()), 65ULL ) - CHECK_EQUAL( (extended_symbol{s1, n2}.get_symbol().raw()), 23040ULL ) - CHECK_EQUAL( (extended_symbol{s1, n3}.get_symbol().code().raw()), 90ULL ) - CHECK_EQUAL( (extended_symbol{s0, n0}.get_contract().value), 576460752303423488ULL ) - CHECK_EQUAL( (extended_symbol{s0, n1}.get_contract().value), 2882303761517117440ULL ) - CHECK_EQUAL( (extended_symbol{s1, n2}.get_contract().value), 3458764513820540928ULL ) - CHECK_EQUAL( (extended_symbol{s1, n3}.get_contract().value), 17870283321406128128ULL ) - CHECK_EQUAL( (extended_symbol{s2, n4}.get_symbol().raw()), 4702111234474983680ULL ) - CHECK_EQUAL( (extended_symbol{s2, n5}.get_symbol().code().raw()), 18367622009667905ULL ) - CHECK_EQUAL( (extended_symbol{s3, n6}.get_symbol().raw()), 6510615555426900480ULL ) - CHECK_EQUAL( (extended_symbol{s3, n7}.get_symbol().code().raw()), 25432092013386330ULL ) - CHECK_EQUAL( (extended_symbol{s2, n4}.get_contract().value), 595056260442243615ULL ) - CHECK_EQUAL( (extended_symbol{s2, n5}.get_contract().value), 2975281302211218015ULL ) - CHECK_EQUAL( (extended_symbol{s3, n6}.get_contract().value), 3570337562653461615ULL ) - CHECK_EQUAL( (extended_symbol{s3, n7}.get_contract().value), u64max ) - - // Must fix the behavior of `print` in order for these tests to work + REQUIRE_EQUAL( (extended_symbol{s0, n0}.get_symbol().raw()), 16640ULL ) + REQUIRE_EQUAL( (extended_symbol{s0, n1}.get_symbol().code().raw()), 65ULL ) + REQUIRE_EQUAL( (extended_symbol{s1, n2}.get_symbol().raw()), 23040ULL ) + REQUIRE_EQUAL( (extended_symbol{s1, n3}.get_symbol().code().raw()), 90ULL ) + REQUIRE_EQUAL( (extended_symbol{s0, n0}.get_contract().value), 576460752303423488ULL ) + REQUIRE_EQUAL( (extended_symbol{s0, n1}.get_contract().value), 2882303761517117440ULL ) + REQUIRE_EQUAL( (extended_symbol{s1, n2}.get_contract().value), 3458764513820540928ULL ) + REQUIRE_EQUAL( (extended_symbol{s1, n3}.get_contract().value), 17870283321406128128ULL ) + REQUIRE_EQUAL( (extended_symbol{s2, n4}.get_symbol().raw()), 4702111234474983680ULL ) + REQUIRE_EQUAL( (extended_symbol{s2, n5}.get_symbol().code().raw()), 18367622009667905ULL ) + REQUIRE_EQUAL( (extended_symbol{s3, n6}.get_symbol().raw()), 6510615555426900480ULL ) + REQUIRE_EQUAL( (extended_symbol{s3, n7}.get_symbol().code().raw()), 25432092013386330ULL ) + REQUIRE_EQUAL( (extended_symbol{s2, n4}.get_contract().value), 595056260442243615ULL ) + REQUIRE_EQUAL( (extended_symbol{s2, n5}.get_contract().value), 2975281302211218015ULL ) + REQUIRE_EQUAL( (extended_symbol{s3, n6}.get_contract().value), 3570337562653461615ULL ) + REQUIRE_EQUAL( (extended_symbol{s3, n7}.get_contract().value), u64max ) + + // Note: uncomment once the appended '\n' is removed from the `print` function // --------------------- // void print(bool)const // extended_symbol{s0, n0}.print(true); // extended_symbol{s0, n0}.print(true); - // CHECK_PRINT( "0@576460752303423488", [](){extended_symbol{s0, n0}.print(true);} ); - // CHECK_PRINT( "0@2882303761517117440", [](){extended_symbol{s1, n1}.print(true);} ); - // CHECK_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); - // CHECK_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); + // REQUIRE_PRINT( "0@576460752303423488", [](){extended_symbol{s0, n0}.print(true);} ); + // REQUIRE_PRINT( "0@2882303761517117440", [](){extended_symbol{s1, n1}.print(true);} ); + // REQUIRE_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); + // REQUIRE_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); - // CHECK_PRINT( "@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); - // CHECK_PRINT( "@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); - // CHECK_PRINT( "@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); - // CHECK_PRINT( "@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); + // REQUIRE_PRINT( "@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); + // REQUIRE_PRINT( "@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); + // REQUIRE_PRINT( "@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); + // REQUIRE_PRINT( "@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); // ------------------------------------------------------------------------------- // friend constexpr bool operator==(const extended_symbol&, const extended_symbol&) - CHECK_EQUAL( (extended_symbol{s0, n0} == extended_symbol{s0, n0}), true ) - CHECK_EQUAL( (extended_symbol{s1, n3} == extended_symbol{s1, n3}), true ) - CHECK_EQUAL( (extended_symbol{s2, n4} == extended_symbol{s2, n4}), true ) - CHECK_EQUAL( (extended_symbol{s3, n7} == extended_symbol{s3, n7}), true ) + REQUIRE_EQUAL( (extended_symbol{s0, n0} == extended_symbol{s0, n0}), true ) + REQUIRE_EQUAL( (extended_symbol{s1, n3} == extended_symbol{s1, n3}), true ) + REQUIRE_EQUAL( (extended_symbol{s2, n4} == extended_symbol{s2, n4}), true ) + REQUIRE_EQUAL( (extended_symbol{s3, n7} == extended_symbol{s3, n7}), true ) // ------------------------------------------------------------------------------- // friend constexpr bool operator!=(const extended_symbol&, const extended_symbol&) - CHECK_EQUAL( (extended_symbol{} != extended_symbol{s0, {}}), true ) - CHECK_EQUAL( (extended_symbol{s0, {}} != extended_symbol{s1, {}}), true ) - CHECK_EQUAL( (extended_symbol{s1, {}} != extended_symbol{s2, {}}), true ) - CHECK_EQUAL( (extended_symbol{s2, {}} != extended_symbol{s3, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{} != extended_symbol{s0, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{s0, {}} != extended_symbol{s1, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{s1, {}} != extended_symbol{s2, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{s2, {}} != extended_symbol{s3, {}}), true ) // ------------------------------------------------------------------------------- // friend constexpr bool operator<(const extended_symbol&, const extended_symbol&) - CHECK_EQUAL( (extended_symbol{} < extended_symbol{s0, {}}), true ) - CHECK_EQUAL( (extended_symbol{} < extended_symbol{s1, {}}), true ) - CHECK_EQUAL( (extended_symbol{} < extended_symbol{s2, {}}), true ) - CHECK_EQUAL( (extended_symbol{} < extended_symbol{s3, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s0, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s1, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s2, {}}), true ) + REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s3, {}}), true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index 333489517a..8bc4c8b703 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -10,18 +10,18 @@ using eosio::block_timestamp; using namespace eosio::native; -const uint32_t u32MIN = std::numeric_limits::min(); // 0 -const uint32_t u32MAX = std::numeric_limits::max(); // 18446744073709551615 +const uint32_t u32min = std::numeric_limits::min(); // 0 +const uint32_t u32max = std::numeric_limits::max(); // 18446744073709551615 -const int64_t i64MIN = std::numeric_limits::min(); // -9223372036854775808 -const int64_t i64MAX = std::numeric_limits::max(); // 9223372036854775807 +const int64_t i64min = std::numeric_limits::min(); // -9223372036854775808 +const int64_t i64max = std::numeric_limits::max(); // 9223372036854775807 // This is interesting. `print_f` won't print the '-' character. // `printf` won't print anything. -// `printf("%\n",i64MIN);` +// `printf("%\n",i64min);` // Note that there are no invariants established to protect this type against overflow. -// For example: if you define a microsecond type from i64MAX and then check that value in +// For example: if you define a microsecond type from i64max and then check that value in // Or if you check the `seconds` of a (u64MAX / 1000000 + 1), it will overflow. // Potentially could be abused in a contract? @@ -35,8 +35,8 @@ EOSIO_TEST_BEGIN(microseconds_type_test) // ----------------------------------------------- /// explicit microseconds(uint64_t)/int64_t count() REQUIRE_EQUAL( microseconds{}.count(), 0ULL ) - REQUIRE_EQUAL( microseconds{i64MAX}.count(), i64MAX ) - REQUIRE_EQUAL( microseconds{i64MIN}.count(), i64MIN ) + REQUIRE_EQUAL( microseconds{i64max}.count(), i64max ) + REQUIRE_EQUAL( microseconds{i64min}.count(), i64min ) // ----------------------------- // static microseconds maximum() @@ -45,32 +45,32 @@ EOSIO_TEST_BEGIN(microseconds_type_test) // ------------------------------------------------------------------------ // friend microseconds operator+(const microseconds&, const microseconds&) // friend microseconds operator-(const microseconds&, const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} + microseconds{1LL}), microseconds{1LL} ) + REQUIRE_EQUAL( (microseconds{0LL} + microseconds{1LL}), microseconds{ 1LL} ) REQUIRE_EQUAL( (microseconds{0LL} - microseconds{1LL}), microseconds{-1LL} ) // ------------------------------------- // bool operator==(const microseconds&) // bool operator!=(const microseconds&) - // bool operator<(const microseconds&) + // bool operator< (const microseconds&) // bool operator<=(const microseconds&) - // bool operator>(const microseconds&) + // bool operator> (const microseconds&) // bool operator>=(const microseconds&) REQUIRE_EQUAL( (microseconds{1LL} == microseconds{1LL}), true ) REQUIRE_EQUAL( (microseconds{1LL} != microseconds{0LL}), true ) - REQUIRE_EQUAL( (microseconds{0LL} < microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{0LL} < microseconds{1LL}), true ) REQUIRE_EQUAL( (microseconds{1LL} <= microseconds{1LL}), true ) - REQUIRE_EQUAL( (microseconds{1LL} > microseconds{0LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} > microseconds{0LL}), true ) REQUIRE_EQUAL( (microseconds{1LL} >= microseconds{1LL}), true ) // ---------------------------------------------- // microseconds& operator+=(const microseconds&) // microseconds& operator-=(const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} += microseconds{1LL}), microseconds{1LL} ) + REQUIRE_EQUAL( (microseconds{0LL} += microseconds{1LL}), microseconds{ 1LL} ) REQUIRE_EQUAL( (microseconds{0LL} -= microseconds{1LL}), microseconds{-1LL} ) // -------------------- // int64_t to_seconds() - REQUIRE_EQUAL( (microseconds{i64MAX}.to_seconds()), i64MAX / 1000000 ) + REQUIRE_EQUAL( (microseconds{i64max}.to_seconds()), i64max / 1000000 ) // ------------------------------------ // inline microseconds seconds(int64_t) @@ -88,15 +88,15 @@ EOSIO_TEST_BEGIN(microseconds_type_test) EOSIO_TEST_END EOSIO_TEST_BEGIN(time_point_type_test) - silence_output(true); + silence_output(false); // ------------ // constructors - microseconds ms0{0LL}; - microseconds ms1{1LL}; + microseconds ms0{ 0LL}; + microseconds ms1{ 1LL}; microseconds ms2{-1LL}; - microseconds ms_max{i64MAX}; - microseconds ms_min{i64MIN}; + microseconds ms_max{i64max}; + microseconds ms_min{i64min}; /// explicit time_point(microseconds)/microseconds& time_since_epoch() REQUIRE_EQUAL( time_point{ms0}.time_since_epoch(), ms0 ) @@ -106,21 +106,21 @@ EOSIO_TEST_BEGIN(time_point_type_test) // uint32_t sec_since_epoch() REQUIRE_EQUAL( time_point{ms0}.sec_since_epoch(), 0 / 1000000 ) REQUIRE_EQUAL( time_point{ms1}.sec_since_epoch(), 1 / 1000000 ) - REQUIRE_EQUAL( time_point{ms_max}.sec_since_epoch(), i64MAX / 1000000 ) - REQUIRE_EQUAL( time_point{ms_min}.sec_since_epoch(), i64MIN / 1000000 ) + REQUIRE_EQUAL( time_point{ms_max}.sec_since_epoch(), i64max / 1000000 ) + REQUIRE_EQUAL( time_point{ms_min}.sec_since_epoch(), i64min / 1000000 ) // ----------------------------------- - // bool operator==(const time_point&) - // bool operator!=(const time_point&) + // bool operator==(const time_point&) + // bool operator!=(const time_point&) // bool operator<(const time_point&) - // bool operator<=(const time_point&) + // bool operator<=(const time_point&) // bool operator>(const time_point&) - // bool operator>=(const time_point&) + // bool operator>=(const time_point&) REQUIRE_EQUAL( (time_point{ms0} == time_point{ms0}), true ) REQUIRE_EQUAL( (time_point{ms1} != time_point{ms0}), true ) - REQUIRE_EQUAL( (time_point{ms0} < time_point{ms1}), true ) + REQUIRE_EQUAL( (time_point{ms0} < time_point{ms1}), true ) REQUIRE_EQUAL( (time_point{ms1} <= time_point{ms1}), true ) - REQUIRE_EQUAL( (time_point{ms1} > time_point{ms0}), true ) + REQUIRE_EQUAL( (time_point{ms1} > time_point{ms0}), true ) REQUIRE_EQUAL( (time_point{ms1} >= time_point{ms1}), true ) // -------------------------------------------- @@ -147,11 +147,11 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) // ------------ // constructors - microseconds ms0{0LL}; - microseconds ms1{1LL}; + microseconds ms0{ 0LL}; + microseconds ms1{ 1LL}; microseconds ms2{-1LL}; - microseconds ms_max{i64MAX}; - microseconds ms_min{i64MIN}; + microseconds ms_max{i64max}; + microseconds ms_min{i64min}; time_point tp0{ms0}; time_point tp1{ms1}; @@ -163,15 +163,15 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) REQUIRE_EQUAL( time_point_sec{}.sec_since_epoch(), 0 ) /// explicit time_point_sec(uint32_t) - REQUIRE_EQUAL( time_point_sec{u32MAX}.sec_since_epoch(), u32MAX ) - REQUIRE_EQUAL( time_point_sec{u32MAX + 1}.sec_since_epoch(), 0 ) + REQUIRE_EQUAL( time_point_sec{u32max}.sec_since_epoch(), u32max ) + REQUIRE_EQUAL( time_point_sec{u32max + 1}.sec_since_epoch(), 0 ) /// time_point_sec(const time_point&) REQUIRE_EQUAL( time_point_sec{tp0}.sec_since_epoch(), 0LL / 1000000LL ) REQUIRE_EQUAL( time_point_sec{tp1}.sec_since_epoch(), 1LL / 1000000LL ) REQUIRE_EQUAL( time_point_sec{tp2}.sec_since_epoch(), -1LL / 1000000LL ) - REQUIRE_EQUAL( time_point_sec{tp_max}.sec_since_epoch(), i64MAX / 1000000LL ) - REQUIRE_EQUAL( time_point_sec{tp_min}.sec_since_epoch(), i64MIN / 1000000LL ) + REQUIRE_EQUAL( time_point_sec{tp_max}.sec_since_epoch(), i64max / 1000000LL ) + REQUIRE_EQUAL( time_point_sec{tp_min}.sec_since_epoch(), i64min / 1000000LL ) // ------------------------------- // static time_point_sec maximum() @@ -188,16 +188,16 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) // microseconds mms0{1}; // eosio::print_f("_count: %\n",mms0._count); // time_point ttp0{mms0}; - // eosio::print_f("_count: %\n",ttp0.elapsed._count); + // eosio::print_f("elapsed: %\n",ttp0.elapsed._count); // time_point_sec ttps0{ttp0}; // eosio::print_f("utc_seconds: %\n",ttps0.utc_seconds); // ttp0 = ttps0; // eosio::print_f("_count: %\n",ttp0.elapsed._count); - // + // Also may have found a `print_f` bug/ // To replicate: - // eosio::print_f("%", time_point{time_point_sec{tp_max}}.elapsed.count()); - // eosio::print_f("%", 9223372036854775807 / 1000000); + eosio::print_f("%", time_point{time_point_sec{tp_max}}.elapsed.count()); + eosio::print_f("%", 9223372036854775807 / 1000000); // -------------------------- // operator time_point()const REQUIRE_EQUAL( (time_point{time_point_sec{tp0}}.elapsed.count()), 0 ) @@ -219,11 +219,11 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) // friend bool operator<=(const time_point_sec&, const time_point_sec&) // friend bool operator>(const time_point_sec&, const time_point_sec&) // friend bool operator>=(const time_point_sec&, const time_point_sec&) - REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) - REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) - REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) + REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) + REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) + REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) REQUIRE_EQUAL( (time_point_sec{tp_max} <= time_point{tp_max}), true ) - REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) + REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) REQUIRE_EQUAL( (time_point_sec{tp_max} >= time_point{tp_max}), true ) @@ -236,24 +236,24 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) // time_point_sec& operator-=(time_point_sec) // time_point_sec operator+(uint32_t)const // time_point_sec operator-(uint32_t)const - REQUIRE_EQUAL( (time_point_sec{} += i64MAX), time_point_sec{i64MAX} ) - REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64MAX}), time_point_sec{tp_max} ) - REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64MAX}), time_point_sec{i64MAX} ) - REQUIRE_EQUAL( (time_point_sec{i64MAX} -= i64MAX), time_point_sec{} ) - REQUIRE_EQUAL( (time_point_sec{i64MAX} -= microseconds{i64MAX}), time_point_sec{i64MAX - (i64MAX / 1000000LL)} ) - REQUIRE_EQUAL( (time_point_sec{i64MAX} -= time_point_sec{i64MAX}), time_point_sec{} ) - REQUIRE_EQUAL( (time_point_sec{} + i64MAX), time_point_sec{i64MAX} ) - REQUIRE_EQUAL( (time_point_sec{i64MAX} - i64MAX), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{} += i64max), time_point_sec{i64max} ) + REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64max}), time_point_sec{tp_max} ) + REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64max}), time_point_sec{i64max} ) + REQUIRE_EQUAL( (time_point_sec{i64max} -= i64max), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{i64max} -= microseconds{i64max}), time_point_sec{i64max - (i64max / 1000000LL)} ) + REQUIRE_EQUAL( (time_point_sec{i64max} -= time_point_sec{i64max}), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{} + i64max), time_point_sec{i64max} ) + REQUIRE_EQUAL( (time_point_sec{i64max} - i64max), time_point_sec{} ) // ----------------------------------------------------------------------- // friend time_point operator+(const time_point_sec&, const microseconds&) // friend time_point operator-(const time_point_sec&, const microseconds&) // friend microseconds operator-(const time_point_sec&, const time_point_sec&) // friend microseconds operator-(const time_point&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{} + microseconds{}), time_point{} ) - // REQUIRE_EQUAL( (time_point_sec{i64MAX} - microseconds{i64MAX}), time_point{} ) - // REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) - // REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) + REQUIRE_EQUAL( (time_point_sec{} + microseconds{}), time_point{} ) + REQUIRE_EQUAL( (time_point_sec{i64max} - microseconds{i64max}), time_point{} ) + REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) + REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) silence_output(false); EOSIO_TEST_END @@ -264,8 +264,8 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) microseconds ms0{0LL}; microseconds ms1{1LL}; microseconds ms2{-1LL}; - microseconds ms_max{i64MAX}; - microseconds ms_min{i64MIN}; + microseconds ms_max{i64max}; + microseconds ms_min{i64min}; time_point tp0{ms0}; time_point tp1{ms1}; @@ -279,9 +279,6 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) time_point_sec tps_max{tp_max}; time_point_sec tps_min{tp_min}; -// eosio::print_f("%",block_timestamp{tp0}.slot); -// eosio::print_f("%",block_timestamp{0}.slot); - // ------------ // constructors @@ -299,11 +296,12 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) // int64_t micro_since_epoch = t.time_since_epoch().count(); // 0 // int64_t msec_since_epoch = micro_since_epoch / 1000; // 0 / 1000 = 0 // slot = uint32_t(( msec_since_epoch - block_timestamp_epoch ) / int64_t(block_interval_ms)); // 0 - 946684800000ll / 500 + /// block_timestamp(const time_point&) - // REQUIRE_EQUAL( block_timestamp{tp0}.slot == block_timestamp{0}.slot, true ) + REQUIRE_EQUAL( block_timestamp{tp0}.slot == block_timestamp{0}.slot, true ) /// block_timestamp(const time_point_sec&) - // REQUIRE_EQUAL( block_timestamp{tps0}.slot == block_timestamp{0}.slot, true ) + REQUIRE_EQUAL( block_timestamp{tps0}.slot == block_timestamp{0}.slot, true ) // -------------------------------- // static block_timestamp maximum() @@ -353,20 +351,20 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) // bool operator<=(const block_timestamp&) // bool operator>(const block_timestamp&) // bool operator>=(const block_timestamp&) - REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) - REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) - // REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) + REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) + REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) + REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) REQUIRE_EQUAL( bt_max <= block_timestamp{tps_max}, true ) - // REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) - REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) + REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) + REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { - // EOSIO_TEST(microseconds_type_test); - // EOSIO_TEST(time_point_type_test); - // EOSIO_TEST(time_point_sec_type_test); + EOSIO_TEST(microseconds_type_test); + EOSIO_TEST(time_point_type_test); + EOSIO_TEST(time_point_sec_type_test); EOSIO_TEST(block_timestamp_type_test); return has_failed(); } diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp new file mode 100644 index 0000000000..c32926913f --- /dev/null +++ b/tests/unit/varint_tests.cpp @@ -0,0 +1,144 @@ +#include +#include + +#include + +using namespace eosio::native; + +// Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` +EOSIO_TEST_BEGIN(unsigned_int_type_test) + silence_output(false); + + // ------------ + // constructors + + /// unsigned_int(uint32_t) + + + /// template + /// unsigned_int(T) + + + // template + // operator T()const + + // --------------------------------- + // unsigned_int& operator=(uint32_t) + + // ------------------------------------------------------------ + // friend bool operator==(const unsigned_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator==(const uint32_t&, const unsigned_int&) + + // ---------------------------------------------------------------- + // friend bool operator==(const unsigned_int&, const unsigned_int&) + + // ------------------------------------------------------------ + // friend bool operator!=(const unsigned_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator!=(const uint32_t&, const unsigned_int&) + + // ---------------------------------------------------------------- + // friend bool operator!=(const unsigned_int&, const unsigned_int&) + + // ------------------------------------------------------------ + // friend bool operator< (const unsigned_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator< (const uint32_t&, const unsigned_int&) + + // ---------------------------------------------------------------- + // friend bool operator< (const unsigned_int&, const unsigned_int&) + + // ------------------------------------------------------------ + // friend bool operator>=(const unsigned_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator>=(const uint32_t&, const unsigned_int&) + + // ---------------------------------------------------------------- + // friend bool operator>=(const unsigned_int&, const unsigned_int&) + + // ----------------------------- + // template + // friend DataStream& operator<<(DataStream&, const unsigned_int&) + + // ----------------------------- + // template + // friend DataStream& operator>>(DataStream&, unsigned_int&) + + silence_output(false); +EOSIO_TEST_END + +// Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` +EOSIO_TEST_BEGIN(signed_int_type_test) + silence_output(false); + + // ------------ + // constructors + + /// signed_int(uint32_t) + + // template + // signed_int& operator=(const T&) + + // -------------------------- + // signed_int operator++(int) + + // ------------------------ + // signed_int& operator++() + + // ------------------------------------------------------------ + // friend bool operator==(const signed_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator==(const uint32_t&, const signed_int&) + + // ---------------------------------------------------------------- + // friend bool operator==(const signed_int&, const signed_int&) + + // ------------------------------------------------------------ + // friend bool operator!=(const signed_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator!=(const uint32_t&, const signed_int&) + + // ---------------------------------------------------------------- + // friend bool operator!=(const signed_int&, const signed_int&) + + // ------------------------------------------------------------ + // friend bool operator< (const signed_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator< (const uint32_t&, const signed_int&) + + // ---------------------------------------------------------------- + // friend bool operator< (const signed_int&, const signed_int&) + + // ------------------------------------------------------------ + // friend bool operator>=(const signed_int&, const uint32_t&) + + // ------------------------------------------------------------ + // friend bool operator>=(const uint32_t&, const signed_int&) + + // ---------------------------------------------------------------- + // friend bool operator>=(const signed_int&, const signed_int&) + + // ----------------------------- + // template + // friend DataStream& operator<<(DataStream&, const signed_int&) + + // ----------------------------- + // template + // friend DataStream& operator>>(DataStream&, signed_int&) + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char* argv[]) { + EOSIO_TEST(signed_int_type_test); + // EOSIO_TEST(signed_int_type_test); + return has_failed(); +} From b486c4def02c384c9428f6522802d217d85b26e0 Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 23 Jan 2019 23:00:24 -0500 Subject: [PATCH 14/51] Update `CMakeLists.txt` files --- tests/CMakeLists.txt | 7 ++++++- tests/unit/CMakeLists.txt | 8 +++++++- tests/unit/print_tests.cpp | 8 ++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fe31e26526..32e2314756 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,10 @@ set(unit_test_dir ${CMAKE_BINARY_DIR}/EosioWasmTests-prefix/src/EosioWasmTests-build) +add_test(asset_tests ${unit_test_dir}/asset_tests) +add_test(eosio_tests ${unit_test_dir}/eosio_tests) add_test(name_tests ${unit_test_dir}/name_tests) -add_test(system_tests ${unit_test_dir}/system_tests) add_test(print_tests ${unit_test_dir}/print_tests) +add_test(symbol_tests ${unit_test_dir}/symbol_tests) +add_test(system_tests ${unit_test_dir}/system_tests) +add_test(time_tests ${unit_test_dir}/time_tests) +add_test(varint_tests ${unit_test_dir}/varint_tests) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 9f5cfa9494..340fab5200 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -1,9 +1,15 @@ list(APPEND CMAKE_MODULE_PATH ${EOSIO_CDT_BIN}) include(EosioCDTMacros) +add_native_executable(asset_tests asset_tests.cpp) +add_native_executable(eosio_tests eosio_tests.cpp) add_native_executable(name_tests name_tests.cpp) -add_native_executable(system_tests system_tests.cpp) add_native_executable(print_tests print_tests.cpp) +add_native_executable(symbol_tests symbol_tests.cpp) +add_native_executable(system_tests system_tests.cpp) +add_native_executable(time_tests time_tests.cpp) +add_native_executable(varint_tests varint_tests.cpp) + add_dependencies(name_tests EosioTools) add_dependencies(system_tests EosioTools) add_dependencies(print_tests EosioTools) diff --git a/tests/unit/print_tests.cpp b/tests/unit/print_tests.cpp index 34ff0f5c42..8fe390dfa3 100644 --- a/tests/unit/print_tests.cpp +++ b/tests/unit/print_tests.cpp @@ -69,10 +69,10 @@ EOSIO_TEST_BEGIN(print_test) // ------------------------ // inline void print(float) - float f{}; - for(float i{}; i < 1; f += 0.01) { - CHECK_PRINT( [&](std::string){return std::to_string{f};}, [&](){eosio::print(f);} ); - } + // float f{}; + // for(float i{}; i < 1; f += 0.01) { + // CHECK_PRINT( [&](std::string){return std::to_string{f};}, [&](){eosio::print(f);} ); + // } // s = ; // CHECK_PRINT( "", [&](){eosio::print(s.c_str());} ); From 65783b959e38c7a88d29c704c3db277e75b02baf Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 25 Jan 2019 18:17:49 -0500 Subject: [PATCH 15/51] Add `fixed_bytes` and `crypto` tests --- tests/unit/crypto_tests.cpp | 74 +++++++++++++++++ tests/unit/fixed_bytes_tests.cpp | 131 +++++++++++++++++++++++++++++++ tests/unit/print_tests.cpp | 39 ++++++++- 3 files changed, 240 insertions(+), 4 deletions(-) create mode 100644 tests/unit/crypto_tests.cpp create mode 100644 tests/unit/fixed_bytes_tests.cpp diff --git a/tests/unit/crypto_tests.cpp b/tests/unit/crypto_tests.cpp new file mode 100644 index 0000000000..912619bce2 --- /dev/null +++ b/tests/unit/crypto_tests.cpp @@ -0,0 +1,74 @@ +#include +#include + +#include + +using eosio::public_key; +using eosio::signature; + +using namespace eosio::native; + +// Defined in `eosio.cdt/libraries/eosiolib/crypto.hpp` +EOSIO_TEST_BEGIN(public_key_type_test) + silence_output(false); + + // bool operator==(const public_key&, const public_key&) + REQUIRE_EQUAL( (public_key{0, std::array{}} == public_key{0, std::array{}}), true ) + REQUIRE_EQUAL( (public_key{0, std::array{1}} == public_key{0, std::array{}}), false ) + + // bool operator!=(const public_key&, const public_key&) + REQUIRE_EQUAL( (public_key{0, std::array{}} != public_key{0, std::array{}}), false ) + REQUIRE_EQUAL( (public_key{0, std::array{1}} != public_key{0, std::array{}}), true ) + + silence_output(false); +EOSIO_TEST_END + +// Defined in `eosio.cdt/libraries/eosiolib/crypto.hpp` +EOSIO_TEST_BEGIN(signature_type_test) + silence_output(false); + + // bool operator==(const signature&, const signature&) + REQUIRE_EQUAL( (signature{0, std::array{}} == signature{0, std::array{}}), true ) + REQUIRE_EQUAL( (signature{0, std::array{1}} == signature{0, std::array{}}), false ) + + // bool operator!=(const signature&, const signature&) + REQUIRE_EQUAL( (signature{0, std::array{}} != signature{0, std::array{}}), false ) + REQUIRE_EQUAL( (signature{0, std::array{1}} != signature{0, std::array{}}), true ) + + silence_output(false); +EOSIO_TEST_END + +// Currently, these intrinsics are not supported +// Defined in `eosio.cdt/libraries/eosiolib/crypto.hpp` +// EOSIO_TEST_BEGIN(crypto_test) +// silence_output(false); + +// void assert_sha256( const char*, uint32_t, const eosio::checksum256&) + +// void assert_sha1(const char*, uint32_t, const eosio::checksum160&) + +// void assert_sha512(const char*, uint32_t, const eosio::checksum512&) + +// void assert_ripemd160(const char*, uint32_t, const eosio::checksum160&) + +// eosio::checksum256 sha256(const char*, uint32_t) + +// eosio::checksum160 sha1(const char*, uint32_t) + +// eosio::checksum512 sha512(const char*, uint32_t) + +// eosio::checksum160 ripemd160(const char*, uint32_t) + +// eosio::public_key recover_key(const eosio::checksum256&, const eosio::signature&) + +// void assert_recover_key(const eosio::checksum256&, const eosio::signature&, const eosio::public_key&) + +// silence_output(false); +// EOSIO_TEST_END + +int main(int argc, char* argv[]) { + EOSIO_TEST(public_key_type_test) + EOSIO_TEST(signature_type_test) + // EOSIO_TEST(crypto_test) + return has_failed(); +} diff --git a/tests/unit/fixed_bytes_tests.cpp b/tests/unit/fixed_bytes_tests.cpp new file mode 100644 index 0000000000..cf33a76c91 --- /dev/null +++ b/tests/unit/fixed_bytes_tests.cpp @@ -0,0 +1,131 @@ +#include +#include + +using eosio::fixed_bytes; + +using namespace eosio::native; + +EOSIO_TEST_BEGIN(fixed_bytes_test) + silence_output(false); + +// constructors + +/// fixed_bytes(const std::array&)d +// static constexpr size_t num_words() +REQUIRE_EQUAL( (fixed_bytes<0>{std::array{}}.num_words()), 0 ) +REQUIRE_EQUAL( (fixed_bytes<1>{std::array{}}.num_words()), 1 ) +REQUIRE_EQUAL( (fixed_bytes<32>{std::array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<512>{std::array{}}.num_words()), 32 ) + +/// constexpr fixed_bytes() +REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), 0 ) +REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), 1 ) +REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), 2 ) +REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), 32 ) + +/// fixed_bytes(const Word(&arr)[NumWords]) +// const uint128_t arr0[0]{}; +// const uint128_t arr1[1]{}; +// const uint128_t arr2[2]{}; +// const uint128_t arr3[32]{}; + +// static constexpr size_t padded_bytes() +REQUIRE_EQUAL( fixed_bytes<0>{}.padded_bytes(), 0 ) +REQUIRE_EQUAL( fixed_bytes<1>{}.padded_bytes(), 15 ) +REQUIRE_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) +REQUIRE_EQUAL( fixed_bytes<512>{}.padded_bytes(), 0 ) + +// const auto& get_array()const +REQUIRE_EQUAL( (fixed_bytes<0>{}.get_array()), (std::array{}) ) +REQUIRE_EQUAL( (fixed_bytes<1>{}.get_array()), (std::array{}) ) +REQUIRE_EQUAL( (fixed_bytes<32>{}.get_array()), (std::array{}) ) +REQUIRE_EQUAL( (fixed_bytes<512>{}.get_array()), (std::array{}) ) + +// eosio::print((fixed_bytes<2>{1}.data())); +// eosio::print("\n"); +// eosio::print(*((uint128_t*){})); +// eosio::print("\n"); + +// auto data() +std::array arr0{}; +std::array arr1{}; +std::array arr2{}; +std::array arr3{}; +fixed_bytes<0> fb0(arr0); +fixed_bytes<1> fb1{arr1}; +fixed_bytes<32> fb2{arr2}; +fixed_bytes<512> fb3{arr3}; + +REQUIRE_EQUAL( fb0.data(), arr0.data() ) +// REQUIRE_EQUAL( (fixed_bytes<0>{}.data()), uint128_t*(std::array{0}) ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.data(), 1 ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.data(), 2 ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.data(), 32 ) + +// // auto data()const +// REQUIRE_EQUAL( fixed_bytes<0>{}.data(), 0 ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.data(), 1 ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.data(), 2 ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.data(), 32 ) + +// // auto size()const +// REQUIRE_EQUAL( fixed_bytes<0>{}.size(), 0 ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.size(), 1 ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.size(), 2 ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.size(), 32 ) + +// // std::array extract_as_byte_array()const +// REQUIRE_EQUAL( fixed_bytes<0>{}.extract_as_byte_array(), 0 ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.extract_as_byte_array(), 1 ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.extract_as_byte_array(), 2 ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.extract_as_byte_array(), 32 ) + +// // template +// // bool operator==(const fixed_bytes, const fixed_bytes) +// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) + +// // template +// // bool operator!=(const fixed_bytes, const fixed_bytes) +// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) + +// // template +// // bool operator>(const fixed_bytes, const fixed_bytes) +// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) + +// // template +// // bool operator<(const fixed_bytes, const fixed_bytes) +// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) + +// // template +// // bool operator>=(const fixed_bytes, const fixed_bytes) +// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) + +// // template +// // bool operator<=(const fixed_bytes, const fixed_bytes) +// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) +// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char* argv[]) { + EOSIO_TEST(fixed_bytes_test); + return has_failed(); +} diff --git a/tests/unit/print_tests.cpp b/tests/unit/print_tests.cpp index 8fe390dfa3..62904eaead 100644 --- a/tests/unit/print_tests.cpp +++ b/tests/unit/print_tests.cpp @@ -5,6 +5,9 @@ #include #include +using eosio::name; +using eosio::print; + using namespace eosio::native; // EOSIO_TEST_BEGIN(print_test) @@ -33,9 +36,9 @@ EOSIO_TEST_BEGIN(print_test) // ------------------------------ // inline void print(const char*) - std::string s{}; - char c{}; - CHECK_PRINT( "", [&](){eosio::print(s.c_str());} ); + // std::string s{}; + // char c{}; + // CHECK_PRINT( "", [&](){eosio::print(s.c_str());} ); // Not understanding how this test isn't passing // for(unsigned short i{}; i < 256; ++i) { @@ -71,7 +74,7 @@ EOSIO_TEST_BEGIN(print_test) // inline void print(float) // float f{}; // for(float i{}; i < 1; f += 0.01) { - // CHECK_PRINT( [&](std::string){return std::to_string{f};}, [&](){eosio::print(f);} ); + // CHECK_PRINT( [&](std::string){return std::to_string{f};}, [&](){eosio::print(f);} ); // } // s = ; @@ -93,6 +96,34 @@ EOSIO_TEST_BEGIN(print_test) // ----------------------- // inline void print(name) +// CHECK_PRINT( [](const std::string& s) {print("HI\n", s.size(), "\n", s, "\n", std_out.index, "\n"); return false;}, []() {print(name{"1"});} ) +// CHECK_PRINT( [](const std::string& s) {print("HI\n", s.size(), "\n", s, "\n", std_out.index, "\n"); return false;}, []() {print(name{"1"});} ) +// print("------\n"); + CHECK_PRINT( "1", []() {print(name{"1"});} ) + // REQUIRE_PRINT( "5", []() {print(name{"5"});} ) + // REQUIRE_PRINT( "a", []() {print(name{"a"});} ) + // REQUIRE_PRINT( "z", []() {print(name{"z"});} ) + + // REQUIRE_PRINT( "abc", []() {print(name{"abc"});} ) + // REQUIRE_PRINT( "123", []() {print(name{"123"});} ) + + // REQUIRE_PRINT( ".abc", []() {print(name{".abc"});} ) + // REQUIRE_PRINT( ".........abc", []() {print(name{".........abc"});} ) + // REQUIRE_PRINT( "123.", []() {print(name{"123."});} ) + // REQUIRE_PRINT( "123.........", []() {print(name{"123........."});} ) + // REQUIRE_PRINT( ".a.b.c.1.2.3.", []() {print(name{".a.b.c.1.2.3."});} ) + + // REQUIRE_PRINT( "abc.123", []() {print(name{"abc.123"});} ) + // REQUIRE_PRINT( "123.abc", []() {print(name{"123.abc"});} ) + + // REQUIRE_PRINT( "12345abcdefgj", []() {print(name{"12345abcdefgj"});} ) + // REQUIRE_PRINT( "hijklmnopqrsj", []() {print(name{"hijklmnopqrsj"});} ) + // REQUIRE_PRINT( "tuvwxyz.1234j", []() {print(name{"tuvwxyz.1234j"});} ) + + // REQUIRE_PRINT( "111111111111j", []() {print(name{"111111111111j"});} ) + // REQUIRE_PRINT( "555555555555j", []() {print(name{"555555555555j"});} ) + // REQUIRE_PRINT( "aaaaaaaaaaaaj", []() {print(name{"aaaaaaaaaaaaj"});} ) + // REQUIRE_PRINT( "zzzzzzzzzzzzj", []() {print(name{"zzzzzzzzzzzzj"});} ) // ------------------------------------- // inline void print(eosio::symbol_code) From f5bed230c8b5b0a0e62414f8306bad001f95543f Mon Sep 17 00:00:00 2001 From: johndebord Date: Sun, 27 Jan 2019 23:57:05 -0500 Subject: [PATCH 16/51] Add `datastream` tests --- tests/unit/asset_tests.cpp | 9 +-- tests/unit/crypto_tests.cpp | 42 +++---------- tests/unit/datastream_tests.cpp | 108 ++++++++++++++++++++++++++++++++ tests/unit/eosio_tests.cpp | 26 ++++++-- tests/unit/name_tests.cpp | 3 +- tests/unit/symbol_tests.cpp | 31 ++++----- tests/unit/system_tests.cpp | 9 ++- tests/unit/time_tests.cpp | 3 +- 8 files changed, 167 insertions(+), 64 deletions(-) create mode 100644 tests/unit/datastream_tests.cpp diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 2635004450..2eaddc1840 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -1,4 +1,5 @@ #include + #include using eosio::name; @@ -16,7 +17,7 @@ const int64_t asset_max{ asset_mask}; // 4611686018427387903 // therefore a constexpr function is needed to dynamically test `precision` constexpr uint8_t increment_precision( uint8_t precision ) { return ++precision; } -// Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) silence_output(true); @@ -225,7 +226,7 @@ EOSIO_TEST_BEGIN(asset_type_test) ) REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ([&]() { - asset{{1LL}, s0} / asset{{1LL}, s1}; + asset{1LL, s0} / asset{1LL, s1}; }) ) @@ -236,7 +237,7 @@ EOSIO_TEST_BEGIN(asset_type_test) REQUIRE_EQUAL( (asset{asset_max, sym_no_prec} == asset{asset_max, sym_no_prec} ), true ) REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( []() { - asset{{}, symbol{"SYMBOLL", 0}} == asset{{}, symbol{"LLOBMYS", 0}}; + asset{{}, symbol{"SYMBOLL", 0}} == asset{{}, symbol{"LLOBMYS", 0}}; }) ) @@ -267,7 +268,7 @@ EOSIO_TEST_BEGIN(asset_type_test) silence_output(false); EOSIO_TEST_END -// Defined in `eosio.cdt/libraries/eosiolib/asset.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(extended_asset_type_test) silence_output(true); diff --git a/tests/unit/crypto_tests.cpp b/tests/unit/crypto_tests.cpp index 912619bce2..74c01e513e 100644 --- a/tests/unit/crypto_tests.cpp +++ b/tests/unit/crypto_tests.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -8,9 +7,9 @@ using eosio::signature; using namespace eosio::native; -// Defined in `eosio.cdt/libraries/eosiolib/crypto.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/crypto.hpp` EOSIO_TEST_BEGIN(public_key_type_test) - silence_output(false); + silence_output(true); // bool operator==(const public_key&, const public_key&) REQUIRE_EQUAL( (public_key{0, std::array{}} == public_key{0, std::array{}}), true ) @@ -23,52 +22,25 @@ EOSIO_TEST_BEGIN(public_key_type_test) silence_output(false); EOSIO_TEST_END -// Defined in `eosio.cdt/libraries/eosiolib/crypto.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/crypto.hpp` EOSIO_TEST_BEGIN(signature_type_test) - silence_output(false); + silence_output(true); // bool operator==(const signature&, const signature&) REQUIRE_EQUAL( (signature{0, std::array{}} == signature{0, std::array{}}), true ) REQUIRE_EQUAL( (signature{0, std::array{1}} == signature{0, std::array{}}), false ) // bool operator!=(const signature&, const signature&) - REQUIRE_EQUAL( (signature{0, std::array{}} != signature{0, std::array{}}), false ) REQUIRE_EQUAL( (signature{0, std::array{1}} != signature{0, std::array{}}), true ) + REQUIRE_EQUAL( (signature{0, std::array{}} != signature{0, std::array{}}), false ) silence_output(false); EOSIO_TEST_END -// Currently, these intrinsics are not supported -// Defined in `eosio.cdt/libraries/eosiolib/crypto.hpp` -// EOSIO_TEST_BEGIN(crypto_test) -// silence_output(false); - -// void assert_sha256( const char*, uint32_t, const eosio::checksum256&) - -// void assert_sha1(const char*, uint32_t, const eosio::checksum160&) - -// void assert_sha512(const char*, uint32_t, const eosio::checksum512&) - -// void assert_ripemd160(const char*, uint32_t, const eosio::checksum160&) - -// eosio::checksum256 sha256(const char*, uint32_t) - -// eosio::checksum160 sha1(const char*, uint32_t) - -// eosio::checksum512 sha512(const char*, uint32_t) - -// eosio::checksum160 ripemd160(const char*, uint32_t) - -// eosio::public_key recover_key(const eosio::checksum256&, const eosio::signature&) - -// void assert_recover_key(const eosio::checksum256&, const eosio::signature&, const eosio::public_key&) - -// silence_output(false); -// EOSIO_TEST_END - int main(int argc, char* argv[]) { + EOSIO_TEST(public_key_type_test) EOSIO_TEST(signature_type_test) - // EOSIO_TEST(crypto_test) + return has_failed(); } diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp new file mode 100644 index 0000000000..1b07fea6e8 --- /dev/null +++ b/tests/unit/datastream_tests.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + +#include + +using std::vector; +using eosio::datastream; + +using namespace eosio::native; + +// Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` +EOSIO_TEST_BEGIN(datastream_test) + silence_output(false); + +char test_stream_bytes[256]{}; // Buffer for the datastream to point to +char buffer[256]{}; // Buffer to act upon for testing + +// Fill the char array `test_stream_bytes` with all all 256 ASCII characters +for(int i{0}, j{-0x80}; i < 256; ++i, ++j) + test_stream_bytes[i] = j; + +// Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? +/// datastream(T, size_t) +datastream ds{test_stream_bytes, 256}; + +// inline void skip(size_t) +ds.skip(1); +REQUIRE_EQUAL( ds.pos() == test_stream_bytes+1, true ) +ds.skip(-1); +REQUIRE_EQUAL( ds.pos() == test_stream_bytes, true ) + +// inline bool read(char*, size_t) +REQUIRE_EQUAL( ds.read(buffer, 256), true ) +REQUIRE_EQUAL( strcmp(buffer, test_stream_bytes), 0 ) + +REQUIRE_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) + +// T pos()const +// inline bool seekp(size_t) +REQUIRE_EQUAL( ds.pos() == test_stream_bytes+255, true ) +REQUIRE_EQUAL( ds.pos() == test_stream_bytes, false ) +ds.seekp(0); +REQUIRE_EQUAL( ds.pos() == test_stream_bytes, true ) +REQUIRE_EQUAL( ds.pos() == test_stream_bytes+255, false ) + +// inline bool write(const char*, size_t) +// REQUIRE_EQUAL( ds.write(buffer, 256), true ) +// REQUIRE_EQUAL( strcmp(buffer, test_stream_bytes), 0 ) + +// inline bool put(char) + +// inline bool get(unsigned char&) +// eosio::print("_start: ", (uint64_t)ds._start, "_pos: ", (uint64_t)ds._pos, "_end: ", (uint64_t)ds._end); + +// inline bool get(char&) + +// REQUIRE_EQUAL( ds.pos() == 0, true ) +// REQUIRE_EQUAL( ds.pos() == 1, false ) + +// inline bool valid()const + +// inline size_t tellp()const + +//inline size_t remaining()const + + + + +// Note that if the size given to the buffer (which is of type `size_t`) is +// the value of the maximum underlying type (which is a generic type `T`), then +// the `_pos` will always be valid because `_pos` will overflow before it exceeds +// the size of the buffer. +// The solution, I see is to make the type of `_pos` type `size_t` with an +// invariant maximum value of `numeric_limits::max()-1` +// REQUIRE_EQUAL( ds.valid() == true, true ) +// ds.seekp(257); +// REQUIRE_EQUAL( ds.valid() == true, false ) +// ds.seekp(0); +// REQUIRE_EQUAL( ds.tellp() == 0, true ) +// REQUIRE_EQUAL( ds.tellp() == 1, false ) +// REQUIRE_EQUAL( ds.remaining() == 256, true ) +// REQUIRE_EQUAL( ds.remaining() == 255, false ) + +// ds.seekp(10); +// REQUIRE_EQUAL( ds.pos() == 10, true ) +// REQUIRE_EQUAL( ds.valid() == true, true ) +// REQUIRE_EQUAL( ds.tellp() == 10, true ) +// REQUIRE_EQUAL( ds.remaining() == 246, true ) + + + + +// ds.skip(257); +// eosio::print("_start: ", ds._start, "_pos: ", ds._pos, "_end: ", ds._end); +// REQUIRE_EQUAL( ds.valid() == true, true ) + +// eosio::print("_start: ", (uint64_t)ds._start, "_pos: ", (uint64_t)ds._pos, "_end: ", (uint64_t)ds._end); +// eosio::print((uint64_t)ds._end - (uint64_t)ds._start); +// eosio::print(strcmp(buffer, test_stream_bytes)); + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char** argv) { + EOSIO_TEST(datastream_test); + return has_failed(); +} diff --git a/tests/unit/eosio_tests.cpp b/tests/unit/eosio_tests.cpp index 803b792816..ad6d1dfd97 100644 --- a/tests/unit/eosio_tests.cpp +++ b/tests/unit/eosio_tests.cpp @@ -1,14 +1,32 @@ -#include #include -using namespace eosio; +#include + using namespace eosio::native; -// Defined in `eosio.cdt/libraries/eosiolib/eosio.hpp` +namespace using_no_macros { +class [[eosio::contract]] con { + + struct [[eosio::table]] tab {}; + + [[eosio::action]] void act() {} +}; +} + +namespace using_macros { +CONTRACT con { + + TABLE tab {}; + + ACTION act() {} +}; +} + +// Definitions in `eosio.cdt/libraries/eosiolib/eosio.hpp` EOSIO_TEST_BEGIN(eosio_test) silence_output(false); - +// REQUIRE_EQUAL( typeid(using_no_macros::con).name() == typeid(using_macros::con).name(), true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 7f1f7343c9..05824d3c27 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,4 +1,5 @@ #include + #include using eosio::name; @@ -6,7 +7,7 @@ using eosio::name; const uint64_t u64min = std::numeric_limits::min(); // 0ULL const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL -// Defined in `eosio.cdt/libraries/eosiolib/name.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/name.hpp` EOSIO_TEST_BEGIN(name_type_test) silence_output(true); diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index e2dee3893e..f31c0d4967 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -1,4 +1,5 @@ #include + #include using eosio::name; @@ -9,7 +10,7 @@ using eosio::extended_symbol; const uint64_t u64min = std::numeric_limits::min(); // 0ULL const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL -// Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_code_type_test) silence_output(true); @@ -114,7 +115,7 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) silence_output(false); EOSIO_TEST_END -// Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_type_test) silence_output(true); @@ -227,7 +228,7 @@ EOSIO_TEST_BEGIN(symbol_type_test) silence_output(false); EOSIO_TEST_END -// Defined in `eosio.cdt/libraries/eosiolib/symbol.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(extended_symbol_type_test) silence_output(true); @@ -270,20 +271,20 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) REQUIRE_EQUAL( (extended_symbol{s3, n6}.get_contract().value), 3570337562653461615ULL ) REQUIRE_EQUAL( (extended_symbol{s3, n7}.get_contract().value), u64max ) - // Note: uncomment once the appended '\n' is removed from the `print` function - // --------------------- - // void print(bool)const + // // Note: uncomment once the appended '\n' is removed from the `print` function + // // --------------------- + // // void print(bool)const // extended_symbol{s0, n0}.print(true); // extended_symbol{s0, n0}.print(true); - // REQUIRE_PRINT( "0@576460752303423488", [](){extended_symbol{s0, n0}.print(true);} ); - // REQUIRE_PRINT( "0@2882303761517117440", [](){extended_symbol{s1, n1}.print(true);} ); - // REQUIRE_PRINT( "255@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); - // REQUIRE_PRINT( "255@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); - - // REQUIRE_PRINT( "@576460752303423488", [](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); - // REQUIRE_PRINT( "@2882303761517117440", [](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); - // REQUIRE_PRINT( "@595056260442243615", [](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); - // REQUIRE_PRINT( "@2975281302211218015", [](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); + // REQUIRE_PRINT( "0@576460752303423488", [&](){extended_symbol{s0, n0}.print(true);} ); + // REQUIRE_PRINT( "0@2882303761517117440", [&](){extended_symbol{s1, n1}.print(true);} ); + // REQUIRE_PRINT( "255@595056260442243615", [&](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); + // REQUIRE_PRINT( "255@2975281302211218015", [&](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); + + // REQUIRE_PRINT( "@576460752303423488", [&](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); + // REQUIRE_PRINT( "@2882303761517117440", [&](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); + // REQUIRE_PRINT( "@595056260442243615", [&](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); + // REQUIRE_PRINT( "@2975281302211218015", [&](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); // ------------------------------------------------------------------------------- // friend constexpr bool operator==(const extended_symbol&, const extended_symbol&) diff --git a/tests/unit/system_tests.cpp b/tests/unit/system_tests.cpp index 81c3091feb..731d96143d 100644 --- a/tests/unit/system_tests.cpp +++ b/tests/unit/system_tests.cpp @@ -1,14 +1,17 @@ -#include #include +#include + +#include + using eosio::check; using std::string; using namespace eosio::native; -// Defined in `eosio.cdt/libraries/eosiolib/system.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/system.hpp` EOSIO_TEST_BEGIN(system_test) - silence_output(false); + silence_output(true); // inline void check(bool, const char*) REQUIRE_ASSERT( "asserted", []() { const char* str{"asserted"}; check(false, str);} ); diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index 8bc4c8b703..e6c57c69fc 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -1,4 +1,3 @@ -#include #include #include @@ -25,7 +24,7 @@ const int64_t i64max = std::numeric_limits::max(); // 9223372036854775 // Or if you check the `seconds` of a (u64MAX / 1000000 + 1), it will overflow. // Potentially could be abused in a contract? -// Defined in `eosio.cdt/libraries/eosiolib/time.hpp` +// Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(microseconds_type_test) silence_output(false); From 62d514091f2765d7ae73bd490c1f9c0c5c70dc1c Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 28 Jan 2019 15:14:15 -0500 Subject: [PATCH 17/51] Continuation of `datastream` tests --- tests/unit/datastream_tests.cpp | 325 +++++++++++++++++++++++++------- 1 file changed, 259 insertions(+), 66 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 1b07fea6e8..b5c2ef1645 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,108 +1,301 @@ #include #include +#include #include #include -using std::vector; using eosio::datastream; +using std::list; using namespace eosio::native; // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_test) - silence_output(false); + silence_output(true); + + char datastream_buffer[256]{}; // Buffer for the datastream to point to + char buffer[256]{}; // Buffer to act upon for testing + + // Fill the char array `datastream_buffer` with all all 256 ASCII characters + for(int i{0}, j{-0x80}; i < 256; ++i, ++j) + datastream_buffer[i] = j; + + // Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? + /// datastream(T, size_t) + datastream ds{datastream_buffer, 256}; -char test_stream_bytes[256]{}; // Buffer for the datastream to point to -char buffer[256]{}; // Buffer to act upon for testing + // inline void skip(size_t) + ds.skip(1); + REQUIRE_EQUAL( ds.pos() == datastream_buffer+1, true ) + ds.skip(-1); + REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) -// Fill the char array `test_stream_bytes` with all all 256 ASCII characters -for(int i{0}, j{-0x80}; i < 256; ++i, ++j) - test_stream_bytes[i] = j; + // inline bool read(char*, size_t) + REQUIRE_EQUAL( ds.read(buffer, 256), true ) + REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) -// Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? -/// datastream(T, size_t) -datastream ds{test_stream_bytes, 256}; + REQUIRE_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) -// inline void skip(size_t) -ds.skip(1); -REQUIRE_EQUAL( ds.pos() == test_stream_bytes+1, true ) -ds.skip(-1); -REQUIRE_EQUAL( ds.pos() == test_stream_bytes, true ) + // T pos()const + // inline bool seekp(size_t) + REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, true ) + REQUIRE_EQUAL( ds.pos() == datastream_buffer, false ) + ds.seekp(0); + REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) + REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, false ) -// inline bool read(char*, size_t) -REQUIRE_EQUAL( ds.read(buffer, 256), true ) -REQUIRE_EQUAL( strcmp(buffer, test_stream_bytes), 0 ) + // inline bool write(const char*, size_t) + // Fill `buffer` with a new set of values + for(int i{0}, j{1}; i < 256; ++i) + buffer[i] = j; -REQUIRE_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) + REQUIRE_EQUAL( ds.write(buffer, 256), true ) + REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) -// T pos()const -// inline bool seekp(size_t) -REQUIRE_EQUAL( ds.pos() == test_stream_bytes+255, true ) -REQUIRE_EQUAL( ds.pos() == test_stream_bytes, false ) -ds.seekp(0); -REQUIRE_EQUAL( ds.pos() == test_stream_bytes, true ) -REQUIRE_EQUAL( ds.pos() == test_stream_bytes+255, false ) + REQUIRE_ASSERT( "write", ([&]() {ds.write(buffer, 1);}) ) -// inline bool write(const char*, size_t) -// REQUIRE_EQUAL( ds.write(buffer, 256), true ) -// REQUIRE_EQUAL( strcmp(buffer, test_stream_bytes), 0 ) + // I'm getting the error: `read-only variable is not assignable *_pos = c;` + // inline bool put(char) + ds.seekp(0); + REQUIRE_EQUAL( ds.put('c'), true ) + *buffer = 'c'; + REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) -// inline bool put(char) + ds.seekp(256); + REQUIRE_ASSERT( "put", ([&]() {ds.put('c');}) ) -// inline bool get(unsigned char&) -// eosio::print("_start: ", (uint64_t)ds._start, "_pos: ", (uint64_t)ds._pos, "_end: ", (uint64_t)ds._end); + // inline bool get(unsigned char&) + unsigned char c0{}; + + ds.seekp(0); + REQUIRE_EQUAL( ds.get(c0), true ) + REQUIRE_EQUAL( c0, 'c' ) + + // inline bool get(char&) + char c1{}; + + ds.seekp(0); + REQUIRE_EQUAL( ds.get(c1), true ) + REQUIRE_EQUAL( c1, 'c' ) + + // inline bool valid()const + ds.seekp(256); + REQUIRE_EQUAL( ds.valid(), true ) + + ds.seekp(257); + REQUIRE_EQUAL( ds.valid(), false ) + + // inline size_t tellp()const + ds.seekp(0); + REQUIRE_EQUAL( ds.tellp(), 0 ) + ds.seekp(256); + REQUIRE_EQUAL( ds.tellp(), 256 ) + ds.seekp(257); + REQUIRE_EQUAL( ds.tellp(), 257 ) + + //inline size_t remaining()const + ds.seekp(0); + REQUIRE_EQUAL( ds.remaining(), 256 ) + ds.seekp(256); + REQUIRE_EQUAL( ds.remaining(), 0 ) + // I don't understand: + // If the return type is of type `size_t`, how then does `_end - _pos` equate to + // -1? Should it be the maximum value of a `size_t`? + ds.seekp(257); + REQUIRE_EQUAL( ds.remaining(), -1) + + // eosio::print("_start: ", (uint64_t)ds._start, "_pos: ", (uint64_t)ds._pos, "_end: ", (uint64_t)ds._end); + + silence_output(false); +EOSIO_TEST_END + +// Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` +EOSIO_TEST_BEGIN(datastream_specialization_test) + silence_output(true); + + char datastream_buffer[256]{}; // Buffer for the datastream to point to + char buffer[256]{}; // Buffer to act upon for testing + + // Is this portable? Is there a better way? + // Fill the char array `datastream_buffer` with all all 256 ASCII characters + for(int i{0}, j{-0x80}; i < 256; ++i, ++j) + datastream_buffer[i] = j; + + // Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? + /// datastream(T, size_t) + datastream ds{256}; + + // inline void skip(size_t) + // inline size_t tellp()const + REQUIRE_EQUAL( ds.skip(0), true) + REQUIRE_EQUAL( ds.tellp(), 256) + + REQUIRE_EQUAL( ds.skip(1), true) + REQUIRE_EQUAL( ds.tellp(), 257) + + REQUIRE_EQUAL( ds.skip(255), true) + REQUIRE_EQUAL( ds.tellp(), 512) + + REQUIRE_EQUAL( ds.skip(1028), true) + REQUIRE_EQUAL( ds.tellp(),1540) + + // inline bool write(const char*,size_t) + // inline bool seekp(size_t) + ds.seekp(0); + REQUIRE_EQUAL( ds.write(buffer, 256), true ) + REQUIRE_EQUAL( ds.tellp(), 256 ) + + REQUIRE_EQUAL( ds.write(buffer, 1), true ) + REQUIRE_EQUAL( ds.tellp(), 257 ) + + // inline bool put(char) + char c0{'c'}; + + ds.seekp(0); + REQUIRE_EQUAL( ds.put(c0), true ) + REQUIRE_EQUAL( ds.tellp(), 1 ) + + // inline bool valid() + ds.seekp(0); + REQUIRE_EQUAL( ds.valid(), true ) + + ds.seekp(1); + REQUIRE_EQUAL( ds.valid(), true ) + + ds.seekp(256); + REQUIRE_EQUAL( ds.valid(), true ) + + ds.seekp(257); + REQUIRE_EQUAL( ds.valid(), true ) + + // inline size_t remaining() + ds.seekp(0); + REQUIRE_EQUAL( ds.remaining(), 0 ) + + ds.seekp(1); + REQUIRE_EQUAL( ds.remaining(), 0 ) + + ds.seekp(256); + REQUIRE_EQUAL( ds.remaining(), 0 ) + + ds.seekp(257); + REQUIRE_EQUAL( ds.remaining(), 0 ) + + silence_output(false); +EOSIO_TEST_END + +// Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` +EOSIO_TEST_BEGIN(datastream_stream_test) + silence_output(false); + + char datastream_buffer[8+sizeof(unsigned_int)]{}; // Buffer for the datastream to point to + char const buffer[8]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; // Buffer to act upon for testing + + // // Fill the char array `buffer` with all all 256 ASCII characters + // for(int i{0}, j{-0x80}; i < 256; ++i, ++j) + // buffer[i] = j; + + // --------- + // std::list + +const std::list l{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', }; +eosio::print(sizeof(unsigned_int)); + + datastream ds{datastream_buffer, 8+sizeof(unsigned_int)}; + + for(int i{0}; i < 8; ++i) + eosio::print(datastream_buffer[i]); + eosio::print("\n"); + for(int i{0}; i < 8; ++i) + eosio::print(buffer[i]); + eosio::print("\n"); + for(auto& x : l) + eosio::print(x); + eosio::print("\n"); + + eosio::print("\n"); + + ds << l; + + for(int i{0}; i < 8+sizeof(unsigned_int); ++i) + eosio::print(datastream_buffer[i]); + eosio::print("\n"); + for(int i{0}; i < 8; ++i) + eosio::print(buffer[i]); + eosio::print("\n"); + for(auto& x : l) + eosio::print(x); + eosio::print("\n"); + + // std::vector um_ds(std::begin(datastream_buffer), std::end(datastream_buffer)); + // std::vector um_list(std::begin(datastream_buffer), std::end(datastream_buffer)); + + // for(uint16_t i{0}; i < 256; ++i) + // eosio::print(datastream_buffer); + + // for(uint16_t i{0}; i < 256; ++i) + // eosio::print(datastream_buffer); + + // REQUIRE_EQUAL( um_ds == um_list, true ) + + // ---------- + // std::deque + + // ---------------- + // binary_extension + + // ------------ + // std::variant -// inline bool get(char&) - -// REQUIRE_EQUAL( ds.pos() == 0, true ) -// REQUIRE_EQUAL( ds.pos() == 1, false ) + // --------- + // std::pair -// inline bool valid()const + // --------- + // std::optional -// inline size_t tellp()const + // --------- + // std::pair -//inline size_t remaining()const + // --------- + // symbol_code + // --------- + // symbol + // --------- + // ignore_wrapper + // --------- + // ignore -// Note that if the size given to the buffer (which is of type `size_t`) is -// the value of the maximum underlying type (which is a generic type `T`), then -// the `_pos` will always be valid because `_pos` will overflow before it exceeds -// the size of the buffer. -// The solution, I see is to make the type of `_pos` type `size_t` with an -// invariant maximum value of `numeric_limits::max()-1` -// REQUIRE_EQUAL( ds.valid() == true, true ) -// ds.seekp(257); -// REQUIRE_EQUAL( ds.valid() == true, false ) -// ds.seekp(0); -// REQUIRE_EQUAL( ds.tellp() == 0, true ) -// REQUIRE_EQUAL( ds.tellp() == 1, false ) -// REQUIRE_EQUAL( ds.remaining() == 256, true ) -// REQUIRE_EQUAL( ds.remaining() == 255, false ) + // --------- + // capi_public_key -// ds.seekp(10); -// REQUIRE_EQUAL( ds.pos() == 10, true ) -// REQUIRE_EQUAL( ds.valid() == true, true ) -// REQUIRE_EQUAL( ds.tellp() == 10, true ) -// REQUIRE_EQUAL( ds.remaining() == 246, true ) + // --------- + // public_key + // --------- + // signature + // --------- + // public_keykey256 + // ----------- + // fixed_bytes -// ds.skip(257); -// eosio::print("_start: ", ds._start, "_pos: ", ds._pos, "_end: ", ds._end); -// REQUIRE_EQUAL( ds.valid() == true, true ) + // ----------- + // bool -// eosio::print("_start: ", (uint64_t)ds._start, "_pos: ", (uint64_t)ds._pos, "_end: ", (uint64_t)ds._end); -// eosio::print((uint64_t)ds._end - (uint64_t)ds._start); -// eosio::print(strcmp(buffer, test_stream_bytes)); + // ----------- + // std::array silence_output(false); EOSIO_TEST_END -int main(int argc, char** argv) { - EOSIO_TEST(datastream_test); +int main(int argc, char* argv[]) { + //EOSIO_TEST(datastream_test); + // EOSIO_TEST(datastream_specialization_test); + EOSIO_TEST(datastream_stream_test); return has_failed(); } From 63c1d9a627df3a3cdb1e275ca896b521ee65c310 Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 28 Jan 2019 18:26:47 -0500 Subject: [PATCH 18/51] Progress on `datastream` tests and `varint` tests almost done --- tests/unit/datastream_tests.cpp | 27 ++++-- tests/unit/name_tests.cpp | 1 - tests/unit/varint_tests.cpp | 152 ++++++++++++++++++++++++++++++-- 3 files changed, 164 insertions(+), 16 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index b5c2ef1645..b181d57406 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -190,21 +190,16 @@ EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(false); char datastream_buffer[8+sizeof(unsigned_int)]{}; // Buffer for the datastream to point to - char const buffer[8]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; // Buffer to act upon for testing - - // // Fill the char array `buffer` with all all 256 ASCII characters - // for(int i{0}, j{-0x80}; i < 256; ++i, ++j) - // buffer[i] = j; + char buffer[8]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; // Buffer to act upon for testing // --------- // std::list -const std::list l{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', }; -eosio::print(sizeof(unsigned_int)); + std::list l{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', }; datastream ds{datastream_buffer, 8+sizeof(unsigned_int)}; - for(int i{0}; i < 8; ++i) + for(int i{0}; i < 17; ++i) eosio::print(datastream_buffer[i]); eosio::print("\n"); for(int i{0}; i < 8; ++i) @@ -218,7 +213,21 @@ eosio::print(sizeof(unsigned_int)); ds << l; - for(int i{0}; i < 8+sizeof(unsigned_int); ++i) + for(int i{0}; i < 17; ++i) + eosio::print(datastream_buffer[i]); + eosio::print("\n"); + for(int i{0}; i < 8; ++i) + eosio::print(buffer[i]); + eosio::print("\n"); + for(auto& x : l) + eosio::print(x); + eosio::print("\n"); + + ds >> l; + + eosio::print("\n"); + + for(int i{0}; i < 17; ++i) eosio::print(datastream_buffer[i]); eosio::print("\n"); for(int i{0}; i < 8; ++i) diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 05824d3c27..acca6e1c01 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,5 +1,4 @@ #include - #include using eosio::name; diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index c32926913f..d2a42f4528 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -1,10 +1,17 @@ -#include #include - +#include #include +using eosio::datastream; + using namespace eosio::native; +const uint32_t u32min = std::numeric_limits::min(); // 0 +const uint32_t u32max = std::numeric_limits::max(); // 18446744073709551615 + +const int32_t i32min = std::numeric_limits::min(); // -9223372036854775808 +const int32_t i32max = std::numeric_limits::max(); // 9223372036854775807 + // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(unsigned_int_type_test) silence_output(false); @@ -13,61 +20,115 @@ EOSIO_TEST_BEGIN(unsigned_int_type_test) // constructors /// unsigned_int(uint32_t) - + REQUIRE_EQUAL( unsigned_int{}.value, 0 ) + REQUIRE_EQUAL( unsigned_int{u32min}.value, 0 ) + REQUIRE_EQUAL( unsigned_int{u32max}.value, 18446744073709551615 ) /// template /// unsigned_int(T) - + REQUIRE_EQUAL( unsigned_int{uint8_t{0}}.value, 0 ) + REQUIRE_EQUAL( unsigned_int{uint16_t{1}}.value, 1 ) + REQUIRE_EQUAL( unsigned_int{uint32_t{2}}.value, 2 ) + REQUIRE_EQUAL( unsigned_int{uint64_t{3}}.value, 3 ) // template // operator T()const + REQUIRE_EQUAL( unsigned_int{0}.operator bool(), false ) + REQUIRE_EQUAL( unsigned_int{1}.operator bool(), true ) // --------------------------------- // unsigned_int& operator=(uint32_t) + unsigned_int ui0{}; + unsigned_int ui1{42}; + + ui0 = ui1; + REQUIRE_EQUAL( ui0 == ui1, true ) // ------------------------------------------------------------ // friend bool operator==(const unsigned_int&, const uint32_t&) + REQUIRE_EQUAL( unsigned_int{42} == uint32_t{42}, true ) + REQUIRE_EQUAL( unsigned_int{42} == uint32_t{43}, false ) // ------------------------------------------------------------ // friend bool operator==(const uint32_t&, const unsigned_int&) + REQUIRE_EQUAL( uint32_t{42} == unsigned_int{42}, true ) + REQUIRE_EQUAL( uint32_t{43} == unsigned_int{42}, false ) // ---------------------------------------------------------------- // friend bool operator==(const unsigned_int&, const unsigned_int&) + REQUIRE_EQUAL( unsigned_int{42} == unsigned_int{42}, true ) + REQUIRE_EQUAL( unsigned_int{42} == unsigned_int{43}, false ) // ------------------------------------------------------------ // friend bool operator!=(const unsigned_int&, const uint32_t&) + REQUIRE_EQUAL( unsigned_int{42} != uint32_t{42}, false ) + REQUIRE_EQUAL( unsigned_int{42} != uint32_t{43}, true ) // ------------------------------------------------------------ // friend bool operator!=(const uint32_t&, const unsigned_int&) + REQUIRE_EQUAL( uint32_t{42} != unsigned_int{42}, false ) + REQUIRE_EQUAL( uint32_t{43} != unsigned_int{42}, true ) // ---------------------------------------------------------------- // friend bool operator!=(const unsigned_int&, const unsigned_int&) + REQUIRE_EQUAL( unsigned_int{42} != unsigned_int{42}, false ) + REQUIRE_EQUAL( unsigned_int{42} != unsigned_int{43}, true ) // ------------------------------------------------------------ // friend bool operator< (const unsigned_int&, const uint32_t&) + REQUIRE_EQUAL( unsigned_int{42} < uint32_t{42}, false ) + REQUIRE_EQUAL( unsigned_int{42} < uint32_t{43}, true ) // ------------------------------------------------------------ // friend bool operator< (const uint32_t&, const unsigned_int&) + REQUIRE_EQUAL( uint32_t{42} < unsigned_int{42}, false ) + REQUIRE_EQUAL( uint32_t{42} < unsigned_int{43}, true ) // ---------------------------------------------------------------- // friend bool operator< (const unsigned_int&, const unsigned_int&) + REQUIRE_EQUAL( unsigned_int{42} < unsigned_int{42}, false ) + REQUIRE_EQUAL( unsigned_int{42} < unsigned_int{43}, true ) // ------------------------------------------------------------ // friend bool operator>=(const unsigned_int&, const uint32_t&) + REQUIRE_EQUAL( unsigned_int{42} >= uint32_t{42}, true ) + REQUIRE_EQUAL( unsigned_int{42} >= uint32_t{43}, false ) // ------------------------------------------------------------ // friend bool operator>=(const uint32_t&, const unsigned_int&) + REQUIRE_EQUAL( uint32_t{42} >= unsigned_int{42}, true ) + REQUIRE_EQUAL( uint32_t{42} >= unsigned_int{43}, false ) // ---------------------------------------------------------------- // friend bool operator>=(const unsigned_int&, const unsigned_int&) + REQUIRE_EQUAL( unsigned_int{42} >= unsigned_int{42}, true ) + REQUIRE_EQUAL( unsigned_int{42} >= unsigned_int{43}, false ) // ----------------------------- // template // friend DataStream& operator<<(DataStream&, const unsigned_int&) + const unsigned_int cui{0x7F}; + const char expected_buffer0[1]{0x7F}; + + char buffer0[1]{}; + datastream ds0{buffer0, 1}; + + ds0 << cui; + + REQUIRE_EQUAL( memcmp(expected_buffer0, buffer0, 1), 0 ) // ----------------------------- // template - // friend DataStream& operator>>(DataStream&, unsigned_int&) + // friend DataStream& operator>>(DataStream&, unsigned_int&) + unsigned_int ui{}; + const char expected_buffer1[1]{0x7F}; + + char buffer1[1]{0x7F}; + datastream ds1{buffer1, 1}; + + ds1 >> ui; + + REQUIRE_EQUAL( memcmp(expected_buffer1, buffer1, 1), 0 ) silence_output(false); EOSIO_TEST_END @@ -80,65 +141,144 @@ EOSIO_TEST_BEGIN(signed_int_type_test) // constructors /// signed_int(uint32_t) + REQUIRE_EQUAL( signed_int{}.value, 0 ) + REQUIRE_EQUAL( signed_int{i32min}.value, -9223372036854775808 ) + REQUIRE_EQUAL( signed_int{i32max}.value, 9223372036854775807 ) + // ----------------------- + // operator int32_t()const + REQUIRE_EQUAL( signed_int{}.operator int32_t(), 0 ) + REQUIRE_EQUAL( signed_int{i32min}.operator int32_t(), -9223372036854775808 ) + REQUIRE_EQUAL( signed_int{i32max}.operator int32_t(), 9223372036854775807 ) + + // --------------------- // template // signed_int& operator=(const T&) + signed_int i0{}; + signed_int i1{}; + signed_int i2{}; + signed_int i3{}; + + int8_t i8{0}; + int16_t i16{1}; + int32_t i32{2}; + int64_t i64{3}; + + i0 = i8; + i1 = i16; + i2 = i32; + i3 = i64; + + REQUIRE_EQUAL( i0.value, 0 ) + REQUIRE_EQUAL( i1.value, 1 ) + REQUIRE_EQUAL( i2.value, 2 ) + REQUIRE_EQUAL( i3.value, 3 ) // -------------------------- // signed_int operator++(int) + signed_int post_inc0{0}; + signed_int post_inc1{1}; + REQUIRE_EQUAL( post_inc0++.value, 0 ) + REQUIRE_EQUAL( post_inc1++.value, 1 ) // ------------------------ // signed_int& operator++() + signed_int pre_inc0{0}; + signed_int pre_inc1{1}; + REQUIRE_EQUAL( ++pre_inc0.value, 1 ) + REQUIRE_EQUAL( ++pre_inc1.value, 2 ) // ------------------------------------------------------------ // friend bool operator==(const signed_int&, const uint32_t&) + REQUIRE_EQUAL( signed_int{42} == int32_t{42}, true ) + REQUIRE_EQUAL( signed_int{42} == int32_t{43}, false ) // ------------------------------------------------------------ // friend bool operator==(const uint32_t&, const signed_int&) + REQUIRE_EQUAL( int32_t{42} == signed_int{42}, true ) + REQUIRE_EQUAL( int32_t{43} == signed_int{42}, false ) // ---------------------------------------------------------------- // friend bool operator==(const signed_int&, const signed_int&) + REQUIRE_EQUAL( signed_int{42} == signed_int{42}, true ) + REQUIRE_EQUAL( signed_int{42} == signed_int{43}, false ) // ------------------------------------------------------------ // friend bool operator!=(const signed_int&, const uint32_t&) + REQUIRE_EQUAL( signed_int{42} != int32_t{42}, false ) + REQUIRE_EQUAL( signed_int{42} != int32_t{43}, true ) // ------------------------------------------------------------ // friend bool operator!=(const uint32_t&, const signed_int&) + REQUIRE_EQUAL( int32_t{42} != signed_int{42}, false ) + REQUIRE_EQUAL( int32_t{43} != signed_int{42}, true ) // ---------------------------------------------------------------- // friend bool operator!=(const signed_int&, const signed_int&) + REQUIRE_EQUAL( signed_int{42} != signed_int{42}, false ) + REQUIRE_EQUAL( signed_int{42} != signed_int{43}, true ) // ------------------------------------------------------------ // friend bool operator< (const signed_int&, const uint32_t&) + REQUIRE_EQUAL( signed_int{42} < int32_t{42}, false ) + REQUIRE_EQUAL( signed_int{42} < int32_t{43}, true ) // ------------------------------------------------------------ // friend bool operator< (const uint32_t&, const signed_int&) + REQUIRE_EQUAL( int32_t{42} < signed_int{42}, false ) + REQUIRE_EQUAL( int32_t{42} < signed_int{43}, true ) // ---------------------------------------------------------------- // friend bool operator< (const signed_int&, const signed_int&) + REQUIRE_EQUAL( signed_int{42} < signed_int{42}, false ) + REQUIRE_EQUAL( signed_int{42} < signed_int{43}, true ) // ------------------------------------------------------------ // friend bool operator>=(const signed_int&, const uint32_t&) + REQUIRE_EQUAL( signed_int{42} >= int32_t{42}, true ) + REQUIRE_EQUAL( signed_int{42} >= int32_t{43}, false ) // ------------------------------------------------------------ // friend bool operator>=(const uint32_t&, const signed_int&) + REQUIRE_EQUAL( int32_t{42} >= signed_int{42}, true ) + REQUIRE_EQUAL( int32_t{42} >= signed_int{43}, false ) // ---------------------------------------------------------------- // friend bool operator>=(const signed_int&, const signed_int&) + REQUIRE_EQUAL( signed_int{42} >= signed_int{42}, true ) + REQUIRE_EQUAL( signed_int{42} >= signed_int{43}, false ) // ----------------------------- // template // friend DataStream& operator<<(DataStream&, const signed_int&) + const signed_int ci{0x7F}; + const char expected_buffer0[1]{0x7F}; + + char buffer0[1]{}; + datastream ds0{buffer0, 1}; + + // ds0 << ci; + + // REQUIRE_EQUAL( memcmp(expected_buffer0, buffer0, 1), 0 ) // ----------------------------- // template // friend DataStream& operator>>(DataStream&, signed_int&) + signed_int i{}; + const char expected_buffer1[1]{0x7F}; + + char buffer1[1]{0x7F}; + datastream ds1{buffer1, 1}; + + ds1 >> i; + + REQUIRE_EQUAL( memcmp(expected_buffer1, buffer1, 1), 0 ) silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { + EOSIO_TEST(unsigned_int_type_test) EOSIO_TEST(signed_int_type_test); - // EOSIO_TEST(signed_int_type_test); return has_failed(); } From 253297a39d44d5ce4787978e93b61cb68eee8def Mon Sep 17 00:00:00 2001 From: johndebord Date: Tue, 29 Jan 2019 00:39:48 -0500 Subject: [PATCH 19/51] Continuing work on `fixed_bytes` tests --- tests/unit/fixed_bytes_tests.cpp | 144 +++++++++++++++++++++++-------- 1 file changed, 108 insertions(+), 36 deletions(-) diff --git a/tests/unit/fixed_bytes_tests.cpp b/tests/unit/fixed_bytes_tests.cpp index cf33a76c91..563d1ba22b 100644 --- a/tests/unit/fixed_bytes_tests.cpp +++ b/tests/unit/fixed_bytes_tests.cpp @@ -2,44 +2,116 @@ #include using eosio::fixed_bytes; +using std::array; using namespace eosio::native; +// Definitions in `eosio.cdt/libraries/eosiolib/fixed_bytes.hpp` EOSIO_TEST_BEGIN(fixed_bytes_test) silence_output(false); +// ------------ // constructors -/// fixed_bytes(const std::array&)d -// static constexpr size_t num_words() -REQUIRE_EQUAL( (fixed_bytes<0>{std::array{}}.num_words()), 0 ) -REQUIRE_EQUAL( (fixed_bytes<1>{std::array{}}.num_words()), 1 ) -REQUIRE_EQUAL( (fixed_bytes<32>{std::array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<512>{std::array{}}.num_words()), 32 ) - /// constexpr fixed_bytes() -REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), 0 ) -REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), 1 ) -REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), 2 ) -REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), 32 ) +// static constexpr size_t padded_bytes() +REQUIRE_EQUAL( fixed_bytes<20>{}.padded_bytes(), 12 ) +REQUIRE_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) +REQUIRE_EQUAL( fixed_bytes<64>{}.padded_bytes(), 0 ) -/// fixed_bytes(const Word(&arr)[NumWords]) -// const uint128_t arr0[0]{}; -// const uint128_t arr1[1]{}; -// const uint128_t arr2[2]{}; -// const uint128_t arr3[32]{}; +// eosio::print(fixed_bytes<20>{}.num_words()); +// eosio::print(fixed_bytes<32>{}.num_words()); +// eosio::print(fixed_bytes<64>{}.num_words(), "\n"); -// static constexpr size_t padded_bytes() -REQUIRE_EQUAL( fixed_bytes<0>{}.padded_bytes(), 0 ) -REQUIRE_EQUAL( fixed_bytes<1>{}.padded_bytes(), 15 ) -REQUIRE_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) -REQUIRE_EQUAL( fixed_bytes<512>{}.padded_bytes(), 0 ) +// eosio::print(fixed_bytes<20>{array{}}.num_words()); +// eosio::print(fixed_bytes<32>{array{}}.num_words()); +// eosio::print(fixed_bytes<64>{array{}}.num_words(), "\n"); + +/// fixed_bytes(const std::array&) +// static constexpr size_t num_words() +REQUIRE_EQUAL( (fixed_bytes<20>{array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32>{array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64>{array{}}.num_words()), 4 ) + +// /// fixed_bytes(const std::array&) +REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +// eosio::print(fixed_bytes<20>{std::array{}}.num_words()); +// eosio::print(fixed_bytes<32>{std::array{}}.num_words()); +// eosio::print(fixed_bytes<64>{std::array{}}.num_words()); +// eosio::print(fixed_bytes<128>{std::array{}}.num_words()); +// eosio::print(fixed_bytes<256>{std::array{}}.num_words()); +// REQUIRE_EQUAL( (fixed_bytes<32>{std::array{}}.num_words()), 1 ) +// REQUIRE_EQUAL( (fixed_bytes<64>{std::array{}}.num_words()), 1 ) +// REQUIRE_EQUAL( (fixed_bytes<128>{std::array{}}.num_words()), 1 ) -// const auto& get_array()const -REQUIRE_EQUAL( (fixed_bytes<0>{}.get_array()), (std::array{}) ) -REQUIRE_EQUAL( (fixed_bytes<1>{}.get_array()), (std::array{}) ) -REQUIRE_EQUAL( (fixed_bytes<32>{}.get_array()), (std::array{}) ) -REQUIRE_EQUAL( (fixed_bytes<512>{}.get_array()), (std::array{}) ) +/// fixed_bytes(const Word(&arr)[NumWords]) +const uint8_t arr8[1]{}; +const uint16_t arr16[1]{}; +const uint32_t arr32[1]{}; +const uint64_t arr64[1]{}; + +REQUIRE_EQUAL( (fixed_bytes<20> {arr8}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {arr8}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {arr8}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{arr8}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{arr8}.num_words()), 16 ) + +REQUIRE_EQUAL( (fixed_bytes<20> {arr16}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {arr16}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {arr16}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{arr16}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{arr16}.num_words()), 16 ) + +REQUIRE_EQUAL( (fixed_bytes<20> {arr32}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {arr32}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {arr32}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{arr32}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{arr32}.num_words()), 16 ) + +REQUIRE_EQUAL( (fixed_bytes<20> {arr64}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<32> {arr64}.num_words()), 2 ) +REQUIRE_EQUAL( (fixed_bytes<64> {arr64}.num_words()), 4 ) +REQUIRE_EQUAL( (fixed_bytes<128>{arr64}.num_words()), 8 ) +REQUIRE_EQUAL( (fixed_bytes<256>{arr64}.num_words()), 16 ) + +// make_from_word_sequence(FirstWord, Rest...) +array arr{1ULL,2ULL,3ULL,4ULL}; +fixed_bytes<32> fb1{fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)}; +fixed_bytes<32> fb2{arr}; + +REQUIRE_EQUAL( (fixed_bytes<20>::make_from_word_sequence(1ULL)), + (fixed_bytes<20>{array{1ULL}}) ) +REQUIRE_EQUAL( (fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)), + (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}) ) + +// // const auto& get_array()const +// REQUIRE_EQUAL( (fixed_bytes<0>{}.get_array()), (std::array{}) ) +// REQUIRE_EQUAL( (fixed_bytes<1>{}.get_array()), (std::array{}) ) +// REQUIRE_EQUAL( (fixed_bytes<32>{}.get_array()), (std::array{}) ) +// REQUIRE_EQUAL( (fixed_bytes<512>{}.get_array()), (std::array{}) ) // eosio::print((fixed_bytes<2>{1}.data())); // eosio::print("\n"); @@ -47,16 +119,16 @@ REQUIRE_EQUAL( (fixed_bytes<512>{}.get_array()), (std::array{}) ) // eosio::print("\n"); // auto data() -std::array arr0{}; -std::array arr1{}; -std::array arr2{}; -std::array arr3{}; -fixed_bytes<0> fb0(arr0); -fixed_bytes<1> fb1{arr1}; -fixed_bytes<32> fb2{arr2}; -fixed_bytes<512> fb3{arr3}; - -REQUIRE_EQUAL( fb0.data(), arr0.data() ) +// std::array arr0{}; +// std::array arr1{}; +// std::array arr2{}; +// std::array arr3{}; +// fixed_bytes<0> fb0(arr0); +// fixed_bytes<1> fb1{arr1}; +// fixed_bytes<32> fb2{arr2}; +// fixed_bytes<512> fb3{arr3}; + +// REQUIRE_EQUAL( fb0.data(), arr0.data() ) // REQUIRE_EQUAL( (fixed_bytes<0>{}.data()), uint128_t*(std::array{0}) ) // REQUIRE_EQUAL( fixed_bytes<1>{}.data(), 1 ) // REQUIRE_EQUAL( fixed_bytes<32>{}.data(), 2 ) From 751f9fec344b798ed729193b1fb2762522de323a Mon Sep 17 00:00:00 2001 From: johndebord Date: Tue, 29 Jan 2019 17:18:22 -0500 Subject: [PATCH 20/51] Add `binary_extension` tests --- tests/unit/binary_extension_tests.cpp | 207 ++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 tests/unit/binary_extension_tests.cpp diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp new file mode 100644 index 0000000000..4153e8b5b0 --- /dev/null +++ b/tests/unit/binary_extension_tests.cpp @@ -0,0 +1,207 @@ +#include +#include + +using eosio::binary_extension; + +using namespace eosio::native; + +// TODO: come up with more descriptive test variable names + +// Definitions in `eosio.cdt/libraries/eosiolib/binary_extension.hpp` +EOSIO_TEST_BEGIN(binary_extension_test) + silence_output(false); + + /// constexpr binary_extension() + // constexpr bool has_value()const + REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) ///// Should I test more than just the `const char*` ///// + + REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) ///// Should I test more than just the `const char*` ///// + + /// constexpr binary_extension(const T&) + REQUIRE_EQUAL( (binary_extension{'c'}.has_value()), true ) + + REQUIRE_EQUAL( (binary_extension{"abcd"}.has_value()), true ) + + /// constexpr binary_extension(T&&) + REQUIRE_EQUAL( (binary_extension{std::move('c')}.has_value()), true ) + + REQUIRE_EQUAL( (binary_extension{std::move("abcd")}.has_value()), true ) + + /// constexpr binary_extension(std::in_place_t, Args&&...) + REQUIRE_EQUAL( (binary_extension{std::in_place, 'c'}.has_value()), true ) ///// How do I pack more args into this?? ///// + + REQUIRE_EQUAL( (binary_extension{std::in_place, "abcd"}.has_value()), true ) ///// How do I pack more args into this?? ///// + + // ~binary_extension() + binary_extension be_destruct_char{'c'}; + REQUIRE_EQUAL( be_destruct_char.has_value(), true ) + be_destruct_char.~binary_extension(); + REQUIRE_EQUAL( be_destruct_char.has_value(), false ) + + binary_extension be_destruct{"abcd"}; + REQUIRE_EQUAL( be_destruct.has_value(), true ) + be_destruct.~binary_extension(); + REQUIRE_EQUAL( be_destruct.has_value(), false ) + + // constexpr binary_extension(const binary_extension&) + binary_extension be_copy_char_lval0{'c'}; + binary_extension be_copy_char_lval1{be_copy_char_lval0}; + REQUIRE_EQUAL( (be_copy_char_lval0.value() == be_copy_char_lval1.value() && be_copy_char_lval0.has_value() == be_copy_char_lval1.has_value()), true) + + binary_extension be_copy_lval0{"abcd"}; + binary_extension be_copy_lval1{be_copy_lval0}; + REQUIRE_EQUAL( (be_copy_lval0.value() == be_copy_lval1.value() && be_copy_lval0.has_value() == be_copy_lval1.has_value()), true) + + // constexpr binary_extension(binary_extension&&) + binary_extension be_copy_char_rval0{'c'}; + REQUIRE_EQUAL( be_copy_char_rval0.has_value(), true ) + binary_extension be_copy_char_rval1{std::move(be_copy_char_rval0)}; + REQUIRE_EQUAL( be_copy_char_rval0.has_value(), false ) + REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){be_copy_char_rval0.value();} ) + + binary_extension be_copy_rval0{"abcd"}; + REQUIRE_EQUAL( be_copy_rval0.has_value(), true ) + binary_extension be_copy_rval1{std::move(be_copy_rval0)}; + REQUIRE_EQUAL( be_copy_rval0.has_value(), false ) + REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){be_copy_rval0.value();} ) + + // constexpr explicit operator bool()const + REQUIRE_EQUAL( (binary_extension{}.operator bool()), false ) + REQUIRE_EQUAL( (binary_extension{0}.operator bool()), true ) + REQUIRE_EQUAL( (binary_extension{'c'}.operator bool()), true ) + + REQUIRE_EQUAL( (binary_extension{}.operator bool()), false ) + REQUIRE_EQUAL( (binary_extension{nullptr}.operator bool()), true ) + REQUIRE_EQUAL( (binary_extension{"abcd"}.operator bool()), true ) + + // constexpr T& value()& + binary_extension be_char_value0{'c'}; + REQUIRE_EQUAL( (be_char_value0.value() == 'c'), true ) + REQUIRE_EQUAL( (be_char_value0.value() == 'd'), false ) + + binary_extension be_value0{"abcd"}; + REQUIRE_EQUAL( (be_value0.value() == "abcd"), true ) + REQUIRE_EQUAL( (be_value0.value() == "efgh"), false ) + + // constexpr const T& value()const& + binary_extension be_char_const_value0{'c'}; + REQUIRE_EQUAL( (be_char_const_value0.value() == 'c'), true ) + REQUIRE_EQUAL( (be_char_const_value0.value() == 'd'), false ) + + binary_extension be_const_value0{"abcd"}; + REQUIRE_EQUAL( (be_const_value0.value() == "abcd"), true ) + REQUIRE_EQUAL( (be_const_value0.value() == "efgh"), false ) + + // constexpr auto value_or(U&&) -> std::enable_if_t::value, T&>& + binary_extension be_temp_value_or{}; + // REQUIRE_EQUAL( (be_temp_value_or = be_temp_value_or.value_or(binary_extension{0x63}) == 0x63), true ) + + // constexpr T&& value_or()&& + REQUIRE_EQUAL( (binary_extension{'c'}.value_or() == 'c'), true ) + + binary_extension be_value_or_rval{"abcd"}; + REQUIRE_EQUAL( (be_value_or_rval.value_or() == "abcd"), true ) + + // constexpr const T&& value_or()const&& + REQUIRE_EQUAL( (binary_extension{'c'}.value_or() == 'c'), true ) + + binary_extension be_const_value_or_rval{"abcd"}; + REQUIRE_EQUAL( (be_const_value_or_rval.value_or() == "abcd"), true ) + + // constexpr T value_or()& + binary_extension be_char_value_or_val{'c'}; + REQUIRE_EQUAL( (be_char_value_or_val.value_or() == 'c'), true ) + + binary_extension be_value_or_val{"abcd"}; + REQUIRE_EQUAL( (be_value_or_val.value_or() == "abcd"), true ) + + // constexpr T value_or()const& + binary_extension be_char_const_value_or_val{'c'}; + REQUIRE_EQUAL( (be_char_const_value_or_val.value_or() == 'c'), true ) + + binary_extension be_const_value_or_val{"abcd"}; + REQUIRE_EQUAL( (be_const_value_or_val.value_or() == "abcd"), true ) + + // constexpr T* operator->() + binary_extension be_char_arrow_op{'c'}; + REQUIRE_EQUAL( *be_char_arrow_op.operator->() == 'c', true ) + REQUIRE_EQUAL( *be_char_arrow_op.operator->() != 'd', true ) + + binary_extension be_arrow_op{"abcd"}; + REQUIRE_EQUAL( *be_arrow_op.operator->() == "abcd", true ) + REQUIRE_EQUAL( *be_arrow_op.operator->() != "efgh", true ) + + // constexpr const T* operator->()const + binary_extension be_char_const_arrow_op{'c'}; + REQUIRE_EQUAL( *be_char_const_arrow_op.operator->() == 'c', true ) + REQUIRE_EQUAL( *be_char_const_arrow_op.operator->() != 'd', true ) + + binary_extension be_const_arrow_op{"abcd"}; + REQUIRE_EQUAL( *be_const_arrow_op.operator->() == "abcd", true ) + REQUIRE_EQUAL( *be_const_arrow_op.operator->() != "efgh", true ) + + // constexpr T& operator*()& + binary_extension be_char_star_op{'c'}; + REQUIRE_EQUAL( be_char_star_op.operator*() == 'c', true ) + REQUIRE_EQUAL( be_char_star_op.operator*() != 'd', true ) + + binary_extension be_star_op{"abcd"}; + REQUIRE_EQUAL( be_star_op.operator*() == "abcd", true ) + REQUIRE_EQUAL( be_star_op.operator*() != "efgh", true ) + + // constexpr const T& operator*()const& + binary_extension be_char_const_star_op{'c'}; + REQUIRE_EQUAL( be_char_const_star_op.operator*() == 'c', true ) + REQUIRE_EQUAL( be_char_const_star_op.operator*() != 'd', true ) + + binary_extension be_const_star_op{"abcd"}; + REQUIRE_EQUAL( be_const_star_op.operator*() == "abcd", true ) + REQUIRE_EQUAL( be_const_star_op.operator*() != "efgh", true ) + + // constexpr T&& operator*()&& + binary_extension be_char_rval_star_op{'c'}; + REQUIRE_EQUAL( std::move(be_char_rval_star_op.operator*()) == 'c', true ) + REQUIRE_EQUAL( std::move(be_char_rval_star_op.operator*()) != 'd', true ) + + binary_extension be_rval_star_op{"abcd"}; + REQUIRE_EQUAL( std::move(be_rval_star_op.operator*()) == "abcd", true ) + REQUIRE_EQUAL( std::move(be_rval_star_op.operator*()) != "efgh", true ) + + // constexpr const T&& operator*()const&& + binary_extension be_char_const_rval_star_op{'c'}; + REQUIRE_EQUAL( std::move(be_char_const_rval_star_op.operator*()) == 'c', true ) + REQUIRE_EQUAL( std::move(be_char_const_rval_star_op.operator*()) != 'd', true ) + + binary_extension be_const_rval_star_op{"abcd"}; + REQUIRE_EQUAL( std::move(be_const_rval_star_op.operator*()) == "abcd", true ) + REQUIRE_EQUAL( std::move(be_const_rval_star_op.operator*()) != "efgh", true ) + + // T& emplace(Args&& ... args)& + binary_extension be_char_emplace{'c'}; + be_char_emplace.emplace(std::move('d')); + REQUIRE_EQUAL( be_char_emplace.value() == 'd', true ) + REQUIRE_EQUAL( be_char_emplace.value() != 'c', true ) + + binary_extension be_emplace{"abcd"}; + be_emplace.emplace(std::move("efgh")); + REQUIRE_EQUAL( be_emplace.value() == "efgh", true ) + REQUIRE_EQUAL( be_emplace.value() != "abcd", true ) + + // void reset() + binary_extension be_char_reset{'c'}; + REQUIRE_EQUAL( be_char_reset.has_value(), true ) + be_char_reset.reset(); + REQUIRE_EQUAL( be_char_reset.has_value(), false ) + + binary_extension be_reset{"abcd"}; + REQUIRE_EQUAL( be_reset.has_value(), true ) + be_reset.reset(); + REQUIRE_EQUAL( be_reset.has_value(), false ) + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char* argv[]) { + EOSIO_TEST(binary_extension_test); + return has_failed(); +} From 0aab6459c2a7a23bfef746ca4d3205af06bc5d16 Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 30 Jan 2019 10:25:46 -0500 Subject: [PATCH 21/51] Continuation --- tests/unit/binary_extension_tests.cpp | 5 +++-- tests/unit/datastream_tests.cpp | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index 4153e8b5b0..4fcd9cf25c 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -93,8 +93,9 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( (be_const_value0.value() == "efgh"), false ) // constexpr auto value_or(U&&) -> std::enable_if_t::value, T&>& - binary_extension be_temp_value_or{}; - // REQUIRE_EQUAL( (be_temp_value_or = be_temp_value_or.value_or(binary_extension{0x63}) == 0x63), true ) + binary_extension be_temp_value_or{}; + uint16_t some_value = be_temp_value_or.value_or(uint16_t{0x63}); + REQUIRE_EQUAL( (some_value == 0x63), true ) // constexpr T&& value_or()&& REQUIRE_EQUAL( (binary_extension{'c'}.value_or() == 'c'), true ) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index b181d57406..14af7da47a 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -12,7 +12,7 @@ using namespace eosio::native; // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_test) - silence_output(true); + silence_output(false); char datastream_buffer[256]{}; // Buffer for the datastream to point to char buffer[256]{}; // Buffer to act upon for testing @@ -29,6 +29,11 @@ EOSIO_TEST_BEGIN(datastream_test) ds.skip(1); REQUIRE_EQUAL( ds.pos() == datastream_buffer+1, true ) ds.skip(-1); +volatile int* ip = (int*)(200); +size_t ss = -1; +eosio::print_f("ip : %\n", (size_t)ip); +ip += ss; +eosio::print_f("ip : %\n", (size_t)ip); REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) // inline bool read(char*, size_t) @@ -55,7 +60,6 @@ EOSIO_TEST_BEGIN(datastream_test) REQUIRE_ASSERT( "write", ([&]() {ds.write(buffer, 1);}) ) - // I'm getting the error: `read-only variable is not assignable *_pos = c;` // inline bool put(char) ds.seekp(0); REQUIRE_EQUAL( ds.put('c'), true ) @@ -189,7 +193,9 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(false); - char datastream_buffer[8+sizeof(unsigned_int)]{}; // Buffer for the datastream to point to + static constexpr size_t elements{8}; + unsigned_int ui{elements}; + char datastream_buffer[256]{}; // Buffer for the datastream to point to char buffer[8]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; // Buffer to act upon for testing // --------- @@ -199,7 +205,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) datastream ds{datastream_buffer, 8+sizeof(unsigned_int)}; - for(int i{0}; i < 17; ++i) + for(int i{0}; i < 16; ++i) eosio::print(datastream_buffer[i]); eosio::print("\n"); for(int i{0}; i < 8; ++i) @@ -227,7 +233,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) eosio::print("\n"); - for(int i{0}; i < 17; ++i) + for(int i{0}; i < 16; ++i) eosio::print(datastream_buffer[i]); eosio::print("\n"); for(int i{0}; i < 8; ++i) @@ -251,6 +257,8 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // ---------- // std::deque + // Make custom small struct; use default constructor; spit in/spit out. Then compare + // the value in the default constructor (expected) to result. And also put custom input to .value_or as well // ---------------- // binary_extension @@ -303,7 +311,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) EOSIO_TEST_END int main(int argc, char* argv[]) { - //EOSIO_TEST(datastream_test); + // EOSIO_TEST(datastream_test); // EOSIO_TEST(datastream_specialization_test); EOSIO_TEST(datastream_stream_test); return has_failed(); From b5d8b4349e0a30dea43c1b84bf1d1b0a7d7629ab Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 30 Jan 2019 16:25:32 -0500 Subject: [PATCH 22/51] Continuation of `datastream` tests --- tests/unit/binary_extension_tests.cpp | 215 +++++++++++++------------- tests/unit/datastream_tests.cpp | 159 ++++++++++++------- tests/unit/varint_tests.cpp | 21 ++- 3 files changed, 222 insertions(+), 173 deletions(-) diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index 4fcd9cf25c..b298d256c0 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -5,8 +5,6 @@ using eosio::binary_extension; using namespace eosio::native; -// TODO: come up with more descriptive test variable names - // Definitions in `eosio.cdt/libraries/eosiolib/binary_extension.hpp` EOSIO_TEST_BEGIN(binary_extension_test) silence_output(false); @@ -33,37 +31,38 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( (binary_extension{std::in_place, "abcd"}.has_value()), true ) ///// How do I pack more args into this?? ///// // ~binary_extension() - binary_extension be_destruct_char{'c'}; - REQUIRE_EQUAL( be_destruct_char.has_value(), true ) - be_destruct_char.~binary_extension(); - REQUIRE_EQUAL( be_destruct_char.has_value(), false ) + binary_extension bin_destruct_char{'c'}; + REQUIRE_EQUAL( bin_destruct_char.has_value(), true ) + bin_destruct_char.~binary_extension(); + REQUIRE_EQUAL( bin_destruct_char.has_value(), false ) - binary_extension be_destruct{"abcd"}; - REQUIRE_EQUAL( be_destruct.has_value(), true ) - be_destruct.~binary_extension(); - REQUIRE_EQUAL( be_destruct.has_value(), false ) + binary_extension bin_destruct_str{"abcd"}; + REQUIRE_EQUAL( bin_destruct_str.has_value(), true ) + bin_destruct_str.~binary_extension(); + REQUIRE_EQUAL( bin_destruct_str.has_value(), false ) // constexpr binary_extension(const binary_extension&) - binary_extension be_copy_char_lval0{'c'}; - binary_extension be_copy_char_lval1{be_copy_char_lval0}; - REQUIRE_EQUAL( (be_copy_char_lval0.value() == be_copy_char_lval1.value() && be_copy_char_lval0.has_value() == be_copy_char_lval1.has_value()), true) + binary_extension bin_copy_char_lval0{'c'}; + binary_extension bin_copy_char_lval1{bin_copy_char_lval0}; + REQUIRE_EQUAL( (bin_copy_char_lval0.value() == bin_copy_char_lval1.value() && + bin_copy_char_lval0.has_value() == bin_copy_char_lval1.has_value()), true) - binary_extension be_copy_lval0{"abcd"}; - binary_extension be_copy_lval1{be_copy_lval0}; - REQUIRE_EQUAL( (be_copy_lval0.value() == be_copy_lval1.value() && be_copy_lval0.has_value() == be_copy_lval1.has_value()), true) + binary_extension bin_copy_str_lval0{"abcd"}; + binary_extension bin_copy_str_lval1{bin_copy_str_lval0}; + REQUIRE_EQUAL( (bin_copy_str_lval0.value() == bin_copy_str_lval1.value() && bin_copy_str_lval0.has_value() == bin_copy_str_lval1.has_value()), true) // constexpr binary_extension(binary_extension&&) - binary_extension be_copy_char_rval0{'c'}; - REQUIRE_EQUAL( be_copy_char_rval0.has_value(), true ) - binary_extension be_copy_char_rval1{std::move(be_copy_char_rval0)}; - REQUIRE_EQUAL( be_copy_char_rval0.has_value(), false ) - REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){be_copy_char_rval0.value();} ) - - binary_extension be_copy_rval0{"abcd"}; - REQUIRE_EQUAL( be_copy_rval0.has_value(), true ) - binary_extension be_copy_rval1{std::move(be_copy_rval0)}; - REQUIRE_EQUAL( be_copy_rval0.has_value(), false ) - REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){be_copy_rval0.value();} ) + binary_extension bin_copy_char_rval0{'c'}; + REQUIRE_EQUAL( bin_copy_char_rval0.has_value(), true ) + binary_extension bin_copy_char_rval1{std::move(bin_copy_char_rval0)}; + REQUIRE_EQUAL( bin_copy_char_rval0.has_value(), false ) + REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){bin_copy_char_rval0.value();} ) + + binary_extension bin_copy_str_rval0{"abcd"}; + REQUIRE_EQUAL( bin_copy_str_rval0.has_value(), true ) + binary_extension bin_copy_str_rval1{std::move(bin_copy_str_rval0)}; + REQUIRE_EQUAL( bin_copy_str_rval0.has_value(), false ) + REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){bin_copy_str_rval0.value();} ) // constexpr explicit operator bool()const REQUIRE_EQUAL( (binary_extension{}.operator bool()), false ) @@ -75,129 +74,129 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( (binary_extension{"abcd"}.operator bool()), true ) // constexpr T& value()& - binary_extension be_char_value0{'c'}; - REQUIRE_EQUAL( (be_char_value0.value() == 'c'), true ) - REQUIRE_EQUAL( (be_char_value0.value() == 'd'), false ) + binary_extension bin_char_value0{'c'}; + REQUIRE_EQUAL( (bin_char_value0.value() == 'c'), true ) + REQUIRE_EQUAL( (bin_char_value0.value() == 'd'), false ) - binary_extension be_value0{"abcd"}; - REQUIRE_EQUAL( (be_value0.value() == "abcd"), true ) - REQUIRE_EQUAL( (be_value0.value() == "efgh"), false ) + binary_extension bin_str_value0{"abcd"}; + REQUIRE_EQUAL( (bin_str_value0.value() == "abcd"), true ) + REQUIRE_EQUAL( (bin_str_value0.value() == "efgh"), false ) // constexpr const T& value()const& - binary_extension be_char_const_value0{'c'}; - REQUIRE_EQUAL( (be_char_const_value0.value() == 'c'), true ) - REQUIRE_EQUAL( (be_char_const_value0.value() == 'd'), false ) + binary_extension bin_const_char_value0{'c'}; + REQUIRE_EQUAL( (bin_const_char_value0.value() == 'c'), true ) + REQUIRE_EQUAL( (bin_const_char_value0.value() == 'd'), false ) - binary_extension be_const_value0{"abcd"}; - REQUIRE_EQUAL( (be_const_value0.value() == "abcd"), true ) - REQUIRE_EQUAL( (be_const_value0.value() == "efgh"), false ) + binary_extension bin_const_str_value0{"abcd"}; + REQUIRE_EQUAL( (bin_const_str_value0.value() == "abcd"), true ) + REQUIRE_EQUAL( (bin_const_str_value0.value() == "efgh"), false ) // constexpr auto value_or(U&&) -> std::enable_if_t::value, T&>& - binary_extension be_temp_value_or{}; - uint16_t some_value = be_temp_value_or.value_or(uint16_t{0x63}); + binary_extension bin_temp_value_or{}; + uint8_t some_value = bin_temp_value_or.value_or(uint8_t{0x63}); REQUIRE_EQUAL( (some_value == 0x63), true ) // constexpr T&& value_or()&& REQUIRE_EQUAL( (binary_extension{'c'}.value_or() == 'c'), true ) - binary_extension be_value_or_rval{"abcd"}; - REQUIRE_EQUAL( (be_value_or_rval.value_or() == "abcd"), true ) + binary_extension bin_value_or_rval{"abcd"}; + REQUIRE_EQUAL( (bin_value_or_rval.value_or() == "abcd"), true ) // constexpr const T&& value_or()const&& REQUIRE_EQUAL( (binary_extension{'c'}.value_or() == 'c'), true ) - binary_extension be_const_value_or_rval{"abcd"}; - REQUIRE_EQUAL( (be_const_value_or_rval.value_or() == "abcd"), true ) + binary_extension bin_const_value_or_rval{"abcd"}; + REQUIRE_EQUAL( (bin_const_value_or_rval.value_or() == "abcd"), true ) // constexpr T value_or()& - binary_extension be_char_value_or_val{'c'}; - REQUIRE_EQUAL( (be_char_value_or_val.value_or() == 'c'), true ) + binary_extension bin_char_value_or_val{'c'}; + REQUIRE_EQUAL( (bin_char_value_or_val.value_or() == 'c'), true ) - binary_extension be_value_or_val{"abcd"}; - REQUIRE_EQUAL( (be_value_or_val.value_or() == "abcd"), true ) + binary_extension bin_value_or_val{"abcd"}; + REQUIRE_EQUAL( (bin_value_or_val.value_or() == "abcd"), true ) // constexpr T value_or()const& - binary_extension be_char_const_value_or_val{'c'}; - REQUIRE_EQUAL( (be_char_const_value_or_val.value_or() == 'c'), true ) + binary_extension bin_char_const_value_or_val{'c'}; + REQUIRE_EQUAL( (bin_char_const_value_or_val.value_or() == 'c'), true ) - binary_extension be_const_value_or_val{"abcd"}; - REQUIRE_EQUAL( (be_const_value_or_val.value_or() == "abcd"), true ) + binary_extension bin_const_value_or_val{"abcd"}; + REQUIRE_EQUAL( (bin_const_value_or_val.value_or() == "abcd"), true ) // constexpr T* operator->() - binary_extension be_char_arrow_op{'c'}; - REQUIRE_EQUAL( *be_char_arrow_op.operator->() == 'c', true ) - REQUIRE_EQUAL( *be_char_arrow_op.operator->() != 'd', true ) + binary_extension bin_char_arrow_op{'c'}; + REQUIRE_EQUAL( *bin_char_arrow_op.operator->() == 'c', true ) + REQUIRE_EQUAL( *bin_char_arrow_op.operator->() != 'd', true ) - binary_extension be_arrow_op{"abcd"}; - REQUIRE_EQUAL( *be_arrow_op.operator->() == "abcd", true ) - REQUIRE_EQUAL( *be_arrow_op.operator->() != "efgh", true ) + binary_extension bin_arrow_op{"abcd"}; + REQUIRE_EQUAL( *bin_arrow_op.operator->() == "abcd", true ) + REQUIRE_EQUAL( *bin_arrow_op.operator->() != "efgh", true ) // constexpr const T* operator->()const - binary_extension be_char_const_arrow_op{'c'}; - REQUIRE_EQUAL( *be_char_const_arrow_op.operator->() == 'c', true ) - REQUIRE_EQUAL( *be_char_const_arrow_op.operator->() != 'd', true ) + binary_extension bin_char_const_arrow_op{'c'}; + REQUIRE_EQUAL( *bin_char_const_arrow_op.operator->() == 'c', true ) + REQUIRE_EQUAL( *bin_char_const_arrow_op.operator->() != 'd', true ) - binary_extension be_const_arrow_op{"abcd"}; - REQUIRE_EQUAL( *be_const_arrow_op.operator->() == "abcd", true ) - REQUIRE_EQUAL( *be_const_arrow_op.operator->() != "efgh", true ) + binary_extension bin_const_arrow_op{"abcd"}; + REQUIRE_EQUAL( *bin_const_arrow_op.operator->() == "abcd", true ) + REQUIRE_EQUAL( *bin_const_arrow_op.operator->() != "efgh", true ) // constexpr T& operator*()& - binary_extension be_char_star_op{'c'}; - REQUIRE_EQUAL( be_char_star_op.operator*() == 'c', true ) - REQUIRE_EQUAL( be_char_star_op.operator*() != 'd', true ) + binary_extension bin_char_star_op{'c'}; + REQUIRE_EQUAL( bin_char_star_op.operator*() == 'c', true ) + REQUIRE_EQUAL( bin_char_star_op.operator*() != 'd', true ) - binary_extension be_star_op{"abcd"}; - REQUIRE_EQUAL( be_star_op.operator*() == "abcd", true ) - REQUIRE_EQUAL( be_star_op.operator*() != "efgh", true ) + binary_extension bin_star_op{"abcd"}; + REQUIRE_EQUAL( bin_star_op.operator*() == "abcd", true ) + REQUIRE_EQUAL( bin_star_op.operator*() != "efgh", true ) // constexpr const T& operator*()const& - binary_extension be_char_const_star_op{'c'}; - REQUIRE_EQUAL( be_char_const_star_op.operator*() == 'c', true ) - REQUIRE_EQUAL( be_char_const_star_op.operator*() != 'd', true ) + binary_extension bin_char_const_star_op{'c'}; + REQUIRE_EQUAL( bin_char_const_star_op.operator*() == 'c', true ) + REQUIRE_EQUAL( bin_char_const_star_op.operator*() != 'd', true ) - binary_extension be_const_star_op{"abcd"}; - REQUIRE_EQUAL( be_const_star_op.operator*() == "abcd", true ) - REQUIRE_EQUAL( be_const_star_op.operator*() != "efgh", true ) + binary_extension bin_const_star_op{"abcd"}; + REQUIRE_EQUAL( bin_const_star_op.operator*() == "abcd", true ) + REQUIRE_EQUAL( bin_const_star_op.operator*() != "efgh", true ) // constexpr T&& operator*()&& - binary_extension be_char_rval_star_op{'c'}; - REQUIRE_EQUAL( std::move(be_char_rval_star_op.operator*()) == 'c', true ) - REQUIRE_EQUAL( std::move(be_char_rval_star_op.operator*()) != 'd', true ) + binary_extension bin_char_rval_star_op{'c'}; + REQUIRE_EQUAL( std::move(bin_char_rval_star_op.operator*()) == 'c', true ) + REQUIRE_EQUAL( std::move(bin_char_rval_star_op.operator*()) != 'd', true ) - binary_extension be_rval_star_op{"abcd"}; - REQUIRE_EQUAL( std::move(be_rval_star_op.operator*()) == "abcd", true ) - REQUIRE_EQUAL( std::move(be_rval_star_op.operator*()) != "efgh", true ) + binary_extension bin_str_rval_star_op{"abcd"}; + REQUIRE_EQUAL( std::move(bin_str_rval_star_op.operator*()) == "abcd", true ) + REQUIRE_EQUAL( std::move(bin_str_rval_star_op.operator*()) != "efgh", true ) // constexpr const T&& operator*()const&& - binary_extension be_char_const_rval_star_op{'c'}; - REQUIRE_EQUAL( std::move(be_char_const_rval_star_op.operator*()) == 'c', true ) - REQUIRE_EQUAL( std::move(be_char_const_rval_star_op.operator*()) != 'd', true ) - - binary_extension be_const_rval_star_op{"abcd"}; - REQUIRE_EQUAL( std::move(be_const_rval_star_op.operator*()) == "abcd", true ) - REQUIRE_EQUAL( std::move(be_const_rval_star_op.operator*()) != "efgh", true ) - + binary_extension bin_const_char_rval_star_op{'c'}; + REQUIRE_EQUAL( std::move(bin_const_char_rval_star_op.operator*()) == 'c', true ) + REQUIRE_EQUAL( std::move(bin_const_char_rval_star_op.operator*()) != 'd', true ) + + binary_extension bin_const_str_rval_star_op{"abcd"}; + REQUIRE_EQUAL( std::move(bin_const_str_rval_star_op.operator*()) == "abcd", true ) + REQUIRE_EQUAL( std::move(bin_const_str_rval_star_op.operator*()) != "efgh", true ) + // T& emplace(Args&& ... args)& - binary_extension be_char_emplace{'c'}; - be_char_emplace.emplace(std::move('d')); - REQUIRE_EQUAL( be_char_emplace.value() == 'd', true ) - REQUIRE_EQUAL( be_char_emplace.value() != 'c', true ) + binary_extension bin_char_emplace{'c'}; + bin_char_emplace.emplace(std::move('d')); + REQUIRE_EQUAL( bin_char_emplace.value() == 'd', true ) + REQUIRE_EQUAL( bin_char_emplace.value() != 'c', true ) - binary_extension be_emplace{"abcd"}; - be_emplace.emplace(std::move("efgh")); - REQUIRE_EQUAL( be_emplace.value() == "efgh", true ) - REQUIRE_EQUAL( be_emplace.value() != "abcd", true ) + binary_extension bin_str_emplace{"abcd"}; + bin_str_emplace.emplace(std::move("efgh")); + REQUIRE_EQUAL( bin_str_emplace.value() == "efgh", true ) + REQUIRE_EQUAL( bin_str_emplace.value() != "abcd", true ) // void reset() - binary_extension be_char_reset{'c'}; - REQUIRE_EQUAL( be_char_reset.has_value(), true ) - be_char_reset.reset(); - REQUIRE_EQUAL( be_char_reset.has_value(), false ) - - binary_extension be_reset{"abcd"}; - REQUIRE_EQUAL( be_reset.has_value(), true ) - be_reset.reset(); - REQUIRE_EQUAL( be_reset.has_value(), false ) + binary_extension bin_char_reset{'c'}; + REQUIRE_EQUAL( bin_char_reset.has_value(), true ) + bin_char_reset.reset(); + REQUIRE_EQUAL( bin_char_reset.has_value(), false ) + + binary_extension bin_str_reset{"abcd"}; + REQUIRE_EQUAL( bin_str_reset.has_value(), true ) + bin_str_reset.reset(); + REQUIRE_EQUAL( bin_str_reset.has_value(), false ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 14af7da47a..fb2237853c 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,11 +1,17 @@ +// eosio-cpp -fnative -w datastream_tests.cpp; ./a.out + #include #include +#include #include #include #include using eosio::datastream; +using std::begin; +using std::end; +using std::fill; using std::list; using namespace eosio::native; @@ -189,73 +195,59 @@ EOSIO_TEST_BEGIN(datastream_specialization_test) silence_output(false); EOSIO_TEST_END +template +void print_bufs(const char* datastream_buffer, const T& container) { + eosio::print("\n"); + eosio::print("\033[1;36mdatastream_buffer:\033[0m\n"); + eosio::print((int)datastream_buffer[0]); + for (int i = 0; i < 15; ++i) { + eosio::print(datastream_buffer[i+1]); + } + eosio::print("\n"); + + eosio::print("\033[1;36mcontainer:\033[0m\n"); + for (auto& x : container) { + eosio::print(x); + } + eosio::print("\n"); +} + // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(false); - static constexpr size_t elements{8}; - unsigned_int ui{elements}; - char datastream_buffer[256]{}; // Buffer for the datastream to point to - char buffer[8]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; // Buffer to act upon for testing + static constexpr uint8_t buffer_size{16}; + char datastream_buffer[buffer_size]; // Buffer for the datastream to point to + char expectation_buffer[buffer_size]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; // Buffer to act upon for testing + datastream ds{datastream_buffer, buffer_size}; // --------- // std::list - - std::list l{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', }; - - datastream ds{datastream_buffer, 8+sizeof(unsigned_int)}; - - for(int i{0}; i < 16; ++i) - eosio::print(datastream_buffer[i]); - eosio::print("\n"); - for(int i{0}; i < 8; ++i) - eosio::print(buffer[i]); - eosio::print("\n"); - for(auto& x : l) - eosio::print(x); - eosio::print("\n"); - - eosio::print("\n"); + // `datastream_buffer+1` is used to skip the size byte + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + std::list l{}; + ds << cl; + REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) - ds << l; - - for(int i{0}; i < 17; ++i) - eosio::print(datastream_buffer[i]); - eosio::print("\n"); - for(int i{0}; i < 8; ++i) - eosio::print(buffer[i]); - eosio::print("\n"); - for(auto& x : l) - eosio::print(x); - eosio::print("\n"); - + ds.seekp(0); ds >> l; - - eosio::print("\n"); - - for(int i{0}; i < 16; ++i) - eosio::print(datastream_buffer[i]); - eosio::print("\n"); - for(int i{0}; i < 8; ++i) - eosio::print(buffer[i]); - eosio::print("\n"); - for(auto& x : l) - eosio::print(x); - eosio::print("\n"); - - // std::vector um_ds(std::begin(datastream_buffer), std::end(datastream_buffer)); - // std::vector um_list(std::begin(datastream_buffer), std::end(datastream_buffer)); - - // for(uint16_t i{0}; i < 256; ++i) - // eosio::print(datastream_buffer); - - // for(uint16_t i{0}; i < 256; ++i) - // eosio::print(datastream_buffer); - - // REQUIRE_EQUAL( um_ds == um_list, true ) - + REQUIRE_EQUAL( cl == l, true ) + // ---------- // std::deque + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const std::deque cd{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + std::deque d{}; + ds << cd; + REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + + // This part segfaults + // ds.seekp(0); + // ds >> d; + // REQUIRE_EQUAL( cd == d, true ) // Make custom small struct; use default constructor; spit in/spit out. Then compare // the value in the default constructor (expected) to result. And also put custom input to .value_or as well @@ -264,15 +256,42 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // ------------ // std::variant + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const std::variant cv{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // std::variant v{}; + // ds << cv; + // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + + // ds.seekp(0); + // ds >> v; + // REQUIRE_EQUAL( cv == v, true ) // --------- // std::pair + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const std::pair cp{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // std::pair p{}; + // ds << cp; + // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + + // ds.seekp(0); + // ds >> p; + // REQUIRE_EQUAL( cp == p, true ) // --------- // std::optional - - // --------- - // std::pair + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const std::optional co{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // std::optional o{}; + // ds << co; + // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + + // ds.seekp(0); + // ds >> o; + // REQUIRE_EQUAL( co == o, true ) // --------- // symbol_code @@ -301,11 +320,31 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // ----------- // fixed_bytes - // ----------- + // ---- // bool + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // std::list l{}; + // ds << cl; + // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + + // ds.seekp(0); + // ds >> l; + // REQUIRE_EQUAL( cl == l, true ) // ----------- // std::array + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const std::array ca{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // std::array a{}; + // ds << ca; + // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + + // ds.seekp(0); + // ds >> a; + // REQUIRE_EQUAL( ca == a, true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index d2a42f4528..d219c7ea03 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -2,6 +2,8 @@ #include #include +#include + using eosio::datastream; using namespace eosio::native; @@ -252,14 +254,23 @@ EOSIO_TEST_BEGIN(signed_int_type_test) // template // friend DataStream& operator<<(DataStream&, const signed_int&) const signed_int ci{0x7F}; - const char expected_buffer0[1]{0x7F}; - char buffer0[1]{}; - datastream ds0{buffer0, 1}; + char buffer0[10]{}; + datastream ds0{buffer0, 10}; - // ds0 << ci; +eosio::print(ci.value, "\n"); + ds0 << ci; +eosio::print(static_cast(buffer0[0]), " "); +eosio::print(static_cast(buffer0[1]), "\n"); + signed_int temp{}; + ds0 >> temp; +eosio::print(static_cast(buffer0[0]), " "); +eosio::print(static_cast(buffer0[1]), "\n"); +eosio::print(temp.value, "\n"); - // REQUIRE_EQUAL( memcmp(expected_buffer0, buffer0, 1), 0 ) + REQUIRE_EQUAL( ci == temp, true ) + + // REQUIRE_EQUAL( memcmp(expected_buffer0, buffer0, 2), 0 ) // ----------------------------- // template From f35f6afba75c75f53f304d3c2e84645fb2faec87 Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 31 Jan 2019 17:54:46 -0500 Subject: [PATCH 23/51] Tidy up tests; `name_tests`, `symbol_tests`, `asset_tests`, `system_tests`, and `crypto_tests` now ready --- tests/unit/asset_tests.cpp | 446 ++++++++++++++++++-------------- tests/unit/crypto_tests.cpp | 23 +- tests/unit/eosio_tests.cpp | 1 - tests/unit/name_tests.cpp | 493 ++++++++++++++++++------------------ tests/unit/symbol_tests.cpp | 352 ++++++++++++------------- tests/unit/system_tests.cpp | 33 ++- tests/unit/time_tests.cpp | 369 +++++++++++++++++---------- 7 files changed, 932 insertions(+), 785 deletions(-) diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 2eaddc1840..9013f2ad4f 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -1,5 +1,4 @@ #include - #include using eosio::name; @@ -12,11 +11,6 @@ const int64_t asset_mask{(1LL << 62) - 1}; const int64_t asset_min{-asset_mask}; // -4611686018427387903 const int64_t asset_max{ asset_mask}; // 4611686018427387903 -// For incrementing the `precision` in any test relating to `precision` -// The `precision` field in the `symbol` type must be constructed via constexpr, -// therefore a constexpr function is needed to dynamically test `precision` -constexpr uint8_t increment_precision( uint8_t precision ) { return ++precision; } - // Definitions in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) silence_output(true); @@ -28,242 +22,288 @@ EOSIO_TEST_BEGIN(asset_type_test) symbol sym_no_prec{"SYMBOLL", 0}; // Symbol with no precision symbol sym_prec{"SYMBOLL", 63}; // Symbol with precision - // -------------------------------------------------------------------- - // constructors/bool is_amount_within_range()const/bool is_valid()const - - /// constexpr asset() - REQUIRE_EQUAL( asset{}.amount, 0ULL ) - REQUIRE_EQUAL( asset{}.symbol.raw(), 0ULL ) + //// constexpr asset() + CHECK_EQUAL( asset{}.amount, 0ULL ) + CHECK_EQUAL( asset{}.symbol.raw(), 0ULL ) - /// constexpr asset(int64_t, symbol) - REQUIRE_EQUAL( (asset{0LL, s0}.amount), 0LL ) - REQUIRE_EQUAL( (asset{asset_min, s0}.amount), asset_min ) - REQUIRE_EQUAL( (asset{asset_max, s0}.amount), asset_max ) - - REQUIRE_EQUAL( (asset{0LL, s0}.symbol.raw()), 16640ULL ) // "A", precision: 0 - REQUIRE_EQUAL( (asset{0LL, s1}.symbol.raw()), 23040ULL ) // "Z", precision: 0 - REQUIRE_EQUAL( (asset{0LL, s2}.symbol.raw()), 4702111234474983680ULL ) // "AAAAAAA", precision: 0 - REQUIRE_EQUAL( (asset{0LL, s3}.symbol.raw()), 6510615555426900480ULL ) // "ZZZZZZZ", precision: 0 + //// constexpr asset(int64_t, symbol) + CHECK_EQUAL( (asset{0LL, s0}.amount), 0LL ) + CHECK_EQUAL( (asset{asset_min, s0}.amount), asset_min ) + CHECK_EQUAL( (asset{asset_max, s0}.amount), asset_max ) + + CHECK_EQUAL( (asset{0LL, s0}.symbol.raw()), 16640ULL ) // "A", precision: 0 + CHECK_EQUAL( (asset{0LL, s1}.symbol.raw()), 23040ULL ) // "Z", precision: 0 + CHECK_EQUAL( (asset{0LL, s2}.symbol.raw()), 4702111234474983680ULL ) // "AAAAAAA", precision: 0 + CHECK_EQUAL( (asset{0LL, s3}.symbol.raw()), 6510615555426900480ULL ) // "ZZZZZZZ", precision: 0 - // There is an invariant established for `asset` that isn't enforced with `symbol` + // Note: there is an invariant established for `asset` that is not enforced for `symbol` // For example: // valid code: // `symbol{};` // throws "invalid symbol name": // `asset{{}, symbol{}};` - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{0LL}};}) ) - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{1LL}};}) ) - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{16639ULL}};}) ) - REQUIRE_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{6510615555426900736ULL}};}) ) - - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_min - 1, {}};}) ) - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_max + 1, {}};}) ) + CHECK_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{0LL}};}) ) + CHECK_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{1LL}};}) ) + CHECK_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{16639ULL}};}) ) + CHECK_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{6510615555426900736ULL}};}) ) + + CHECK_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_min - 1, {}};}) ) + CHECK_ASSERT( "magnitude of asset amount must be less than 2^62", ([]() {asset{ asset_max + 1, {}};}) ) + + // ---------------------------------- + // bool is_amount_within_range()const + asset asset_check_amount{}; + asset_check_amount.amount = asset_min; + CHECK_EQUAL( asset_check_amount.is_amount_within_range(), true ) + asset_check_amount.amount = asset_max; + CHECK_EQUAL( asset_check_amount.is_amount_within_range(), true ) + + asset_check_amount.amount = asset_min-1; + CHECK_EQUAL( asset_check_amount.is_amount_within_range(), false ) + asset_check_amount.amount = asset_max+1; + CHECK_EQUAL( asset_check_amount.is_amount_within_range(), false ) + + // -------------------- + // bool is_valid()const + asset asset_valid{}; + asset_valid.symbol = symbol{16640ULL}; // "A", precision: 0 + CHECK_EQUAL( asset_valid.is_valid(), true ) + asset_valid.symbol = symbol{23040ULL}; // "Z", precision: 0 + CHECK_EQUAL( asset_valid.is_valid(), true ) + asset_valid.symbol = symbol{4702111234474983680ULL}; + CHECK_EQUAL( asset_valid.is_valid(), true ) // "AAAAAAA", precision: 0 + asset_valid.symbol = symbol{6510615555426900480ULL}; + CHECK_EQUAL( asset_valid.is_valid(), true ) // "ZZZZZZZ", precision: 0 + + asset_valid.symbol = symbol{16639ULL}; + CHECK_EQUAL( asset_valid.is_valid(), false ) + asset_valid.symbol = symbol{6510615555426900736ULL}; + CHECK_EQUAL( asset_valid.is_valid(), false ) // ------------------------ // void set_amount(int64_t) asset a{0LL, sym_no_prec}; - REQUIRE_EQUAL( (a.set_amount(0LL), a.amount), 0LL ) - REQUIRE_EQUAL( (a.set_amount(1LL), a.amount), 1LL ) - REQUIRE_EQUAL( (a.set_amount(asset_min), a.amount), asset_min ) - REQUIRE_EQUAL( (a.set_amount(asset_max), a.amount), asset_max ) + CHECK_EQUAL( (a.set_amount(0LL), a.amount), 0LL ) + CHECK_EQUAL( (a.set_amount(1LL), a.amount), 1LL ) + CHECK_EQUAL( (a.set_amount(asset_min), a.amount), asset_min ) + CHECK_EQUAL( (a.set_amount(asset_max), a.amount), asset_max ) - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([&]() { + CHECK_ASSERT( "magnitude of asset amount must be less than 2^62", ( + [&]() { a.set_amount(asset_min - 1); }) ) - REQUIRE_ASSERT( "magnitude of asset amount must be less than 2^62", ([&]() { + CHECK_ASSERT( "magnitude of asset amount must be less than 2^62", ( + [&]() { a.set_amount(asset_max + 1); }) ) - // --------------------------------------- - // std::to_string()const/void print()const + // --------------------- + // std::to_string()const // Note that printing an `asset` is limited to a precision of 63 // This will trigger an error: // `asset{int64_t{1LL}, symbol{"SYMBOLL", 64}}.print();` // output: "Floating point exception: ..." // This produces weird behavior when the `precision` is 0: - // `asset{int64_t{1LL}, symbol{"SYMBOLL", precision}}.print();` + // `asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.print();` // output: "1. SYMBOLL" // Note that `assets` with negative amounts with 0 `precision` print have the odd behavior of printing two '-' characters: - REQUIRE_EQUAL( (asset{0LL, sym_no_prec}.to_string()), - "0. SYMBOLL" ) - REQUIRE_EQUAL( (asset{-0LL, sym_no_prec}.to_string()), - "0. SYMBOLL" ) - REQUIRE_EQUAL( (asset{0LL, sym_prec}.to_string()), - "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) - REQUIRE_EQUAL( (asset{-0LL, sym_prec}.to_string()), - "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) - - REQUIRE_EQUAL( (asset{1LL, sym_no_prec}.to_string()), - "1. SYMBOLL" ) - REQUIRE_EQUAL( (asset{-1LL, sym_no_prec}.to_string()), - "--1. SYMBOLL" ) - REQUIRE_EQUAL( (asset{1LL, sym_prec}.to_string()), - "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) - REQUIRE_EQUAL( (asset{-1LL, sym_prec}.to_string()), - "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) - - REQUIRE_EQUAL( (asset{asset_min, sym_no_prec}.to_string()), - "--4611686018427387903. SYMBOLL" ) - REQUIRE_EQUAL( (asset{asset_max, sym_no_prec}.to_string()), - "4611686018427387903. SYMBOLL" ) - REQUIRE_EQUAL( (asset{asset_min, sym_prec}.to_string()), - "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) - REQUIRE_EQUAL( (asset{asset_max, sym_prec}.to_string()), - "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) + CHECK_EQUAL( (asset{0LL, sym_no_prec}.to_string()), "0. SYMBOLL" ) + CHECK_EQUAL( (asset{-0LL, sym_no_prec}.to_string()), "0. SYMBOLL" ) + CHECK_EQUAL( (asset{0LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) + CHECK_EQUAL( (asset{-0LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) + + CHECK_EQUAL( (asset{1LL, sym_no_prec}.to_string()), "1. SYMBOLL" ) + CHECK_EQUAL( (asset{-1LL, sym_no_prec}.to_string()), "--1. SYMBOLL" ) + CHECK_EQUAL( (asset{1LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) + CHECK_EQUAL( (asset{-1LL, sym_prec}.to_string()), "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) + + CHECK_EQUAL( (asset{asset_min, sym_no_prec}.to_string()), "--4611686018427387903. SYMBOLL" ) + CHECK_EQUAL( (asset{asset_max, sym_no_prec}.to_string()), "4611686018427387903. SYMBOLL" ) + CHECK_EQUAL( (asset{asset_min, sym_prec}.to_string()), "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) + CHECK_EQUAL( (asset{asset_max, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) + + // Note: uncomment once print checking has been resolved + // ----------------- + // void print()const + // CHECK_PRINT( "0. SYMBOLL", [&](){asset{0LL, sym_no_prec}.print();} ); + // CHECK_PRINT( "0. SYMBOLL", [&](){asset{-0LL, sym_no_prec}.print();} ); + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", [&](){asset{0LL, sym_prec}.print();} ); + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", [&](){asset{-0LL, sym_prec}.print();} ); + + // CHECK_PRINT( "1. SYMBOLL", [&](){asset{1LL, sym_no_prec}.print();} ); + // CHECK_PRINT( "--1. SYMBOLL", [&](){asset{-1LL, sym_no_prec}.print();} ); + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", [&](){asset{1LL, sym_prec}.print();} ); + // CHECK_PRINT( "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", [&](){asset{-1LL, sym_prec}.print();} ); + + // CHECK_PRINT( "--4611686018427387903. SYMBOLL", [&](){asset{asset_min, sym_no_prec}.print();} ); + // CHECK_PRINT( "4611686018427387903. SYMBOLL", [&](){asset{asset_max, sym_no_prec}.print();} ); + // CHECK_PRINT( "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", [&](){asset{asset_min, sym_prec}.print();} ); + // CHECK_PRINT( "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", [&](){asset{asset_max, sym_prec}.print();} ); // How are these `assets` constructed? Isn't `uint8_t precision` supposed to be constexpr when constructing a symbol? for( uint8_t precision{0}; precision < 64; ++precision ) { - REQUIRE_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), + CHECK_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ) } // ---------------------- // asset operator-()const - REQUIRE_EQUAL( (-asset{ 0LL, sym_no_prec}.amount), (asset{0LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (-asset{-0LL, sym_no_prec}.amount), (asset{0LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (-asset{ 0LL, sym_prec}.amount), (asset{0LL, sym_prec}.amount) ) - REQUIRE_EQUAL( (-asset{-0LL, sym_prec}.amount), (asset{0LL, sym_prec}.amount) ) + CHECK_EQUAL( (-asset{ 0LL, sym_no_prec}.amount), (asset{0LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (-asset{-0LL, sym_no_prec}.amount), (asset{0LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (-asset{ 0LL, sym_prec}.amount), (asset{0LL, sym_prec}.amount) ) + CHECK_EQUAL( (-asset{-0LL, sym_prec}.amount), (asset{0LL, sym_prec}.amount) ) - REQUIRE_EQUAL( (-asset{ 1LL, sym_no_prec}.amount), (asset{-1LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (-asset{-1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (-asset{ 1LL, sym_prec}.amount), (asset{-1LL, sym_prec}.amount) ) - REQUIRE_EQUAL( (-asset{-1LL, sym_prec}.amount), (asset{ 1LL, sym_prec}.amount) ) + CHECK_EQUAL( (-asset{ 1LL, sym_no_prec}.amount), (asset{-1LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (-asset{-1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (-asset{ 1LL, sym_prec}.amount), (asset{-1LL, sym_prec}.amount) ) + CHECK_EQUAL( (-asset{-1LL, sym_prec}.amount), (asset{ 1LL, sym_prec}.amount) ) - REQUIRE_EQUAL( (-asset{asset_min, sym_no_prec}.amount), (asset{asset_max, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (-asset{asset_max, sym_no_prec}.amount), (asset{asset_min, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (-asset{asset_min, sym_prec}.amount), (asset{asset_max, sym_prec}.amount) ) - REQUIRE_EQUAL( (-asset{asset_max, sym_prec}.amount), (asset{asset_min, sym_prec}.amount) ) + CHECK_EQUAL( (-asset{asset_min, sym_no_prec}.amount), (asset{asset_max, sym_no_prec}.amount) ) + CHECK_EQUAL( (-asset{asset_max, sym_no_prec}.amount), (asset{asset_min, sym_no_prec}.amount) ) + CHECK_EQUAL( (-asset{asset_min, sym_prec}.amount), (asset{asset_max, sym_prec}.amount) ) + CHECK_EQUAL( (-asset{asset_max, sym_prec}.amount), (asset{asset_min, sym_prec}.amount) ) // ------------------------------------------------------------------------------------------ // inline friend asset& operator+(const asset&, const asset&)/asset& operator+=(const asset&) - REQUIRE_EQUAL( (asset{0LL, sym_no_prec} += asset{ 0LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{1LL, sym_no_prec} += asset{-1LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{0LL, sym_no_prec} += asset{ 0LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{1LL, sym_no_prec} += asset{-1LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) - REQUIRE_ASSERT( "attempt to add asset with different symbol", ([&]() { + CHECK_ASSERT( "attempt to add asset with different symbol", ( + [&]() { asset{1LL, sym_no_prec} += asset{1LL, symbol{"LLOBMYS", 0}}; }) ) - REQUIRE_ASSERT( "addition underflow", ([&]() { + CHECK_ASSERT( "addition underflow", ( + [&]() { asset{asset_min, sym_no_prec} += -asset{1LL, sym_no_prec}; }) ) - REQUIRE_ASSERT( "addition overflow", ([&]() { + CHECK_ASSERT( "addition overflow", ( + [&]() { asset{asset_max, sym_no_prec} += asset{1LL, sym_no_prec}; }) ) // ------------------------------------------------------------------------------------------ // inline friend asset& operator-(const asset&, const asset&)/asset& operator-=(const asset&) - REQUIRE_EQUAL( (asset{0LL, sym_no_prec} -= asset{0LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{1LL, sym_no_prec} -= asset{1LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{0LL, sym_no_prec} -= asset{0LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{1LL, sym_no_prec} -= asset{1LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) - REQUIRE_ASSERT( "attempt to subtract asset with different symbol", ([&]() { + CHECK_ASSERT( "attempt to subtract asset with different symbol", ( + [&]() { asset{1LL, sym_no_prec} -= asset{1LL, symbol{"LLOBMYS", 0}}; }) ) - REQUIRE_ASSERT( "subtraction underflow", ([&]() { + CHECK_ASSERT( "subtraction underflow", ( + [&]() { asset{asset_min, sym_no_prec} -= asset{1LL, sym_no_prec}; }) ) - REQUIRE_ASSERT( "subtraction overflow", ([&]() { + CHECK_ASSERT( "subtraction overflow", ( + [&]() { asset{asset_max, sym_no_prec} -= -asset{1LL, sym_no_prec}; }) ) // ----------------------------------------------------------------------- // friend asset operator*(const asset&, int64_t)/asset operator*=(int64_t) - REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} *= 0LL ), (asset{ 0LL, sym_no_prec}) ); - REQUIRE_EQUAL( (asset{ 2LL, sym_no_prec} *= 1LL ), (asset{ 2LL, sym_no_prec}) ); - REQUIRE_EQUAL( (asset{ 2LL, sym_no_prec} *= -1LL ), (asset{-2LL, sym_no_prec}) ); + CHECK_EQUAL( (asset{ 0LL, sym_no_prec} *= 0LL ), (asset{ 0LL, sym_no_prec}) ); + CHECK_EQUAL( (asset{ 2LL, sym_no_prec} *= 1LL ), (asset{ 2LL, sym_no_prec}) ); + CHECK_EQUAL( (asset{ 2LL, sym_no_prec} *= -1LL ), (asset{-2LL, sym_no_prec}) ); - REQUIRE_ASSERT( "multiplication underflow", ([&]() { + CHECK_ASSERT( "multiplication underflow", ( + [&]() { asset{asset_min, sym_no_prec} *= 2LL; }) ) - REQUIRE_ASSERT( "multiplication overflow", ([&]() { + CHECK_ASSERT( "multiplication overflow", ( + [&]() { asset{ asset_max, sym_no_prec} *= 2LL; }) ) // ----------------------------------------------------------------------------------------------------------------------------- // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) - REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) - - REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{ 1LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{ 4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) - REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} /= asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) - - REQUIRE_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 0LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 1LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{-2LL, sym_no_prec}.amount) ) - REQUIRE_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) - - REQUIRE_ASSERT( "divide by zero", ([&]() {asset{1LL, sym_no_prec} /= 0;}) ) + CHECK_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + + CHECK_EQUAL( (asset{ 0LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 1LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + + CHECK_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 0LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 1LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{-2LL, sym_no_prec}.amount) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) + + CHECK_ASSERT( "divide by zero", ([&]() {asset{1LL, sym_no_prec} /= 0;}) ) // Note that there is no invariant established here when adding or setting the `amount` - REQUIRE_ASSERT( "signed division overflow", ([&]() { + CHECK_ASSERT( "signed division overflow", ( + []() { asset a{}; a.amount = std::numeric_limits::min(); a /= -1LL; }) ) - REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ([&]() { + CHECK_ASSERT( "comparison of assets with different symbols is not allowed", ( + [&]() { asset{1LL, s0} / asset{1LL, s1}; }) ) // -------------------------------------------------- // friend bool operator==(const asset&, const asset&) - REQUIRE_EQUAL( (asset{{}, sym_no_prec} == asset{{}, sym_no_prec} ), true ) - REQUIRE_EQUAL( (asset{asset_min, sym_no_prec} == asset{asset_min, sym_no_prec} ), true ) - REQUIRE_EQUAL( (asset{asset_max, sym_no_prec} == asset{asset_max, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{{}, sym_no_prec} == asset{{}, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{asset_min, sym_no_prec} == asset{asset_min, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{asset_max, sym_no_prec} == asset{asset_max, sym_no_prec} ), true ) - REQUIRE_ASSERT( "comparison of assets with different symbols is not allowed", ( []() { + CHECK_ASSERT( "comparison of assets with different symbols is not allowed", ( + []() { asset{{}, symbol{"SYMBOLL", 0}} == asset{{}, symbol{"LLOBMYS", 0}}; }) ) // --------------------------------------------------- // friend bool operator!=( const asset&, const asset&) - REQUIRE_EQUAL( (asset{asset_min, sym_no_prec} != -asset{asset_min, sym_no_prec} ), true ) - REQUIRE_EQUAL( (asset{asset_max, sym_no_prec} != -asset{asset_max, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{asset_min, sym_no_prec} != -asset{asset_min, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{asset_max, sym_no_prec} != -asset{asset_max, sym_no_prec} ), true ) // ------------------------------------------------- // friend bool operator<(const asset&, const asset&) - REQUIRE_EQUAL( (asset{{}, sym_no_prec} < asset{1LL, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{{}, sym_no_prec} < asset{1LL, sym_no_prec} ), true ) // -------------------------------------------------- // friend bool operator<=(const asset&, const asset&) - REQUIRE_EQUAL( ( asset{{}, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) - REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{{}, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{1LL, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) // ------------------------------------------------- // friend bool operator>(const asset&, const asset&) - REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} > asset{{}, sym_no_prec} ), true ) - REQUIRE_EQUAL( ( asset{{}, sym_no_prec} > asset{1LL, sym_no_prec} ), false ) + CHECK_EQUAL( ( asset{1LL, sym_no_prec} > asset{{}, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{{}, sym_no_prec} > asset{1LL, sym_no_prec} ), false ) // -------------------------------------------------- // friend bool operator>=( const asset&, const asset&) - REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} >= asset{{}, sym_no_prec} ), true ) - REQUIRE_EQUAL( ( asset{1LL, sym_no_prec} >= asset{1LL, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{1LL, sym_no_prec} >= asset{{}, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{1LL, sym_no_prec} >= asset{1LL, sym_no_prec} ), true ) silence_output(false); EOSIO_TEST_END @@ -281,65 +321,91 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) asset asset_no_prec{0LL, sym_no_prec}; asset asset_prec{0LL, sym_prec}; - // ------------ - // constructors - - /// extended_asset() - REQUIRE_EQUAL( extended_asset{}.quantity, asset{} ) - REQUIRE_EQUAL( extended_asset{}.contract, name{} ) + //// extended_asset() + CHECK_EQUAL( extended_asset{}.quantity, asset{} ) + CHECK_EQUAL( extended_asset{}.contract, name{} ) - /// extended_asset(int64_t, extended_symbol) - REQUIRE_EQUAL( (extended_asset{{}, ex_sym_no_prec}.quantity), (asset{0LL, sym_no_prec}) ) - REQUIRE_EQUAL( (extended_asset{{}, ex_sym_no_prec}.contract), (name{"eosioaccountj"}) ) + //// extended_asset(int64_t, extended_symbol) + CHECK_EQUAL( (extended_asset{{}, ex_sym_no_prec}.quantity), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (extended_asset{{}, ex_sym_no_prec}.contract), (name{"eosioaccountj"}) ) - /// extended_asset(asset, name) - REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.quantity), (asset{ 0LL, sym_no_prec}) ) - REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.contract), (name{"eosioaccountj"}) ) + //// extended_asset(asset, name) + CHECK_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.quantity), (asset{ 0LL, sym_no_prec}) ) + CHECK_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.contract), (name{"eosioaccountj"}) ) // ------------------------------------------ // extended_symbol get_extended_symbol()const - REQUIRE_EQUAL( (extended_asset{{}, ex_sym_no_prec}.get_extended_symbol()), (ex_sym_no_prec) ) - REQUIRE_EQUAL( (extended_asset{{}, ex_sym_prec}.get_extended_symbol()), (ex_sym_prec) ) + CHECK_EQUAL( (extended_asset{{}, ex_sym_no_prec}.get_extended_symbol()), (ex_sym_no_prec) ) + CHECK_EQUAL( (extended_asset{{}, ex_sym_prec}.get_extended_symbol()), (ex_sym_prec) ) - // Note: uncomment once the appended '\n' is removed from the `print` function - // Fix this printing functionality + // Note: uncomment once print checking has been resolved // Note that if there is no precision, there will be odd output: // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` // output: "0. A@1" - - // REQUIRE_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) - // REQUIRE_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) - // REQUIRE_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) - // REQUIRE_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ) - - // REQUIRE_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - // REQUIRE_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - // REQUIRE_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - // REQUIRE_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + // ----------------- + // void print()const + // CHECK_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) + // CHECK_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) + // CHECK_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) + // CHECK_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ) + + // CHECK_PRINT( "1.1 A@1", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"1"}}.print();} ) + // CHECK_PRINT( "1.1 A@5", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"5"}}.print();} ) + // CHECK_PRINT( "1.1 Z@a", [](){extended_asset{asset{int64_t{11}, symbol{"Z", 1}}, name{"a"}}.print();} ) + // CHECK_PRINT( "1.1 Z@z", [](){extended_asset{asset{int64_t{11}, symbol{"Z", 1}}, name{"z"}}.print();} ) + + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 A@1", + // [](){extended_asset{asset{int64_t{11}, symbol{"A", 63}}, name{"1"}}.print();} ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 A@5", + // [](){extended_asset{asset{int64_t{11}, symbol{"A", 63}}, name{"5"}}.print();} ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 Z@a", + // [](){extended_asset{asset{int64_t{11}, symbol{"Z", 63}}, name{"a"}}.print();} ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 Z@z", + // [](){extended_asset{asset{int64_t{11}, symbol{"Z", 63}}, name{"z"}}.print();} ) + + // CHECK_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + // CHECK_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + // CHECK_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + // CHECK_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + + // CHECK_PRINT( "1.1 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + // CHECK_PRINT( "1.1 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + // CHECK_PRINT( "1.1 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + // CHECK_PRINT( "1.1 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@111111111111j", + // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@555555555555j", + // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@aaaaaaaaaaaaj", + // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@zzzzzzzzzzzzj", + // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) // ------------------------------- // extended_asset operator-()const - REQUIRE_EQUAL( (-extended_asset{asset{ 0, sym_no_prec}, {}}.quantity), (extended_asset{asset_no_prec, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{-0, sym_no_prec}, {}}.quantity), (extended_asset{asset_no_prec, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{ 0, sym_prec}, {}}.quantity), (extended_asset{asset_prec, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{-0, sym_prec}, {}}.quantity), (extended_asset{asset_prec, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{ 0, sym_no_prec}, {}}.quantity), (extended_asset{asset_no_prec, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{-0, sym_no_prec}, {}}.quantity), (extended_asset{asset_no_prec, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{ 0, sym_prec}, {}}.quantity), (extended_asset{asset_prec, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{-0, sym_prec}, {}}.quantity), (extended_asset{asset_prec, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_no_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_no_prec}, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_no_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_no_prec}, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_prec}, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{1LL, sym_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{1LL, sym_no_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_no_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{1LL, sym_no_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_no_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{1LL, sym_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{1LL, sym_prec}, {}}.quantity), (extended_asset{asset{-1LL, sym_prec}, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_no_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_no_prec}, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_no_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_no_prec}, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_prec}, {}}.quantity) ) - REQUIRE_EQUAL( (-extended_asset{asset{asset_max, sym_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{asset_max, sym_no_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_no_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{asset_max, sym_no_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_no_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{asset_max, sym_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_prec}, {}}.quantity) ) + CHECK_EQUAL( (-extended_asset{asset{asset_max, sym_prec}, {}}.quantity), (extended_asset{asset{asset_min, sym_prec}, {}}.quantity) ) // ----------------------------------------------------------------------------- // friend extended_asset operator+(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (extended_asset{asset{0LL, sym_no_prec}, {}} + extended_asset{asset{ 0LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) - REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} + extended_asset{asset{-1LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) + CHECK_EQUAL( (extended_asset{asset{0LL, sym_no_prec}, {}} + extended_asset{asset{ 0LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) + CHECK_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} + extended_asset{asset{-1LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) - REQUIRE_ASSERT( "type mismatch", ([&]() { + CHECK_ASSERT( "type mismatch", ( + [&]() { extended_asset{asset_no_prec, name{"eosioaccountj"}} + extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}; }) ) @@ -347,23 +413,25 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // ------------------------------------------------------------------------- // friend extended_asset& operator+=(extended_asset&, const extended_asset&) extended_asset temp{asset_no_prec, {}}; - REQUIRE_EQUAL( (temp += temp), (extended_asset{asset_no_prec, {}}) ) + CHECK_EQUAL( (temp += temp), (extended_asset{asset_no_prec, {}}) ) temp = extended_asset{asset{1LL, sym_no_prec}, {}}; - REQUIRE_EQUAL( (temp += extended_asset{asset{-1LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) + CHECK_EQUAL( (temp += extended_asset{asset{-1LL, sym_no_prec}, {}}), (extended_asset{asset_no_prec, {}}) ) - REQUIRE_ASSERT( "type mismatch", ([&]() { + CHECK_ASSERT( "type mismatch", ( + [&]() { temp += extended_asset{asset_no_prec, name{"eosioaccountj"}}; }) ) // ----------------------------------------------------------------------------- // friend extended_asset operator-(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (extended_asset{asset_no_prec, {}} - extended_asset{asset_no_prec, {}}), + CHECK_EQUAL( (extended_asset{asset_no_prec, {}} - extended_asset{asset_no_prec, {}}), (extended_asset{asset_no_prec, {}}) ) - REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} - extended_asset{asset{1LL, sym_no_prec}, {}}), + CHECK_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} - extended_asset{asset{1LL, sym_no_prec}, {}}), (extended_asset{asset{asset_no_prec}, {}}) ) - REQUIRE_ASSERT( "type mismatch", ([&]() { + CHECK_ASSERT( "type mismatch", ( + [&]() { extended_asset{asset_no_prec, name{"eosioaccountj"}} - extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}; }) ) @@ -371,43 +439,41 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // -------------------------------------------------------------------------- // friend extended_asset& operator-=(extended_asset&, const extended_asset&) temp = extended_asset{asset_no_prec, {}}; - REQUIRE_EQUAL( (temp -= temp), (extended_asset{asset_no_prec, {}}) ) + CHECK_EQUAL( (temp -= temp), (extended_asset{asset_no_prec, {}}) ) temp = extended_asset{asset{1LL, sym_no_prec}, {}}; - REQUIRE_EQUAL( (temp -= temp), (extended_asset{asset_no_prec, {}}) ) + CHECK_EQUAL( (temp -= temp), (extended_asset{asset_no_prec, {}}) ) - REQUIRE_ASSERT( "type mismatch", ([&]() { + CHECK_ASSERT( "type mismatch", ( + [&]() { temp -= extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}; }) ) // -------------------------------------------------------------------- // friend bool operator==(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (extended_asset{asset_no_prec, {}} == extended_asset{asset_no_prec, {}}), true ) - REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} == extended_asset{asset{1LL, sym_no_prec}, {}}), true ) - - // // Why is this not throwing?? - // REQUIRE_ASSERT( "type mismatch", ([&]() {asset1 == asset2;}) ); + CHECK_EQUAL( (extended_asset{asset_no_prec, {}} == extended_asset{asset_no_prec, {}}), true ) + CHECK_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} == extended_asset{asset{1LL, sym_no_prec}, {}}), true ) // -------------------------------------------------------------------- // friend bool operator!=(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}} != extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}), true ) - REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} != extended_asset{asset{-1LL, sym_no_prec}, {}}), true ) - REQUIRE_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} != extended_asset{asset{ 0LL, sym_no_prec}, name{"eosioaccountj"}}), true ) + CHECK_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}} != extended_asset{asset_no_prec, name{"jtnuoccaoisoe"}}), true ) + CHECK_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} != extended_asset{asset{-1LL, sym_no_prec}, {}}), true ) + CHECK_EQUAL( (extended_asset{asset{1LL, sym_no_prec}, {}} != extended_asset{asset{ 0LL, sym_no_prec}, name{"eosioaccountj"}}), true ) // ------------------------------------------------------------------- // friend bool operator<(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{}} < extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ) - REQUIRE_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} < extended_asset{{}, name{"eosioaccountj"}};}) ) + CHECK_EQUAL( (extended_asset{asset_no_prec, name{}} < extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ) + CHECK_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} < extended_asset{{}, name{"eosioaccountj"}};}) ) // -------------------------------------------------------------------- // friend bool operator<=(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (extended_asset{asset_no_prec, name{}} <= extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ); - REQUIRE_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} <= extended_asset{{}, name{"eosioaccountj"}};}) ) + CHECK_EQUAL( (extended_asset{asset_no_prec, name{}} <= extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ); + CHECK_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} <= extended_asset{{}, name{"eosioaccountj"}};}) ) // -------------------------------------------------------------------- // friend bool operator>=(const extended_asset&, const extended_asset&) - REQUIRE_EQUAL( (extended_asset{asset{ 1LL, sym_no_prec}, {}} >= extended_asset{asset_no_prec, name{}}), true ); - REQUIRE_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} >= extended_asset{{}, name{"eosioaccountj"}};}) ) + CHECK_EQUAL( (extended_asset{asset{ 1LL, sym_no_prec}, {}} >= extended_asset{asset_no_prec, name{}}), true ); + CHECK_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} >= extended_asset{{}, name{"eosioaccountj"}};}) ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/crypto_tests.cpp b/tests/unit/crypto_tests.cpp index 74c01e513e..7834b46518 100644 --- a/tests/unit/crypto_tests.cpp +++ b/tests/unit/crypto_tests.cpp @@ -1,5 +1,4 @@ #include - #include using eosio::public_key; @@ -11,13 +10,15 @@ using namespace eosio::native; EOSIO_TEST_BEGIN(public_key_type_test) silence_output(true); + // ----------------------------------------------------- // bool operator==(const public_key&, const public_key&) - REQUIRE_EQUAL( (public_key{0, std::array{}} == public_key{0, std::array{}}), true ) - REQUIRE_EQUAL( (public_key{0, std::array{1}} == public_key{0, std::array{}}), false ) + CHECK_EQUAL( (public_key{0, std::array{}} == public_key{0, std::array{}}), true ) + CHECK_EQUAL( (public_key{0, std::array{1}} == public_key{0, std::array{}}), false ) + // ----------------------------------------------------- // bool operator!=(const public_key&, const public_key&) - REQUIRE_EQUAL( (public_key{0, std::array{}} != public_key{0, std::array{}}), false ) - REQUIRE_EQUAL( (public_key{0, std::array{1}} != public_key{0, std::array{}}), true ) + CHECK_EQUAL( (public_key{0, std::array{}} != public_key{0, std::array{}}), false ) + CHECK_EQUAL( (public_key{0, std::array{1}} != public_key{0, std::array{}}), true ) silence_output(false); EOSIO_TEST_END @@ -26,21 +27,21 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(signature_type_test) silence_output(true); + // --------------------------------------------------- // bool operator==(const signature&, const signature&) - REQUIRE_EQUAL( (signature{0, std::array{}} == signature{0, std::array{}}), true ) - REQUIRE_EQUAL( (signature{0, std::array{1}} == signature{0, std::array{}}), false ) + CHECK_EQUAL( (signature{0, std::array{}} == signature{0, std::array{}}), true ) + CHECK_EQUAL( (signature{0, std::array{1}} == signature{0, std::array{}}), false ) + // --------------------------------------------------- // bool operator!=(const signature&, const signature&) - REQUIRE_EQUAL( (signature{0, std::array{1}} != signature{0, std::array{}}), true ) - REQUIRE_EQUAL( (signature{0, std::array{}} != signature{0, std::array{}}), false ) + CHECK_EQUAL( (signature{0, std::array{1}} != signature{0, std::array{}}), true ) + CHECK_EQUAL( (signature{0, std::array{}} != signature{0, std::array{}}), false ) silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { - EOSIO_TEST(public_key_type_test) EOSIO_TEST(signature_type_test) - return has_failed(); } diff --git a/tests/unit/eosio_tests.cpp b/tests/unit/eosio_tests.cpp index ad6d1dfd97..c3125876ca 100644 --- a/tests/unit/eosio_tests.cpp +++ b/tests/unit/eosio_tests.cpp @@ -1,5 +1,4 @@ #include - #include using namespace eosio::native; diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index acca6e1c01..59467b41b4 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -10,115 +10,112 @@ const uint64_t u64max = std::numeric_limits::max(); // 184467440737095 EOSIO_TEST_BEGIN(name_type_test) silence_output(true); - // ------------ - // constructors + //// constexpr name() + CHECK_EQUAL( name{}.value, 0ULL ) - /// constexpr name() - REQUIRE_EQUAL( name{}.value, 0ULL ) + //// constexpr explicit name(uint64_t) + CHECK_EQUAL( name{0ULL}.value, 0ULL ) + CHECK_EQUAL( name{1ULL}.value, 1ULL ) + CHECK_EQUAL( name{u64max}.value, u64max ) - /// constexpr explicit name(uint64_t) - REQUIRE_EQUAL( name{0ULL}.value, 0ULL ) - REQUIRE_EQUAL( name{1ULL}.value, 1ULL ) - REQUIRE_EQUAL( name{u64max}.value, u64max ) - - /// constexpr explicit name(name::raw) - REQUIRE_EQUAL( name{name::raw{0ULL}}.value, 0ULL ) - REQUIRE_EQUAL( name{name::raw{1ULL}}.value, 1ULL ) - REQUIRE_EQUAL( name{name::raw{u64max}}.value, u64max ) + //// constexpr explicit name(name::raw) + CHECK_EQUAL( name{name::raw{0ULL}}.value, 0ULL ) + CHECK_EQUAL( name{name::raw{1ULL}}.value, 1ULL ) + CHECK_EQUAL( name{name::raw{u64max}}.value, u64max ) // Note that these are the exact `uint64_t` value representations of the given string - /// constexpr explicit name(std::string_view) - REQUIRE_EQUAL( name{"1"}.value, 576460752303423488ULL ) - REQUIRE_EQUAL( name{"5"}.value, 2882303761517117440ULL ) - REQUIRE_EQUAL( name{"a"}.value, 3458764513820540928ULL ) - REQUIRE_EQUAL( name{"z"}.value, 17870283321406128128ULL ) - - REQUIRE_EQUAL( name{"abc"}.value, 3589368903014285312ULL ) - REQUIRE_EQUAL( name{"123"}.value, 614178399182651392ULL ) - - REQUIRE_EQUAL( name{".abc"}.value, 112167778219196416ULL ) - REQUIRE_EQUAL( name{".........abc"}.value, 102016ULL ) - REQUIRE_EQUAL( name{"123."}.value, 614178399182651392ULL ) - REQUIRE_EQUAL( name{"123........."}.value, 614178399182651392ULL ) - REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ) + //// constexpr explicit name(std::string_view) + CHECK_EQUAL( name{"1"}.value, 576460752303423488ULL ) + CHECK_EQUAL( name{"5"}.value, 2882303761517117440ULL ) + CHECK_EQUAL( name{"a"}.value, 3458764513820540928ULL ) + CHECK_EQUAL( name{"z"}.value, 17870283321406128128ULL ) + + CHECK_EQUAL( name{"abc"}.value, 3589368903014285312ULL ) + CHECK_EQUAL( name{"123"}.value, 614178399182651392ULL ) + + CHECK_EQUAL( name{".abc"}.value, 112167778219196416ULL ) + CHECK_EQUAL( name{".........abc"}.value, 102016ULL ) + CHECK_EQUAL( name{"123."}.value, 614178399182651392ULL ) + CHECK_EQUAL( name{"123........."}.value, 614178399182651392ULL ) + CHECK_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ) - REQUIRE_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ) - REQUIRE_EQUAL( name{"123.abc"}.value, 614181822271586304ULL ) + CHECK_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ) + CHECK_EQUAL( name{"123.abc"}.value, 614181822271586304ULL ) - REQUIRE_EQUAL( name{"12345abcdefgj"}.value, 614251623682315983ULL ) - REQUIRE_EQUAL( name{"hijklmnopqrsj"}.value, 7754926748989239183ULL ) - REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.value, 14895601873741973071ULL ) + CHECK_EQUAL( name{"12345abcdefgj"}.value, 614251623682315983ULL ) + CHECK_EQUAL( name{"hijklmnopqrsj"}.value, 7754926748989239183ULL ) + CHECK_EQUAL( name{"tuvwxyz.1234j"}.value, 14895601873741973071ULL ) - REQUIRE_EQUAL( name{"111111111111j"}.value, 595056260442243615ULL ) - REQUIRE_EQUAL( name{"555555555555j"}.value, 2975281302211218015ULL ) - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.value, 3570337562653461615ULL ) - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.value, u64max ) + CHECK_EQUAL( name{"111111111111j"}.value, 595056260442243615ULL ) + CHECK_EQUAL( name{"555555555555j"}.value, 2975281302211218015ULL ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.value, 3570337562653461615ULL ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.value, u64max ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ) - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ) - REQUIRE_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ) - REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"-1"};}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"0"};}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name{"6"};}) ) + CHECK_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"111111111111k"};}) ) + CHECK_ASSERT( "thirteenth character in name cannot be a letter that comes after j", ([]() {name{"zzzzzzzzzzzzk"};}) ) + CHECK_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"};}) ) // -------------------------------------------- // static constexpr uint8_t char_to_value(char) char c{'.'}; uint8_t expected_value{}; // Will increment to the expected correct value in the set [0,32) - REQUIRE_EQUAL( name::char_to_value(c), expected_value ) + CHECK_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; - for(c = '1'; c <= '5'; ++c ) { - REQUIRE_EQUAL( name::char_to_value(c), expected_value ) + for(c = '1'; c <= '5'; ++c) { + CHECK_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; } - for(c = 'a'; c <= 'z'; ++c ) { - REQUIRE_EQUAL( name::char_to_value(c), expected_value ) + for(c = 'a'; c <= 'z'; ++c) { + CHECK_EQUAL( name::char_to_value(c), expected_value ) ++expected_value; } - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ) - REQUIRE_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'-'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'/'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'6'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'A'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'Z'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'`'});}) ) + CHECK_ASSERT( "character is not in allowed character set for names", ([]() {name::char_to_value(char{'{'});}) ); // ------------------------------- // constexpr uint8_t length()cosnt - REQUIRE_EQUAL( name{""}.length(), 0 ) - REQUIRE_EQUAL( name{"e"}.length(), 1 ) - REQUIRE_EQUAL( name{"eo"}.length(), 2 ) - REQUIRE_EQUAL( name{"eos"}.length(), 3 ) - REQUIRE_EQUAL( name{"eosi"}.length(), 4 ) - REQUIRE_EQUAL( name{"eosio"}.length(), 5 ) - REQUIRE_EQUAL( name{"eosioa"}.length(), 6 ) - REQUIRE_EQUAL( name{"eosioac"}.length(), 7 ) - REQUIRE_EQUAL( name{"eosioacc"}.length(), 8 ) - REQUIRE_EQUAL( name{"eosioacco"}.length(), 9 ) - REQUIRE_EQUAL( name{"eosioaccou"}.length(), 10 ) - REQUIRE_EQUAL( name{"eosioaccoun"}.length(), 11 ) - REQUIRE_EQUAL( name{"eosioaccount"}.length(), 12 ) - REQUIRE_EQUAL( name{"eosioaccountj"}.length(), 13 ) - - REQUIRE_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ) + CHECK_EQUAL( name{""}.length(), 0 ) + CHECK_EQUAL( name{"e"}.length(), 1 ) + CHECK_EQUAL( name{"eo"}.length(), 2 ) + CHECK_EQUAL( name{"eos"}.length(), 3 ) + CHECK_EQUAL( name{"eosi"}.length(), 4 ) + CHECK_EQUAL( name{"eosio"}.length(), 5 ) + CHECK_EQUAL( name{"eosioa"}.length(), 6 ) + CHECK_EQUAL( name{"eosioac"}.length(), 7 ) + CHECK_EQUAL( name{"eosioacc"}.length(), 8 ) + CHECK_EQUAL( name{"eosioacco"}.length(), 9 ) + CHECK_EQUAL( name{"eosioaccou"}.length(), 10 ) + CHECK_EQUAL( name{"eosioaccoun"}.length(), 11 ) + CHECK_EQUAL( name{"eosioaccount"}.length(), 12 ) + CHECK_EQUAL( name{"eosioaccountj"}.length(), 13 ) + + CHECK_ASSERT( "string is too long to be a valid name", ([]() {name{"12345abcdefghj"}.length();}) ) // ---------------------------- // constexpr name suffix()const - REQUIRE_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ) - REQUIRE_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ) - REQUIRE_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ) - REQUIRE_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ) - REQUIRE_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ) - REQUIRE_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ) - REQUIRE_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ) - REQUIRE_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ) - REQUIRE_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ) - REQUIRE_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ) - REQUIRE_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ) - REQUIRE_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ) + CHECK_EQUAL( name{".eosioaccounj"}.suffix(), name{"eosioaccounj"} ) + CHECK_EQUAL( name{"e.osioaccounj"}.suffix(), name{"osioaccounj"} ) + CHECK_EQUAL( name{"eo.sioaccounj"}.suffix(), name{"sioaccounj"} ) + CHECK_EQUAL( name{"eos.ioaccounj"}.suffix(), name{"ioaccounj"} ) + CHECK_EQUAL( name{"eosi.oaccounj"}.suffix(), name{"oaccounj"} ) + CHECK_EQUAL( name{"eosio.accounj"}.suffix(), name{"accounj"} ) + CHECK_EQUAL( name{"eosioa.ccounj"}.suffix(), name{"ccounj"} ) + CHECK_EQUAL( name{"eosioac.counj"}.suffix(), name{"counj"} ) + CHECK_EQUAL( name{"eosioacc.ounj"}.suffix(), name{"ounj"} ) + CHECK_EQUAL( name{"eosioacco.unj"}.suffix(), name{"unj"} ) + CHECK_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ) + CHECK_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ) // Note that this case is ignored because '.' characters at the end of a name are ignored // Make functions perfect mirrors of eachother (01/07/2019) @@ -126,49 +123,49 @@ EOSIO_TEST_BEGIN(name_type_test) // `print_f("Value of expected suffix: \n" );` // `eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" );` - REQUIRE_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ) - REQUIRE_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ) + CHECK_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ) + CHECK_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ) // ----------------------------- // constexpr operator raw()const - REQUIRE_EQUAL( name{"1"}.operator name::raw(), static_cast(576460752303423488ULL) ) - REQUIRE_EQUAL( name{"5"}.operator name::raw(), static_cast(2882303761517117440ULL) ) - REQUIRE_EQUAL( name{"a"}.operator name::raw(), static_cast(3458764513820540928ULL) ) - REQUIRE_EQUAL( name{"z"}.operator name::raw(), static_cast(17870283321406128128ULL) ) - - REQUIRE_EQUAL( name{"abc"}.operator name::raw(), static_cast(3589368903014285312ULL) ) - REQUIRE_EQUAL( name{"123"}.operator name::raw(), static_cast(614178399182651392ULL) ) - - REQUIRE_EQUAL( name{".abc"}.operator name::raw(), static_cast(112167778219196416ULL) ) - REQUIRE_EQUAL( name{".........abc"}.operator name::raw(), static_cast(102016ULL) ) - REQUIRE_EQUAL( name{"123."}.operator name::raw(), static_cast(614178399182651392ULL) ) - REQUIRE_EQUAL( name{"123........."}.operator name::raw(), static_cast(614178399182651392ULL) ) - REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.operator name::raw(), static_cast(108209673814966320ULL) ) - - REQUIRE_EQUAL( name{"abc.123"}.operator name::raw(), static_cast(3589369488740450304ULL) ) - REQUIRE_EQUAL( name{"123.abc"}.operator name::raw(), static_cast(614181822271586304ULL) ) - - REQUIRE_EQUAL( name{"12345abcdefgj"}.operator name::raw(), static_cast(614251623682315983ULL) ) - REQUIRE_EQUAL( name{"hijklmnopqrsj"}.operator name::raw(), static_cast(7754926748989239183ULL) ) - REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.operator name::raw(), static_cast(14895601873741973071ULL) ) - - REQUIRE_EQUAL( name{"111111111111j"}.operator name::raw(), static_cast(595056260442243615ULL) ) - REQUIRE_EQUAL( name{"555555555555j"}.operator name::raw(), static_cast(2975281302211218015ULL) ) - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.operator name::raw(), static_cast(3570337562653461615ULL) ) - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.operator name::raw(), static_cast(u64max) ) + CHECK_EQUAL( name{"1"}.operator name::raw(), static_cast(576460752303423488ULL) ) + CHECK_EQUAL( name{"5"}.operator name::raw(), static_cast(2882303761517117440ULL) ) + CHECK_EQUAL( name{"a"}.operator name::raw(), static_cast(3458764513820540928ULL) ) + CHECK_EQUAL( name{"z"}.operator name::raw(), static_cast(17870283321406128128ULL) ) + + CHECK_EQUAL( name{"abc"}.operator name::raw(), static_cast(3589368903014285312ULL) ) + CHECK_EQUAL( name{"123"}.operator name::raw(), static_cast(614178399182651392ULL) ) + + CHECK_EQUAL( name{".abc"}.operator name::raw(), static_cast(112167778219196416ULL) ) + CHECK_EQUAL( name{".........abc"}.operator name::raw(), static_cast(102016ULL) ) + CHECK_EQUAL( name{"123."}.operator name::raw(), static_cast(614178399182651392ULL) ) + CHECK_EQUAL( name{"123........."}.operator name::raw(), static_cast(614178399182651392ULL) ) + CHECK_EQUAL( name{".a.b.c.1.2.3."}.operator name::raw(), static_cast(108209673814966320ULL) ) + + CHECK_EQUAL( name{"abc.123"}.operator name::raw(), static_cast(3589369488740450304ULL) ) + CHECK_EQUAL( name{"123.abc"}.operator name::raw(), static_cast(614181822271586304ULL) ) + + CHECK_EQUAL( name{"12345abcdefgj"}.operator name::raw(), static_cast(614251623682315983ULL) ) + CHECK_EQUAL( name{"hijklmnopqrsj"}.operator name::raw(), static_cast(7754926748989239183ULL) ) + CHECK_EQUAL( name{"tuvwxyz.1234j"}.operator name::raw(), static_cast(14895601873741973071ULL) ) + + CHECK_EQUAL( name{"111111111111j"}.operator name::raw(), static_cast(595056260442243615ULL) ) + CHECK_EQUAL( name{"555555555555j"}.operator name::raw(), static_cast(2975281302211218015ULL) ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.operator name::raw(), static_cast(3570337562653461615ULL) ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.operator name::raw(), static_cast(u64max) ) // --------------------------------------- // constexpr explicit operator bool()const // Note that I must be explicit about calling the operator because it is defined as `explicit` - REQUIRE_EQUAL( name{0}.operator bool(), false ) - REQUIRE_EQUAL( name{1}.operator bool(), true ) - REQUIRE_EQUAL( !name{0}.operator bool(), true ) - REQUIRE_EQUAL( !name{1}.operator bool(), false ) + CHECK_EQUAL( name{0}.operator bool(), false ) + CHECK_EQUAL( name{1}.operator bool(), true ) + CHECK_EQUAL( !name{0}.operator bool(), true ) + CHECK_EQUAL( !name{1}.operator bool(), false ) - REQUIRE_EQUAL( name{""}.operator bool(), false ) - REQUIRE_EQUAL( name{"1"}.operator bool(), true ) - REQUIRE_EQUAL( !name{""}.operator bool(), true ) - REQUIRE_EQUAL( !name{"1"}.operator bool(), false ) + CHECK_EQUAL( name{""}.operator bool(), false ) + CHECK_EQUAL( name{"1"}.operator bool(), true ) + CHECK_EQUAL( !name{""}.operator bool(), true ) + CHECK_EQUAL( !name{"1"}.operator bool(), false ) // ---------------------------------------- // char* write_as_string(char*, char*)const @@ -176,193 +173,193 @@ EOSIO_TEST_BEGIN(name_type_test) std::string test_str{"1"}; name{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // Note that any '.' characters at the end of a name are ignored name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ) + CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) name{test_str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp("123", buffer, 3), 0 ) + CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) name{test_str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ) + CHECK_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ) name{test_str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // ---------------------------- // std::string to_string()const - REQUIRE_EQUAL( name{"1"}.to_string(), "1" ) - REQUIRE_EQUAL( name{"5"}.to_string(), "5" ) - REQUIRE_EQUAL( name{"a"}.to_string(), "a" ) - REQUIRE_EQUAL( name{"z"}.to_string(), "z" ) - - REQUIRE_EQUAL( name{"abc"}.to_string(), "abc" ) - REQUIRE_EQUAL( name{"123"}.to_string(), "123" ) - - REQUIRE_EQUAL( name{".abc"}.to_string(), ".abc" ) - REQUIRE_EQUAL( name{".........abc"}.to_string(), ".........abc" ) - REQUIRE_EQUAL( name{"123."}.to_string(), "123" ) - REQUIRE_EQUAL( name{"123........."}.to_string(), "123" ) - REQUIRE_EQUAL( name{".a.b.c.1.2.3."}.to_string(), ".a.b.c.1.2.3" ) + CHECK_EQUAL( name{"1"}.to_string(), "1" ) + CHECK_EQUAL( name{"5"}.to_string(), "5" ) + CHECK_EQUAL( name{"a"}.to_string(), "a" ) + CHECK_EQUAL( name{"z"}.to_string(), "z" ) + + CHECK_EQUAL( name{"abc"}.to_string(), "abc" ) + CHECK_EQUAL( name{"123"}.to_string(), "123" ) + + CHECK_EQUAL( name{".abc"}.to_string(), ".abc" ) + CHECK_EQUAL( name{".........abc"}.to_string(), ".........abc" ) + CHECK_EQUAL( name{"123."}.to_string(), "123" ) + CHECK_EQUAL( name{"123........."}.to_string(), "123" ) + CHECK_EQUAL( name{".a.b.c.1.2.3."}.to_string(), ".a.b.c.1.2.3" ) - REQUIRE_EQUAL( name{"abc.123"}.to_string(), "abc.123" ) - REQUIRE_EQUAL( name{"123.abc"}.to_string(), "123.abc" ) + CHECK_EQUAL( name{"abc.123"}.to_string(), "abc.123" ) + CHECK_EQUAL( name{"123.abc"}.to_string(), "123.abc" ) - REQUIRE_EQUAL( name{"12345abcdefgj"}.to_string(), "12345abcdefgj" ) - REQUIRE_EQUAL( name{"hijklmnopqrsj"}.to_string(), "hijklmnopqrsj" ) - REQUIRE_EQUAL( name{"tuvwxyz.1234j"}.to_string(), "tuvwxyz.1234j" ) + CHECK_EQUAL( name{"12345abcdefgj"}.to_string(), "12345abcdefgj" ) + CHECK_EQUAL( name{"hijklmnopqrsj"}.to_string(), "hijklmnopqrsj" ) + CHECK_EQUAL( name{"tuvwxyz.1234j"}.to_string(), "tuvwxyz.1234j" ) - REQUIRE_EQUAL( name{"111111111111j"}.to_string(), "111111111111j" ) - REQUIRE_EQUAL( name{"555555555555j"}.to_string(), "555555555555j" ) - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ) - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ) + CHECK_EQUAL( name{"111111111111j"}.to_string(), "111111111111j" ) + CHECK_EQUAL( name{"555555555555j"}.to_string(), "555555555555j" ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"}.to_string(), "aaaaaaaaaaaaj" ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"}.to_string(), "zzzzzzzzzzzzj" ) // ---------------------------------------------------------- // friend constexpr bool operator==(const name&, const name&) - REQUIRE_EQUAL( name{"1"} == name{"1"}, true ) - REQUIRE_EQUAL( name{"5"} == name{"5"}, true ) - REQUIRE_EQUAL( name{"a"} == name{"a"}, true ) - REQUIRE_EQUAL( name{"z"} == name{"z"}, true ) - - REQUIRE_EQUAL( name{"abc"} == name{"abc"}, true ) - REQUIRE_EQUAL( name{"123"} == name{"123"}, true ) - - REQUIRE_EQUAL( name{".abc"} == name{".abc"}, true ) - REQUIRE_EQUAL( name{".........abc"} == name{".........abc"}, true ) - REQUIRE_EQUAL( name{"123."} == name{"123"}, true ) - REQUIRE_EQUAL( name{"123........."} == name{"123"}, true ) - REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == name{".a.b.c.1.2.3"}, true ) + CHECK_EQUAL( name{"1"} == name{"1"}, true ) + CHECK_EQUAL( name{"5"} == name{"5"}, true ) + CHECK_EQUAL( name{"a"} == name{"a"}, true ) + CHECK_EQUAL( name{"z"} == name{"z"}, true ) + + CHECK_EQUAL( name{"abc"} == name{"abc"}, true ) + CHECK_EQUAL( name{"123"} == name{"123"}, true ) + + CHECK_EQUAL( name{".abc"} == name{".abc"}, true ) + CHECK_EQUAL( name{".........abc"} == name{".........abc"}, true ) + CHECK_EQUAL( name{"123."} == name{"123"}, true ) + CHECK_EQUAL( name{"123........."} == name{"123"}, true ) + CHECK_EQUAL( name{".a.b.c.1.2.3."} == name{".a.b.c.1.2.3"}, true ) - REQUIRE_EQUAL( name{"abc.123"} == name{"abc.123"}, true ) - REQUIRE_EQUAL( name{"123.abc"} == name{"123.abc"}, true ) + CHECK_EQUAL( name{"abc.123"} == name{"abc.123"}, true ) + CHECK_EQUAL( name{"123.abc"} == name{"123.abc"}, true ) - REQUIRE_EQUAL( name{"12345abcdefgj"} == name{"12345abcdefgj"}, true ) - REQUIRE_EQUAL( name{"hijklmnopqrsj"} == name{"hijklmnopqrsj"}, true ) - REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == name{"tuvwxyz.1234j"}, true ) + CHECK_EQUAL( name{"12345abcdefgj"} == name{"12345abcdefgj"}, true ) + CHECK_EQUAL( name{"hijklmnopqrsj"} == name{"hijklmnopqrsj"}, true ) + CHECK_EQUAL( name{"tuvwxyz.1234j"} == name{"tuvwxyz.1234j"}, true ) - REQUIRE_EQUAL( name{"111111111111j"} == name{"111111111111j"}, true ) - REQUIRE_EQUAL( name{"555555555555j"} == name{"555555555555j"}, true ) - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ) - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ) + CHECK_EQUAL( name{"111111111111j"} == name{"111111111111j"}, true ) + CHECK_EQUAL( name{"555555555555j"} == name{"555555555555j"}, true ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"} == name{"aaaaaaaaaaaaj"}, true ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"} == name{"zzzzzzzzzzzzj"}, true ) // ----------------------------------------------------------- // friend constexpr bool operator!=(const name&, const name&) - REQUIRE_EQUAL( name{"1"} != name{}, true ) - REQUIRE_EQUAL( name{"5"} != name{}, true ) - REQUIRE_EQUAL( name{"a"} != name{}, true ) - REQUIRE_EQUAL( name{"z"} != name{}, true ) - - REQUIRE_EQUAL( name{"abc"} != name{}, true ) - REQUIRE_EQUAL( name{"123"} != name{}, true ) - - REQUIRE_EQUAL( name{".abc"} != name{}, true ) - REQUIRE_EQUAL( name{".........abc"} != name{}, true ) - REQUIRE_EQUAL( name{"123."} != name{}, true ) - REQUIRE_EQUAL( name{"123........."} != name{}, true ) - REQUIRE_EQUAL( name{".a.b.c.1.2.3."} != name{}, true ) + CHECK_EQUAL( name{"1"} != name{}, true ) + CHECK_EQUAL( name{"5"} != name{}, true ) + CHECK_EQUAL( name{"a"} != name{}, true ) + CHECK_EQUAL( name{"z"} != name{}, true ) + + CHECK_EQUAL( name{"abc"} != name{}, true ) + CHECK_EQUAL( name{"123"} != name{}, true ) + + CHECK_EQUAL( name{".abc"} != name{}, true ) + CHECK_EQUAL( name{".........abc"} != name{}, true ) + CHECK_EQUAL( name{"123."} != name{}, true ) + CHECK_EQUAL( name{"123........."} != name{}, true ) + CHECK_EQUAL( name{".a.b.c.1.2.3."} != name{}, true ) - REQUIRE_EQUAL( name{"abc.123"} != name{}, true ) - REQUIRE_EQUAL( name{"123.abc"} != name{}, true ) + CHECK_EQUAL( name{"abc.123"} != name{}, true ) + CHECK_EQUAL( name{"123.abc"} != name{}, true ) - REQUIRE_EQUAL( name{"12345abcdefgj"} != name{}, true ) - REQUIRE_EQUAL( name{"hijklmnopqrsj"} != name{}, true ) - REQUIRE_EQUAL( name{"tuvwxyz.1234j"} != name{}, true ) + CHECK_EQUAL( name{"12345abcdefgj"} != name{}, true ) + CHECK_EQUAL( name{"hijklmnopqrsj"} != name{}, true ) + CHECK_EQUAL( name{"tuvwxyz.1234j"} != name{}, true ) - REQUIRE_EQUAL( name{"111111111111j"} != name{}, true ) - REQUIRE_EQUAL( name{"555555555555j"} != name{}, true ) - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} != name{}, true ) - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} != name{}, true ) + CHECK_EQUAL( name{"111111111111j"} != name{}, true ) + CHECK_EQUAL( name{"555555555555j"} != name{}, true ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"} != name{}, true ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"} != name{}, true ) // --------------------------------------------------------- // friend constexpr bool operator<(const name&, const name&) - REQUIRE_EQUAL( name{} < name{"1"}, true ) - REQUIRE_EQUAL( name{} < name{"5"}, true ) - REQUIRE_EQUAL( name{} < name{"a"}, true ) - REQUIRE_EQUAL( name{} < name{"z"}, true ) - - REQUIRE_EQUAL( name{} < name{"abc"}, true ) - REQUIRE_EQUAL( name{} < name{"123"}, true ) - - REQUIRE_EQUAL( name{} < name{".abc"}, true ) - REQUIRE_EQUAL( name{} < name{".........abc"}, true ) - REQUIRE_EQUAL( name{} < name{"123."}, true ) - REQUIRE_EQUAL( name{} < name{"123........."}, true ) - REQUIRE_EQUAL( name{} < name{".a.b.c.1.2.3."}, true ) + CHECK_EQUAL( name{} < name{"1"}, true ) + CHECK_EQUAL( name{} < name{"5"}, true ) + CHECK_EQUAL( name{} < name{"a"}, true ) + CHECK_EQUAL( name{} < name{"z"}, true ) + + CHECK_EQUAL( name{} < name{"abc"}, true ) + CHECK_EQUAL( name{} < name{"123"}, true ) + + CHECK_EQUAL( name{} < name{".abc"}, true ) + CHECK_EQUAL( name{} < name{".........abc"}, true ) + CHECK_EQUAL( name{} < name{"123."}, true ) + CHECK_EQUAL( name{} < name{"123........."}, true ) + CHECK_EQUAL( name{} < name{".a.b.c.1.2.3."}, true ) - REQUIRE_EQUAL( name{} < name{"abc.123"}, true ) - REQUIRE_EQUAL( name{} < name{"123.abc"}, true ) + CHECK_EQUAL( name{} < name{"abc.123"}, true ) + CHECK_EQUAL( name{} < name{"123.abc"}, true ) - REQUIRE_EQUAL( name{} < name{"12345abcdefgj"}, true ) - REQUIRE_EQUAL( name{} < name{"hijklmnopqrsj"}, true ) - REQUIRE_EQUAL( name{} < name{"tuvwxyz.1234j"}, true ) + CHECK_EQUAL( name{} < name{"12345abcdefgj"}, true ) + CHECK_EQUAL( name{} < name{"hijklmnopqrsj"}, true ) + CHECK_EQUAL( name{} < name{"tuvwxyz.1234j"}, true ) - REQUIRE_EQUAL( name{} < name{"111111111111j"}, true ) - REQUIRE_EQUAL( name{} < name{"555555555555j"}, true ) - REQUIRE_EQUAL( name{} < name{"aaaaaaaaaaaaj"}, true ) - REQUIRE_EQUAL( name{} < name{"zzzzzzzzzzzzj"}, true ) + CHECK_EQUAL( name{} < name{"111111111111j"}, true ) + CHECK_EQUAL( name{} < name{"555555555555j"}, true ) + CHECK_EQUAL( name{} < name{"aaaaaaaaaaaaj"}, true ) + CHECK_EQUAL( name{} < name{"zzzzzzzzzzzzj"}, true ) // ------------------------------------ // inline constexpr name operator""_n() - REQUIRE_EQUAL( name{} == ""_n, true ) + CHECK_EQUAL( name{} == ""_n, true ) - REQUIRE_EQUAL( name{"1"} == "1"_n, true ) - REQUIRE_EQUAL( name{"5"} == "5"_n, true ) - REQUIRE_EQUAL( name{"a"} == "a"_n, true ) - REQUIRE_EQUAL( name{"z"} == "z"_n, true ) - - REQUIRE_EQUAL( name{"abc"} == "abc"_n, true ) - REQUIRE_EQUAL( name{"123"} == "123"_n, true ) - - REQUIRE_EQUAL( name{".abc"} == ".abc"_n, true ) - REQUIRE_EQUAL( name{".........abc"} == ".........abc"_n, true ) - REQUIRE_EQUAL( name{"123."} == "123."_n, true ) - REQUIRE_EQUAL( name{"123........."} == "123........."_n, true ) - REQUIRE_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ) + CHECK_EQUAL( name{"1"} == "1"_n, true ) + CHECK_EQUAL( name{"5"} == "5"_n, true ) + CHECK_EQUAL( name{"a"} == "a"_n, true ) + CHECK_EQUAL( name{"z"} == "z"_n, true ) + + CHECK_EQUAL( name{"abc"} == "abc"_n, true ) + CHECK_EQUAL( name{"123"} == "123"_n, true ) + + CHECK_EQUAL( name{".abc"} == ".abc"_n, true ) + CHECK_EQUAL( name{".........abc"} == ".........abc"_n, true ) + CHECK_EQUAL( name{"123."} == "123."_n, true ) + CHECK_EQUAL( name{"123........."} == "123........."_n, true ) + CHECK_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ) - REQUIRE_EQUAL( name{"abc.123"} == "abc.123"_n, true ) - REQUIRE_EQUAL( name{"123.abc"} == "123.abc"_n, true ) + CHECK_EQUAL( name{"abc.123"} == "abc.123"_n, true ) + CHECK_EQUAL( name{"123.abc"} == "123.abc"_n, true ) - REQUIRE_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ) - REQUIRE_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ) - REQUIRE_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ) + CHECK_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ) + CHECK_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ) + CHECK_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ) - REQUIRE_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ) - REQUIRE_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ) - REQUIRE_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ) - REQUIRE_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ) + CHECK_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ) + CHECK_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index f31c0d4967..4e0e111487 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -1,5 +1,4 @@ #include - #include using eosio::name; @@ -14,61 +13,59 @@ const uint64_t u64max = std::numeric_limits::max(); // 184467440737095 EOSIO_TEST_BEGIN(symbol_code_type_test) silence_output(true); - // ------------------------------------------ - // constructors/constexpr uint64_t raw()const - - /// constexpr symbol_code() - REQUIRE_EQUAL( symbol_code{}.raw(), 0ULL ) + //// constexpr symbol_code() + // constexpr uint64_t raw()const + CHECK_EQUAL( symbol_code{}.raw(), 0ULL ) - /// constexpr explicit symbol_code(uint64_t raw) - REQUIRE_EQUAL( symbol_code{0ULL}.raw(), 0ULL ) - REQUIRE_EQUAL( symbol_code{1ULL}.raw(), 1ULL ) - REQUIRE_EQUAL( symbol_code{u64max}.raw(), u64max ) + //// constexpr explicit symbol_code(uint64_t raw) + CHECK_EQUAL( symbol_code{0ULL}.raw(), 0ULL ) + CHECK_EQUAL( symbol_code{1ULL}.raw(), 1ULL ) + CHECK_EQUAL( symbol_code{u64max}.raw(), u64max ) - /// constexpr explicit symbol_code(std::string_view str) - REQUIRE_EQUAL( symbol_code{"A"}.raw(), 65ULL ) - REQUIRE_EQUAL( symbol_code{"Z"}.raw(), 90ULL ) - REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ) - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.raw(), 25432092013386330ULL ) + //// constexpr explicit symbol_code(std::string_view str) + CHECK_EQUAL( symbol_code{"A"}.raw(), 65ULL ) + CHECK_EQUAL( symbol_code{"Z"}.raw(), 90ULL ) + CHECK_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"}.raw(), 25432092013386330ULL ) - REQUIRE_ASSERT( "string is too long to be a valid symbol_code", ([]() {symbol_code{"ABCDEFGH"};}) ) - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"a"};}) ) - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"z"};}) ) - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ) - REQUIRE_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ) + CHECK_ASSERT( "string is too long to be a valid symbol_code", ([]() {symbol_code{"ABCDEFGH"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"a"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"z"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"@"};}) ) + CHECK_ASSERT( "only uppercase letters allowed in symbol_code string", ([]() {symbol_code{"["};}) ) // ------------------------------ // constexpr bool is_valid()const - REQUIRE_EQUAL( symbol_code{65ULL}.is_valid(), true ) // "A" - REQUIRE_EQUAL( symbol_code{90ULL}.is_valid(), true ) // "Z" - REQUIRE_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ) // "AAAAAAA" - REQUIRE_EQUAL( symbol_code{25432092013386330ULL}.is_valid(), true ) // "ZZZZZZZ" + CHECK_EQUAL( symbol_code{65ULL}.is_valid(), true ) // "A" + CHECK_EQUAL( symbol_code{90ULL}.is_valid(), true ) // "Z" + CHECK_EQUAL( symbol_code{18367622009667905ULL}.is_valid(), true ) // "AAAAAAA" + CHECK_EQUAL( symbol_code{25432092013386330ULL}.is_valid(), true ) // "ZZZZZZZ" - REQUIRE_EQUAL( symbol_code{64ULL}.is_valid(), false ) - REQUIRE_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ) + CHECK_EQUAL( symbol_code{64ULL}.is_valid(), false ) + CHECK_EQUAL( symbol_code{25432092013386331ULL}.is_valid(), false ) // -------------------------------- // constexpr uint32_t length()const - REQUIRE_EQUAL( symbol_code{""}.length(), 0 ) - REQUIRE_EQUAL( symbol_code{"S"}.length(), 1 ) - REQUIRE_EQUAL( symbol_code{"SY"}.length(), 2 ) - REQUIRE_EQUAL( symbol_code{"SYM"}.length(), 3 ) - REQUIRE_EQUAL( symbol_code{"SYMB"}.length(), 4 ) - REQUIRE_EQUAL( symbol_code{"SYMBO"}.length(), 5 ) - REQUIRE_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ) - REQUIRE_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ) + CHECK_EQUAL( symbol_code{""}.length(), 0 ) + CHECK_EQUAL( symbol_code{"S"}.length(), 1 ) + CHECK_EQUAL( symbol_code{"SY"}.length(), 2 ) + CHECK_EQUAL( symbol_code{"SYM"}.length(), 3 ) + CHECK_EQUAL( symbol_code{"SYMB"}.length(), 4 ) + CHECK_EQUAL( symbol_code{"SYMBO"}.length(), 5 ) + CHECK_EQUAL( symbol_code{"SYMBOL"}.length(), 6 ) + CHECK_EQUAL( symbol_code{"SYMBOLL"}.length(), 7 ) // --------------------------------------- // constexpr explicit operator bool()const - REQUIRE_EQUAL( symbol_code{0ULL}.operator bool(), false ) - REQUIRE_EQUAL( symbol_code{1ULL}.operator bool(), true ) - REQUIRE_EQUAL( !symbol_code{0ULL}.operator bool(), true ) - REQUIRE_EQUAL( !symbol_code{1ULL}.operator bool(), false ) + CHECK_EQUAL( symbol_code{0ULL}.operator bool(), false ) + CHECK_EQUAL( symbol_code{1ULL}.operator bool(), true ) + CHECK_EQUAL( !symbol_code{0ULL}.operator bool(), true ) + CHECK_EQUAL( !symbol_code{1ULL}.operator bool(), false ) - REQUIRE_EQUAL( symbol_code{""}.operator bool(), false ) - REQUIRE_EQUAL( symbol_code{"SYMBOL"}.operator bool(), true ) - REQUIRE_EQUAL( !symbol_code{""}.operator bool(), true ) - REQUIRE_EQUAL( !symbol_code{"SYMBOL"}.operator bool(), false ) + CHECK_EQUAL( symbol_code{""}.operator bool(), false ) + CHECK_EQUAL( symbol_code{"SYMBOL"}.operator bool(), true ) + CHECK_EQUAL( !symbol_code{""}.operator bool(), true ) + CHECK_EQUAL( !symbol_code{"SYMBOL"}.operator bool(), false ) // ---------------------------------------- // char* write_as_string(char*, char*)const @@ -76,41 +73,41 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) std::string test_str{"A"}; symbol_code{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "Z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "AAAAAAA"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "ZZZZZZZ"}.write_as_string( buffer, buffer + sizeof(buffer) ); - REQUIRE_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) + CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) // ---------------------------- // std::string to_string()const - REQUIRE_EQUAL( symbol_code{"A"}.to_string(), "A" ) - REQUIRE_EQUAL( symbol_code{"Z"}.to_string(), "Z" ) - REQUIRE_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ) - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ) + CHECK_EQUAL( symbol_code{"A"}.to_string(), "A" ) + CHECK_EQUAL( symbol_code{"Z"}.to_string(), "Z" ) + CHECK_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"}.to_string(), "ZZZZZZZ" ) // -------------------------------------------------------------- // friend bool operator==(const symbol_code&, const symbol_code&) - REQUIRE_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ) - REQUIRE_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ) - REQUIRE_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ) - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ) + CHECK_EQUAL( symbol_code{"A"} == symbol_code{"A"}, true ) + CHECK_EQUAL( symbol_code{"Z"} == symbol_code{"Z"}, true ) + CHECK_EQUAL( symbol_code{"AAAAAAA"} == symbol_code{"AAAAAAA"}, true ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"} == symbol_code{"ZZZZZZZ"}, true ) // -------------------------------------------------------------- // friend bool operator!=(const symbol_code&, const symbol_code&) - REQUIRE_EQUAL( symbol_code{"A"} != symbol_code{}, true ) - REQUIRE_EQUAL( symbol_code{"Z"} != symbol_code{}, true ) - REQUIRE_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{}, true ) - REQUIRE_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{}, true ) + CHECK_EQUAL( symbol_code{"A"} != symbol_code{}, true ) + CHECK_EQUAL( symbol_code{"Z"} != symbol_code{}, true ) + CHECK_EQUAL( symbol_code{"AAAAAAA"} != symbol_code{}, true ) + CHECK_EQUAL( symbol_code{"ZZZZZZZ"} != symbol_code{}, true ) // ------------------------------------------------------------- // friend bool operator<(const symbol_code&, const symbol_code&) - REQUIRE_EQUAL( symbol_code{} < symbol_code{"A"}, true ) - REQUIRE_EQUAL( symbol_code{} < symbol_code{"Z"}, true ) - REQUIRE_EQUAL( symbol_code{} < symbol_code{"AAAAAAA"}, true ) - REQUIRE_EQUAL( symbol_code{} < symbol_code{"ZZZZZZZ"}, true ) + CHECK_EQUAL( symbol_code{} < symbol_code{"A"}, true ) + CHECK_EQUAL( symbol_code{} < symbol_code{"Z"}, true ) + CHECK_EQUAL( symbol_code{} < symbol_code{"AAAAAAA"}, true ) + CHECK_EQUAL( symbol_code{} < symbol_code{"ZZZZZZZ"}, true ) silence_output(false); EOSIO_TEST_END @@ -124,106 +121,97 @@ EOSIO_TEST_BEGIN(symbol_type_test) symbol_code sc2{"AAAAAAA"}; symbol_code sc3{"ZZZZZZZ"}; - // ------------------------------------------ - // constructors/constexpr uint64_t raw()const + //// constexpr symbol() + // constexpr uint64_t raw()const + CHECK_EQUAL( symbol{}.raw(), 0ULL ) - /// constexpr symbol() - REQUIRE_EQUAL( symbol{}.raw(), 0ULL ) + //// constexpr explicit symbol(uint64_t) + CHECK_EQUAL( symbol{0ULL}.raw(), 0ULL ) + CHECK_EQUAL( symbol{1ULL}.raw(), 1ULL ) + CHECK_EQUAL( symbol{u64max}.raw(), u64max ) - /// constexpr explicit symbol(uint64_t) - REQUIRE_EQUAL( symbol{0ULL}.raw(), 0ULL ) - REQUIRE_EQUAL( symbol{1ULL}.raw(), 1ULL ) - REQUIRE_EQUAL( symbol{u64max}.raw(), u64max ) - - /// constexpr symbol(std::string_view, uint8_t) + //// constexpr symbol(std::string_view, uint8_t) // Note that unless constructed with `initializer_list`, precision does not check for wrap-around - REQUIRE_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) - REQUIRE_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) - REQUIRE_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) - REQUIRE_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) + CHECK_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) + CHECK_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) + CHECK_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) + CHECK_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) - /// constexpr symbol(symbol_code, uint8_t) - REQUIRE_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) - REQUIRE_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) - REQUIRE_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) - REQUIRE_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) + //// constexpr symbol(symbol_code, uint8_t) + CHECK_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) + CHECK_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) + CHECK_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) + CHECK_EQUAL( (symbol{sc3, 0}.raw()), 6510615555426900480ULL ) // -------------------- // bool is_valid()const - REQUIRE_EQUAL( symbol{16640ULL}.is_valid(), true ) // "A", precision: 0 - REQUIRE_EQUAL( symbol{23040ULL}.is_valid(), true ) // "Z", precision: 0 - REQUIRE_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ) // "AAAAAAA", precision: 0 - REQUIRE_EQUAL( symbol{6510615555426900480ULL}.is_valid(), true ) // "ZZZZZZZ", precision: 0 + CHECK_EQUAL( symbol{16640ULL}.is_valid(), true ) // "A", precision: 0 + CHECK_EQUAL( symbol{23040ULL}.is_valid(), true ) // "Z", precision: 0 + CHECK_EQUAL( symbol{4702111234474983680ULL}.is_valid(), true ) // "AAAAAAA", precision: 0 + CHECK_EQUAL( symbol{6510615555426900480ULL}.is_valid(), true ) // "ZZZZZZZ", precision: 0 - REQUIRE_EQUAL( symbol{16639ULL}.is_valid(), false ) - REQUIRE_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ) + CHECK_EQUAL( symbol{16639ULL}.is_valid(), false ) + CHECK_EQUAL( symbol{6510615555426900736ULL}.is_valid(), false ) // ------------------------- // uint8_t precision()const - REQUIRE_EQUAL( (symbol{sc0, 0}.precision()), 0 ) - REQUIRE_EQUAL( (symbol{sc1, 0}.precision()), 0 ) - REQUIRE_EQUAL( (symbol{sc2, 0}.precision()), 0 ) - REQUIRE_EQUAL( (symbol{sc3, 0}.precision()), 0 ) + CHECK_EQUAL( (symbol{sc0,0}.precision()), 0 ) + CHECK_EQUAL( (symbol{sc1,0}.precision()), 0 ) + CHECK_EQUAL( (symbol{sc2,0}.precision()), 0 ) + CHECK_EQUAL( (symbol{sc3,0}.precision()), 0 ) - REQUIRE_EQUAL( (symbol{sc0, 255}.precision()), 255 ) - REQUIRE_EQUAL( (symbol{sc1, 255}.precision()), 255 ) - REQUIRE_EQUAL( (symbol{sc2, 255}.precision()), 255 ) - REQUIRE_EQUAL( (symbol{sc3, 255}.precision()), 255 ) + CHECK_EQUAL( (symbol{sc0,255}.precision()), 255 ) + CHECK_EQUAL( (symbol{sc1,255}.precision()), 255 ) + CHECK_EQUAL( (symbol{sc2,255}.precision()), 255 ) + CHECK_EQUAL( (symbol{sc3,255}.precision()), 255 ) // ----------------------- // symbol_code code()const - REQUIRE_EQUAL( (symbol{sc0, 0}.code()), sc0 ) - REQUIRE_EQUAL( (symbol{sc1, 0}.code()), sc1 ) - REQUIRE_EQUAL( (symbol{sc2, 0}.code()), sc2 ) - REQUIRE_EQUAL( (symbol{sc3, 0}.code()), sc3 ) + CHECK_EQUAL( (symbol{sc0,0}.code()), sc0 ) + CHECK_EQUAL( (symbol{sc1,0}.code()), sc1 ) + CHECK_EQUAL( (symbol{sc2,0}.code()), sc2 ) + CHECK_EQUAL( (symbol{sc3,0}.code()), sc3 ) // --------------------------------------- // constexpr explicit operator bool()const - REQUIRE_EQUAL( symbol{0}.operator bool(), false ) - REQUIRE_EQUAL( symbol{1}.operator bool(), true ) - REQUIRE_EQUAL( !symbol{0}.operator bool(), true ) - REQUIRE_EQUAL( !symbol{1}.operator bool(), false ) + CHECK_EQUAL( symbol{0}.operator bool(), false ) + CHECK_EQUAL( symbol{1}.operator bool(), true ) + CHECK_EQUAL( !symbol{0}.operator bool(), true ) + CHECK_EQUAL( !symbol{1}.operator bool(), false ) - REQUIRE_EQUAL( (symbol{"", 0}.operator bool()), false ) - REQUIRE_EQUAL( (symbol{"SYMBOLL", 0}.operator bool()), true ) - REQUIRE_EQUAL( (!symbol{"", 0}.operator bool()), true ) - REQUIRE_EQUAL( (!symbol{"SYMBOLL", 0}.operator bool()), false ) + CHECK_EQUAL( (symbol{"", 0}.operator bool()), false ) + CHECK_EQUAL( (symbol{"SYMBOLL", 0}.operator bool()), true ) + CHECK_EQUAL( (!symbol{"", 0}.operator bool()), true ) + CHECK_EQUAL( (!symbol{"SYMBOLL", 0}.operator bool()), false ) - // Note: uncomment once the appended '\n' is removed from the `print` function + // Note: uncomment once print checking has been resolved // --------------------- // void print(bool)const - // symbol{"A", 0}.print(true); - // symbol{"AAAAAAA", 255}.print(true); - // REQUIRE_PRINT( "0,A", [&](){symbol{"A", 0}.print(true);} ); - // REQUIRE_PRINT( "0,Z", [&](){symbol{"Z", 0}.print(true);} ); - // REQUIRE_PRINT( "255,AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(true);} ); - // REQUIRE_PRINT( "255,ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(true);} ); - - // REQUIRE_PRINT( ",A", [&](){symbol{"A", 0}.print(false);} ); - // REQUIRE_PRINT( ",Z", [&](){symbol{"Z", 0}.print(false);} ); - // REQUIRE_PRINT( ",AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(false);} ); - // REQUIRE_PRINT( ",ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(false);} ); + // CHECK_PRINT( "0,A", [&](){symbol{"A", 0}.print(true);} ); + // CHECK_PRINT( "0,Z", [&](){symbol{"Z", 0}.print(true);} ); + // CHECK_PRINT( "255,AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(true);} ); + // CHECK_PRINT( "255,ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(true);} ); // -------------------------------------------------------------- // friend constexpr bool operator==(const symbol&, const symbol&) - REQUIRE_EQUAL( (symbol{sc0, 0} == symbol{sc0, 0}), true ) - REQUIRE_EQUAL( (symbol{sc1, 0} == symbol{sc1, 0}), true ) - REQUIRE_EQUAL( (symbol{sc2, 0} == symbol{sc2, 0}), true ) - REQUIRE_EQUAL( (symbol{sc3, 0} == symbol{sc3, 0}), true ) + CHECK_EQUAL( (symbol{sc0, 0} == symbol{sc0, 0}), true ) + CHECK_EQUAL( (symbol{sc1, 0} == symbol{sc1, 0}), true ) + CHECK_EQUAL( (symbol{sc2, 0} == symbol{sc2, 0}), true ) + CHECK_EQUAL( (symbol{sc3, 0} == symbol{sc3, 0}), true ) // -------------------------------------------------------------- // friend constexpr bool operator!=(const symbol&, const symbol&) - REQUIRE_EQUAL( (symbol{sc0, 0} != symbol{}), true ) - REQUIRE_EQUAL( (symbol{sc1, 0} != symbol{}), true ) - REQUIRE_EQUAL( (symbol{sc2, 0} != symbol{}), true ) - REQUIRE_EQUAL( (symbol{sc3, 0} != symbol{}), true ) + CHECK_EQUAL( (symbol{sc0, 0} != symbol{}), true ) + CHECK_EQUAL( (symbol{sc1, 0} != symbol{}), true ) + CHECK_EQUAL( (symbol{sc2, 0} != symbol{}), true ) + CHECK_EQUAL( (symbol{sc3, 0} != symbol{}), true ) // -------------------------------------------------------------- // friend constexpr bool operator<(const symbol&, const symbol&) - REQUIRE_EQUAL( (symbol{} < symbol{sc0, 0}), true ) - REQUIRE_EQUAL( (symbol{} < symbol{sc1, 0}), true ) - REQUIRE_EQUAL( (symbol{} < symbol{sc2, 0}), true ) - REQUIRE_EQUAL( (symbol{} < symbol{sc3, 0}), true ) + CHECK_EQUAL( (symbol{} < symbol{sc0, 0}), true ) + CHECK_EQUAL( (symbol{} < symbol{sc1, 0}), true ) + CHECK_EQUAL( (symbol{} < symbol{sc2, 0}), true ) + CHECK_EQUAL( (symbol{} < symbol{sc3, 0}), true ) silence_output(false); EOSIO_TEST_END @@ -243,69 +231,65 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) symbol s0{"A", 0}; symbol s1{"Z", 0}; - symbol s2{"AAAAAAA", 0}; - symbol s3{"ZZZZZZZ", 0}; - - // ------------------------------------ - // constructors/get_symbol/get_contract - - /// constexpr extended_symbol() - REQUIRE_EQUAL( (extended_symbol{{}, {}}.get_symbol().raw()), 0ULL ) - REQUIRE_EQUAL( (extended_symbol{{}, {}}.get_contract().value), 0ULL ) + symbol s2{"AAAAAAA", 255}; + symbol s3{"ZZZZZZZ", 255}; + + //// constexpr extended_symbol() + // constexpr name get_symbol() + // constexpr name get_contract() + CHECK_EQUAL( (extended_symbol{{}, {}}.get_symbol().raw()), 0ULL ) + CHECK_EQUAL( (extended_symbol{{}, {}}.get_contract().value), 0ULL ) + + //// constexpr extended_symbol(symbol, name) + CHECK_EQUAL( (extended_symbol{s0, n0}.get_symbol().raw()), 16640ULL ) + CHECK_EQUAL( (extended_symbol{s0, n1}.get_symbol().code().raw()), 65ULL ) + CHECK_EQUAL( (extended_symbol{s1, n2}.get_symbol().raw()), 23040ULL ) + CHECK_EQUAL( (extended_symbol{s1, n3}.get_symbol().code().raw()), 90ULL ) + CHECK_EQUAL( (extended_symbol{s0, n0}.get_contract().value), 576460752303423488ULL ) + CHECK_EQUAL( (extended_symbol{s0, n1}.get_contract().value), 2882303761517117440ULL ) + CHECK_EQUAL( (extended_symbol{s1, n2}.get_contract().value), 3458764513820540928ULL ) + CHECK_EQUAL( (extended_symbol{s1, n3}.get_contract().value), 17870283321406128128ULL ) + CHECK_EQUAL( (extended_symbol{s2, n4}.get_symbol().raw()), 4702111234474983935ULL ) + CHECK_EQUAL( (extended_symbol{s2, n5}.get_symbol().code().raw()), 18367622009667905ULL ) + CHECK_EQUAL( (extended_symbol{s3, n6}.get_symbol().raw()), 6510615555426900735ULL ) + CHECK_EQUAL( (extended_symbol{s3, n7}.get_symbol().code().raw()), 25432092013386330ULL ) + CHECK_EQUAL( (extended_symbol{s2, n4}.get_contract().value), 595056260442243615ULL ) + CHECK_EQUAL( (extended_symbol{s2, n5}.get_contract().value), 2975281302211218015ULL ) + CHECK_EQUAL( (extended_symbol{s3, n6}.get_contract().value), 3570337562653461615ULL ) + CHECK_EQUAL( (extended_symbol{s3, n7}.get_contract().value), u64max ) - /// constexpr extended_symbol(symbol, name)/constexpr symbol get_symbol()const/constexpr name get_contract()const - REQUIRE_EQUAL( (extended_symbol{s0, n0}.get_symbol().raw()), 16640ULL ) - REQUIRE_EQUAL( (extended_symbol{s0, n1}.get_symbol().code().raw()), 65ULL ) - REQUIRE_EQUAL( (extended_symbol{s1, n2}.get_symbol().raw()), 23040ULL ) - REQUIRE_EQUAL( (extended_symbol{s1, n3}.get_symbol().code().raw()), 90ULL ) - REQUIRE_EQUAL( (extended_symbol{s0, n0}.get_contract().value), 576460752303423488ULL ) - REQUIRE_EQUAL( (extended_symbol{s0, n1}.get_contract().value), 2882303761517117440ULL ) - REQUIRE_EQUAL( (extended_symbol{s1, n2}.get_contract().value), 3458764513820540928ULL ) - REQUIRE_EQUAL( (extended_symbol{s1, n3}.get_contract().value), 17870283321406128128ULL ) - REQUIRE_EQUAL( (extended_symbol{s2, n4}.get_symbol().raw()), 4702111234474983680ULL ) - REQUIRE_EQUAL( (extended_symbol{s2, n5}.get_symbol().code().raw()), 18367622009667905ULL ) - REQUIRE_EQUAL( (extended_symbol{s3, n6}.get_symbol().raw()), 6510615555426900480ULL ) - REQUIRE_EQUAL( (extended_symbol{s3, n7}.get_symbol().code().raw()), 25432092013386330ULL ) - REQUIRE_EQUAL( (extended_symbol{s2, n4}.get_contract().value), 595056260442243615ULL ) - REQUIRE_EQUAL( (extended_symbol{s2, n5}.get_contract().value), 2975281302211218015ULL ) - REQUIRE_EQUAL( (extended_symbol{s3, n6}.get_contract().value), 3570337562653461615ULL ) - REQUIRE_EQUAL( (extended_symbol{s3, n7}.get_contract().value), u64max ) - - // // Note: uncomment once the appended '\n' is removed from the `print` function - // // --------------------- - // // void print(bool)const - // extended_symbol{s0, n0}.print(true); - // extended_symbol{s0, n0}.print(true); - // REQUIRE_PRINT( "0@576460752303423488", [&](){extended_symbol{s0, n0}.print(true);} ); - // REQUIRE_PRINT( "0@2882303761517117440", [&](){extended_symbol{s1, n1}.print(true);} ); - // REQUIRE_PRINT( "255@595056260442243615", [&](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(true);} ); - // REQUIRE_PRINT( "255@2975281302211218015", [&](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(true);} ); - - // REQUIRE_PRINT( "@576460752303423488", [&](){extended_symbol{symbol{"A", 0}, name{"1"}}.print(false);} ); - // REQUIRE_PRINT( "@2882303761517117440", [&](){extended_symbol{symbol{"Z", 0}, name{"5"}}.print(false);} ); - // REQUIRE_PRINT( "@595056260442243615", [&](){extended_symbol{symbol{"AAAAAAA", 255}, name{"111111111111j"}}.print(false);} ); - // REQUIRE_PRINT( "@2975281302211218015", [&](){extended_symbol{symbol{"ZZZZZZZ", 255}, name{"555555555555j"}}.print(false);} ); + // Note: uncomment once print checking has been resolved + // --------------------- + // void print(bool)const + // CHECK_PRINT( "0,A@1", [&](){extended_symbol{s0, n0}.print(true);} ); + // CHECK_PRINT( "0,A@5", [&](){extended_symbol{s0, n1}.print(true);} ); + // CHECK_PRINT( "0,Z@a", [&](){extended_symbol{s1, n2}.print(true);} ); + // CHECK_PRINT( "0,Z@z", [&](){extended_symbol{s1, n3}.print(true);} ); + // CHECK_PRINT( "255,AAAAAAA@111111111111j", [&](){extended_symbol{s2, n4}.print(true);} ); + // CHECK_PRINT( "255,AAAAAAA@555555555555j", [&](){extended_symbol{s2, n5}.print(true);} ); + // CHECK_PRINT( "255,ZZZZZZZ@aaaaaaaaaaaaj", [&](){extended_symbol{s3, n6}.print(true);} ); + // CHECK_PRINT( "255,ZZZZZZZ@zzzzzzzzzzzzj", [&](){extended_symbol{s3, n7}.print(true);} ); // ------------------------------------------------------------------------------- // friend constexpr bool operator==(const extended_symbol&, const extended_symbol&) - REQUIRE_EQUAL( (extended_symbol{s0, n0} == extended_symbol{s0, n0}), true ) - REQUIRE_EQUAL( (extended_symbol{s1, n3} == extended_symbol{s1, n3}), true ) - REQUIRE_EQUAL( (extended_symbol{s2, n4} == extended_symbol{s2, n4}), true ) - REQUIRE_EQUAL( (extended_symbol{s3, n7} == extended_symbol{s3, n7}), true ) + CHECK_EQUAL( (extended_symbol{s0, n0} == extended_symbol{s0, n0}), true ) + CHECK_EQUAL( (extended_symbol{s1, n3} == extended_symbol{s1, n3}), true ) + CHECK_EQUAL( (extended_symbol{s2, n4} == extended_symbol{s2, n4}), true ) + CHECK_EQUAL( (extended_symbol{s3, n7} == extended_symbol{s3, n7}), true ) // ------------------------------------------------------------------------------- // friend constexpr bool operator!=(const extended_symbol&, const extended_symbol&) - REQUIRE_EQUAL( (extended_symbol{} != extended_symbol{s0, {}}), true ) - REQUIRE_EQUAL( (extended_symbol{s0, {}} != extended_symbol{s1, {}}), true ) - REQUIRE_EQUAL( (extended_symbol{s1, {}} != extended_symbol{s2, {}}), true ) - REQUIRE_EQUAL( (extended_symbol{s2, {}} != extended_symbol{s3, {}}), true ) + CHECK_EQUAL( (extended_symbol{} != extended_symbol{s0, {}}), true ) + CHECK_EQUAL( (extended_symbol{s0, {}} != extended_symbol{s1, {}}), true ) + CHECK_EQUAL( (extended_symbol{s1, {}} != extended_symbol{s2, {}}), true ) + CHECK_EQUAL( (extended_symbol{s2, {}} != extended_symbol{s3, {}}), true ) // ------------------------------------------------------------------------------- // friend constexpr bool operator<(const extended_symbol&, const extended_symbol&) - REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s0, {}}), true ) - REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s1, {}}), true ) - REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s2, {}}), true ) - REQUIRE_EQUAL( (extended_symbol{} < extended_symbol{s3, {}}), true ) + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s0, {}}), true ) + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s1, {}}), true ) + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s2, {}}), true ) + CHECK_EQUAL( (extended_symbol{} < extended_symbol{s3, {}}), true ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/system_tests.cpp b/tests/unit/system_tests.cpp index 731d96143d..c7944cd929 100644 --- a/tests/unit/system_tests.cpp +++ b/tests/unit/system_tests.cpp @@ -1,7 +1,5 @@ #include - #include - #include using eosio::check; @@ -13,34 +11,41 @@ using namespace eosio::native; EOSIO_TEST_BEGIN(system_test) silence_output(true); + // ------------------------------------ // inline void check(bool, const char*) - REQUIRE_ASSERT( "asserted", []() { const char* str{"asserted"}; check(false, str);} ); + CHECK_ASSERT( "asserted", []() { const char* str{"asserted"}; check(false, str);} ); + // ------------------------------------------- // inline void check(bool, const std::string&) - REQUIRE_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, str);} ); + CHECK_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, str);} ); + // -------------------------------------------- // inline void check(bool, const std::string&&) - REQUIRE_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, std::move(str));} ); + CHECK_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, std::move(str));} ); + // -------------------------------------------- // inline void check(bool, const char*, size_t) - REQUIRE_ASSERT( "assert", []() { const char* str{"asserted"}; check(false, str, 6);} ); + CHECK_ASSERT( "assert", []() { const char* str{"asserted"}; check(false, str, 6);} ); + // --------------------------------------------------- // inline void check(bool, const std::string&, size_t) - REQUIRE_ASSERT( "assert", []() { const string str{"asserted"}; check(false, str, 6);} ); + CHECK_ASSERT( "assert", []() { const string str{"asserted"}; check(false, str, 6);} ); - // Note: this function will not work when give `0` as the `uint64_t` value. + // Note: this function will not work when given `0` as the `uint64_t` value. // Maybe some invariants should be established as to what is acceptable and what isn't. - // REQUIRE_ASSERT("0", []() { check(false, 0);} ); + // For example: + // CHECK_ASSERT("0", []() { check(false, 0);} ); + // --------------------------------- // inline void check(bool, uint64_t) - REQUIRE_ASSERT("1", []() { check(false, 1);} ); - REQUIRE_ASSERT("100", []() { check(false, 100);} ); - REQUIRE_ASSERT("18446744073709551615", []() { check(false, 18446744073709551615ULL);} ); - REQUIRE_ASSERT("18446744073709551615", []() { check(false, -1ULL);} ); + CHECK_ASSERT("1", []() { check(false, 1);} ); + CHECK_ASSERT("100", []() { check(false, 100);} ); + CHECK_ASSERT("18446744073709551615", []() { check(false, 18446744073709551615ULL);} ); + CHECK_ASSERT("18446744073709551615", []() { check(false, -1ULL);} ); silence_output(false); EOSIO_TEST_END -int main(int argc, char** argv) { +int main(int argc, char* argv[]) { EOSIO_TEST(system_test); return has_failed(); } diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index e6c57c69fc..b36535c9cb 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -1,5 +1,4 @@ #include - #include using eosio::microseconds; @@ -15,24 +14,11 @@ const uint32_t u32max = std::numeric_limits::max(); // 184467440737095 const int64_t i64min = std::numeric_limits::min(); // -9223372036854775808 const int64_t i64max = std::numeric_limits::max(); // 9223372036854775807 -// This is interesting. `print_f` won't print the '-' character. -// `printf` won't print anything. -// `printf("%\n",i64min);` - -// Note that there are no invariants established to protect this type against overflow. -// For example: if you define a microsecond type from i64max and then check that value in -// Or if you check the `seconds` of a (u64MAX / 1000000 + 1), it will overflow. -// Potentially could be abused in a contract? - // Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(microseconds_type_test) silence_output(false); - // ------------ - // constructors - - // ----------------------------------------------- - /// explicit microseconds(uint64_t)/int64_t count() + //// explicit microseconds(uint64_t)/int64_t count() REQUIRE_EQUAL( microseconds{}.count(), 0ULL ) REQUIRE_EQUAL( microseconds{i64max}.count(), i64max ) REQUIRE_EQUAL( microseconds{i64min}.count(), i64min ) @@ -43,61 +29,102 @@ EOSIO_TEST_BEGIN(microseconds_type_test) // ------------------------------------------------------------------------ // friend microseconds operator+(const microseconds&, const microseconds&) + REQUIRE_EQUAL( (microseconds{0LL} + microseconds{ 1LL}), microseconds{ 1LL} ) + REQUIRE_EQUAL( (microseconds{1LL} + microseconds{-1LL}), microseconds{ 0LL} ) + + // ------------------------------------------------------------------------ // friend microseconds operator-(const microseconds&, const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} + microseconds{1LL}), microseconds{ 1LL} ) - REQUIRE_EQUAL( (microseconds{0LL} - microseconds{1LL}), microseconds{-1LL} ) + REQUIRE_EQUAL( (microseconds{0LL} - microseconds{ 1LL}), microseconds{-1LL} ) + REQUIRE_EQUAL( (microseconds{1LL} - microseconds{-1LL}), microseconds{ 2LL} ) + + // ---------------------------------------------- + // microseconds& operator+=(const microseconds&) + REQUIRE_EQUAL( (microseconds{0LL} += microseconds{ 1LL}), microseconds{ 1LL} ) + REQUIRE_EQUAL( (microseconds{1LL} += microseconds{-1LL}), microseconds{ 0LL} ) + + // ---------------------------------------------- + // microseconds& operator-=(const microseconds&) + REQUIRE_EQUAL( (microseconds{0LL} -= microseconds{ 1LL}), microseconds{-1LL} ) + REQUIRE_EQUAL( (microseconds{1LL} -= microseconds{-1LL}), microseconds{ 2LL} ) // ------------------------------------- // bool operator==(const microseconds&) - // bool operator!=(const microseconds&) - // bool operator< (const microseconds&) - // bool operator<=(const microseconds&) - // bool operator> (const microseconds&) - // bool operator>=(const microseconds&) REQUIRE_EQUAL( (microseconds{1LL} == microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{0LL} == microseconds{1LL}), false ) + + // ------------------------------------- + // bool operator!=(const microseconds&) REQUIRE_EQUAL( (microseconds{1LL} != microseconds{0LL}), true ) + REQUIRE_EQUAL( (microseconds{0LL} != microseconds{0LL}), false ) + + // ------------------------------------- + // bool operator<(const microseconds&) REQUIRE_EQUAL( (microseconds{0LL} < microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} < microseconds{1LL}), false ) + + // ------------------------------------- + // bool operator<=(const microseconds&) REQUIRE_EQUAL( (microseconds{1LL} <= microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{2LL} <= microseconds{1LL}), false ) + + // ------------------------------------- + // bool operator>(const microseconds&) REQUIRE_EQUAL( (microseconds{1LL} > microseconds{0LL}), true ) - REQUIRE_EQUAL( (microseconds{1LL} >= microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} > microseconds{1LL}), false ) - // ---------------------------------------------- - // microseconds& operator+=(const microseconds&) - // microseconds& operator-=(const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} += microseconds{1LL}), microseconds{ 1LL} ) - REQUIRE_EQUAL( (microseconds{0LL} -= microseconds{1LL}), microseconds{-1LL} ) + // ------------------------------------- + // bool operator>=(const microseconds&) + REQUIRE_EQUAL( (microseconds{1LL} >= microseconds{1LL}), true ) + REQUIRE_EQUAL( (microseconds{1LL} >= microseconds{2LL}), false ) // -------------------- // int64_t to_seconds() + REQUIRE_EQUAL( (microseconds{i64min-1}.to_seconds()), i64max / 1000000 ) REQUIRE_EQUAL( (microseconds{i64max}.to_seconds()), i64max / 1000000 ) + // ----------------------------------------- + // inline microseconds milliseconds(int64_t) + REQUIRE_EQUAL( eosio::milliseconds(0LL), microseconds{0LL} ) + REQUIRE_EQUAL( eosio::milliseconds(1LL), microseconds{1000LL} ) + REQUIRE_EQUAL( eosio::milliseconds(60LL), microseconds{60LL*1000LL} ) + // ------------------------------------ // inline microseconds seconds(int64_t) - // inline microseconds milliseconds(int64_t) - // inline microseconds minutes(int64_t) - // inline microseconds hours(int64_t) - // inline microseconds days(int64_t) + REQUIRE_EQUAL( eosio::seconds(0LL), microseconds{0LL} ) + REQUIRE_EQUAL( eosio::seconds(1LL), microseconds{1000000LL} ) REQUIRE_EQUAL( eosio::seconds(60LL), microseconds{60LL*1000000LL} ) - REQUIRE_EQUAL( eosio::milliseconds(60LL), microseconds{60LL*1000LL} ) + + // ------------------------------------ + // inline microseconds minutes(int64_t) + REQUIRE_EQUAL( eosio::minutes(0LL), microseconds{0LL} ) + REQUIRE_EQUAL( eosio::minutes(1LL), microseconds{60LL*1000000LL} ) REQUIRE_EQUAL( eosio::minutes(60LL), microseconds{60LL*60LL*1000000LL} ) + + // ---------------------------------- + // inline microseconds hours(int64_t) + REQUIRE_EQUAL( eosio::hours(0LL), microseconds{0LL} ) + REQUIRE_EQUAL( eosio::hours(1LL), microseconds{60LL*60LL*1000000LL} ) REQUIRE_EQUAL( eosio::hours(60LL), microseconds{60LL*60LL*60LL*1000000LL} ) - REQUIRE_EQUAL( eosio::days(60LL), microseconds{24LL*60LL*60LL*60LL*1000000LL} ) + // --------------------------------- + // inline microseconds days(int64_t) + REQUIRE_EQUAL( eosio::days(0LL), microseconds{0LL} ) + REQUIRE_EQUAL( eosio::days(1LL), microseconds{24LL*60LL*60LL*1000000LL} ) + REQUIRE_EQUAL( eosio::days(60LL), microseconds{24LL*60LL*60LL*60LL*1000000LL} ) + silence_output(false); EOSIO_TEST_END EOSIO_TEST_BEGIN(time_point_type_test) silence_output(false); - // ------------ - // constructors microseconds ms0{ 0LL}; microseconds ms1{ 1LL}; microseconds ms2{-1LL}; microseconds ms_max{i64max}; microseconds ms_min{i64min}; - /// explicit time_point(microseconds)/microseconds& time_since_epoch() + //// explicit time_point(microseconds)/microseconds& time_since_epoch() REQUIRE_EQUAL( time_point{ms0}.time_since_epoch(), ms0 ) REQUIRE_EQUAL( time_point{ms1}.time_since_epoch(), ms1 ) @@ -108,44 +135,72 @@ EOSIO_TEST_BEGIN(time_point_type_test) REQUIRE_EQUAL( time_point{ms_max}.sec_since_epoch(), i64max / 1000000 ) REQUIRE_EQUAL( time_point{ms_min}.sec_since_epoch(), i64min / 1000000 ) - // ----------------------------------- + // ------------------------------------------ + // time_point operator+(const microseconds&) + REQUIRE_EQUAL( (time_point{ms0} + ms1), time_point{ms1} ) + REQUIRE_EQUAL( (time_point{ms2} + ms1), time_point{ms0} ) + + // ---------------------------------------- + // time_point operator+(const time_point&) + REQUIRE_EQUAL( (time_point{ms0} + time_point{ms1}), time_point{ms1} ) + REQUIRE_EQUAL( (time_point{ms2} + time_point{ms1}), time_point{ms0} ) + + // ------------------------------------------ + // time_point operator-(const microseconds&) + REQUIRE_EQUAL( (time_point{ms0} - ms1), time_point{ms2} ) + REQUIRE_EQUAL( (time_point{ms0} - ms2), time_point{ms1} ) + + // ------------------------------------------ + // microseconds operator-(const time_point&) + REQUIRE_EQUAL( (time_point{ms0} - time_point{ms1}), ms2 ) + REQUIRE_EQUAL( (time_point{ms0} - time_point{ms2}), ms1 ) + + // -------------------------------------------- + // time_point& operator+=(const microseconds&) + REQUIRE_EQUAL( (time_point{ms0} += ms1), time_point{ms1} ) + REQUIRE_EQUAL( (time_point{ms2} += ms1), time_point{ms0} ) + + // -------------------------------------------- + // time_point& operator-=(const microseconds&) + REQUIRE_EQUAL( (time_point{ms0} -= ms1), time_point{ms2} ) + REQUIRE_EQUAL( (time_point{ms0} -= ms2), time_point{ms1} ) + + // ---------------------------------- // bool operator==(const time_point&) - // bool operator!=(const time_point&) - // bool operator<(const time_point&) - // bool operator<=(const time_point&) - // bool operator>(const time_point&) - // bool operator>=(const time_point&) REQUIRE_EQUAL( (time_point{ms0} == time_point{ms0}), true ) + REQUIRE_EQUAL( (time_point{ms0} == time_point{ms1}), false ) + + // ---------------------------------- + // bool operator!=(const time_point&) REQUIRE_EQUAL( (time_point{ms1} != time_point{ms0}), true ) + REQUIRE_EQUAL( (time_point{ms1} != time_point{ms1}), false ) + + // ---------------------------------- + // bool operator<(const time_point&) REQUIRE_EQUAL( (time_point{ms0} < time_point{ms1}), true ) + REQUIRE_EQUAL( (time_point{ms1} < time_point{ms1}), false ) + + // ---------------------------------- + // bool operator<=(const time_point&) REQUIRE_EQUAL( (time_point{ms1} <= time_point{ms1}), true ) - REQUIRE_EQUAL( (time_point{ms1} > time_point{ms0}), true ) - REQUIRE_EQUAL( (time_point{ms1} >= time_point{ms1}), true ) + REQUIRE_EQUAL( (time_point{ms1} <= time_point{ms0}), false ) - // -------------------------------------------- - // time_point& operator+=(const microseconds&) - // time_point& operator-=(const microseconds&) - REQUIRE_EQUAL( (time_point{ms0} += ms1), time_point{ms1} ) - REQUIRE_EQUAL( (time_point{ms0} -= ms1), time_point{ms2} ) + // ---------------------------------- + // bool operator>(const time_point&) + REQUIRE_EQUAL( (time_point{ms1} > time_point{ms0}), true ) + REQUIRE_EQUAL( (time_point{ms1} > time_point{ms1}), false ) - // ------------------------------------------ - // time_point operator+(const microseconds&) - // time_point operator+(const time_point&) - // time_point operator-(const microseconds&) - // microseconds operator-(const time_point&) - REQUIRE_EQUAL( (time_point{ms0} + ms1), time_point{ms1} ) - REQUIRE_EQUAL( (time_point{ms0} + time_point{ms1}), time_point{ms1} ) - REQUIRE_EQUAL( (time_point{ms0} - ms1), time_point{ms2} ) - REQUIRE_EQUAL( (time_point{ms0} - time_point{ms1}), ms2 ) + // ---------------------------------- + // bool operator>=(const time_point&) + REQUIRE_EQUAL( (time_point{ms1} >= time_point{ms1}), true ) + REQUIRE_EQUAL( (time_point{ms0} >= time_point{ms1}), false ) silence_output(false); EOSIO_TEST_END EOSIO_TEST_BEGIN(time_point_sec_type_test) - silence_output(false); + silence_output(true); - // ------------ - // constructors microseconds ms0{ 0LL}; microseconds ms1{ 1LL}; microseconds ms2{-1LL}; @@ -158,16 +213,16 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) time_point tp_max{ms_max}; time_point tp_min{ms_min}; - /// time_point_sec()/uint32_t sec_since_epoch()const + //// time_point_sec()/uint32_t sec_since_epoch()const REQUIRE_EQUAL( time_point_sec{}.sec_since_epoch(), 0 ) - /// explicit time_point_sec(uint32_t) + //// explicit time_point_sec(uint32_t) REQUIRE_EQUAL( time_point_sec{u32max}.sec_since_epoch(), u32max ) REQUIRE_EQUAL( time_point_sec{u32max + 1}.sec_since_epoch(), 0 ) - /// time_point_sec(const time_point&) - REQUIRE_EQUAL( time_point_sec{tp0}.sec_since_epoch(), 0LL / 1000000LL ) - REQUIRE_EQUAL( time_point_sec{tp1}.sec_since_epoch(), 1LL / 1000000LL ) + //// time_point_sec(const time_point&) + REQUIRE_EQUAL( time_point_sec{tp0}.sec_since_epoch(), 0LL / 1000000LL ) + REQUIRE_EQUAL( time_point_sec{tp1}.sec_since_epoch(), 1LL / 1000000LL ) REQUIRE_EQUAL( time_point_sec{tp2}.sec_since_epoch(), -1LL / 1000000LL ) REQUIRE_EQUAL( time_point_sec{tp_max}.sec_since_epoch(), i64max / 1000000LL ) REQUIRE_EQUAL( time_point_sec{tp_min}.sec_since_epoch(), i64min / 1000000LL ) @@ -182,21 +237,6 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) REQUIRE_EQUAL( time_point_sec{}.min().utc_seconds, 0 ) REQUIRE_EQUAL( time_point_sec{}.min().utc_seconds != 1, true ) - // Millisecond data is lost when using these data types in certain ways. - // For Example: - // microseconds mms0{1}; - // eosio::print_f("_count: %\n",mms0._count); - // time_point ttp0{mms0}; - // eosio::print_f("elapsed: %\n",ttp0.elapsed._count); - // time_point_sec ttps0{ttp0}; - // eosio::print_f("utc_seconds: %\n",ttps0.utc_seconds); - // ttp0 = ttps0; - // eosio::print_f("_count: %\n",ttp0.elapsed._count); - - // Also may have found a `print_f` bug/ - // To replicate: - eosio::print_f("%", time_point{time_point_sec{tp_max}}.elapsed.count()); - eosio::print_f("%", 9223372036854775807 / 1000000); // -------------------------- // operator time_point()const REQUIRE_EQUAL( (time_point{time_point_sec{tp0}}.elapsed.count()), 0 ) @@ -211,48 +251,95 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) REQUIRE_EQUAL( (time_point_sec{} = time_point{tp1}), time_point_sec{} ) REQUIRE_EQUAL( (time_point_sec{} = time_point{tp_max}), time_point_sec{tp_max} ) - // -------------------------------------------------------------------- - // friend bool operator==(const time_point_sec&, const time_point_sec&) - // friend bool operator!=(const time_point_sec&, const time_point_sec&) - // friend bool operator<(const time_point_sec&, const time_point_sec&) - // friend bool operator<=(const time_point_sec&, const time_point_sec&) - // friend bool operator>(const time_point_sec&, const time_point_sec&) - // friend bool operator>=(const time_point_sec&, const time_point_sec&) - REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) - REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) - REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) - REQUIRE_EQUAL( (time_point_sec{tp_max} <= time_point{tp_max}), true ) - REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) - REQUIRE_EQUAL( (time_point_sec{tp_max} >= time_point{tp_max}), true ) - - - // ------------------------------------ - // time_point_sec& operator+=(uint32_t) - // time_point_sec& operator+=(microseconds) - // time_point_sec& operator+=(time_point_sec) - // time_point_sec& operator-=(uint32_t) - // time_point_sec& operator-=(microseconds) - // time_point_sec& operator-=(time_point_sec) + // --------------------------------------- // time_point_sec operator+(uint32_t)const - // time_point_sec operator-(uint32_t)const - REQUIRE_EQUAL( (time_point_sec{} += i64max), time_point_sec{i64max} ) - REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64max}), time_point_sec{tp_max} ) - REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64max}), time_point_sec{i64max} ) - REQUIRE_EQUAL( (time_point_sec{i64max} -= i64max), time_point_sec{} ) - REQUIRE_EQUAL( (time_point_sec{i64max} -= microseconds{i64max}), time_point_sec{i64max - (i64max / 1000000LL)} ) - REQUIRE_EQUAL( (time_point_sec{i64max} -= time_point_sec{i64max}), time_point_sec{} ) - REQUIRE_EQUAL( (time_point_sec{} + i64max), time_point_sec{i64max} ) - REQUIRE_EQUAL( (time_point_sec{i64max} - i64max), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{} + u32min), time_point_sec{u32min} ) + REQUIRE_EQUAL( (time_point_sec{} + u32max), time_point_sec{u32max} ) // ----------------------------------------------------------------------- // friend time_point operator+(const time_point_sec&, const microseconds&) + REQUIRE_EQUAL( (time_point_sec{} + ms1), time_point{tp1} ) + REQUIRE_EQUAL( (time_point_sec{} + ms2), time_point{tp2} ) + + // --------------------------------------- + // time_point_sec operator-(uint32_t)const + REQUIRE_EQUAL( (time_point_sec{u32max} - u32max), time_point_sec{} ) + REQUIRE_EQUAL( (time_point_sec{u32min} - 1UL), time_point_sec{u32max} ) + + // ----------------------------------------------------------------------- // friend time_point operator-(const time_point_sec&, const microseconds&) - // friend microseconds operator-(const time_point_sec&, const time_point_sec&) - // friend microseconds operator-(const time_point&, const time_point_sec&) - REQUIRE_EQUAL( (time_point_sec{} + microseconds{}), time_point{} ) - REQUIRE_EQUAL( (time_point_sec{i64max} - microseconds{i64max}), time_point{} ) - REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) - REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) + // REQUIRE_EQUAL( (time_point_sec{u32max} - microseconds{i32max}), time_point{u32max-i32max} ) + // REQUIRE_EQUAL( (time_point_sec{u32min} - microseconds{}), time_point{} ) // new + + // // --------------------------------------------------------------------------- + // // friend microseconds operator-(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) + // REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) // new + + // // ----------------------------------------------------------------------- + // // friend microseconds operator-(const time_point&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) + // REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) // new + + // // ------------------------------------ + // // time_point_sec& operator+=(uint32_t) + // REQUIRE_EQUAL( (time_point_sec{} += i64max), time_point_sec{i64max} ) + // REQUIRE_EQUAL( (time_point_sec{} += i64max), time_point_sec{i64max} ) // new + + // // ---------------------------------------- + // // time_point_sec& operator+=(microseconds) + // REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64max}), time_point_sec{tp_max} ) + // REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64max}), time_point_sec{tp_max} ) // new + + // // ------------------------------------------ + // // time_point_sec& operator+=(time_point_sec) + // REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64max}), time_point_sec{i64max} ) + // REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64max}), time_point_sec{i64max} ) // new + + // // ------------------------------------ + // // time_point_sec& operator-=(uint32_t) + // REQUIRE_EQUAL( (time_point_sec{i64max} -= i64max), time_point_sec{} ) + // REQUIRE_EQUAL( (time_point_sec{i64max} -= i64max), time_point_sec{} ) // new + + // // ---------------------------------------- + // // time_point_sec& operator-=(microseconds) + // REQUIRE_EQUAL( (time_point_sec{i64max} -= microseconds{i64max}), time_point_sec{i64max - (i64max / 1000000LL)} ) + // REQUIRE_EQUAL( (time_point_sec{i64max} -= microseconds{i64max}), time_point_sec{i64max - (i64max / 1000000LL)} ) // new + + // // ------------------------------------------ + // // time_point_sec& operator-=(time_point_sec) + // REQUIRE_EQUAL( (time_point_sec{i64max} -= time_point_sec{i64max}), time_point_sec{} ) + // REQUIRE_EQUAL( (time_point_sec{i64max} -= time_point_sec{i64max}), time_point_sec{} ) // new + + // // -------------------------------------------------------------------- + // // friend bool operator==(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) // new + + // // -------------------------------------------------------------------- + // // friend bool operator!=(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) // new + + // // ------------------------------------------------------------------- + // // friend bool operator<(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) // new + + // // -------------------------------------------------------------------- + // // friend bool operator<=(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) // new + + // // ------------------------------------------------------------------- + // // friend bool operator>(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) // new + + // // -------------------------------------------------------------------- + // // friend bool operator>=(const time_point_sec&, const time_point_sec&) + // REQUIRE_EQUAL( (time_point_sec{tp_max} >= time_point{tp_max}), true ) + // REQUIRE_EQUAL( (time_point_sec{tp_max} >= time_point{tp_max}), true ) // new silence_output(false); EOSIO_TEST_END @@ -260,8 +347,8 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(block_timestamp_type_test) silence_output(false); - microseconds ms0{0LL}; - microseconds ms1{1LL}; + microseconds ms0{ 0LL}; + microseconds ms1{ 1LL}; microseconds ms2{-1LL}; microseconds ms_max{i64max}; microseconds ms_min{i64min}; @@ -278,10 +365,7 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) time_point_sec tps_max{tp_max}; time_point_sec tps_min{tp_min}; - // ------------ - // constructors - - /// explicit block_timestamp(uint32_t) + //// explicit block_timestamp(uint32_t) REQUIRE_EQUAL( block_timestamp{}.slot == block_timestamp{0}.slot, true ) // NOTE: No invariant established. `block_timestamp` gets wonky with certain inputs. @@ -296,10 +380,10 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) // int64_t msec_since_epoch = micro_since_epoch / 1000; // 0 / 1000 = 0 // slot = uint32_t(( msec_since_epoch - block_timestamp_epoch ) / int64_t(block_interval_ms)); // 0 - 946684800000ll / 500 - /// block_timestamp(const time_point&) + //// block_timestamp(const time_point&) REQUIRE_EQUAL( block_timestamp{tp0}.slot == block_timestamp{0}.slot, true ) - /// block_timestamp(const time_point_sec&) + //// block_timestamp(const time_point_sec&) REQUIRE_EQUAL( block_timestamp{tps0}.slot == block_timestamp{0}.slot, true ) // -------------------------------- @@ -345,25 +429,36 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) // --------------------------------------- // bool operator==(const block_timestamp&) + REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) + REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) // new + // bool operator!=(const block_timestamp&) + REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) + REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) // new + // bool operator<(const block_timestamp&) + REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) + REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) // new + // bool operator<=(const block_timestamp&) + REQUIRE_EQUAL( bt_max <= block_timestamp{tps_max}, true ) + REQUIRE_EQUAL( bt_max <= block_timestamp{tps_max}, true ) // new + // bool operator>(const block_timestamp&) + REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) + REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) // new + // bool operator>=(const block_timestamp&) - REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) - REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) - REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) - REQUIRE_EQUAL( bt_max <= block_timestamp{tps_max}, true ) - REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) - REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) - + REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) + REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) // new + silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { - EOSIO_TEST(microseconds_type_test); - EOSIO_TEST(time_point_type_test); + // EOSIO_TEST(microseconds_type_test); + // EOSIO_TEST(time_point_type_test); EOSIO_TEST(time_point_sec_type_test); - EOSIO_TEST(block_timestamp_type_test); + // EOSIO_TEST(block_timestamp_type_test); return has_failed(); } From 5e4b00335232d4d8417bcaa23019bbdd3043aa54 Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 1 Feb 2019 18:01:23 -0500 Subject: [PATCH 24/51] Continuation of `datastream` tests --- temp/TODO | 331 -------------------------------- tests/unit/datastream_tests.cpp | 120 +++++++----- 2 files changed, 72 insertions(+), 379 deletions(-) delete mode 100644 temp/TODO diff --git a/temp/TODO b/temp/TODO deleted file mode 100644 index 14c69a42ed..0000000000 --- a/temp/TODO +++ /dev/null @@ -1,331 +0,0 @@ -[✓] asset.hpp - dependencies - ************ - "serialize.hpp" - "print.hpp" - "system.hpp" - "symbol.hpp" - - - ************ - namespace eosio - --------------- - - [✓] asset - - constructors - [✓] asset() - [✓] asset(uint64_t, class symbol s) - - methods - [✓] bool is_amount_within_range() - [✓] bool is_valid() - [✓] void set_amount(int64_t) - [✓] std::string to_string() - [✓] void print() - [✓] operator- - [✓] operator-= - [✓] operator+= - [✓] operator*= - [✓] operator/= - [✓] friend operator- - [✓] friend operator+ - [✓] friend operator* - [✓] friend operator* - [✓] friend operator/ - [✓] friend operator/ - [✓] friend operator== - [✓] friend operator!= - [✓] friend operator< - [✓] friend operator<= - [✓] friend operator> - [✓] friend operator>= - - members - [✓] uint64_t amount - [✓] symbol symbol - [✓] int64_t max_amount - - [✓] extended_asset - - constructors - [✓] extended_asset() - [✓] extended_asset(int64_t, extended_symbol) - [✓] extended_asset(asset, name) - - methods - [✓] extended_symbol get_extended_symbol() - [✓] void print() - [✓] operator- - [✓] friend operator- - [✓] friend operator+ - [✓] friend operator+= - [✓] friend operator-= - [✓] friend operator< - [✓] friend operator== - [✓] friend operator!= - [✓] friend operator<= - [✓] friend operator>= - - members - [✓] asset quantity - [✓] name contract - --------------- - -[*] binary_extension.hpp -[*] datastream.hpp - -[✓] eosio.hpp - dependencies - ************ - "action.hpp" - "print.hpp" - "multi_index.hpp" - "dispatcher.hpp" - "contract.hpp" - ************ - namespace eosio - --------------- - - macros - [✓] #define CONTRACT - [✓] #define ACTION - [✓] #define TABLE - --------------- - -[*] fixed_bytes.hpp - -[✓] name.hpp - dependencies - ************ - "system.hpp" - "serialize.hpp" - - - ************ - namespace eosio - --------------- - - [✓] name - - constructors - [✓] name() - [✓] name(uint64_t) - [✓] name(name::raw) - [✓] name(std::string_view) - - methods - [✓] uint8_t char_to_value(char) - [✓] uint8_t length() - [✓] name suffix() - [✓] operator raw() - [✓] operator bool() - [✓] char* write_as_string(char*, char*) - [✓] std::string to_string() - [✓] friend operator== - [✓] friend operator!= - [✓] friend operator< - - members - [✓] uint64_t value - namespace detail - ---------------- - [✓] to_const_char_arr - - members - [✓] char value[] - ---------------- - ---------------- - [✓] eosio::name operator"" _n - -[*] print.hpp -[*] public_key.hpp -[*] serialize.hpp - -[✓] symbol.hpp - dependencies - ************ - "system.hpp" - "print.h" - "name.hpp" - "serialize.hpp" - - - - ************ - namespace eosio - --------------- - - [✓] symbol_code - - constructors - [✓] symbol_code() - [✓] symbol_code(uint64_t) - [✓] symbol_code(std::string_view) - - methods - [✓] bool is_valid() - [✓] uint32_t length() - [✓] uint64_t raw() - [✓] operator bool() - [✓] char* write_as_string(char*, char*) - [✓] std::string to_string() - [✓] friend operator== - [✓] friend operator!= - [✓] friend operator< - - members - [✓] uint64_t value - - [✓] symbol - - constructors - [✓] symbol() - [✓] symbol(uint64_t) - [✓] symbol(symbol_code, uint8_t) - [✓] symbol(string_view, uint8_t) - - methods - [✓] bool is_valid() - [✓] uint8_t precision() - [✓] symbol_code code() - [✓] uint64_t raw() - [✓] operator bool() - [✓] void print(bool show_precision) - [✓] friend operator== - [✓] friend operator!= - [✓] friend operator< - - members - [✓] uint64_t value - - [✓] extended_symbol - - constructors - [✓] extended_symbol() - [✓] extended_symbol(symbol, name) - - methods - [✓] symbol get_symbol() - [✓] name get_contract() - [✓] void print(bool show_precision) - [✓] friend operator== - [✓] friend operator!= - [✓] friend operator< - - members - [✓] symbol symbol - [✓] name contract - --------------- - -[✓] system.hpp - dependencies - ************ - "system.h" - - ************ - namespace eosio - --------------- - - methods - [✓] inline void check(bool pred, const char* msg) - [✓] inline void check(bool pred, const std::string& msg) - [✓] inline void check(bool pred, const std::string&& msg) - [✓] inline void check(bool pred, const char* msg, size_t n) - [✓] inline void check(bool pred, const std::string& msg, size_t n) - [✓] inline void check(bool pred, uint64_t code) - --------------- - -[✓] time.hpp - dependencies - ************ - "eosio.hpp" - - - ************ - namespace eosio - --------------- - [✓] microseconds - - constructors - [✓] microseconds(uint64_t) - - methods - [✓] static microseconds maximum() - [✓] friend microseconds operator+(const microseconds&, const microseconds&) - [✓] friend microseconds operator-(const microseconds&, const microseconds&) - [✓] bool operator==(const microseconds&) - [✓] bool operator!=(const microseconds&) - [✓] friend bool operator>(const microseconds&, const microseconds&) - [✓] friend bool operator>=(const microseconds&, const microseconds&) - [✓] friend bool operator<(const microseconds&, const microseconds&) - [✓] friend bool operator<=(const microseconds&, const microseconds&) - [✓] microseconds& operator+=(const microseconds&) - [✓] microseconds& operator-=(const microseconds&) - [✓] int64_t count() - [✓] int64_t to_seconds() - - members - [✓] count - - [✓] inline microseconds seconds(uint64_t) - [✓] inline microseconds milliseconds(uint64_t) - [✓] inline microseconds minutes(uint64_t) - [✓] inline microseconds hours(uint64_t) - [✓] inline microseconds days(uint64_t) - - [✓] time_point - - constructors - [✓] explicit time_point(microseconds) - - methods - [✓] const microseconds& time_since_epoch()const - [✓] uint32_t sec_since_epoch()const - [✓] bool operator>(const time_point&) - [✓] bool operator>=(const time_point&) - [✓] bool operator<(const time_point&) - [✓] bool operator<=(const time_point&) - [✓] bool operator==(const time_point&) - [✓] bool operator!=(const time_point&) - [✓] time_point& operator+=(const microseconds&) - [✓] time_point& operator-=(const microseconds&) - [✓] time_point operator+(const microseconds&) - [✓] time_point operator+(const time_point&) - [✓] time_point operator-(const microseconds&) - [✓] time_point operator-(const time_point&) - - members - [✓] microseconds elapsed - - [✓] time_point_sec - - constructors - [✓] time_point_sec() - [✓] explicit time_point_sec(uint32_t) - [✓] time_point_sec(const time_point&) - - methods - [✓] static time_point_sec maximum() - [✓] static time_point_sec min() - [✓] operator time_point()const - [✓] uint32_t sec_since_epoch()const - [✓] time_point_sec operator=(const eosio::time_point&) - [✓] friend bool operator<(const time_point_sec&, const time_point_sec&) - [✓] friend bool operator>(const time_point_sec&, const time_point_sec&) - [✓] friend bool operator<=(const time_point_sec&, const time_point_sec&) - [✓] friend bool operator>=(const time_point_sec&, const time_point_sec&) - [✓] friend bool operator==(const time_point_sec&, const time_point_sec&) - [✓] friend bool operator!=(const time_point_sec&, const time_point_sec&) - [✓] time_point_sec& operator+=(uint32_t) - [✓] time_point_sec& operator+=(microseconds) - [✓] time_point_sec& operator+=(time_point_sec) - [✓] time_point_sec& operator-=(uint32_t) - [✓] time_point_sec& operator-=(microseconds) - [✓] time_point_sec& operator-=(time_point_sec) - [✓] friend bool operator+(uint32_t) - [✓] friend bool operator-(uint32_t) - [✓] friend time_point operator+(const time_point_sec&, const microseconds&) - [✓] friend time_point operator-(const time_point_sec&, const microseconds&) - [✓] friend time_point operator-(const time_point_sec&, const microseconds&) - [✓] friend time_point operator-(const time_point_sec&, const time_point_sec&) - - members - [✓] uint32_t utc_seconds - - [✓] block_timestamp - - constructors - [✓] explicit block_timestamp(uint32_t) - [✓] block_timestamp(const time_point&) - [✓] block_timestamp(const time_point_sec&) - - methods - [✓] static block_timestamp maximum() - [✓] static block_timestamp min() - [✓] block_timestamp next()const - [✓] time_point to_time_point()const - [✓] operator time_point()const - [✓] void operator=(const time_point&) - [✓] bool operator>(const block_timestamp&) - [✓] bool operator>=(const block_timestamp&) - [✓] bool operator<(const block_timestamp&) - [✓] bool operator<=(const block_timestamp&) - [✓] bool operator==(const block_timestamp&) - [✓] bool operator!=(const block_timestamp&) - [✓] void set_time_point(const time_point&) - [✓] void set_time_point(const time_point_sec&) - - members - [✓] static constexpr int32_t block_interval_ms - [✓] static constexpr int32_t block_timestamp_epoch - [✓] uint32_t slot - --------------- -[*] varint.hpp diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index fb2237853c..24e03c9867 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,4 +1,4 @@ -// eosio-cpp -fnative -w datastream_tests.cpp; ./a.out +// eosio-cpp -fnative -w -o a.out datastream_tests.cpp; ./a.out #include #include @@ -216,9 +216,10 @@ void print_bufs(const char* datastream_buffer, const T& container) { EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(false); - static constexpr uint8_t buffer_size{16}; + static constexpr uint8_t buffer_size{160}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to - char expectation_buffer[buffer_size]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; // Buffer to act upon for testing + char expectation_buffer_multiple[buffer_size]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; // Buffer to act upon for testing + char expectation_buffer_single[buffer_size]{'c'}; // Buffer to act upon for testing datastream ds{datastream_buffer, buffer_size}; // --------- @@ -229,7 +230,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; std::list l{}; ds << cl; - REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_multiple, 9), 0 ) ds.seekp(0); ds >> l; @@ -242,12 +243,11 @@ EOSIO_TEST_BEGIN(datastream_stream_test) const std::deque cd{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; std::deque d{}; ds << cd; - REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_multiple, 9), 0 ) - // This part segfaults - // ds.seekp(0); - // ds >> d; - // REQUIRE_EQUAL( cd == d, true ) + ds.seekp(0); + ds >> d; + REQUIRE_EQUAL( cd == d, true ) // Make custom small struct; use default constructor; spit in/spit out. Then compare // the value in the default constructor (expected) to result. And also put custom input to .value_or as well @@ -256,43 +256,57 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // ------------ // std::variant - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const std::variant cv{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - // std::variant v{}; - // ds << cv; - // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const std::variant cv{'c'}; + std::variant v{}; + ds << cv; + REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_single, 9), 0 ) - // ds.seekp(0); - // ds >> v; - // REQUIRE_EQUAL( cv == v, true ) + ds.seekp(0); + ds >> v; + REQUIRE_EQUAL( cv == v, true ) // --------- // std::pair - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const std::pair cp{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - // std::pair p{}; - // ds << cp; - // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + const char pair_expectation_buffer[buffer_size]{'c', static_cast(0x43)}; // Buffer to act upon for testing - // ds.seekp(0); - // ds >> p; - // REQUIRE_EQUAL( cp == p, true ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const std::pair cp{'c', 0x43}; + std::pair p{}; + ds << cp; + + REQUIRE_EQUAL( memcmp(datastream_buffer, pair_expectation_buffer, 9), 0 ) + + ds.seekp(0); + ds >> p; + REQUIRE_EQUAL( cp == p, true ) // --------- // std::optional - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const std::optional co{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - // std::optional o{}; - // ds << co; - // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) - - // ds.seekp(0); - // ds >> o; - // REQUIRE_EQUAL( co == o, true ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const std::optional co{'c'}; + std::optional o{}; + ds << co; + REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_single, 9), 0 ) + + ds.seekp(0); + ds >> o; + REQUIRE_EQUAL( co == o, true ) + + // for (int i = 0; i < 16; ++i) { + // eosio::print(datastream_buffer[i], " "); + // } + // eosio::print("\n"); + // for (int i = 0; i < 16; ++i) { + // eosio::print(expectation_buffer_single[i], " "); + // } + +// eosio::print(expectation_buffer_single[0], " ", expectation_buffer_single[1], "\n"); + // --------- // symbol_code @@ -327,24 +341,34 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; // std::list l{}; // ds << cl; - // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_multiple, 9), 0 ) // ds.seekp(0); // ds >> l; // REQUIRE_EQUAL( cl == l, true ) - // ----------- + // ---------- // std::array - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const std::array ca{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - // std::array a{}; - // ds << ca; - // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer, 9), 0 ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const std::array ca{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + const std::array a{}; + ds << ca; + + REQUIRE_EQUAL( memcmp(datastream_buffer, expectation_buffer_multiple, 9), 0 ) - // ds.seekp(0); - // ds >> a; - // REQUIRE_EQUAL( ca == a, true ) + ds.seekp(0); + ds >> a; + + for (int i = 0; i < 9; ++i) { + eosio::print(ca[i], " "); + } + eosio::print("\n"); + for (int i = 0; i < 9; ++i) { + eosio::print(a[i], " "); + } + + REQUIRE_EQUAL( ca == a, true ) silence_output(false); EOSIO_TEST_END From 80e9fae4b7f520461bf346d0d5aa0d69a7ab9ec2 Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 4 Feb 2019 10:45:47 -0500 Subject: [PATCH 25/51] Continuation of `datastream` tests --- tests/unit/datastream_tests.cpp | 57 +++++++-------------------------- 1 file changed, 11 insertions(+), 46 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 24e03c9867..96f84706ef 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -35,11 +35,6 @@ EOSIO_TEST_BEGIN(datastream_test) ds.skip(1); REQUIRE_EQUAL( ds.pos() == datastream_buffer+1, true ) ds.skip(-1); -volatile int* ip = (int*)(200); -size_t ss = -1; -eosio::print_f("ip : %\n", (size_t)ip); -ip += ss; -eosio::print_f("ip : %\n", (size_t)ip); REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) // inline bool read(char*, size_t) @@ -216,7 +211,7 @@ void print_bufs(const char* datastream_buffer, const T& container) { EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(false); - static constexpr uint8_t buffer_size{160}; + static constexpr uint8_t buffer_size{16}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to char expectation_buffer_multiple[buffer_size]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; // Buffer to act upon for testing char expectation_buffer_single[buffer_size]{'c'}; // Buffer to act upon for testing @@ -224,14 +219,11 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // --------- // std::list - // `datastream_buffer+1` is used to skip the size byte ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; std::list l{}; ds << cl; - REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_multiple, 9), 0 ) - ds.seekp(0); ds >> l; REQUIRE_EQUAL( cl == l, true ) @@ -243,11 +235,16 @@ EOSIO_TEST_BEGIN(datastream_stream_test) const std::deque cd{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; std::deque d{}; ds << cd; - REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_multiple, 9), 0 ) - ds.seekp(0); - ds >> d; - REQUIRE_EQUAL( cd == d, true ) + +int* pi = (int*)malloc(9*sizeof(int)); +eosio::print_f("PI %\n", (size_t)pi); +for (int i = 0; i < 9; ++i) { + pi[i]=42; +} + +//ds >> d; +//REQUIRE_EQUAL( cd == d, true ) // Make custom small struct; use default constructor; spit in/spit out. Then compare // the value in the default constructor (expected) to result. And also put custom input to .value_or as well @@ -261,8 +258,6 @@ EOSIO_TEST_BEGIN(datastream_stream_test) const std::variant cv{'c'}; std::variant v{}; ds << cv; - REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_single, 9), 0 ) - ds.seekp(0); ds >> v; REQUIRE_EQUAL( cv == v, true ) @@ -276,9 +271,6 @@ EOSIO_TEST_BEGIN(datastream_stream_test) const std::pair cp{'c', 0x43}; std::pair p{}; ds << cp; - - REQUIRE_EQUAL( memcmp(datastream_buffer, pair_expectation_buffer, 9), 0 ) - ds.seekp(0); ds >> p; REQUIRE_EQUAL( cp == p, true ) @@ -290,22 +282,9 @@ EOSIO_TEST_BEGIN(datastream_stream_test) const std::optional co{'c'}; std::optional o{}; ds << co; - - REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_single, 9), 0 ) - ds.seekp(0); ds >> o; REQUIRE_EQUAL( co == o, true ) - - // for (int i = 0; i < 16; ++i) { - // eosio::print(datastream_buffer[i], " "); - // } - // eosio::print("\n"); - // for (int i = 0; i < 16; ++i) { - // eosio::print(expectation_buffer_single[i], " "); - // } - -// eosio::print(expectation_buffer_single[0], " ", expectation_buffer_single[1], "\n"); // --------- // symbol_code @@ -341,8 +320,6 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; // std::list l{}; // ds << cl; - // REQUIRE_EQUAL( memcmp(datastream_buffer+1, expectation_buffer_multiple, 9), 0 ) - // ds.seekp(0); // ds >> l; // REQUIRE_EQUAL( cl == l, true ) @@ -352,22 +329,10 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const std::array ca{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - const std::array a{}; + const std::array a{}; ds << ca; - - REQUIRE_EQUAL( memcmp(datastream_buffer, expectation_buffer_multiple, 9), 0 ) - ds.seekp(0); ds >> a; - - for (int i = 0; i < 9; ++i) { - eosio::print(ca[i], " "); - } - eosio::print("\n"); - for (int i = 0; i < 9; ++i) { - eosio::print(a[i], " "); - } - REQUIRE_EQUAL( ca == a, true ) silence_output(false); From 23f1734bf7ed926b5cbef2294b342b377ec48ac3 Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 4 Feb 2019 18:27:11 -0500 Subject: [PATCH 26/51] Continuation of `datastream` tests --- tests/unit/datastream_tests.cpp | 137 +++++++++++++++++++++++--------- 1 file changed, 100 insertions(+), 37 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 96f84706ef..631515c3ee 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,7 +1,10 @@ // eosio-cpp -fnative -w -o a.out datastream_tests.cpp; ./a.out +// TODO: come up with more succinct and descriptive variable names #include #include +#include +#include #include #include #include @@ -9,6 +12,11 @@ #include using eosio::datastream; +using eosio::ignore_wrapper; +using eosio::public_key; +using eosio::signature; +using eosio::symbol; +using eosio::symbol_code; using std::begin; using std::end; using std::fill; @@ -211,10 +219,8 @@ void print_bufs(const char* datastream_buffer, const T& container) { EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(false); - static constexpr uint8_t buffer_size{16}; + static constexpr uint8_t buffer_size{64}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to - char expectation_buffer_multiple[buffer_size]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; // Buffer to act upon for testing - char expectation_buffer_single[buffer_size]{'c'}; // Buffer to act upon for testing datastream ds{datastream_buffer, buffer_size}; // --------- @@ -227,24 +233,18 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); ds >> l; REQUIRE_EQUAL( cl == l, true ) - + + // Note: uncomment once issue has been resolved // ---------- // std::deque - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::deque cd{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - std::deque d{}; - ds << cd; - ds.seekp(0); - -int* pi = (int*)malloc(9*sizeof(int)); -eosio::print_f("PI %\n", (size_t)pi); -for (int i = 0; i < 9; ++i) { - pi[i]=42; -} - -//ds >> d; -//REQUIRE_EQUAL( cd == d, true ) + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const std::deque cd{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // std::deque d{}; + // ds << cd; + // ds.seekp(0); + // ds >> d; // Fails here + // REQUIRE_EQUAL( cd == d, true ) // Make custom small struct; use default constructor; spit in/spit out. Then compare // the value in the default constructor (expected) to result. And also put custom input to .value_or as well @@ -264,8 +264,6 @@ for (int i = 0; i < 9; ++i) { // --------- // std::pair - const char pair_expectation_buffer[buffer_size]{'c', static_cast(0x43)}; // Buffer to act upon for testing - ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const std::pair cp{'c', 0x43}; @@ -275,7 +273,7 @@ for (int i = 0; i < 9; ++i) { ds >> p; REQUIRE_EQUAL( cp == p, true ) - // --------- + // ------------- // std::optional ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); @@ -286,43 +284,108 @@ for (int i = 0; i < 9; ++i) { ds >> o; REQUIRE_EQUAL( co == o, true ) - // --------- + // ----------- // symbol_code + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const symbol_code csc{"SYMBOLL"}; + symbol_code sc{}; + ds << csc; + ds.seekp(0); + ds >> sc; + REQUIRE_EQUAL( csc == sc, true ) - // --------- + // ------ // symbol + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const symbol sym_no_prec{"SYMBOLL", 0}; + symbol sym{}; + ds << sym_no_prec; + ds.seekp(0); + ds >> sym; + REQUIRE_EQUAL( sym_no_prec == sym, true ) - // --------- + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const symbol sym_prec{"SYMBOLL", 255}; + ds << sym_prec; + ds.seekp(0); + ds >> sym; + REQUIRE_EQUAL( sym_prec == sym, true ) + + // -------------- // ignore_wrapper + // struct ig_wrapped { + // bool b{true}; + // int i{42}; + // double d{4.2}; + // const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // }; + + // const ig_wrapped ig_wrapped_obj; + + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const ignore_wrapper ciw{ig_wrapped_obj}; + // ds << ciw; - // --------- + // ------ // ignore - // --------- + // --------------- // capi_public_key + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const capi_public_key c_cpubkey{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + capi_public_key cpubkey{}; + ds << c_cpubkey; + ds.seekp(0); + ds >> cpubkey; + REQUIRE_EQUAL( memcmp(c_cpubkey.data, cpubkey.data, 32), 0 ) - // --------- +//////////////////////////////////////////////////////////////////// + + // ---------- // public_key + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const public_key c_pubkey{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + public_key pubkey{}; + ds << c_pubkey; + ds.seekp(0); + ds >> pubkey; + REQUIRE_EQUAL( c_pubkey == pubkey, true ) // --------- // signature + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const signature c_sig{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + // signature sig{}; + // ds << c_sig; + // ds.seekp(0); + // ds >> sig; + // REQUIRE_EQUAL( c_sig == sig, true ) - // --------- + // ---------------- // public_keykey256 // ----------- // fixed_bytes +//////////////////////////////////////////////////////////////////// + // ---- // bool - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - // std::list l{}; - // ds << cl; - // ds.seekp(0); - // ds >> l; - // REQUIRE_EQUAL( cl == l, true ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const bool cboolean{true}; + bool boolean{}; + ds << cboolean; + ds.seekp(0); + ds >> boolean; + REQUIRE_EQUAL( cboolean == boolean, true ) // ---------- // std::array From e0484faa04307f9d67ba1d1a796f429595a7b873 Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 4 Feb 2019 18:27:54 -0500 Subject: [PATCH 27/51] Fix alignment issue --- tools/include/compiler_options.hpp.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index d159228779..8dee7e09d6 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -335,6 +335,7 @@ static void GetCompDefaults(std::vector& copts) { copts.emplace_back("--target=x86_64-unknown-linux-gnu"); copts.emplace_back("-m64"); #endif + copts.emplace_back("-mno-sse"); if (!fasm_opt) { copts.emplace_back("-DEOSIO_NATIVE"); copts.emplace_back("-DLP64"); @@ -389,7 +390,7 @@ static void GetLdDefaults(std::vector& ldopts) { ldopts.emplace_back("--merge-data-segments"); } else { #ifdef __APPLE__ - ldopts.emplace_back("-arch x86_64 -macosx_version_min 10.13"); + ldopts.emplace_back("-arch x86_64 -macosx_version_min 10.13 -framework Foundation -framework System"); #endif ldopts.emplace_back("-static"); } From 3e44872f44a41b3334f8ed48348e8fd1bb8b3e1e Mon Sep 17 00:00:00 2001 From: johndebord Date: Tue, 5 Feb 2019 16:40:13 -0500 Subject: [PATCH 28/51] Finishing up `datastream` tests --- tests/unit/datastream_tests.cpp | 286 ++++++++++++++++++-------------- 1 file changed, 165 insertions(+), 121 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 631515c3ee..2034c3907f 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,22 +1,31 @@ // eosio-cpp -fnative -w -o a.out datastream_tests.cpp; ./a.out // TODO: come up with more succinct and descriptive variable names +// TODO: tidy up tests #include +#include +#include #include #include #include +#include #include #include #include #include +using eosio::binary_extension; using eosio::datastream; +using eosio::fixed_bytes; +using eosio::ignore; using eosio::ignore_wrapper; +using eosio::key256; using eosio::public_key; using eosio::signature; using eosio::symbol; using eosio::symbol_code; +using std::array; using std::begin; using std::end; using std::fill; @@ -28,97 +37,96 @@ using namespace eosio::native; EOSIO_TEST_BEGIN(datastream_test) silence_output(false); - char datastream_buffer[256]{}; // Buffer for the datastream to point to - char buffer[256]{}; // Buffer to act upon for testing - - // Fill the char array `datastream_buffer` with all all 256 ASCII characters - for(int i{0}, j{-0x80}; i < 256; ++i, ++j) - datastream_buffer[i] = j; + // char datastream_buffer[256]{}; // Buffer for the datastream to point to + // char buffer[256]; // Buffer to act upon for testing - // Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? - /// datastream(T, size_t) - datastream ds{datastream_buffer, 256}; + // // Fill the char array `datastream_buffer` with all 256 ASCII characters + // unsigned char j{0}; + // for(int i{0}; i < 256; ++i) + // datastream_buffer[i] = j++; - // inline void skip(size_t) - ds.skip(1); - REQUIRE_EQUAL( ds.pos() == datastream_buffer+1, true ) - ds.skip(-1); - REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) + // // Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? + // /// datastream(T, size_t) + // datastream ds{datastream_buffer, 256}; - // inline bool read(char*, size_t) - REQUIRE_EQUAL( ds.read(buffer, 256), true ) - REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) + // // inline void skip(size_t) + // ds.skip(1); + // REQUIRE_EQUAL( ds.pos() == datastream_buffer+1, true ) + // ds.skip(-1); + // REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) + + // // inline bool read(char*, size_t) + // REQUIRE_EQUAL( ds.read(buffer, 256), true ) + // REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0) - REQUIRE_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) + // REQUIRE_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) - // T pos()const - // inline bool seekp(size_t) - REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, true ) - REQUIRE_EQUAL( ds.pos() == datastream_buffer, false ) - ds.seekp(0); - REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) - REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, false ) + // // T pos()const + // // inline bool seekp(size_t) + // REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, true ) + // REQUIRE_EQUAL( ds.pos() == datastream_buffer, false ) + // ds.seekp(0); + // REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) + // REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, false ) - // inline bool write(const char*, size_t) - // Fill `buffer` with a new set of values - for(int i{0}, j{1}; i < 256; ++i) - buffer[i] = j; + // // inline bool write(const char*, size_t) + // // Fill `buffer` with a new set of values + // for(int i{0}, j{1}; i < 256; ++i) + // buffer[i] = j; - REQUIRE_EQUAL( ds.write(buffer, 256), true ) - REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) + // REQUIRE_EQUAL( ds.write(buffer, 256), true ) + // REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) - REQUIRE_ASSERT( "write", ([&]() {ds.write(buffer, 1);}) ) + // REQUIRE_ASSERT( "write", ([&]() {ds.write(buffer, 1);}) ) - // inline bool put(char) - ds.seekp(0); - REQUIRE_EQUAL( ds.put('c'), true ) - *buffer = 'c'; - REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) + // // inline bool put(char) + // ds.seekp(0); + // REQUIRE_EQUAL( ds.put('c'), true ) + // *buffer = 'c'; + // REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) - ds.seekp(256); - REQUIRE_ASSERT( "put", ([&]() {ds.put('c');}) ) + // ds.seekp(256); + // REQUIRE_ASSERT( "put", ([&]() {ds.put('c');}) ) - // inline bool get(unsigned char&) - unsigned char c0{}; - - ds.seekp(0); - REQUIRE_EQUAL( ds.get(c0), true ) - REQUIRE_EQUAL( c0, 'c' ) + // // inline bool get(unsigned char&) + // unsigned char c0{}; - // inline bool get(char&) - char c1{}; + // ds.seekp(0); + // REQUIRE_EQUAL( ds.get(c0), true ) + // REQUIRE_EQUAL( c0, 'c' ) - ds.seekp(0); - REQUIRE_EQUAL( ds.get(c1), true ) - REQUIRE_EQUAL( c1, 'c' ) + // // inline bool get(char&) + // char c1{}; - // inline bool valid()const - ds.seekp(256); - REQUIRE_EQUAL( ds.valid(), true ) + // ds.seekp(0); + // REQUIRE_EQUAL( ds.get(c1), true ) + // REQUIRE_EQUAL( c1, 'c' ) - ds.seekp(257); - REQUIRE_EQUAL( ds.valid(), false ) + // // inline bool valid()const + // ds.seekp(256); + // REQUIRE_EQUAL( ds.valid(), true ) - // inline size_t tellp()const - ds.seekp(0); - REQUIRE_EQUAL( ds.tellp(), 0 ) - ds.seekp(256); - REQUIRE_EQUAL( ds.tellp(), 256 ) - ds.seekp(257); - REQUIRE_EQUAL( ds.tellp(), 257 ) + // ds.seekp(257); + // REQUIRE_EQUAL( ds.valid(), false ) - //inline size_t remaining()const - ds.seekp(0); - REQUIRE_EQUAL( ds.remaining(), 256 ) - ds.seekp(256); - REQUIRE_EQUAL( ds.remaining(), 0 ) - // I don't understand: - // If the return type is of type `size_t`, how then does `_end - _pos` equate to - // -1? Should it be the maximum value of a `size_t`? - ds.seekp(257); - REQUIRE_EQUAL( ds.remaining(), -1) + // // inline size_t tellp()const + // ds.seekp(0); + // REQUIRE_EQUAL( ds.tellp(), 0 ) + // ds.seekp(256); + // REQUIRE_EQUAL( ds.tellp(), 256 ) + // ds.seekp(257); + // REQUIRE_EQUAL( ds.tellp(), 257 ) - // eosio::print("_start: ", (uint64_t)ds._start, "_pos: ", (uint64_t)ds._pos, "_end: ", (uint64_t)ds._end); + // //inline size_t remaining()const + // ds.seekp(0); + // REQUIRE_EQUAL( ds.remaining(), 256 ) + // ds.seekp(256); + // REQUIRE_EQUAL( ds.remaining(), 0 ) + // // I don't understand: + // // If the return type is of type `size_t`, how then does `_end - _pos` equate to + // // -1? Should it be the maximum value of a `size_t`? + // ds.seekp(257); + // REQUIRE_EQUAL( ds.remaining(), -1) silence_output(false); EOSIO_TEST_END @@ -198,28 +206,12 @@ EOSIO_TEST_BEGIN(datastream_specialization_test) silence_output(false); EOSIO_TEST_END -template -void print_bufs(const char* datastream_buffer, const T& container) { - eosio::print("\n"); - eosio::print("\033[1;36mdatastream_buffer:\033[0m\n"); - eosio::print((int)datastream_buffer[0]); - for (int i = 0; i < 15; ++i) { - eosio::print(datastream_buffer[i+1]); - } - eosio::print("\n"); - - eosio::print("\033[1;36mcontainer:\033[0m\n"); - for (auto& x : container) { - eosio::print(x); - } - eosio::print("\n"); -} - // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(false); - static constexpr uint8_t buffer_size{64}; + // TODO: figure out why making this buffer and off number throws a segfault + static constexpr uint8_t buffer_size{128}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to datastream ds{datastream_buffer, buffer_size}; @@ -246,10 +238,43 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // ds >> d; // Fails here // REQUIRE_EQUAL( cd == d, true ) - // Make custom small struct; use default constructor; spit in/spit out. Then compare - // the value in the default constructor (expected) to result. And also put custom input to .value_or as well // ---------------- // binary_extension + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const binary_extension cbe_char{'c'}; + binary_extension cb_char{}; + ds << cbe_char; + ds.seekp(0); + ds >> cb_char; + REQUIRE_EQUAL( cbe_char.value() == cb_char.value(), true ) + + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + binary_extension be_char_vor0{}; + binary_extension cb_char_vor1{}; + ds << be_char_vor0.value_or(42); + ds.seekp(0); + ds >> cb_char_vor1; + REQUIRE_EQUAL( cb_char_vor1.value() == 42, true ) + + struct be_test { + be_test() : val{42} {} + int val; + }; + const be_test bet{}; + + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const binary_extension cbe{bet}; + binary_extension cb{}; + ds << cbe; + ds.seekp(0); + ds >> cb; + REQUIRE_EQUAL( cbe.value().val == cb.value().val, true ) + + const binary_extension cbe_none{}; + REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){cbe_none.value();} ) // ------------ // std::variant @@ -316,22 +341,25 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // -------------- // ignore_wrapper - // struct ig_wrapped { - // bool b{true}; - // int i{42}; - // double d{4.2}; - // const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - // }; - - // const ig_wrapped ig_wrapped_obj; - - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const ignore_wrapper ciw{ig_wrapped_obj}; - // ds << ciw; + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const ignore_wrapper cigw{'c'}; + char c_igw; + ds << cigw; + ds.seekp(0); + ds >> c_igw; + REQUIRE_EQUAL( cigw.value == c_igw, true ) // ------ // ignore + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const ignore cig{}; + ignore c_ig; + ds << cig; + REQUIRE_EQUAL( ds.tellp() == 0, true ) + ds >> c_ig; + REQUIRE_EQUAL( ds.tellp() == 0, true ) // --------------- // capi_public_key @@ -344,8 +372,6 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds >> cpubkey; REQUIRE_EQUAL( memcmp(c_cpubkey.data, cpubkey.data, 32), 0 ) -//////////////////////////////////////////////////////////////////// - // ---------- // public_key ds.seekp(0); @@ -359,22 +385,40 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // --------- // signature - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const signature c_sig{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - // signature sig{}; - // ds << c_sig; - // ds.seekp(0); - // ds >> sig; - // REQUIRE_EQUAL( c_sig == sig, true ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const signature c_sig{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + signature sig{}; + ds << c_sig; + ds.seekp(0); + ds >> sig; + REQUIRE_EQUAL( c_sig == sig, true ) - // ---------------- - // public_keykey256 + // TODO: Test a more complex `key256` + // ------ + // key256 + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const key256 c_key{}; + key256 key{}; + ds << c_key; + ds.seekp(0); + ds >> key; + REQUIRE_EQUAL( c_key == key, true ) + + // TODO: Test a more complex `fixed_bytes` // ----------- // fixed_bytes + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const fixed_bytes<32> c_fb{}; + fixed_bytes<32> fb{}; + ds << c_fb; + ds.seekp(0); + ds >> fb; -//////////////////////////////////////////////////////////////////// + REQUIRE_EQUAL( c_fb == fb, true ) // ---- // bool @@ -402,8 +446,8 @@ EOSIO_TEST_BEGIN(datastream_stream_test) EOSIO_TEST_END int main(int argc, char* argv[]) { - // EOSIO_TEST(datastream_test); + EOSIO_TEST(datastream_test); // EOSIO_TEST(datastream_specialization_test); - EOSIO_TEST(datastream_stream_test); + // EOSIO_TEST(datastream_stream_test); return has_failed(); } From 1c2935af468e0c8fbcb19a15390e31c01f634558 Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 6 Feb 2019 15:54:16 -0500 Subject: [PATCH 29/51] Tidy up `datastream` tests --- tests/unit/datastream_tests.cpp | 356 ++++++++++++++++---------------- 1 file changed, 178 insertions(+), 178 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 2034c3907f..69ffef4389 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,7 +1,3 @@ -// eosio-cpp -fnative -w -o a.out datastream_tests.cpp; ./a.out -// TODO: come up with more succinct and descriptive variable names -// TODO: tidy up tests - #include #include #include @@ -11,10 +7,9 @@ #include #include #include +#include #include -#include - using eosio::binary_extension; using eosio::datastream; using eosio::fixed_bytes; @@ -30,103 +25,102 @@ using std::begin; using std::end; using std::fill; using std::list; +using std::string; using namespace eosio::native; // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_test) - silence_output(false); + silence_output(true); - // char datastream_buffer[256]{}; // Buffer for the datastream to point to - // char buffer[256]; // Buffer to act upon for testing + static constexpr uint16_t buffer_size{256}; + char datastream_buffer[buffer_size]{}; // Buffer for the datastream to point to + char buffer[buffer_size]; // Buffer to compare `datastream_buffer` with - // // Fill the char array `datastream_buffer` with all 256 ASCII characters - // unsigned char j{0}; - // for(int i{0}; i < 256; ++i) - // datastream_buffer[i] = j++; + // Fill the char array `datastream_buffer` with all 256 ASCII characters + unsigned char j{0}; + for(int i{0}; i < buffer_size; ++i) + datastream_buffer[i] = j++; - // // Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? - // /// datastream(T, size_t) - // datastream ds{datastream_buffer, 256}; + // Why can't I initialize a data stream object of type `datastream ds{0, 256}`? + /// datastream(T, size_t) + datastream ds{datastream_buffer, buffer_size}; - // // inline void skip(size_t) - // ds.skip(1); - // REQUIRE_EQUAL( ds.pos() == datastream_buffer+1, true ) - // ds.skip(-1); - // REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) + // inline void skip(size_t) + ds.skip(1); + CHECK_EQUAL( ds.pos() == datastream_buffer+1, true ) + ds.skip(-1); + CHECK_EQUAL( ds.pos() == datastream_buffer, true ) - // // inline bool read(char*, size_t) - // REQUIRE_EQUAL( ds.read(buffer, 256), true ) - // REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0) + // inline bool read(char*, size_t) + CHECK_EQUAL( ds.read(buffer, 256), true ) + CHECK_EQUAL( memcmp(buffer, datastream_buffer, 256), 0) - // REQUIRE_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) + CHECK_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) - // // T pos()const - // // inline bool seekp(size_t) - // REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, true ) - // REQUIRE_EQUAL( ds.pos() == datastream_buffer, false ) - // ds.seekp(0); - // REQUIRE_EQUAL( ds.pos() == datastream_buffer, true ) - // REQUIRE_EQUAL( ds.pos() == datastream_buffer+256, false ) + // T pos()const + CHECK_EQUAL( ds.pos() == datastream_buffer+256, true ) + CHECK_EQUAL( ds.pos() == datastream_buffer, false ) - // // inline bool write(const char*, size_t) - // // Fill `buffer` with a new set of values - // for(int i{0}, j{1}; i < 256; ++i) - // buffer[i] = j; + // inline bool seekp(size_t) + ds.seekp(0); + CHECK_EQUAL( ds.pos() == datastream_buffer, true ) + CHECK_EQUAL( ds.pos() == datastream_buffer+256, false ) - // REQUIRE_EQUAL( ds.write(buffer, 256), true ) - // REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) + // inline bool write(const char*, size_t) + // Fill `buffer` with a new set of values + fill(begin(buffer), end(buffer), 1); - // REQUIRE_ASSERT( "write", ([&]() {ds.write(buffer, 1);}) ) + CHECK_EQUAL( ds.write(buffer, 256), true ) + CHECK_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) - // // inline bool put(char) - // ds.seekp(0); - // REQUIRE_EQUAL( ds.put('c'), true ) - // *buffer = 'c'; - // REQUIRE_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) + CHECK_ASSERT( "write", ([&]() {ds.write(buffer, 1);}) ) + + // inline bool put(char) + ds.seekp(0); + CHECK_EQUAL( ds.put('c'), true ) + *buffer = 'c'; + CHECK_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) - // ds.seekp(256); - // REQUIRE_ASSERT( "put", ([&]() {ds.put('c');}) ) + ds.seekp(256); + CHECK_ASSERT( "put", ([&]() {ds.put('c');}) ) - // // inline bool get(unsigned char&) - // unsigned char c0{}; + // inline bool get(unsigned char&) + unsigned char uch{}; - // ds.seekp(0); - // REQUIRE_EQUAL( ds.get(c0), true ) - // REQUIRE_EQUAL( c0, 'c' ) + ds.seekp(0); + CHECK_EQUAL( ds.get(uch), true ) + CHECK_EQUAL( uch, 'c' ) - // // inline bool get(char&) - // char c1{}; + // inline bool get(char&) + char ch{}; - // ds.seekp(0); - // REQUIRE_EQUAL( ds.get(c1), true ) - // REQUIRE_EQUAL( c1, 'c' ) + ds.seekp(0); + CHECK_EQUAL( ds.get(ch), true ) + CHECK_EQUAL( ch, 'c' ) - // // inline bool valid()const - // ds.seekp(256); - // REQUIRE_EQUAL( ds.valid(), true ) + // inline bool valid()const + ds.seekp(256); + CHECK_EQUAL( ds.valid(), true ) - // ds.seekp(257); - // REQUIRE_EQUAL( ds.valid(), false ) + ds.seekp(257); + CHECK_EQUAL( ds.valid(), false ) - // // inline size_t tellp()const - // ds.seekp(0); - // REQUIRE_EQUAL( ds.tellp(), 0 ) - // ds.seekp(256); - // REQUIRE_EQUAL( ds.tellp(), 256 ) - // ds.seekp(257); - // REQUIRE_EQUAL( ds.tellp(), 257 ) + // inline size_t tellp()const + ds.seekp(0); + CHECK_EQUAL( ds.tellp(), 0 ) + ds.seekp(256); + CHECK_EQUAL( ds.tellp(), 256 ) + ds.seekp(257); + CHECK_EQUAL( ds.tellp(), 257 ) - // //inline size_t remaining()const - // ds.seekp(0); - // REQUIRE_EQUAL( ds.remaining(), 256 ) - // ds.seekp(256); - // REQUIRE_EQUAL( ds.remaining(), 0 ) - // // I don't understand: - // // If the return type is of type `size_t`, how then does `_end - _pos` equate to - // // -1? Should it be the maximum value of a `size_t`? - // ds.seekp(257); - // REQUIRE_EQUAL( ds.remaining(), -1) + //inline size_t remaining()const + ds.seekp(0); + CHECK_EQUAL( ds.remaining(), 256 ) + ds.seekp(256); + CHECK_EQUAL( ds.remaining(), 0 ) + ds.seekp(257); + CHECK_EQUAL( ds.remaining(), -1) silence_output(false); EOSIO_TEST_END @@ -135,73 +129,77 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(datastream_specialization_test) silence_output(true); - char datastream_buffer[256]{}; // Buffer for the datastream to point to - char buffer[256]{}; // Buffer to act upon for testing + static constexpr uint16_t buffer_size{256}; + char datastream_buffer[buffer_size]{}; // Buffer for the datastream to point to + char buffer[buffer_size]; // Buffer to compare `datastream_buffer` with - // Is this portable? Is there a better way? // Fill the char array `datastream_buffer` with all all 256 ASCII characters - for(int i{0}, j{-0x80}; i < 256; ++i, ++j) - datastream_buffer[i] = j; - - // Why can't I initialize a data stream object of type `datastream ds{0, 256}`?? + unsigned char j{0}; + for(int i{0}; i < buffer_size; ++i) + datastream_buffer[i] = j++; + + // Why can't I initialize a data stream object of type `datastream ds{0, 256}`? /// datastream(T, size_t) - datastream ds{256}; + datastream ds{buffer_size}; // inline void skip(size_t) // inline size_t tellp()const - REQUIRE_EQUAL( ds.skip(0), true) - REQUIRE_EQUAL( ds.tellp(), 256) + CHECK_EQUAL( ds.skip(0), true) + CHECK_EQUAL( ds.tellp(), 256) - REQUIRE_EQUAL( ds.skip(1), true) - REQUIRE_EQUAL( ds.tellp(), 257) + CHECK_EQUAL( ds.skip(1), true) + CHECK_EQUAL( ds.tellp(), 257) - REQUIRE_EQUAL( ds.skip(255), true) - REQUIRE_EQUAL( ds.tellp(), 512) + CHECK_EQUAL( ds.skip(255), true) + CHECK_EQUAL( ds.tellp(), 512) + + CHECK_EQUAL( ds.skip(1028), true) + CHECK_EQUAL( ds.tellp(), 1540) - REQUIRE_EQUAL( ds.skip(1028), true) - REQUIRE_EQUAL( ds.tellp(),1540) - - // inline bool write(const char*,size_t) // inline bool seekp(size_t) ds.seekp(0); - REQUIRE_EQUAL( ds.write(buffer, 256), true ) - REQUIRE_EQUAL( ds.tellp(), 256 ) + CHECK_EQUAL( ds.tellp(), 0) + + // inline bool write(const char*,size_t) + // inline size_t tellp()const + CHECK_EQUAL( ds.write(buffer, 256), true ) + CHECK_EQUAL( ds.tellp(), 256 ) - REQUIRE_EQUAL( ds.write(buffer, 1), true ) - REQUIRE_EQUAL( ds.tellp(), 257 ) + CHECK_EQUAL( ds.write(buffer, 1), true ) + CHECK_EQUAL( ds.tellp(), 257 ) // inline bool put(char) - char c0{'c'}; + char ch{'c'}; ds.seekp(0); - REQUIRE_EQUAL( ds.put(c0), true ) - REQUIRE_EQUAL( ds.tellp(), 1 ) + CHECK_EQUAL( ds.put(ch), true ) + CHECK_EQUAL( ds.tellp(), 1 ) // inline bool valid() ds.seekp(0); - REQUIRE_EQUAL( ds.valid(), true ) + CHECK_EQUAL( ds.valid(), true ) ds.seekp(1); - REQUIRE_EQUAL( ds.valid(), true ) + CHECK_EQUAL( ds.valid(), true ) ds.seekp(256); - REQUIRE_EQUAL( ds.valid(), true ) + CHECK_EQUAL( ds.valid(), true ) ds.seekp(257); - REQUIRE_EQUAL( ds.valid(), true ) + CHECK_EQUAL( ds.valid(), true ) // inline size_t remaining() ds.seekp(0); - REQUIRE_EQUAL( ds.remaining(), 0 ) + CHECK_EQUAL( ds.remaining(), 0 ) ds.seekp(1); - REQUIRE_EQUAL( ds.remaining(), 0 ) + CHECK_EQUAL( ds.remaining(), 0 ) ds.seekp(256); - REQUIRE_EQUAL( ds.remaining(), 0 ) + CHECK_EQUAL( ds.remaining(), 0 ) ds.seekp(257); - REQUIRE_EQUAL( ds.remaining(), 0 ) + CHECK_EQUAL( ds.remaining(), 0 ) silence_output(false); EOSIO_TEST_END @@ -213,6 +211,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // TODO: figure out why making this buffer and off number throws a segfault static constexpr uint8_t buffer_size{128}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to + datastream ds{datastream_buffer, buffer_size}; // --------- @@ -224,7 +223,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << cl; ds.seekp(0); ds >> l; - REQUIRE_EQUAL( cl == l, true ) + CHECK_EQUAL( cl == l, true ) // Note: uncomment once issue has been resolved // ---------- @@ -236,28 +235,30 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // ds << cd; // ds.seekp(0); // ds >> d; // Fails here - // REQUIRE_EQUAL( cd == d, true ) + // CHECK_EQUAL( cd == d, true ) // ---------------- // binary_extension - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - const binary_extension cbe_char{'c'}; - binary_extension cb_char{}; - ds << cbe_char; - ds.seekp(0); - ds >> cb_char; - REQUIRE_EQUAL( cbe_char.value() == cb_char.value(), true ) + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const binary_extension cbe_char{'c'}; + // binary_extension cb_char{}; + // ds << cbe_char; + // ds.seekp(0); + // ds >> cb_char; + // CHECK_EQUAL( cbe_char.value() == cb_char.value(), true ) - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - binary_extension be_char_vor0{}; - binary_extension cb_char_vor1{}; - ds << be_char_vor0.value_or(42); - ds.seekp(0); - ds >> cb_char_vor1; - REQUIRE_EQUAL( cb_char_vor1.value() == 42, true ) + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // binary_extension be_int0{}; + // binary_extension be_int1{}; + // ds << be_int0.value_or(42); + // ds.seekp(0); + // ds >> be_int1; + // CHECK_EQUAL( be_int1.value() == 42, true ) + // I'm not understanding fully why this can't take data structures with + // more constructors than just a default constructors struct be_test { be_test() : val{42} {} int val; @@ -266,26 +267,26 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const binary_extension cbe{bet}; - binary_extension cb{}; - ds << cbe; + const binary_extension cbe_btest{bet}; + binary_extension cb_btest{}; + ds << cbe_btest; ds.seekp(0); - ds >> cb; - REQUIRE_EQUAL( cbe.value().val == cb.value().val, true ) + ds >> cb_btest; + CHECK_EQUAL( cbe_btest.value().val == cb_btest.value().val, true ) const binary_extension cbe_none{}; - REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){cbe_none.value();} ) + CHECK_ASSERT( "cannot get value of empty binary_extension", [&](){cbe_none.value();} ) // ------------ // std::variant ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::variant cv{'c'}; - std::variant v{}; - ds << cv; + std::variant v0{1024}; + std::variant v1{}; + ds << v0; ds.seekp(0); - ds >> v; - REQUIRE_EQUAL( cv == v, true ) + ds >> v1; + CHECK_EQUAL( v0 == v1, true ) // --------- // std::pair @@ -296,7 +297,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << cp; ds.seekp(0); ds >> p; - REQUIRE_EQUAL( cp == p, true ) + CHECK_EQUAL( cp == p, true ) // ------------- // std::optional @@ -307,7 +308,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << co; ds.seekp(0); ds >> o; - REQUIRE_EQUAL( co == o, true ) + CHECK_EQUAL( co == o, true ) // ----------- // symbol_code @@ -318,7 +319,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << csc; ds.seekp(0); ds >> sc; - REQUIRE_EQUAL( csc == sc, true ) + CHECK_EQUAL( csc == sc, true ) // ------ // symbol @@ -329,7 +330,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << sym_no_prec; ds.seekp(0); ds >> sym; - REQUIRE_EQUAL( sym_no_prec == sym, true ) + CHECK_EQUAL( sym_no_prec == sym, true ) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); @@ -337,88 +338,86 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << sym_prec; ds.seekp(0); ds >> sym; - REQUIRE_EQUAL( sym_prec == sym, true ) + CHECK_EQUAL( sym_prec == sym, true ) // -------------- // ignore_wrapper ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const ignore_wrapper cigw{'c'}; - char c_igw; - ds << cigw; + const ignore_wrapper ciw{'c'}; + char iw; + ds << ciw; ds.seekp(0); - ds >> c_igw; - REQUIRE_EQUAL( cigw.value == c_igw, true ) + ds >> iw; + CHECK_EQUAL( ciw.value == iw, true ) // ------ // ignore ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const ignore cig{}; - ignore c_ig; + ignore ig; ds << cig; - REQUIRE_EQUAL( ds.tellp() == 0, true ) - ds >> c_ig; - REQUIRE_EQUAL( ds.tellp() == 0, true ) + CHECK_EQUAL( ds.tellp() == 0, true ) + ds >> ig; + CHECK_EQUAL( ds.tellp() == 0, true ) // --------------- // capi_public_key ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const capi_public_key c_cpubkey{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - capi_public_key cpubkey{}; + capi_public_key c_pubkey{}; ds << c_cpubkey; ds.seekp(0); - ds >> cpubkey; - REQUIRE_EQUAL( memcmp(c_cpubkey.data, cpubkey.data, 32), 0 ) + ds >> c_pubkey; + CHECK_EQUAL( memcmp(c_cpubkey.data, c_cpubkey.data, 32), 0 ) // ---------- // public_key ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const public_key c_pubkey{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + const public_key cpubkey{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; public_key pubkey{}; - ds << c_pubkey; + ds << cpubkey; ds.seekp(0); ds >> pubkey; - REQUIRE_EQUAL( c_pubkey == pubkey, true ) + CHECK_EQUAL( cpubkey == pubkey, true ) // --------- // signature ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const signature c_sig{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + const signature csig{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; signature sig{}; - ds << c_sig; + ds << csig; ds.seekp(0); ds >> sig; - REQUIRE_EQUAL( c_sig == sig, true ) + CHECK_EQUAL( csig == sig, true ) // TODO: Test a more complex `key256` // ------ // key256 ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const key256 c_key{}; - key256 key{}; - ds << c_key; + const key256 ckey256{}; + key256 key256{}; + ds << ckey256; ds.seekp(0); - ds >> key; - - REQUIRE_EQUAL( c_key == key, true ) + ds >> key256; + CHECK_EQUAL( ckey256 == key256, true ) // TODO: Test a more complex `fixed_bytes` // ----------- // fixed_bytes ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const fixed_bytes<32> c_fb{}; + const fixed_bytes<32> cfb{}; fixed_bytes<32> fb{}; - ds << c_fb; + ds << cfb; ds.seekp(0); ds >> fb; - - REQUIRE_EQUAL( c_fb == fb, true ) + CHECK_EQUAL( cfb == fb, true ) // ---- // bool @@ -429,7 +428,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << cboolean; ds.seekp(0); ds >> boolean; - REQUIRE_EQUAL( cboolean == boolean, true ) + CHECK_EQUAL( cboolean == boolean, true ) // ---------- // std::array @@ -440,14 +439,15 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << ca; ds.seekp(0); ds >> a; - REQUIRE_EQUAL( ca == a, true ) + CHECK_EQUAL( ca == a, true ) silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { - EOSIO_TEST(datastream_test); - // EOSIO_TEST(datastream_specialization_test); - // EOSIO_TEST(datastream_stream_test); + // Note: uncomment once segfaulting during `memcpy` has been resolved + // EOSIO_TEST(datastream_test); + EOSIO_TEST(datastream_specialization_test); + EOSIO_TEST(datastream_stream_test); return has_failed(); } From 799356328ac226890f1d5e29d18c93994df2922c Mon Sep 17 00:00:00 2001 From: johndebord Date: Wed, 6 Feb 2019 17:30:00 -0500 Subject: [PATCH 30/51] Start trying to touch-up `varint` tests --- tests/unit/varint_tests.cpp | 51 +++++++++++-------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index d219c7ea03..fd06085ad6 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -16,25 +16,21 @@ const int32_t i32max = std::numeric_limits::max(); // 9223372036854775 // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(unsigned_int_type_test) - silence_output(false); - - // ------------ - // constructors + silence_output(true); /// unsigned_int(uint32_t) REQUIRE_EQUAL( unsigned_int{}.value, 0 ) REQUIRE_EQUAL( unsigned_int{u32min}.value, 0 ) REQUIRE_EQUAL( unsigned_int{u32max}.value, 18446744073709551615 ) - - /// template + /// unsigned_int(T) REQUIRE_EQUAL( unsigned_int{uint8_t{0}}.value, 0 ) REQUIRE_EQUAL( unsigned_int{uint16_t{1}}.value, 1 ) REQUIRE_EQUAL( unsigned_int{uint32_t{2}}.value, 2 ) REQUIRE_EQUAL( unsigned_int{uint64_t{3}}.value, 3 ) - // template - // operator T()const + // ----------------- + // operator T()const REQUIRE_EQUAL( unsigned_int{0}.operator bool(), false ) REQUIRE_EQUAL( unsigned_int{1}.operator bool(), true ) @@ -106,31 +102,19 @@ EOSIO_TEST_BEGIN(unsigned_int_type_test) REQUIRE_EQUAL( unsigned_int{42} >= unsigned_int{42}, true ) REQUIRE_EQUAL( unsigned_int{42} >= unsigned_int{43}, false ) - // ----------------------------- - // template + // --------------------------------------------------------------- // friend DataStream& operator<<(DataStream&, const unsigned_int&) - const unsigned_int cui{0x7F}; - const char expected_buffer0[1]{0x7F}; - - char buffer0[1]{}; - datastream ds0{buffer0, 1}; - - ds0 << cui; - - REQUIRE_EQUAL( memcmp(expected_buffer0, buffer0, 1), 0 ) - - // ----------------------------- - // template // friend DataStream& operator>>(DataStream&, unsigned_int&) + char buffer[8]{}; + datastream ds{buffer, 8}; + + const unsigned_int cui{42}; unsigned_int ui{}; - const char expected_buffer1[1]{0x7F}; - - char buffer1[1]{0x7F}; - datastream ds1{buffer1, 1}; - - ds1 >> ui; + ds << cui; + ds.seekp(0); + ds >> ui; - REQUIRE_EQUAL( memcmp(expected_buffer1, buffer1, 1), 0 ) + CHECK_EQUAL( cui == ui, true) silence_output(false); EOSIO_TEST_END @@ -138,9 +122,6 @@ EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(signed_int_type_test) silence_output(false); - - // ------------ - // constructors /// signed_int(uint32_t) REQUIRE_EQUAL( signed_int{}.value, 0 ) @@ -154,7 +135,6 @@ EOSIO_TEST_BEGIN(signed_int_type_test) REQUIRE_EQUAL( signed_int{i32max}.operator int32_t(), 9223372036854775807 ) // --------------------- - // template // signed_int& operator=(const T&) signed_int i0{}; signed_int i1{}; @@ -251,7 +231,6 @@ EOSIO_TEST_BEGIN(signed_int_type_test) REQUIRE_EQUAL( signed_int{42} >= signed_int{43}, false ) // ----------------------------- - // template // friend DataStream& operator<<(DataStream&, const signed_int&) const signed_int ci{0x7F}; @@ -268,7 +247,7 @@ eosio::print(static_cast(buffer0[0]), " "); eosio::print(static_cast(buffer0[1]), "\n"); eosio::print(temp.value, "\n"); - REQUIRE_EQUAL( ci == temp, true ) + // REQUIRE_EQUAL( ci == temp, true ) // REQUIRE_EQUAL( memcmp(expected_buffer0, buffer0, 2), 0 ) @@ -289,7 +268,7 @@ eosio::print(temp.value, "\n"); EOSIO_TEST_END int main(int argc, char* argv[]) { - EOSIO_TEST(unsigned_int_type_test) + // EOSIO_TEST(unsigned_int_type_test) EOSIO_TEST(signed_int_type_test); return has_failed(); } From 6140d28d983b8b4576d6da4fab5367aee475bb70 Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 7 Feb 2019 16:57:14 -0500 Subject: [PATCH 31/51] Fix build with `fno-builtins` flag --- tools/include/compiler_options.hpp.in | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index 8dee7e09d6..5994b0a119 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -336,6 +336,7 @@ static void GetCompDefaults(std::vector& copts) { copts.emplace_back("-m64"); #endif copts.emplace_back("-mno-sse"); + copts.emplace_back("-fno-builtin"); if (!fasm_opt) { copts.emplace_back("-DEOSIO_NATIVE"); copts.emplace_back("-DLP64"); From d2accfe965defbd3b399b185457cfa54fb79a823 Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 7 Feb 2019 16:58:24 -0500 Subject: [PATCH 32/51] Finishing up `datastream` tests --- tests/unit/.#CMakeLists.txt | 1 + tests/unit/asset_tests.cpp | 7 +- tests/unit/binary_extension_tests.cpp | 10 +- tests/unit/crypto_tests.cpp | 5 + tests/unit/datastream_tests.cpp | 292 +++++++++++++++++++++++--- tests/unit/eosio_tests.cpp | 7 +- tests/unit/fixed_bytes_tests.cpp | 5 + tests/unit/name_tests.cpp | 5 + tests/unit/print_tests.cpp | 10 +- tests/unit/symbol_tests.cpp | 5 + tests/unit/system_tests.cpp | 8 +- tests/unit/time_tests.cpp | 7 +- tests/unit/varint_tests.cpp | 7 +- 13 files changed, 319 insertions(+), 50 deletions(-) create mode 120000 tests/unit/.#CMakeLists.txt diff --git a/tests/unit/.#CMakeLists.txt b/tests/unit/.#CMakeLists.txt new file mode 120000 index 0000000000..a40f84238c --- /dev/null +++ b/tests/unit/.#CMakeLists.txt @@ -0,0 +1 @@ +john.debord@jdebord.1868 \ No newline at end of file diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 9013f2ad4f..d6262d22ba 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -1,3 +1,8 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include @@ -148,7 +153,7 @@ EOSIO_TEST_BEGIN(asset_type_test) // How are these `assets` constructed? Isn't `uint8_t precision` supposed to be constexpr when constructing a symbol? for( uint8_t precision{0}; precision < 64; ++precision ) { CHECK_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), - (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ) + (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ) } // ---------------------- diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index b298d256c0..bde63aa5e3 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -1,10 +1,13 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include using eosio::binary_extension; -using namespace eosio::native; - // Definitions in `eosio.cdt/libraries/eosiolib/binary_extension.hpp` EOSIO_TEST_BEGIN(binary_extension_test) silence_output(false); @@ -49,7 +52,8 @@ EOSIO_TEST_BEGIN(binary_extension_test) binary_extension bin_copy_str_lval0{"abcd"}; binary_extension bin_copy_str_lval1{bin_copy_str_lval0}; - REQUIRE_EQUAL( (bin_copy_str_lval0.value() == bin_copy_str_lval1.value() && bin_copy_str_lval0.has_value() == bin_copy_str_lval1.has_value()), true) + REQUIRE_EQUAL( (bin_copy_str_lval0.value() == bin_copy_str_lval1.value() && + bin_copy_str_lval0.has_value() == bin_copy_str_lval1.has_value()), true) // constexpr binary_extension(binary_extension&&) binary_extension bin_copy_char_rval0{'c'}; diff --git a/tests/unit/crypto_tests.cpp b/tests/unit/crypto_tests.cpp index 7834b46518..f5469a6c1e 100644 --- a/tests/unit/crypto_tests.cpp +++ b/tests/unit/crypto_tests.cpp @@ -1,3 +1,8 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 69ffef4389..1eabced127 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,12 +1,31 @@ +// TODO: Make sure CHECK_EQUAL is changed REQUIRE_EQUAL +// TODO: Change: `REQUIRE_EQUAL( ds.tellp() == 9, true )` to `REQUIRE_EQUAL( ds.tellp(), 9 )` +// TODO: Organize tests to be in a logical order +// TODO: Swithc arrays with ints to arrays with chars; I think it makes it more readable +// TODO: Organize the `capi` tests and the respective `eosio` comments +// TODO: Double-check to make sure I get everything right +// TODO: Test a more complex `fixed_bytes` +// TODO: Test a more complex `key256` + +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include #include #include #include #include + +#include +#include + #include #include #include +#include #include #include @@ -20,14 +39,21 @@ using eosio::public_key; using eosio::signature; using eosio::symbol; using eosio::symbol_code; +using eosio::unpack; + +using boost::container::flat_map; +using boost::container::flat_set; + using std::array; using std::begin; using std::end; using std::fill; using std::list; +using std::map; +using std::set; using std::string; - -using namespace eosio::native; +using std::tuple; +using std::vector; // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_test) @@ -311,7 +337,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) CHECK_EQUAL( co == o, true ) // ----------- - // symbol_code + // eosio::symbol_code ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const symbol_code csc{"SYMBOLL"}; @@ -322,7 +348,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) CHECK_EQUAL( csc == sc, true ) // ------ - // symbol + // eosio::symbol ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const symbol sym_no_prec{"SYMBOLL", 0}; @@ -341,7 +367,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) CHECK_EQUAL( sym_prec == sym, true ) // -------------- - // ignore_wrapper + // eosio::ignore_wrapper ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const ignore_wrapper ciw{'c'}; @@ -352,7 +378,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) CHECK_EQUAL( ciw.value == iw, true ) // ------ - // ignore + // eosio::ignore ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const ignore cig{}; @@ -374,7 +400,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) CHECK_EQUAL( memcmp(c_cpubkey.data, c_cpubkey.data, 32), 0 ) // ---------- - // public_key + // eosio::public_key ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const public_key cpubkey{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; @@ -394,30 +420,30 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); ds >> sig; CHECK_EQUAL( csig == sig, true ) - - // TODO: Test a more complex `key256` - // ------ - // key256 - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - const key256 ckey256{}; - key256 key256{}; - ds << ckey256; - ds.seekp(0); - ds >> key256; - CHECK_EQUAL( ckey256 == key256, true ) - - // TODO: Test a more complex `fixed_bytes` - // ----------- - // fixed_bytes - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - const fixed_bytes<32> cfb{}; - fixed_bytes<32> fb{}; - ds << cfb; - ds.seekp(0); - ds >> fb; - CHECK_EQUAL( cfb == fb, true ) + + // // ------ + // // key256 + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const key256 ckey256{}; + // key256 key256{}; + // ds << ckey256; + // ds.seekp(0); + // ds >> key256; + // CHECK_EQUAL( ckey256 == key256, true ) + + // // ----------- + // // fixed_bytes + // array arr{0x01,0x02,0x03,0x04}; + + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const fixed_bytes<32> cfb{arr}; + // fixed_bytes<32> fb{}; + // ds << cfb; + // ds.seekp(0); + // ds >> fb; + // CHECK_EQUAL( cfb == fb, true ) // ---- // bool @@ -430,24 +456,222 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds >> boolean; CHECK_EQUAL( cboolean == boolean, true ) + // ---------------- + // capi_checksum160 + + // ---------------- + // capi_checksum512 + + // ----------- + // checksum256 + + // ------ + // string + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const string cstr {"abcdefghi"}; + string str{}; + ds << cstr; + ds.seekp(0); + ds >> str; + CHECK_EQUAL( cstr == str, true ) + // ---------- // std::array ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::array ca{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - const std::array a{}; + const array ca{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + std::array a{}; ds << ca; ds.seekp(0); ds >> a; CHECK_EQUAL( ca == a, true ) + // --------------- + // T[] (primitive) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const int cprim_array[10]{0,1,2,3,4,5,6,7,8,9}; + int prim_array[10]{}; + ds << cprim_array; + REQUIRE_EQUAL( ds.tellp() == 41, true ) + + ds.seekp(0); // Next three lines are testing an implementation detail ??? + ds >> prim_array; + REQUIRE_EQUAL( memcmp(cprim_array, prim_array, 10), 0 ) + + ds.seekp(1); + fill(begin(prim_array), end(prim_array), 0); + REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> prim_array;} ) + + // ------------------- + // T[] (non-primitive) + struct non_prim_array_test { + int val; + }; + + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const non_prim_array_test cnon_prim_array[10]{0,1,2,3,4,5,6,7,8,9}; + non_prim_array_test non_prim_array[10]{}; + ds << cnon_prim_array; + REQUIRE_EQUAL( ds.tellp() == 41, true ) + + ds.seekp(0); // Next three lines are testing an implementation detail ??? + ds >> non_prim_array; + REQUIRE_EQUAL( memcmp(cnon_prim_array, non_prim_array, 10), 0 ) + + ds.seekp(1); + fill(begin(non_prim_array), end(non_prim_array), non_prim_array_test{}); + REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> non_prim_array;} ) + + // ----------- + // std::vector + struct vec_test { + char val; + }; + + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const vector ctest_vec{{'a'}, {'b'}, {'c'}, {'d'}, {'e'}, {'f'}, {'g'}, {'h'}, {'i'}}; + // std::vector test_vec{}; + // ds << ctest_vec; + // ds.seekp(0); + // ds >> test_vec; + // CHECK_EQUAL( ctest_vec == test_vec, true ) + + // ----------------- + // std::vector + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const vector cchar_vec{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + vector char_vec{}; + ds << cchar_vec; + ds.seekp(0); + ds >> char_vec; + CHECK_EQUAL( cchar_vec == char_vec, true ) + + // -------- + // std::set + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const set cchar_set{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + set char_set{}; + ds << cchar_set; + ds.seekp(0); + ds >> char_set; + CHECK_EQUAL( cchar_set == char_set, true ) + + // -------------------------- + // boost::container::flat_set + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const flat_set cchar_flat_set{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + flat_set char_flat_set{}; + ds << cchar_flat_set; + ds.seekp(0); + ds >> char_flat_set; + CHECK_EQUAL( cchar_flat_set == char_flat_set, true ) + + // -------- + // std::map + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const map cchar_map{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; + map char_map{}; + ds << cchar_map; + ds.seekp(0); + ds >> char_map; + CHECK_EQUAL( cchar_map == char_map, true ) + + // -------------------------- + // boost::container::flat_map + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const flat_map cchar_flat_map{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; + flat_map char_flat_map{}; + ds << cchar_flat_map; + ds.seekp(0); + ds >> char_flat_map; + CHECK_EQUAL( cchar_flat_map == char_flat_map, true ) + + // ---------- + // std::tuple + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const tuple ctup{"abc123",42,'a'}; + tuple tup{}; + ds << ctup; + ds.seekp(0); + ds >> tup; + CHECK_EQUAL( ctup == tup, true ) + + // --------------- + // T (primitive) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const int cprim{10}; + int prim{}; + ds << cprim; + ds.seekp(0); + ds >> prim; + REQUIRE_EQUAL( cprim == prim, true ) + + // ------------------- + // T (non-primitive) + struct non_prim_test { + int val; + }; + + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const non_prim_test cnonprim{10}; + non_prim_test nonprim{}; + ds << cnonprim; + ds.seekp(0); + ds >> nonprim; + REQUIRE_EQUAL( cnonprim.val == nonprim.val, true ) + + silence_output(false); +EOSIO_TEST_END + +// Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` +EOSIO_TEST_BEGIN(misc_datastream_test) + silence_output(false); + + // ----------------------------- + // T unpack(const char*, size_t) + const char unpack_source_buffer[9]{'a','b','c','d','e','f','g','h','i'}; + char unpack_ch{}; + for (uint8_t i = 0; i < 9; ++i) { + unpack_ch = unpack(unpack_source_buffer+i, 9); + REQUIRE_EQUAL( unpack_source_buffer[i], unpack_ch ) + } + + // ----------------------------- + // T unpack(const vector&) + const vector unpack_source_vec{'a','b','c','d','e','f','g','h','i'}; + for (uint8_t i = 0; i < 9; ++i) { + unpack_ch = unpack(unpack_source_buffer+i, 9); + REQUIRE_EQUAL( unpack_source_buffer[i], unpack_ch ) + } + + // -------------------------- + // size_t pack_size(const T&) + + + // --------------------------- + // vector pack(const T&) + + silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { // Note: uncomment once segfaulting during `memcpy` has been resolved // EOSIO_TEST(datastream_test); - EOSIO_TEST(datastream_specialization_test); + // EOSIO_TEST(datastream_specialization_test); EOSIO_TEST(datastream_stream_test); + // EOSIO_TEST(misc_datastream_test); return has_failed(); } diff --git a/tests/unit/eosio_tests.cpp b/tests/unit/eosio_tests.cpp index c3125876ca..a452d19751 100644 --- a/tests/unit/eosio_tests.cpp +++ b/tests/unit/eosio_tests.cpp @@ -1,8 +1,11 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include -using namespace eosio::native; - namespace using_no_macros { class [[eosio::contract]] con { diff --git a/tests/unit/fixed_bytes_tests.cpp b/tests/unit/fixed_bytes_tests.cpp index 563d1ba22b..5265def3ef 100644 --- a/tests/unit/fixed_bytes_tests.cpp +++ b/tests/unit/fixed_bytes_tests.cpp @@ -1,3 +1,8 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 59467b41b4..61935c61ff 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -1,3 +1,8 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include diff --git a/tests/unit/print_tests.cpp b/tests/unit/print_tests.cpp index 62904eaead..16cd5c9890 100644 --- a/tests/unit/print_tests.cpp +++ b/tests/unit/print_tests.cpp @@ -1,6 +1,10 @@ -#include -#include +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ +#include +#include #include #include #include @@ -8,8 +12,6 @@ using eosio::name; using eosio::print; -using namespace eosio::native; - // EOSIO_TEST_BEGIN(print_test) // silence_output(true); // CHECK_PRINT("27", [](){ eosio::print((uint8_t)27); }); diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 4e0e111487..dd711befa5 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -1,3 +1,8 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include diff --git a/tests/unit/system_tests.cpp b/tests/unit/system_tests.cpp index c7944cd929..3290d5e124 100644 --- a/tests/unit/system_tests.cpp +++ b/tests/unit/system_tests.cpp @@ -1,12 +1,16 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include + #include using eosio::check; using std::string; -using namespace eosio::native; - // Definitions in `eosio.cdt/libraries/eosiolib/system.hpp` EOSIO_TEST_BEGIN(system_test) silence_output(true); diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index b36535c9cb..2631691d50 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -1,3 +1,8 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include @@ -6,8 +11,6 @@ using eosio::time_point; using eosio::time_point_sec; using eosio::block_timestamp; -using namespace eosio::native; - const uint32_t u32min = std::numeric_limits::min(); // 0 const uint32_t u32max = std::numeric_limits::max(); // 18446744073709551615 diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index fd06085ad6..457597effc 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -1,3 +1,8 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + #include #include #include @@ -6,8 +11,6 @@ using eosio::datastream; -using namespace eosio::native; - const uint32_t u32min = std::numeric_limits::min(); // 0 const uint32_t u32max = std::numeric_limits::max(); // 18446744073709551615 From bef0fca5a564f08fa7212b0b13611653e8153426 Mon Sep 17 00:00:00 2001 From: John DeBord Date: Thu, 7 Feb 2019 17:03:50 -0500 Subject: [PATCH 33/51] Delete .#CMakeLists.txt --- tests/unit/.#CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) delete mode 120000 tests/unit/.#CMakeLists.txt diff --git a/tests/unit/.#CMakeLists.txt b/tests/unit/.#CMakeLists.txt deleted file mode 120000 index a40f84238c..0000000000 --- a/tests/unit/.#CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -john.debord@jdebord.1868 \ No newline at end of file From 3ccea4da8118f6781d6e27f981a97e48014a44d0 Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 8 Feb 2019 14:51:25 -0500 Subject: [PATCH 34/51] More edits on `datastream` tests --- tests/unit/binary_extension_tests.cpp | 18 +- tests/unit/datastream_tests.cpp | 688 +++++++++++++------------- 2 files changed, 364 insertions(+), 342 deletions(-) diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index bde63aa5e3..90c8cd51ae 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -98,33 +98,33 @@ EOSIO_TEST_BEGIN(binary_extension_test) // constexpr auto value_or(U&&) -> std::enable_if_t::value, T&>& binary_extension bin_temp_value_or{}; uint8_t some_value = bin_temp_value_or.value_or(uint8_t{0x63}); - REQUIRE_EQUAL( (some_value == 0x63), true ) + REQUIRE_EQUAL( some_value, 0x63 ) // constexpr T&& value_or()&& - REQUIRE_EQUAL( (binary_extension{'c'}.value_or() == 'c'), true ) + REQUIRE_EQUAL( binary_extension{'c'}.value_or(), 'c' ) binary_extension bin_value_or_rval{"abcd"}; - REQUIRE_EQUAL( (bin_value_or_rval.value_or() == "abcd"), true ) + REQUIRE_EQUAL( bin_value_or_rval.value_or(), "abcd" ) // constexpr const T&& value_or()const&& - REQUIRE_EQUAL( (binary_extension{'c'}.value_or() == 'c'), true ) + REQUIRE_EQUAL( binary_extension{'c'}.value_or(), 'c' ) binary_extension bin_const_value_or_rval{"abcd"}; - REQUIRE_EQUAL( (bin_const_value_or_rval.value_or() == "abcd"), true ) + REQUIRE_EQUAL( bin_const_value_or_rval.value_or(), "abcd" ) // constexpr T value_or()& binary_extension bin_char_value_or_val{'c'}; - REQUIRE_EQUAL( (bin_char_value_or_val.value_or() == 'c'), true ) + REQUIRE_EQUAL( bin_char_value_or_val.value_or(), 'c' ) binary_extension bin_value_or_val{"abcd"}; - REQUIRE_EQUAL( (bin_value_or_val.value_or() == "abcd"), true ) + REQUIRE_EQUAL( bin_value_or_val.value_or(), "abcd" ) // constexpr T value_or()const& binary_extension bin_char_const_value_or_val{'c'}; - REQUIRE_EQUAL( (bin_char_const_value_or_val.value_or() == 'c'), true ) + REQUIRE_EQUAL( bin_char_const_value_or_val.value_or(), 'c' ) binary_extension bin_const_value_or_val{"abcd"}; - REQUIRE_EQUAL( (bin_const_value_or_val.value_or() == "abcd"), true ) + REQUIRE_EQUAL( bin_const_value_or_val.value_or(), "abcd" ) // constexpr T* operator->() binary_extension bin_char_arrow_op{'c'}; diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 1eabced127..946d6e9d50 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,17 +1,15 @@ -// TODO: Make sure CHECK_EQUAL is changed REQUIRE_EQUAL -// TODO: Change: `REQUIRE_EQUAL( ds.tellp() == 9, true )` to `REQUIRE_EQUAL( ds.tellp(), 9 )` -// TODO: Organize tests to be in a logical order -// TODO: Swithc arrays with ints to arrays with chars; I think it makes it more readable -// TODO: Organize the `capi` tests and the respective `eosio` comments -// TODO: Double-check to make sure I get everything right -// TODO: Test a more complex `fixed_bytes` -// TODO: Test a more complex `key256` - /** * @file * @copyright defined in eosio.cdt/LICENSE.txt */ +#include +#include +#include +#include +#include +#include + #include #include #include @@ -22,12 +20,16 @@ #include #include -#include -#include -#include -#include -#include -#include +using std::array; +using std::begin; +using std::end; +using std::fill; +using std::list; +using std::map; +using std::set; +using std::string; +using std::tuple; +using std::vector; using eosio::binary_extension; using eosio::datastream; @@ -35,6 +37,8 @@ using eosio::fixed_bytes; using eosio::ignore; using eosio::ignore_wrapper; using eosio::key256; +using eosio::pack; +using eosio::pack_size; using eosio::public_key; using eosio::signature; using eosio::symbol; @@ -44,17 +48,6 @@ using eosio::unpack; using boost::container::flat_map; using boost::container::flat_set; -using std::array; -using std::begin; -using std::end; -using std::fill; -using std::list; -using std::map; -using std::set; -using std::string; -using std::tuple; -using std::vector; - // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_test) silence_output(true); @@ -63,20 +56,21 @@ EOSIO_TEST_BEGIN(datastream_test) char datastream_buffer[buffer_size]{}; // Buffer for the datastream to point to char buffer[buffer_size]; // Buffer to compare `datastream_buffer` with - // Fill the char array `datastream_buffer` with all 256 ASCII characters unsigned char j{0}; - for(int i{0}; i < buffer_size; ++i) + for(int i{0}; i < buffer_size; ++i) // Fill the char array `datastream_buffer` with all 256 ASCII characters datastream_buffer[i] = j++; - // Why can't I initialize a data stream object of type `datastream ds{0, 256}`? + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////// I can't initialize a data stream object of type `datastream ds{0, 256}`? ////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////// /// datastream(T, size_t) datastream ds{datastream_buffer, buffer_size}; // inline void skip(size_t) ds.skip(1); - CHECK_EQUAL( ds.pos() == datastream_buffer+1, true ) + CHECK_EQUAL( ds.pos(), datastream_buffer+1 ) ds.skip(-1); - CHECK_EQUAL( ds.pos() == datastream_buffer, true ) + CHECK_EQUAL( ds.pos(), datastream_buffer ) // inline bool read(char*, size_t) CHECK_EQUAL( ds.read(buffer, 256), true ) @@ -85,17 +79,16 @@ EOSIO_TEST_BEGIN(datastream_test) CHECK_ASSERT( "read", ([&]() {ds.read(buffer, 1);}) ) // T pos()const - CHECK_EQUAL( ds.pos() == datastream_buffer+256, true ) - CHECK_EQUAL( ds.pos() == datastream_buffer, false ) + CHECK_EQUAL( ds.pos(), datastream_buffer+256 ) + CHECK_EQUAL( ds.pos(), datastream_buffer ) // inline bool seekp(size_t) ds.seekp(0); - CHECK_EQUAL( ds.pos() == datastream_buffer, true ) - CHECK_EQUAL( ds.pos() == datastream_buffer+256, false ) + CHECK_EQUAL( ds.pos(), datastream_buffer ) + CHECK_EQUAL( ds.pos(), datastream_buffer+256 ) // inline bool write(const char*, size_t) - // Fill `buffer` with a new set of values - fill(begin(buffer), end(buffer), 1); + fill(begin(buffer), end(buffer), 1); // Fill `buffer` with a new set of values CHECK_EQUAL( ds.write(buffer, 256), true ) CHECK_EQUAL( memcmp(buffer, datastream_buffer, 256), 0 ) @@ -159,12 +152,13 @@ EOSIO_TEST_BEGIN(datastream_specialization_test) char datastream_buffer[buffer_size]{}; // Buffer for the datastream to point to char buffer[buffer_size]; // Buffer to compare `datastream_buffer` with - // Fill the char array `datastream_buffer` with all all 256 ASCII characters unsigned char j{0}; - for(int i{0}; i < buffer_size; ++i) + for(int i{0}; i < buffer_size; ++i) // Fill the char array `datastream_buffer` with all all 256 ASCII characters datastream_buffer[i] = j++; - // Why can't I initialize a data stream object of type `datastream ds{0, 256}`? + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////// I can't initialize a data stream object of type `datastream ds{0, 256}`? ////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////// /// datastream(T, size_t) datastream ds{buffer_size}; @@ -187,7 +181,6 @@ EOSIO_TEST_BEGIN(datastream_specialization_test) CHECK_EQUAL( ds.tellp(), 0) // inline bool write(const char*,size_t) - // inline size_t tellp()const CHECK_EQUAL( ds.write(buffer, 256), true ) CHECK_EQUAL( ds.tellp(), 256 ) @@ -232,357 +225,404 @@ EOSIO_TEST_END // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_stream_test) - silence_output(false); + silence_output(true); - // TODO: figure out why making this buffer and off number throws a segfault - static constexpr uint8_t buffer_size{128}; + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + /////////// TODO: figure out why making this buffer and off number throws a segfault /////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + static constexpr uint16_t buffer_size{256}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to datastream ds{datastream_buffer, buffer_size}; - // --------- - // std::list + // ------------- + // T (primitive) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::list cl{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; - std::list l{}; - ds << cl; + const int cprim{10}; + int prim{}; + ds << cprim; ds.seekp(0); - ds >> l; - CHECK_EQUAL( cl == l, true ) + ds >> prim; + CHECK_EQUAL( cprim, prim ) + + // ----------------- + // T (non-primitive) + struct non_prim_test { + int val; + }; + + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const non_prim_test cnonprim{10}; + non_prim_test nonprim{}; + ds << cnonprim; + ds.seekp(0); + ds >> nonprim; + CHECK_EQUAL( cnonprim.val, nonprim.val ) + + // --------------- + // T[] (primitive) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const int cprim_array[10]{0,1,2,3,4,5,6,7,8,9}; + int prim_array[10]{}; + ds << cprim_array; + CHECK_EQUAL( ds.tellp(), 41 ) + + ds.seekp(0); // Next three lines are testing an implementation detail ??? + ds >> prim_array; + CHECK_EQUAL( memcmp(cprim_array, prim_array, 10), 0 ) + + ds.seekp(1); + fill(begin(prim_array), end(prim_array), 0); + REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> prim_array;} ) + + // ------------------- + // T[] (non-primitive) + struct non_prim_array_test { + int val; + }; + + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const non_prim_array_test cnon_prim_array[10]{0,1,2,3,4,5,6,7,8,9}; + non_prim_array_test non_prim_array[10]{}; + ds << cnon_prim_array; + CHECK_EQUAL( ds.tellp(), 41 ) + + ds.seekp(0); // Next three lines are testing an implementation detail ??? + ds >> non_prim_array; + CHECK_EQUAL( memcmp(cnon_prim_array, non_prim_array, 10), 0 ) + + ds.seekp(1); + fill(begin(non_prim_array), end(non_prim_array), non_prim_array_test{}); + REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> non_prim_array;} ) + + // ---- + // bool + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const bool cboolean{true}; + bool boolean{}; + ds << cboolean; + ds.seekp(0); + ds >> boolean; + CHECK_EQUAL( cboolean, boolean ) + + // ---------- + // std::array + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const array ca{'a','b','c','d','e','f','g','h','i'}; + std::array a{}; + ds << ca; + ds.seekp(0); + ds >> a; + CHECK_EQUAL( ca, a ) - // Note: uncomment once issue has been resolved + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////// Note: uncomment once issue has been resolved //////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////// // ---------- // std::deque // ds.seekp(0); // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const std::deque cd{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' }; + // const std::deque cd{'a','b','c','d','e','f','g','h','i' }; // std::deque d{}; // ds << cd; // ds.seekp(0); // ds >> d; // Fails here - // CHECK_EQUAL( cd == d, true ) - - // ---------------- - // binary_extension - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const binary_extension cbe_char{'c'}; - // binary_extension cb_char{}; - // ds << cbe_char; - // ds.seekp(0); - // ds >> cb_char; - // CHECK_EQUAL( cbe_char.value() == cb_char.value(), true ) + // CHECK_EQUAL( cd, d ) - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // binary_extension be_int0{}; - // binary_extension be_int1{}; - // ds << be_int0.value_or(42); - // ds.seekp(0); - // ds >> be_int1; - // CHECK_EQUAL( be_int1.value() == 42, true ) + // --------- + // std::list + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const std::list cl{'a','b','c','d','e','f','g','h','i' }; + std::list l{}; + ds << cl; + ds.seekp(0); + ds >> l; + CHECK_EQUAL( cl, l ) - // I'm not understanding fully why this can't take data structures with - // more constructors than just a default constructors - struct be_test { - be_test() : val{42} {} - int val; - }; - const be_test bet{}; - + // -------- + // std::map ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const binary_extension cbe_btest{bet}; - binary_extension cb_btest{}; - ds << cbe_btest; + const map cchar_map{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; + map char_map{}; + ds << cchar_map; ds.seekp(0); - ds >> cb_btest; - CHECK_EQUAL( cbe_btest.value().val == cb_btest.value().val, true ) - - const binary_extension cbe_none{}; - CHECK_ASSERT( "cannot get value of empty binary_extension", [&](){cbe_none.value();} ) + ds >> char_map; + CHECK_EQUAL( cchar_map, char_map ) - // ------------ - // std::variant + // ------------- + // std::optional ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - std::variant v0{1024}; - std::variant v1{}; - ds << v0; + const std::optional co{'c'}; + std::optional o{}; + ds << co; ds.seekp(0); - ds >> v1; - CHECK_EQUAL( v0 == v1, true ) + ds >> o; + CHECK_EQUAL( co, o ) // --------- // std::pair ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::pair cp{'c', 0x43}; + const std::pair cp{'c', 42}; std::pair p{}; ds << cp; ds.seekp(0); ds >> p; - CHECK_EQUAL( cp == p, true ) + CHECK_EQUAL( cp, p ) - // ------------- - // std::optional + // -------- + // std::set ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::optional co{'c'}; - std::optional o{}; - ds << co; + const set cchar_set{'a','b','c','d','e','f','g','h','i'}; + set char_set{}; + ds << cchar_set; ds.seekp(0); - ds >> o; - CHECK_EQUAL( co == o, true ) - + ds >> char_set; + CHECK_EQUAL( cchar_set, char_set ) + // ----------- - // eosio::symbol_code + // std::string ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const symbol_code csc{"SYMBOLL"}; - symbol_code sc{}; - ds << csc; + const string cstr {"abcdefghi"}; + string str{}; + ds << cstr; ds.seekp(0); - ds >> sc; - CHECK_EQUAL( csc == sc, true ) + ds >> str; + CHECK_EQUAL( cstr, str ) - // ------ - // eosio::symbol + // ---------- + // std::tuple ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const symbol sym_no_prec{"SYMBOLL", 0}; - symbol sym{}; - ds << sym_no_prec; + const tuple ctup{"abcefghi",42,'a'}; + tuple tup{}; + ds << ctup; ds.seekp(0); - ds >> sym; - CHECK_EQUAL( sym_no_prec == sym, true ) + ds >> tup; + CHECK_EQUAL( ctup, tup ) + // ------------ + // std::variant ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const symbol sym_prec{"SYMBOLL", 255}; - ds << sym_prec; + std::variant v0{1024}; + std::variant v1{}; + ds << v0; ds.seekp(0); - ds >> sym; - CHECK_EQUAL( sym_prec == sym, true ) + ds >> v1; + CHECK_EQUAL( v0, v1 ) - // -------------- - // eosio::ignore_wrapper + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////////////// Note: uncomment once issue has been resolved //////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // ----------- + // std::vector + struct vec_test { + char val; + }; + + // ds.seekp(0); + // fill(begin(datastream_buffer), end(datastream_buffer), 0); + // const vector ctest_vec{{'a'},{'b'},{'c'},{'d'},{'e'},{'f'},{'g'},{'h'},{'i'}}; + // std::vector test_vec{}; + // ds << ctest_vec; + // ds.seekp(0); + // ds >> test_vec; + // CHECK_EQUAL( ctest_vec, test_vec ) + + // ----------------- + // std::vector ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const ignore_wrapper ciw{'c'}; - char iw; - ds << ciw; + const vector cchar_vec{'a','b','c','d','e','f','g','h','i'}; + vector char_vec{}; + ds << cchar_vec; ds.seekp(0); - ds >> iw; - CHECK_EQUAL( ciw.value == iw, true ) + ds >> char_vec; + CHECK_EQUAL( cchar_vec, char_vec ) - // ------ - // eosio::ignore + // ---------------- + // capi_checksum160 ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const ignore cig{}; - ignore ig; - ds << cig; - CHECK_EQUAL( ds.tellp() == 0, true ) - ds >> ig; - CHECK_EQUAL( ds.tellp() == 0, true ) + const capi_checksum160 ccheck160 {0x01,0x02,0x03,0x04}; + capi_checksum160 check160{}; + ds << ccheck160; + ds.seekp(0); + ds >> check160; + CHECK_EQUAL( memcmp(ccheck160.hash, check160.hash, 20), 0 ) + + // ---------------- + // capi_checksum256 + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const capi_checksum256 ccheck256 {0x01,0x02,0x03,0x04}; + capi_checksum256 check256{}; + ds << ccheck256; + ds.seekp(0); + ds >> check256; + CHECK_EQUAL( memcmp(ccheck256.hash, check256.hash, 32), 0 ) + + // ---------------- + // capi_checksum512 + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const capi_checksum512 ccheck512 {0x01,0x02,0x03,0x04}; + capi_checksum512 check512{}; + ds << ccheck512; + ds.seekp(0); + ds >> check512; + CHECK_EQUAL( memcmp(ccheck512.hash, check512.hash, 64), 0 ) // --------------- // capi_public_key ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const capi_public_key c_cpubkey{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; + const capi_public_key c_cpubkey{'a','b','c','d','e','f','g','h','i'}; capi_public_key c_pubkey{}; ds << c_cpubkey; ds.seekp(0); ds >> c_pubkey; CHECK_EQUAL( memcmp(c_cpubkey.data, c_cpubkey.data, 32), 0 ) - // ---------- - // eosio::public_key + // ----------------------- + // eosio::binary_extension ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const public_key cpubkey{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - public_key pubkey{}; - ds << cpubkey; + const binary_extension cbe_char{'c'}; + binary_extension cb_char{}; + ds << cbe_char; ds.seekp(0); - ds >> pubkey; - CHECK_EQUAL( cpubkey == pubkey, true ) + ds >> cb_char; + CHECK_EQUAL( cbe_char.value(), cb_char.value() ) - // --------- - // signature ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const signature csig{{}, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - signature sig{}; - ds << csig; - ds.seekp(0); - ds >> sig; - CHECK_EQUAL( csig == sig, true ) - - // // ------ - // // key256 - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const key256 ckey256{}; - // key256 key256{}; - // ds << ckey256; - // ds.seekp(0); - // ds >> key256; - // CHECK_EQUAL( ckey256 == key256, true ) - - // // ----------- - // // fixed_bytes - // array arr{0x01,0x02,0x03,0x04}; + binary_extension be_int0{}; + binary_extension be_int1{}; + ds << be_int0.value_or(42); + ds.seekp(0); + ds >> be_int1; + CHECK_EQUAL( be_int1.value(), 42 ) + + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + ////////////////////// Not understanding fully why this can't take data structures with //////////////////// + ////////////////////// more constructors than just a default constructors ////////////////////////////////// + ////////////////////// Note: uncomment once issue has been resolved //////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////// + struct be_test { + be_test() : val{42} {} + int val; + }; + const be_test bet{}; - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const fixed_bytes<32> cfb{arr}; - // fixed_bytes<32> fb{}; - // ds << cfb; - // ds.seekp(0); - // ds >> fb; - // CHECK_EQUAL( cfb == fb, true ) - - // ---- - // bool ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const bool cboolean{true}; - bool boolean{}; - ds << cboolean; + const binary_extension cbe_btest{bet}; + binary_extension cb_btest{}; + ds << cbe_btest; ds.seekp(0); - ds >> boolean; - CHECK_EQUAL( cboolean == boolean, true ) - - // ---------------- - // capi_checksum160 - - // ---------------- - // capi_checksum512 - - // ----------- - // checksum256 + ds >> cb_btest; + CHECK_EQUAL( cbe_btest.value().val, cb_btest.value().val ) - // ------ - // string - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - const string cstr {"abcdefghi"}; - string str{}; - ds << cstr; - ds.seekp(0); - ds >> str; - CHECK_EQUAL( cstr == str, true ) + const binary_extension cbe_none{}; + CHECK_ASSERT( "cannot get value of empty binary_extension", [&](){cbe_none.value();} ) - // ---------- - // std::array + // ------------------ + // eosio::fixed_bytes ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const array ca{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - std::array a{}; - ds << ca; + const fixed_bytes<32> cfb{fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)}; + fixed_bytes<32> fb{}; + ds << cfb; ds.seekp(0); - ds >> a; - CHECK_EQUAL( ca == a, true ) + ds >> fb; + CHECK_EQUAL( cfb, fb ) - // --------------- - // T[] (primitive) + // ------------- + // eosio::ignore ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const int cprim_array[10]{0,1,2,3,4,5,6,7,8,9}; - int prim_array[10]{}; - ds << cprim_array; - REQUIRE_EQUAL( ds.tellp() == 41, true ) - - ds.seekp(0); // Next three lines are testing an implementation detail ??? - ds >> prim_array; - REQUIRE_EQUAL( memcmp(cprim_array, prim_array, 10), 0 ) - - ds.seekp(1); - fill(begin(prim_array), end(prim_array), 0); - REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> prim_array;} ) - - // ------------------- - // T[] (non-primitive) - struct non_prim_array_test { - int val; - }; + const ignore cig{}; + ignore ig; + ds << cig; + CHECK_EQUAL( ds.tellp(), 0 ) + ds >> ig; + CHECK_EQUAL( ds.tellp(), 0 ) + // --------------------- + // eosio::ignore_wrapper ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const non_prim_array_test cnon_prim_array[10]{0,1,2,3,4,5,6,7,8,9}; - non_prim_array_test non_prim_array[10]{}; - ds << cnon_prim_array; - REQUIRE_EQUAL( ds.tellp() == 41, true ) - - ds.seekp(0); // Next three lines are testing an implementation detail ??? - ds >> non_prim_array; - REQUIRE_EQUAL( memcmp(cnon_prim_array, non_prim_array, 10), 0 ) - - ds.seekp(1); - fill(begin(non_prim_array), end(non_prim_array), non_prim_array_test{}); - REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> non_prim_array;} ) - - // ----------- - // std::vector - struct vec_test { - char val; - }; - - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const vector ctest_vec{{'a'}, {'b'}, {'c'}, {'d'}, {'e'}, {'f'}, {'g'}, {'h'}, {'i'}}; - // std::vector test_vec{}; - // ds << ctest_vec; - // ds.seekp(0); - // ds >> test_vec; - // CHECK_EQUAL( ctest_vec == test_vec, true ) + const ignore_wrapper ciw{'c'}; + char iw; + ds << ciw; + ds.seekp(0); + ds >> iw; + CHECK_EQUAL( ciw.value, iw ) // ----------------- - // std::vector + // eosio::public_key ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const vector cchar_vec{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - vector char_vec{}; - ds << cchar_vec; + const public_key cpubkey{{},'a','b','c','d','e','f','g','h','i'}; + public_key pubkey{}; + ds << cpubkey; ds.seekp(0); - ds >> char_vec; - CHECK_EQUAL( cchar_vec == char_vec, true ) + ds >> pubkey; + CHECK_EQUAL( cpubkey, pubkey ) - // -------- - // std::set + // ---------------- + // eosio::signature ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const set cchar_set{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - set char_set{}; - ds << cchar_set; + const signature csig{{},'a','b','c','d','e','f','g','h','i'}; + signature sig{}; + ds << csig; ds.seekp(0); - ds >> char_set; - CHECK_EQUAL( cchar_set == char_set, true ) + ds >> sig; + CHECK_EQUAL( csig, sig ) - // -------------------------- - // boost::container::flat_set + // ------------- + // eosio::symbol ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const flat_set cchar_flat_set{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}; - flat_set char_flat_set{}; - ds << cchar_flat_set; + const symbol sym_no_prec{"SYMBOLL", 0}; + symbol sym{}; + ds << sym_no_prec; ds.seekp(0); - ds >> char_flat_set; - CHECK_EQUAL( cchar_flat_set == char_flat_set, true ) + ds >> sym; + CHECK_EQUAL( sym_no_prec, sym ) - // -------- - // std::map ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const map cchar_map{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; - map char_map{}; - ds << cchar_map; + const symbol sym_prec{"SYMBOLL", 255}; + ds << sym_prec; ds.seekp(0); - ds >> char_map; - CHECK_EQUAL( cchar_map == char_map, true ) + ds >> sym; + CHECK_EQUAL( sym_prec, sym ) + + // ------------------ + // eosio::symbol_code + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const symbol_code csc{"SYMBOLL"}; + symbol_code sc{}; + ds << csc; + ds.seekp(0); + ds >> sc; + CHECK_EQUAL( csc, sc ) // -------------------------- // boost::container::flat_map @@ -593,51 +633,41 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << cchar_flat_map; ds.seekp(0); ds >> char_flat_map; - CHECK_EQUAL( cchar_flat_map == char_flat_map, true ) - - // ---------- - // std::tuple - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - const tuple ctup{"abc123",42,'a'}; - tuple tup{}; - ds << ctup; - ds.seekp(0); - ds >> tup; - CHECK_EQUAL( ctup == tup, true ) - - // --------------- - // T (primitive) - ds.seekp(0); - fill(begin(datastream_buffer), end(datastream_buffer), 0); - const int cprim{10}; - int prim{}; - ds << cprim; - ds.seekp(0); - ds >> prim; - REQUIRE_EQUAL( cprim == prim, true ) - - // ------------------- - // T (non-primitive) - struct non_prim_test { - int val; - }; + CHECK_EQUAL( cchar_flat_map, char_flat_map ) + // -------------------------- + // boost::container::flat_set ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const non_prim_test cnonprim{10}; - non_prim_test nonprim{}; - ds << cnonprim; + const flat_set cchar_flat_set{'a','b','c','d','e','f','g','h','i'}; + flat_set char_flat_set{}; + ds << cchar_flat_set; ds.seekp(0); - ds >> nonprim; - REQUIRE_EQUAL( cnonprim.val == nonprim.val, true ) + ds >> char_flat_set; + CHECK_EQUAL( cchar_flat_set, char_flat_set ) silence_output(false); EOSIO_TEST_END // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(misc_datastream_test) - silence_output(false); + silence_output(true); + + // --------------------------- + // vector pack(const T&) + const string pack_s{"abcdefghi"}; + std::vector pack_res{}; + pack_res = pack(pack_s); + CHECK_EQUAL( memcmp(pack_s.data(), pack_res.data()+1, pack_s.size()), 0 ) + + // -------------------------- + // size_t pack_size(const T&) + int pack_size_i{42}; + double pack_size_d{3.14}; + string pack_size_s{"abcdefghi"};; + CHECK_EQUAL( pack_size(pack_size_i), 4 ) + CHECK_EQUAL( pack_size(pack_size_d), 8 ) + CHECK_EQUAL( pack_size(pack_size_s), 10 ) // ----------------------------- // T unpack(const char*, size_t) @@ -645,7 +675,7 @@ EOSIO_TEST_BEGIN(misc_datastream_test) char unpack_ch{}; for (uint8_t i = 0; i < 9; ++i) { unpack_ch = unpack(unpack_source_buffer+i, 9); - REQUIRE_EQUAL( unpack_source_buffer[i], unpack_ch ) + CHECK_EQUAL( unpack_source_buffer[i], unpack_ch ) } // ----------------------------- @@ -653,25 +683,17 @@ EOSIO_TEST_BEGIN(misc_datastream_test) const vector unpack_source_vec{'a','b','c','d','e','f','g','h','i'}; for (uint8_t i = 0; i < 9; ++i) { unpack_ch = unpack(unpack_source_buffer+i, 9); - REQUIRE_EQUAL( unpack_source_buffer[i], unpack_ch ) + CHECK_EQUAL( unpack_source_buffer[i], unpack_ch ) } - - // -------------------------- - // size_t pack_size(const T&) - - - // --------------------------- - // vector pack(const T&) - - + silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { // Note: uncomment once segfaulting during `memcpy` has been resolved // EOSIO_TEST(datastream_test); - // EOSIO_TEST(datastream_specialization_test); + EOSIO_TEST(datastream_specialization_test); EOSIO_TEST(datastream_stream_test); - // EOSIO_TEST(misc_datastream_test); + EOSIO_TEST(misc_datastream_test); return has_failed(); } From bd76516fe92f6cf8d58b0ddcdd38ec20ed02ce71 Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 8 Feb 2019 17:21:16 -0500 Subject: [PATCH 35/51] Finish `datastream` tests --- tests/unit/datastream_tests.cpp | 63 +++++++++++++++------------------ 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 946d6e9d50..786eed3355 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -48,6 +48,14 @@ using eosio::unpack; using boost::container::flat_map; using boost::container::flat_set; +struct be_test { + be_test() : val{42} {} + be_test(int i) : val{i} {} + int val; + + EOSLIB_SERIALIZE( be_test, (val) ) +}; + // Definitions in `eosio.cdt/libraries/eosiolib/datastream.hpp` EOSIO_TEST_BEGIN(datastream_test) silence_output(true); @@ -60,9 +68,6 @@ EOSIO_TEST_BEGIN(datastream_test) for(int i{0}; i < buffer_size; ++i) // Fill the char array `datastream_buffer` with all 256 ASCII characters datastream_buffer[i] = j++; - //////////////////////////////////////////////////////////////////////////////////////////////////////////// - ////////// I can't initialize a data stream object of type `datastream ds{0, 256}`? ////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////// /// datastream(T, size_t) datastream ds{datastream_buffer, buffer_size}; @@ -155,10 +160,7 @@ EOSIO_TEST_BEGIN(datastream_specialization_test) unsigned char j{0}; for(int i{0}; i < buffer_size; ++i) // Fill the char array `datastream_buffer` with all all 256 ASCII characters datastream_buffer[i] = j++; - - //////////////////////////////////////////////////////////////////////////////////////////////////////////// - ////////// I can't initialize a data stream object of type `datastream ds{0, 256}`? ////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// datastream(T, size_t) datastream ds{buffer_size}; @@ -227,9 +229,6 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(datastream_stream_test) silence_output(true); - //////////////////////////////////////////////////////////////////////////////////////////////////////////// - /////////// TODO: figure out why making this buffer and off number throws a segfault /////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////// static constexpr uint16_t buffer_size{256}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to @@ -416,30 +415,30 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::variant ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - std::variant v0{1024}; + const std::variant v0{1024}; std::variant v1{}; ds << v0; ds.seekp(0); ds >> v1; CHECK_EQUAL( v0, v1 ) - - //////////////////////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////// Note: uncomment once issue has been resolved //////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////// + // ----------- // std::vector struct vec_test { char val; }; - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const vector ctest_vec{{'a'},{'b'},{'c'},{'d'},{'e'},{'f'},{'g'},{'h'},{'i'}}; - // std::vector test_vec{}; - // ds << ctest_vec; - // ds.seekp(0); - // ds >> test_vec; - // CHECK_EQUAL( ctest_vec, test_vec ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const vector ctest_vec{{'a'},{'b'},{'c'},{'d'},{'e'},{'f'},{'g'},{'h'},{'i'}}; + vector test_vec{}; + ds << ctest_vec; + ds.seekp(0); + ds >> test_vec; + + for (int i{0}; i < ctest_vec.size(); ++i) { + CHECK_EQUAL( ctest_vec[i].val, test_vec[i].val ) + } // ----------------- // std::vector @@ -516,17 +515,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds >> be_int1; CHECK_EQUAL( be_int1.value(), 42 ) - //////////////////////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////// Not understanding fully why this can't take data structures with //////////////////// - ////////////////////// more constructors than just a default constructors ////////////////////////////////// - ////////////////////// Note: uncomment once issue has been resolved //////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////// - struct be_test { - be_test() : val{42} {} - int val; - }; - const be_test bet{}; - + const be_test bet{}; // For default value ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); const binary_extension cbe_btest{bet}; @@ -535,6 +524,12 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); ds >> cb_btest; CHECK_EQUAL( cbe_btest.value().val, cb_btest.value().val ) + + ds.seekp(256); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + binary_extension cb_default_test{}; + ds >> cb_default_test; + CHECK_EQUAL( 42, cb_default_test.value_or().val ) const binary_extension cbe_none{}; CHECK_ASSERT( "cannot get value of empty binary_extension", [&](){cbe_none.value();} ) From 2bcd19a8cf0f77dc42ad945045d4381495a1704e Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 8 Feb 2019 17:24:56 -0500 Subject: [PATCH 36/51] Finish `datastream` tests --- tests/unit/datastream_tests.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 786eed3355..d9e29448f0 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -48,6 +48,8 @@ using eosio::unpack; using boost::container::flat_map; using boost::container::flat_set; +// This data structure (which cannot be defined within a test macro block) needs both a default and a +// user-defined constructor for a specific `binary extension` test struct be_test { be_test() : val{42} {} be_test(int i) : val{i} {} From 0cc99ce071f705b3e30808c515d354f5d79d7736 Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 8 Feb 2019 17:27:40 -0500 Subject: [PATCH 37/51] Spruce-up tests --- tests/unit/asset_tests.cpp | 50 +++++++++++++++++----------------- tests/unit/crypto_tests.cpp | 2 -- tests/unit/name_tests.cpp | 46 +++++++++++++++---------------- tests/unit/serialize_tests.cpp | 39 ++++++++++++++++++++++++++ tests/unit/symbol_tests.cpp | 8 +++--- tests/unit/system_tests.cpp | 4 +-- tests/unit/varint_tests.cpp | 2 -- 7 files changed, 93 insertions(+), 58 deletions(-) create mode 100644 tests/unit/serialize_tests.cpp diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index d6262d22ba..5e3527202b 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -20,12 +20,12 @@ const int64_t asset_max{ asset_mask}; // 4611686018427387903 EOSIO_TEST_BEGIN(asset_type_test) silence_output(true); - symbol s0{"A", 0}; - symbol s1{"Z", 0}; - symbol s2{"AAAAAAA", 0}; - symbol s3{"ZZZZZZZ", 0}; - symbol sym_no_prec{"SYMBOLL", 0}; // Symbol with no precision - symbol sym_prec{"SYMBOLL", 63}; // Symbol with precision + symbol s0{"A",0}; + symbol s1{"Z",0}; + symbol s2{"AAAAAAA",0}; + symbol s3{"ZZZZZZZ",0}; + symbol sym_no_prec{"SYMBOLL",0}; // Symbol with no precision + symbol sym_prec{"SYMBOLL",63}; // Symbol with precision //// constexpr asset() CHECK_EQUAL( asset{}.amount, 0ULL ) @@ -175,8 +175,8 @@ EOSIO_TEST_BEGIN(asset_type_test) // ------------------------------------------------------------------------------------------ // inline friend asset& operator+(const asset&, const asset&)/asset& operator+=(const asset&) - CHECK_EQUAL( (asset{0LL, sym_no_prec} += asset{ 0LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{1LL, sym_no_prec} += asset{-1LL, sym_no_prec} ), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{0LL, sym_no_prec} += asset{ 0LL, sym_no_prec}), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{1LL, sym_no_prec} += asset{-1LL, sym_no_prec}), (asset{0LL, sym_no_prec}) ) CHECK_ASSERT( "attempt to add asset with different symbol", ( [&]() { @@ -239,17 +239,17 @@ EOSIO_TEST_BEGIN(asset_type_test) // ----------------------------------------------------------------------------------------------------------------------------- // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) - CHECK_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) - - CHECK_EQUAL( (asset{ 0LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 0LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{ 1LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount ), (asset{ 1LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{ 4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount ), (asset{-2LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{-2LL, sym_no_prec}.amount ), (asset{ 2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount), (asset{ 0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount), (asset{-2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) + + CHECK_EQUAL( (asset{ 0LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount), (asset{ 0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 1LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount), (asset{-2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{-2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) CHECK_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 0LL, sym_no_prec}.amount) ) CHECK_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 1LL, sym_no_prec}.amount) ) @@ -317,8 +317,8 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(extended_asset_type_test) silence_output(true); - symbol sym_no_prec{"SYMBOLL", 0}; - symbol sym_prec{"SYMBOLL", 63}; + symbol sym_no_prec{"SYMBOLL",0}; + symbol sym_prec{"SYMBOLL",63}; extended_symbol ex_sym_no_prec{sym_no_prec, name{"eosioaccountj"}}; extended_symbol ex_sym_prec{sym_prec, name{"eosioaccountj"}}; @@ -331,8 +331,8 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) CHECK_EQUAL( extended_asset{}.contract, name{} ) //// extended_asset(int64_t, extended_symbol) - CHECK_EQUAL( (extended_asset{{}, ex_sym_no_prec}.quantity), (asset{0LL, sym_no_prec}) ) - CHECK_EQUAL( (extended_asset{{}, ex_sym_no_prec}.contract), (name{"eosioaccountj"}) ) + CHECK_EQUAL( (extended_asset{{},ex_sym_no_prec}.quantity), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (extended_asset{{},ex_sym_no_prec}.contract), (name{"eosioaccountj"}) ) //// extended_asset(asset, name) CHECK_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.quantity), (asset{ 0LL, sym_no_prec}) ) @@ -340,8 +340,8 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // ------------------------------------------ // extended_symbol get_extended_symbol()const - CHECK_EQUAL( (extended_asset{{}, ex_sym_no_prec}.get_extended_symbol()), (ex_sym_no_prec) ) - CHECK_EQUAL( (extended_asset{{}, ex_sym_prec}.get_extended_symbol()), (ex_sym_prec) ) + CHECK_EQUAL( (extended_asset{{},ex_sym_no_prec}.get_extended_symbol()), (ex_sym_no_prec) ) + CHECK_EQUAL( (extended_asset{{},ex_sym_prec}.get_extended_symbol()), (ex_sym_prec) ) // Note: uncomment once print checking has been resolved // Note that if there is no precision, there will be odd output: diff --git a/tests/unit/crypto_tests.cpp b/tests/unit/crypto_tests.cpp index f5469a6c1e..b909e64124 100644 --- a/tests/unit/crypto_tests.cpp +++ b/tests/unit/crypto_tests.cpp @@ -9,8 +9,6 @@ using eosio::public_key; using eosio::signature; -using namespace eosio::native; - // Definitions in `eosio.cdt/libraries/eosiolib/crypto.hpp` EOSIO_TEST_BEGIN(public_key_type_test) silence_output(true); diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 61935c61ff..6ed7714226 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -338,33 +338,33 @@ EOSIO_TEST_BEGIN(name_type_test) // ------------------------------------ // inline constexpr name operator""_n() - CHECK_EQUAL( name{} == ""_n, true ) + CHECK_EQUAL( name{}, ""_n ) - CHECK_EQUAL( name{"1"} == "1"_n, true ) - CHECK_EQUAL( name{"5"} == "5"_n, true ) - CHECK_EQUAL( name{"a"} == "a"_n, true ) - CHECK_EQUAL( name{"z"} == "z"_n, true ) - - CHECK_EQUAL( name{"abc"} == "abc"_n, true ) - CHECK_EQUAL( name{"123"} == "123"_n, true ) - - CHECK_EQUAL( name{".abc"} == ".abc"_n, true ) - CHECK_EQUAL( name{".........abc"} == ".........abc"_n, true ) - CHECK_EQUAL( name{"123."} == "123."_n, true ) - CHECK_EQUAL( name{"123........."} == "123........."_n, true ) - CHECK_EQUAL( name{".a.b.c.1.2.3."} == ".a.b.c.1.2.3."_n, true ) + CHECK_EQUAL( name{"1"}, "1"_n ) + CHECK_EQUAL( name{"5"}, "5"_n ) + CHECK_EQUAL( name{"a"}, "a"_n ) + CHECK_EQUAL( name{"z"}, "z"_n ) + + CHECK_EQUAL( name{"abc"}, "abc"_n ) + CHECK_EQUAL( name{"123"}, "123"_n ) + + CHECK_EQUAL( name{".abc"}, ".abc"_n ) + CHECK_EQUAL( name{".........abc"}, ".........abc"_n ) + CHECK_EQUAL( name{"123."}, "123."_n ) + CHECK_EQUAL( name{"123........."}, "123........."_n ) + CHECK_EQUAL( name{".a.b.c.1.2.3."}, ".a.b.c.1.2.3."_n ) - CHECK_EQUAL( name{"abc.123"} == "abc.123"_n, true ) - CHECK_EQUAL( name{"123.abc"} == "123.abc"_n, true ) + CHECK_EQUAL( name{"abc.123"}, "abc.123"_n ) + CHECK_EQUAL( name{"123.abc"}, "123.abc"_n ) - CHECK_EQUAL( name{"12345abcdefgj"} == "12345abcdefgj"_n, true ) - CHECK_EQUAL( name{"hijklmnopqrsj"} == "hijklmnopqrsj"_n, true ) - CHECK_EQUAL( name{"tuvwxyz.1234j"} == "tuvwxyz.1234j"_n, true ) + CHECK_EQUAL( name{"12345abcdefgj"}, "12345abcdefgj"_n ) + CHECK_EQUAL( name{"hijklmnopqrsj"}, "hijklmnopqrsj"_n ) + CHECK_EQUAL( name{"tuvwxyz.1234j"}, "tuvwxyz.1234j"_n ) - CHECK_EQUAL( name{"111111111111j"} == "111111111111j"_n, true ) - CHECK_EQUAL( name{"555555555555j"} == "555555555555j"_n, true ) - CHECK_EQUAL( name{"aaaaaaaaaaaaj"} == "aaaaaaaaaaaaj"_n, true ) - CHECK_EQUAL( name{"zzzzzzzzzzzzj"} == "zzzzzzzzzzzzj"_n, true ) + CHECK_EQUAL( name{"111111111111j"}, "111111111111j"_n ) + CHECK_EQUAL( name{"555555555555j"}, "555555555555j"_n ) + CHECK_EQUAL( name{"aaaaaaaaaaaaj"}, "aaaaaaaaaaaaj"_n ) + CHECK_EQUAL( name{"zzzzzzzzzzzzj"}, "zzzzzzzzzzzzj"_n ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/serialize_tests.cpp b/tests/unit/serialize_tests.cpp new file mode 100644 index 0000000000..73f9be0763 --- /dev/null +++ b/tests/unit/serialize_tests.cpp @@ -0,0 +1,39 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + +#include +#include +#include + +using eosio::datastream; + +// Definitions in `eosio.cdt/libraries/eosiolib/serialize.hpp` +EOSIO_TEST_BEGIN(serialize_test) + silence_output(true); + + static constexpr uint16_t buffer_size{256}; + char ds_expected_buffer[buffer_size]{}; // Buffer for the datastream to point to + char ds_buffer[buffer_size]{}; // Buffer for the datastream to point to + + datastream ds_expected{ds_expected_buffer, buffer_size}; + datastream ds{ds_buffer, buffer_size}; + +struct Base { + +}; +// EOSLIB_SERIALIZE( (BASE), () () () ) + +struct Derived : public Base { + +}; +// EOSLIB_SERIALIZE( (DERIVED), (BASE), () () () ) + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char* argv[]) { + EOSIO_TEST(serialize_test) + return has_failed(); +} diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index dd711befa5..6c1265b4e4 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -234,10 +234,10 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) name n6{"aaaaaaaaaaaaj"}; name n7{"zzzzzzzzzzzzj"}; - symbol s0{"A", 0}; - symbol s1{"Z", 0}; - symbol s2{"AAAAAAA", 255}; - symbol s3{"ZZZZZZZ", 255}; + symbol s0{"A",0}; + symbol s1{"Z",0}; + symbol s2{"AAAAAAA",255}; + symbol s3{"ZZZZZZZ",255}; //// constexpr extended_symbol() // constexpr name get_symbol() diff --git a/tests/unit/system_tests.cpp b/tests/unit/system_tests.cpp index 3290d5e124..90ec77f79a 100644 --- a/tests/unit/system_tests.cpp +++ b/tests/unit/system_tests.cpp @@ -3,11 +3,11 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include + #include #include -#include - using eosio::check; using std::string; diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index 457597effc..dd67efbc71 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -7,8 +7,6 @@ #include #include -#include - using eosio::datastream; const uint32_t u32min = std::numeric_limits::min(); // 0 From e1deefb134063e29b09cbfb9431e3fa7154d6bea Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 11 Feb 2019 18:25:53 -0500 Subject: [PATCH 38/51] Fix for `__bzero` behavior --- libraries/native/crt.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/native/crt.cpp b/libraries/native/crt.cpp index 4d2b126033..16224ca962 100644 --- a/libraries/native/crt.cpp +++ b/libraries/native/crt.cpp @@ -134,6 +134,7 @@ extern "C" { extern "C" void* memset(void*, int, size_t); extern "C" void __bzero(void* to, size_t cnt) { - memset(to, 0, cnt); + char* cp{static_cast(to)}; + while (cnt--) *cp++ = 0; } } From 677d158a1e4a2ceffdba24e7cb217c3614a46f5d Mon Sep 17 00:00:00 2001 From: johndebord Date: Mon, 11 Feb 2019 18:30:08 -0500 Subject: [PATCH 39/51] Finish `serialize_tests` --- tests/unit/datastream_tests.cpp | 4 ++- tests/unit/serialize_tests.cpp | 47 +++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index d9e29448f0..0764a50752 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,3 +1,5 @@ +// TODO: Make costructor comments across all files consistant +// TODO: Fix line 275 comment /** * @file * @copyright defined in eosio.cdt/LICENSE.txt @@ -234,7 +236,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) static constexpr uint16_t buffer_size{256}; char datastream_buffer[buffer_size]; // Buffer for the datastream to point to - datastream ds{datastream_buffer, buffer_size}; + datastream ds{datastream_buffer, buffer_size}; // ------------- // T (primitive) diff --git a/tests/unit/serialize_tests.cpp b/tests/unit/serialize_tests.cpp index 73f9be0763..aafb1fe941 100644 --- a/tests/unit/serialize_tests.cpp +++ b/tests/unit/serialize_tests.cpp @@ -7,28 +7,53 @@ #include #include +using std::begin; +using std::end; +using std::fill; +using std::list; +using std::string; +using std::vector; + using eosio::datastream; +struct Base { + const char c{'c'}; + const int i{42}; + const double d{3.14}; + EOSLIB_SERIALIZE( Base, (c) (i) (d) ) +}; + +struct Derived : public Base { + const string s{"abcefghi"}; + const list l{'a','b','c','d','e','f','g','h','i'}; + const vector v{0,1,2,3,4,5,6,7,8,9}; + EOSLIB_SERIALIZE_DERIVED( Derived, Base, (s) (l) (v) ) +}; + // Definitions in `eosio.cdt/libraries/eosiolib/serialize.hpp` EOSIO_TEST_BEGIN(serialize_test) silence_output(true); static constexpr uint16_t buffer_size{256}; - char ds_expected_buffer[buffer_size]{}; // Buffer for the datastream to point to char ds_buffer[buffer_size]{}; // Buffer for the datastream to point to + char ds_expected_buffer[buffer_size]{}; + datastream ds{ds_buffer, buffer_size}; + datastream ds_expected{ds_expected_buffer, buffer_size}; - datastream ds_expected{ds_expected_buffer, buffer_size}; - datastream ds{ds_buffer, buffer_size}; + const Base b; + ds_expected << b.c << b.i << b.d; + ds << b; + REQUIRE_EQUAL( memcmp( ds_buffer, ds_expected_buffer, 256), 0 ) -struct Base { - -}; -// EOSLIB_SERIALIZE( (BASE), () () () ) + ds.seekp(0); + fill(begin(ds_buffer), end(ds_buffer), 0); + ds_expected.seekp(0); + fill(begin(ds_expected_buffer), end(ds_expected_buffer), 0); -struct Derived : public Base { - -}; -// EOSLIB_SERIALIZE( (DERIVED), (BASE), () () () ) + const Derived d; + ds_expected << d.c << d.i << d.d << d.s << d.l << d.v; + ds << d; + REQUIRE_EQUAL( memcmp( ds_buffer, ds_expected_buffer, 256), 0 ) silence_output(false); EOSIO_TEST_END From 7cb0f54b0ae73910acea4db3c23c6ef3a85eae2c Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 14 Feb 2019 18:28:42 -0500 Subject: [PATCH 40/51] Nearly finished with `time_tests`; with various other edits --- tests/CMakeLists.txt | 2 - tests/unit/CMakeLists.txt | 3 - tests/unit/binary_extension_tests.cpp | 61 ++- tests/unit/datastream_tests.cpp | 35 +- tests/unit/fixed_bytes_tests.cpp | 3 - tests/unit/serialize_tests.cpp | 73 ++- tests/unit/time_tests.cpp | 613 ++++++++++++++------------ tests/unit/varint_tests.cpp | 10 +- 8 files changed, 452 insertions(+), 348 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 32e2314756..2a4aa692ca 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,9 +1,7 @@ set(unit_test_dir ${CMAKE_BINARY_DIR}/EosioWasmTests-prefix/src/EosioWasmTests-build) add_test(asset_tests ${unit_test_dir}/asset_tests) -add_test(eosio_tests ${unit_test_dir}/eosio_tests) add_test(name_tests ${unit_test_dir}/name_tests) -add_test(print_tests ${unit_test_dir}/print_tests) add_test(symbol_tests ${unit_test_dir}/symbol_tests) add_test(system_tests ${unit_test_dir}/system_tests) add_test(time_tests ${unit_test_dir}/time_tests) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 340fab5200..e104a31f42 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -2,9 +2,7 @@ list(APPEND CMAKE_MODULE_PATH ${EOSIO_CDT_BIN}) include(EosioCDTMacros) add_native_executable(asset_tests asset_tests.cpp) -add_native_executable(eosio_tests eosio_tests.cpp) add_native_executable(name_tests name_tests.cpp) -add_native_executable(print_tests print_tests.cpp) add_native_executable(symbol_tests symbol_tests.cpp) add_native_executable(system_tests system_tests.cpp) add_native_executable(time_tests time_tests.cpp) @@ -12,4 +10,3 @@ add_native_executable(varint_tests varint_tests.cpp) add_dependencies(name_tests EosioTools) add_dependencies(system_tests EosioTools) -add_dependencies(print_tests EosioTools) diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index 90c8cd51ae..2649dd3742 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -14,9 +14,9 @@ EOSIO_TEST_BEGIN(binary_extension_test) /// constexpr binary_extension() // constexpr bool has_value()const - REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) ///// Should I test more than just the `const char*` ///// + REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) - REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) ///// Should I test more than just the `const char*` ///// + REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) /// constexpr binary_extension(const T&) REQUIRE_EQUAL( (binary_extension{'c'}.has_value()), true ) @@ -29,33 +29,22 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( (binary_extension{std::move("abcd")}.has_value()), true ) /// constexpr binary_extension(std::in_place_t, Args&&...) - REQUIRE_EQUAL( (binary_extension{std::in_place, 'c'}.has_value()), true ) ///// How do I pack more args into this?? ///// + REQUIRE_EQUAL( (binary_extension{std::in_place, 'c'}.has_value()), true ) /// How do I pack more args into this?? /// - REQUIRE_EQUAL( (binary_extension{std::in_place, "abcd"}.has_value()), true ) ///// How do I pack more args into this?? ///// + REQUIRE_EQUAL( (binary_extension{std::in_place, "abcd"}.has_value()), true ) /// How do I pack more args into this?? /// - // ~binary_extension() - binary_extension bin_destruct_char{'c'}; - REQUIRE_EQUAL( bin_destruct_char.has_value(), true ) - bin_destruct_char.~binary_extension(); - REQUIRE_EQUAL( bin_destruct_char.has_value(), false ) - - binary_extension bin_destruct_str{"abcd"}; - REQUIRE_EQUAL( bin_destruct_str.has_value(), true ) - bin_destruct_str.~binary_extension(); - REQUIRE_EQUAL( bin_destruct_str.has_value(), false ) - - // constexpr binary_extension(const binary_extension&) + /// constexpr binary_extension(const binary_extension&) binary_extension bin_copy_char_lval0{'c'}; binary_extension bin_copy_char_lval1{bin_copy_char_lval0}; - REQUIRE_EQUAL( (bin_copy_char_lval0.value() == bin_copy_char_lval1.value() && - bin_copy_char_lval0.has_value() == bin_copy_char_lval1.has_value()), true) + REQUIRE_EQUAL( bin_copy_char_lval0.value(), bin_copy_char_lval1.value() ) + REQUIRE_EQUAL( bin_copy_char_lval0.has_value(), bin_copy_char_lval1.has_value() ) binary_extension bin_copy_str_lval0{"abcd"}; binary_extension bin_copy_str_lval1{bin_copy_str_lval0}; - REQUIRE_EQUAL( (bin_copy_str_lval0.value() == bin_copy_str_lval1.value() && - bin_copy_str_lval0.has_value() == bin_copy_str_lval1.has_value()), true) + REQUIRE_EQUAL( bin_copy_str_lval0.value(), bin_copy_str_lval1.value() ) + REQUIRE_EQUAL( bin_copy_str_lval0.has_value(), bin_copy_str_lval1.has_value() ) - // constexpr binary_extension(binary_extension&&) + /// constexpr binary_extension(binary_extension&&) binary_extension bin_copy_char_rval0{'c'}; REQUIRE_EQUAL( bin_copy_char_rval0.has_value(), true ) binary_extension bin_copy_char_rval1{std::move(bin_copy_char_rval0)}; @@ -68,6 +57,19 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( bin_copy_str_rval0.has_value(), false ) REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){bin_copy_str_rval0.value();} ) + // ------------------- + // ~binary_extension() + binary_extension bin_destruct_char{'c'}; + REQUIRE_EQUAL( bin_destruct_char.has_value(), true ) + bin_destruct_char.~binary_extension(); + REQUIRE_EQUAL( bin_destruct_char.has_value(), false ) + + binary_extension bin_destruct_str{"abcd"}; + REQUIRE_EQUAL( bin_destruct_str.has_value(), true ) + bin_destruct_str.~binary_extension(); + REQUIRE_EQUAL( bin_destruct_str.has_value(), false ) + + // --------------------------------------- // constexpr explicit operator bool()const REQUIRE_EQUAL( (binary_extension{}.operator bool()), false ) REQUIRE_EQUAL( (binary_extension{0}.operator bool()), true ) @@ -77,6 +79,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( (binary_extension{nullptr}.operator bool()), true ) REQUIRE_EQUAL( (binary_extension{"abcd"}.operator bool()), true ) + // --------------------- // constexpr T& value()& binary_extension bin_char_value0{'c'}; REQUIRE_EQUAL( (bin_char_value0.value() == 'c'), true ) @@ -86,6 +89,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( (bin_str_value0.value() == "abcd"), true ) REQUIRE_EQUAL( (bin_str_value0.value() == "efgh"), false ) + // -------------------------------- // constexpr const T& value()const& binary_extension bin_const_char_value0{'c'}; REQUIRE_EQUAL( (bin_const_char_value0.value() == 'c'), true ) @@ -95,23 +99,27 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( (bin_const_str_value0.value() == "abcd"), true ) REQUIRE_EQUAL( (bin_const_str_value0.value() == "efgh"), false ) + // --------------------------------------------------------------------------------------- // constexpr auto value_or(U&&) -> std::enable_if_t::value, T&>& binary_extension bin_temp_value_or{}; uint8_t some_value = bin_temp_value_or.value_or(uint8_t{0x63}); REQUIRE_EQUAL( some_value, 0x63 ) + // -------------------------- // constexpr T&& value_or()&& REQUIRE_EQUAL( binary_extension{'c'}.value_or(), 'c' ) binary_extension bin_value_or_rval{"abcd"}; REQUIRE_EQUAL( bin_value_or_rval.value_or(), "abcd" ) + // ------------------------------------- // constexpr const T&& value_or()const&& REQUIRE_EQUAL( binary_extension{'c'}.value_or(), 'c' ) binary_extension bin_const_value_or_rval{"abcd"}; REQUIRE_EQUAL( bin_const_value_or_rval.value_or(), "abcd" ) + // ----------------------- // constexpr T value_or()& binary_extension bin_char_value_or_val{'c'}; REQUIRE_EQUAL( bin_char_value_or_val.value_or(), 'c' ) @@ -119,6 +127,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) binary_extension bin_value_or_val{"abcd"}; REQUIRE_EQUAL( bin_value_or_val.value_or(), "abcd" ) + // ---------------------------- // constexpr T value_or()const& binary_extension bin_char_const_value_or_val{'c'}; REQUIRE_EQUAL( bin_char_const_value_or_val.value_or(), 'c' ) @@ -126,6 +135,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) binary_extension bin_const_value_or_val{"abcd"}; REQUIRE_EQUAL( bin_const_value_or_val.value_or(), "abcd" ) + // ------------------------- // constexpr T* operator->() binary_extension bin_char_arrow_op{'c'}; REQUIRE_EQUAL( *bin_char_arrow_op.operator->() == 'c', true ) @@ -135,6 +145,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( *bin_arrow_op.operator->() == "abcd", true ) REQUIRE_EQUAL( *bin_arrow_op.operator->() != "efgh", true ) + // ------------------------------------ // constexpr const T* operator->()const binary_extension bin_char_const_arrow_op{'c'}; REQUIRE_EQUAL( *bin_char_const_arrow_op.operator->() == 'c', true ) @@ -144,6 +155,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( *bin_const_arrow_op.operator->() == "abcd", true ) REQUIRE_EQUAL( *bin_const_arrow_op.operator->() != "efgh", true ) + // ------------------------- // constexpr T& operator*()& binary_extension bin_char_star_op{'c'}; REQUIRE_EQUAL( bin_char_star_op.operator*() == 'c', true ) @@ -153,6 +165,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( bin_star_op.operator*() == "abcd", true ) REQUIRE_EQUAL( bin_star_op.operator*() != "efgh", true ) + // ------------------------------------ // constexpr const T& operator*()const& binary_extension bin_char_const_star_op{'c'}; REQUIRE_EQUAL( bin_char_const_star_op.operator*() == 'c', true ) @@ -162,6 +175,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( bin_const_star_op.operator*() == "abcd", true ) REQUIRE_EQUAL( bin_const_star_op.operator*() != "efgh", true ) + // --------------------------- // constexpr T&& operator*()&& binary_extension bin_char_rval_star_op{'c'}; REQUIRE_EQUAL( std::move(bin_char_rval_star_op.operator*()) == 'c', true ) @@ -171,6 +185,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( std::move(bin_str_rval_star_op.operator*()) == "abcd", true ) REQUIRE_EQUAL( std::move(bin_str_rval_star_op.operator*()) != "efgh", true ) + // -------------------------------------- // constexpr const T&& operator*()const&& binary_extension bin_const_char_rval_star_op{'c'}; REQUIRE_EQUAL( std::move(bin_const_char_rval_star_op.operator*()) == 'c', true ) @@ -179,7 +194,8 @@ EOSIO_TEST_BEGIN(binary_extension_test) binary_extension bin_const_str_rval_star_op{"abcd"}; REQUIRE_EQUAL( std::move(bin_const_str_rval_star_op.operator*()) == "abcd", true ) REQUIRE_EQUAL( std::move(bin_const_str_rval_star_op.operator*()) != "efgh", true ) - + + // ---------------------------- // T& emplace(Args&& ... args)& binary_extension bin_char_emplace{'c'}; bin_char_emplace.emplace(std::move('d')); @@ -191,6 +207,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) REQUIRE_EQUAL( bin_str_emplace.value() == "efgh", true ) REQUIRE_EQUAL( bin_str_emplace.value() != "abcd", true ) + // ------------ // void reset() binary_extension bin_char_reset{'c'}; REQUIRE_EQUAL( bin_char_reset.has_value(), true ) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 0764a50752..aabe151c17 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,5 +1,23 @@ -// TODO: Make costructor comments across all files consistant -// TODO: Fix line 275 comment +// TODO: Make costructor comments across all files consistant `///` make them three slashes +// TODO: Get rid of redundant tests +// TODO: Organize all tests in the most logical way +// TODO: Figure out a logical ordering of temporary helper variables +// TODO: Should I extern all global variables? +// TODO: Line up everything +// TODO: Make all parenthesis consistent in the `REQUIRE` expressions +// TODO: Change all `REQUIRE`'s to `CHECK`'s +// TODO: Chnage the uint32_t max to the real value in all files +// TODO: Check to make sure that the uint32_t changes didn't break anything for all files +// TODO: Make sure to remove all explicit `std::` in statements +// TODO: Make parenthesis consistent +// TODO: Make sure to check the CMake files +// TODO: Make the appropriate variabels const and/or static +// Is this a good convention? +// microseconds ms0{ 0LL}; +// microseconds ms1{ 1LL}; +// microseconds ms2{-1LL}; +// microseconds ms_min{i64min}; +// microseconds ms_max{i64max}; /** * @file * @copyright defined in eosio.cdt/LICENSE.txt @@ -56,7 +74,6 @@ struct be_test { be_test() : val{42} {} be_test(int i) : val{i} {} int val; - EOSLIB_SERIALIZE( be_test, (val) ) }; @@ -273,13 +290,13 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << cprim_array; CHECK_EQUAL( ds.tellp(), 41 ) - ds.seekp(0); // Next three lines are testing an implementation detail ??? + ds.seekp(0); ds >> prim_array; CHECK_EQUAL( memcmp(cprim_array, prim_array, 10), 0 ) ds.seekp(1); fill(begin(prim_array), end(prim_array), 0); - REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> prim_array;} ) + CHECK_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> prim_array;} ) // ------------------- // T[] (non-primitive) @@ -294,13 +311,13 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds << cnon_prim_array; CHECK_EQUAL( ds.tellp(), 41 ) - ds.seekp(0); // Next three lines are testing an implementation detail ??? + ds.seekp(0); ds >> non_prim_array; CHECK_EQUAL( memcmp(cnon_prim_array, non_prim_array, 10), 0 ) ds.seekp(1); fill(begin(non_prim_array), end(non_prim_array), non_prim_array_test{}); - REQUIRE_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> non_prim_array;} ) + CHECK_ASSERT( "T[] size and unpacked size don't match", [&](){ds >> non_prim_array;} ) // ---- // bool @@ -324,9 +341,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds >> a; CHECK_EQUAL( ca, a ) - //////////////////////////////////////////////////////////////////////////////////////////////////////////// - ////////////////////////////// Note: uncomment once issue has been resolved //////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Note: uncomment once issue has been resolved /// // ---------- // std::deque // ds.seekp(0); diff --git a/tests/unit/fixed_bytes_tests.cpp b/tests/unit/fixed_bytes_tests.cpp index 5265def3ef..1dc23a538a 100644 --- a/tests/unit/fixed_bytes_tests.cpp +++ b/tests/unit/fixed_bytes_tests.cpp @@ -15,9 +15,6 @@ using namespace eosio::native; EOSIO_TEST_BEGIN(fixed_bytes_test) silence_output(false); -// ------------ -// constructors - /// constexpr fixed_bytes() // static constexpr size_t padded_bytes() REQUIRE_EQUAL( fixed_bytes<20>{}.padded_bytes(), 12 ) diff --git a/tests/unit/serialize_tests.cpp b/tests/unit/serialize_tests.cpp index aafb1fe941..d7f2aca3a9 100644 --- a/tests/unit/serialize_tests.cpp +++ b/tests/unit/serialize_tests.cpp @@ -12,22 +12,36 @@ using std::end; using std::fill; using std::list; using std::string; +using std::tie; using std::vector; using eosio::datastream; -struct Base { - const char c{'c'}; - const int i{42}; - const double d{3.14}; - EOSLIB_SERIALIZE( Base, (c) (i) (d) ) +struct B { + const char c{}; + EOSLIB_SERIALIZE( B, (c) ) + + friend bool operator==(const B& lhs, const B& rhs) { + return lhs.c == rhs.c; + } +}; + +struct D1 : public B { + const int i{}; + EOSLIB_SERIALIZE_DERIVED( D1, B, (i) ) + + friend bool operator==(const D1& lhs, const D1& rhs) { + return tie(lhs.c, lhs.i) == tie(rhs.c, rhs.i); + } }; -struct Derived : public Base { - const string s{"abcefghi"}; - const list l{'a','b','c','d','e','f','g','h','i'}; - const vector v{0,1,2,3,4,5,6,7,8,9}; - EOSLIB_SERIALIZE_DERIVED( Derived, Base, (s) (l) (v) ) +struct D2 : public D1 { + const vector v{}; + EOSLIB_SERIALIZE_DERIVED( D2, D1, (v) ) + + friend bool operator==(const D2& lhs, const D2& rhs) { + return tie(lhs.c, lhs.i, lhs.v) == tie(rhs.c, rhs.i, rhs.v); + } }; // Definitions in `eosio.cdt/libraries/eosiolib/serialize.hpp` @@ -36,25 +50,52 @@ EOSIO_TEST_BEGIN(serialize_test) static constexpr uint16_t buffer_size{256}; char ds_buffer[buffer_size]{}; // Buffer for the datastream to point to - char ds_expected_buffer[buffer_size]{}; + char ds_expected_buffer[buffer_size]{}; // Buffer to compare `ds_buffer` with datastream ds{ds_buffer, buffer_size}; datastream ds_expected{ds_expected_buffer, buffer_size}; - const Base b; - ds_expected << b.c << b.i << b.d; + // Testing base structures + const B b{'c'}; + B bb; + ds_expected << b.c; ds << b; REQUIRE_EQUAL( memcmp( ds_buffer, ds_expected_buffer, 256), 0 ) + ds.seekp(0); + ds >> bb; + REQUIRE_EQUAL( b, bb ) + ds.seekp(0); // Clear all buffers + fill(begin(ds_buffer), end(ds_buffer), 0); + ds_expected.seekp(0); + fill(begin(ds_expected_buffer), end(ds_expected_buffer), 0); + + // Testing derived structures + const D1 d1{'c', 42}; + D1 dd1; + ds_expected << d1.c << d1.i; + ds << d1; + REQUIRE_EQUAL( memcmp( ds_buffer, ds_expected_buffer, 256), 0 ) + ds.seekp(0); + ds >> dd1; + REQUIRE_EQUAL( d1, dd1 ) + + ds.seekp(0); // Clear all buffers fill(begin(ds_buffer), end(ds_buffer), 0); ds_expected.seekp(0); fill(begin(ds_expected_buffer), end(ds_expected_buffer), 0); - const Derived d; - ds_expected << d.c << d.i << d.d << d.s << d.l << d.v; - ds << d; + // Testing multiple layers of derived structures + const D2 d2{'c', 42, {1.1, 2.2}}; + D1 dd2; + ds_expected << d2.c << d2.i << d2.v; + ds << d2; REQUIRE_EQUAL( memcmp( ds_buffer, ds_expected_buffer, 256), 0 ) + ds.seekp(0); + ds >> dd2; + REQUIRE_EQUAL( d2, dd2 ) + silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index 2631691d50..6d3092c287 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -6,454 +6,491 @@ #include #include +using std::numeric_limits; + +using eosio::days; +using eosio::hours; using eosio::microseconds; +using eosio::milliseconds; +using eosio::minutes; +using eosio::seconds; using eosio::time_point; using eosio::time_point_sec; using eosio::block_timestamp; -const uint32_t u32min = std::numeric_limits::min(); // 0 -const uint32_t u32max = std::numeric_limits::max(); // 18446744073709551615 +const int64_t i64min = numeric_limits::min(); // -9223372036854775808 +const int64_t i64max = numeric_limits::max(); // 9223372036854775807 -const int64_t i64min = std::numeric_limits::min(); // -9223372036854775808 -const int64_t i64max = std::numeric_limits::max(); // 9223372036854775807 +const uint32_t u32min = numeric_limits::min(); // 0 +const uint32_t u32max = numeric_limits::max(); // 4294967295 // Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(microseconds_type_test) - silence_output(false); +silence_output(false); - //// explicit microseconds(uint64_t)/int64_t count() - REQUIRE_EQUAL( microseconds{}.count(), 0ULL ) - REQUIRE_EQUAL( microseconds{i64max}.count(), i64max ) - REQUIRE_EQUAL( microseconds{i64min}.count(), i64min ) + /// explicit microseconds(uint64_t)/int64_t count() + CHECK_EQUAL( microseconds{}._count, 0ULL ) + CHECK_EQUAL( microseconds{i64max}._count, i64max ) + CHECK_EQUAL( microseconds{i64min}._count, i64min ) // ----------------------------- // static microseconds maximum() - REQUIRE_EQUAL( microseconds::maximum().count(), microseconds{0x7FFFFFFFFFFFFFFFLL}.count() ) + CHECK_EQUAL( microseconds::maximum()._count, microseconds{0x7FFFFFFFFFFFFFFFLL}.count() ) // ------------------------------------------------------------------------ // friend microseconds operator+(const microseconds&, const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} + microseconds{ 1LL}), microseconds{ 1LL} ) - REQUIRE_EQUAL( (microseconds{1LL} + microseconds{-1LL}), microseconds{ 0LL} ) + CHECK_EQUAL( (microseconds{0LL} + microseconds{ 1LL}), microseconds{ 1LL} ) + CHECK_EQUAL( (microseconds{1LL} + microseconds{-1LL}), microseconds{ 0LL} ) // ------------------------------------------------------------------------ // friend microseconds operator-(const microseconds&, const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} - microseconds{ 1LL}), microseconds{-1LL} ) - REQUIRE_EQUAL( (microseconds{1LL} - microseconds{-1LL}), microseconds{ 2LL} ) + CHECK_EQUAL( (microseconds{0LL} - microseconds{ 1LL}), microseconds{-1LL} ) + CHECK_EQUAL( (microseconds{1LL} - microseconds{-1LL}), microseconds{ 2LL} ) // ---------------------------------------------- // microseconds& operator+=(const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} += microseconds{ 1LL}), microseconds{ 1LL} ) - REQUIRE_EQUAL( (microseconds{1LL} += microseconds{-1LL}), microseconds{ 0LL} ) + CHECK_EQUAL( (microseconds{0LL} += microseconds{ 1LL}), microseconds{ 1LL} ) + CHECK_EQUAL( (microseconds{1LL} += microseconds{-1LL}), microseconds{ 0LL} ) // ---------------------------------------------- // microseconds& operator-=(const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} -= microseconds{ 1LL}), microseconds{-1LL} ) - REQUIRE_EQUAL( (microseconds{1LL} -= microseconds{-1LL}), microseconds{ 2LL} ) + CHECK_EQUAL( (microseconds{0LL} -= microseconds{ 1LL}), microseconds{-1LL} ) + CHECK_EQUAL( (microseconds{1LL} -= microseconds{-1LL}), microseconds{ 2LL} ) // ------------------------------------- // bool operator==(const microseconds&) - REQUIRE_EQUAL( (microseconds{1LL} == microseconds{1LL}), true ) - REQUIRE_EQUAL( (microseconds{0LL} == microseconds{1LL}), false ) + CHECK_EQUAL( (microseconds{1LL} == microseconds{1LL}), true ) + CHECK_EQUAL( (microseconds{0LL} == microseconds{1LL}), false ) // ------------------------------------- // bool operator!=(const microseconds&) - REQUIRE_EQUAL( (microseconds{1LL} != microseconds{0LL}), true ) - REQUIRE_EQUAL( (microseconds{0LL} != microseconds{0LL}), false ) + CHECK_EQUAL( (microseconds{1LL} != microseconds{1LL}), false ) + CHECK_EQUAL( (microseconds{0LL} != microseconds{1LL}), true ) // ------------------------------------- // bool operator<(const microseconds&) - REQUIRE_EQUAL( (microseconds{0LL} < microseconds{1LL}), true ) - REQUIRE_EQUAL( (microseconds{1LL} < microseconds{1LL}), false ) + CHECK_EQUAL( (microseconds{0LL} < microseconds{1LL}), true ) + CHECK_EQUAL( (microseconds{1LL} < microseconds{1LL}), false ) // ------------------------------------- // bool operator<=(const microseconds&) - REQUIRE_EQUAL( (microseconds{1LL} <= microseconds{1LL}), true ) - REQUIRE_EQUAL( (microseconds{2LL} <= microseconds{1LL}), false ) + CHECK_EQUAL( (microseconds{1LL} <= microseconds{1LL}), true ) + CHECK_EQUAL( (microseconds{2LL} <= microseconds{1LL}), false ) // ------------------------------------- // bool operator>(const microseconds&) - REQUIRE_EQUAL( (microseconds{1LL} > microseconds{0LL}), true ) - REQUIRE_EQUAL( (microseconds{1LL} > microseconds{1LL}), false ) + CHECK_EQUAL( (microseconds{1LL} > microseconds{0LL}), true ) + CHECK_EQUAL( (microseconds{1LL} > microseconds{1LL}), false ) // ------------------------------------- // bool operator>=(const microseconds&) - REQUIRE_EQUAL( (microseconds{1LL} >= microseconds{1LL}), true ) - REQUIRE_EQUAL( (microseconds{1LL} >= microseconds{2LL}), false ) + CHECK_EQUAL( (microseconds{1LL} >= microseconds{1LL}), true ) + CHECK_EQUAL( (microseconds{1LL} >= microseconds{2LL}), false ) // -------------------- // int64_t to_seconds() - REQUIRE_EQUAL( (microseconds{i64min-1}.to_seconds()), i64max / 1000000 ) - REQUIRE_EQUAL( (microseconds{i64max}.to_seconds()), i64max / 1000000 ) + CHECK_EQUAL( (microseconds{i64max}.to_seconds()), i64max / 1000000 ) // ----------------------------------------- // inline microseconds milliseconds(int64_t) - REQUIRE_EQUAL( eosio::milliseconds(0LL), microseconds{0LL} ) - REQUIRE_EQUAL( eosio::milliseconds(1LL), microseconds{1000LL} ) - REQUIRE_EQUAL( eosio::milliseconds(60LL), microseconds{60LL*1000LL} ) + CHECK_EQUAL( milliseconds(0LL), microseconds{0LL} ) + CHECK_EQUAL( milliseconds(1LL), microseconds{1000LL} ) + CHECK_EQUAL( milliseconds(60LL), microseconds{60LL*1000LL} ) // ------------------------------------ // inline microseconds seconds(int64_t) - REQUIRE_EQUAL( eosio::seconds(0LL), microseconds{0LL} ) - REQUIRE_EQUAL( eosio::seconds(1LL), microseconds{1000000LL} ) - REQUIRE_EQUAL( eosio::seconds(60LL), microseconds{60LL*1000000LL} ) + CHECK_EQUAL( seconds(0LL), microseconds{0LL} ) + CHECK_EQUAL( seconds(1LL), microseconds{1000000LL} ) + CHECK_EQUAL( seconds(60LL), microseconds{60LL*1000000LL} ) // ------------------------------------ // inline microseconds minutes(int64_t) - REQUIRE_EQUAL( eosio::minutes(0LL), microseconds{0LL} ) - REQUIRE_EQUAL( eosio::minutes(1LL), microseconds{60LL*1000000LL} ) - REQUIRE_EQUAL( eosio::minutes(60LL), microseconds{60LL*60LL*1000000LL} ) + CHECK_EQUAL( minutes(0LL), microseconds{0LL} ) + CHECK_EQUAL( minutes(1LL), microseconds{60LL*1000000LL} ) + CHECK_EQUAL( minutes(60LL), microseconds{60LL*60LL*1000000LL} ) // ---------------------------------- // inline microseconds hours(int64_t) - REQUIRE_EQUAL( eosio::hours(0LL), microseconds{0LL} ) - REQUIRE_EQUAL( eosio::hours(1LL), microseconds{60LL*60LL*1000000LL} ) - REQUIRE_EQUAL( eosio::hours(60LL), microseconds{60LL*60LL*60LL*1000000LL} ) + CHECK_EQUAL( hours(0LL), microseconds{0LL} ) + CHECK_EQUAL( hours(1LL), microseconds{60LL*60LL*1000000LL} ) + CHECK_EQUAL( hours(60LL), microseconds{60LL*60LL*60LL*1000000LL} ) // --------------------------------- // inline microseconds days(int64_t) - REQUIRE_EQUAL( eosio::days(0LL), microseconds{0LL} ) - REQUIRE_EQUAL( eosio::days(1LL), microseconds{24LL*60LL*60LL*1000000LL} ) - REQUIRE_EQUAL( eosio::days(60LL), microseconds{24LL*60LL*60LL*60LL*1000000LL} ) + CHECK_EQUAL( days(0LL), microseconds{0LL} ) + CHECK_EQUAL( days(1LL), microseconds{24LL*60LL*60LL*1000000LL} ) + CHECK_EQUAL( days(60LL), microseconds{24LL*60LL*60LL*60LL*1000000LL} ) silence_output(false); EOSIO_TEST_END +// Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(time_point_type_test) silence_output(false); - microseconds ms0{ 0LL}; - microseconds ms1{ 1LL}; - microseconds ms2{-1LL}; - microseconds ms_max{i64max}; + microseconds ms0 { 0LL}; + microseconds ms1 { 1LL}; + microseconds msn1{-1LL}; microseconds ms_min{i64min}; + microseconds ms_max{i64max}; - //// explicit time_point(microseconds)/microseconds& time_since_epoch() - REQUIRE_EQUAL( time_point{ms0}.time_since_epoch(), ms0 ) - REQUIRE_EQUAL( time_point{ms1}.time_since_epoch(), ms1 ) + /// explicit time_point(microseconds) + // microseconds& time_since_epoch() + CHECK_EQUAL( time_point{ms0}.time_since_epoch(), ms0 ) + CHECK_EQUAL( time_point{ms1}.time_since_epoch(), ms1 ) + CHECK_EQUAL( time_point{ms_min}.time_since_epoch(), ms_min ) + CHECK_EQUAL( time_point{ms_max}.time_since_epoch(), ms_max ) // -------------------------- // uint32_t sec_since_epoch() - REQUIRE_EQUAL( time_point{ms0}.sec_since_epoch(), 0 / 1000000 ) - REQUIRE_EQUAL( time_point{ms1}.sec_since_epoch(), 1 / 1000000 ) - REQUIRE_EQUAL( time_point{ms_max}.sec_since_epoch(), i64max / 1000000 ) - REQUIRE_EQUAL( time_point{ms_min}.sec_since_epoch(), i64min / 1000000 ) + CHECK_EQUAL( time_point{ms0}.sec_since_epoch(), 0 ) + CHECK_EQUAL( time_point{ms1}.sec_since_epoch(), 0 ) + CHECK_EQUAL( time_point{microseconds{1000000}}.sec_since_epoch(), 1 ) + CHECK_EQUAL( time_point{microseconds{10000000}}.sec_since_epoch(), 10 ) - // ------------------------------------------ - // time_point operator+(const microseconds&) - REQUIRE_EQUAL( (time_point{ms0} + ms1), time_point{ms1} ) - REQUIRE_EQUAL( (time_point{ms2} + ms1), time_point{ms0} ) + // ----------------------------------------- + // time_point operator+(const microseconds&) + CHECK_EQUAL( (time_point{ms0} + ms1), time_point{ms1} ) + CHECK_EQUAL( (time_point{msn1} + ms1), time_point{ms0} ) - // ---------------------------------------- - // time_point operator+(const time_point&) - REQUIRE_EQUAL( (time_point{ms0} + time_point{ms1}), time_point{ms1} ) - REQUIRE_EQUAL( (time_point{ms2} + time_point{ms1}), time_point{ms0} ) + // --------------------------------------- + // time_point operator+(const time_point&) + CHECK_EQUAL( (time_point{ms0} + time_point{ms1}), time_point{ms1} ) + CHECK_EQUAL( (time_point{msn1} + time_point{ms1}), time_point{ms0} ) - // ------------------------------------------ - // time_point operator-(const microseconds&) - REQUIRE_EQUAL( (time_point{ms0} - ms1), time_point{ms2} ) - REQUIRE_EQUAL( (time_point{ms0} - ms2), time_point{ms1} ) + // ----------------------------------------- + // time_point operator-(const microseconds&) + CHECK_EQUAL( (time_point{ms0} - ms1), time_point{msn1} ) + CHECK_EQUAL( (time_point{ms0} - msn1), time_point{ms1} ) - // ------------------------------------------ - // microseconds operator-(const time_point&) - REQUIRE_EQUAL( (time_point{ms0} - time_point{ms1}), ms2 ) - REQUIRE_EQUAL( (time_point{ms0} - time_point{ms2}), ms1 ) - - // -------------------------------------------- - // time_point& operator+=(const microseconds&) - REQUIRE_EQUAL( (time_point{ms0} += ms1), time_point{ms1} ) - REQUIRE_EQUAL( (time_point{ms2} += ms1), time_point{ms0} ) + // ----------------------------------------- + // microseconds operator-(const time_point&) + CHECK_EQUAL( (time_point{ms0} - time_point{ms1}), msn1 ) + CHECK_EQUAL( (time_point{ms0} - time_point{msn1}), ms1 ) + + // ------------------------------------------- + // time_point& operator+=(const microseconds&) + CHECK_EQUAL( (time_point{ms0} += ms1), time_point{ms1} ) + CHECK_EQUAL( (time_point{msn1} += ms1), time_point{ms0} ) - // -------------------------------------------- - // time_point& operator-=(const microseconds&) - REQUIRE_EQUAL( (time_point{ms0} -= ms1), time_point{ms2} ) - REQUIRE_EQUAL( (time_point{ms0} -= ms2), time_point{ms1} ) + // ------------------------------------------- + // time_point& operator-=(const microseconds&) + CHECK_EQUAL( (time_point{ms0} -= ms1), time_point{msn1} ) + CHECK_EQUAL( (time_point{ms0} -= msn1), time_point{ms1} ) // ---------------------------------- // bool operator==(const time_point&) - REQUIRE_EQUAL( (time_point{ms0} == time_point{ms0}), true ) - REQUIRE_EQUAL( (time_point{ms0} == time_point{ms1}), false ) + CHECK_EQUAL( (time_point{ms0} == time_point{ms0}), true ) + CHECK_EQUAL( (time_point{ms0} == time_point{ms1}), false ) // ---------------------------------- // bool operator!=(const time_point&) - REQUIRE_EQUAL( (time_point{ms1} != time_point{ms0}), true ) - REQUIRE_EQUAL( (time_point{ms1} != time_point{ms1}), false ) + CHECK_EQUAL( (time_point{ms0} != time_point{ms0}), false ) + CHECK_EQUAL( (time_point{ms0} != time_point{ms1}), true ) - // ---------------------------------- - // bool operator<(const time_point&) - REQUIRE_EQUAL( (time_point{ms0} < time_point{ms1}), true ) - REQUIRE_EQUAL( (time_point{ms1} < time_point{ms1}), false ) + // --------------------------------- + // bool operator<(const time_point&) + CHECK_EQUAL( (time_point{ms0} < time_point{ms1}), true ) + CHECK_EQUAL( (time_point{ms1} < time_point{ms1}), false ) // ---------------------------------- // bool operator<=(const time_point&) - REQUIRE_EQUAL( (time_point{ms1} <= time_point{ms1}), true ) - REQUIRE_EQUAL( (time_point{ms1} <= time_point{ms0}), false ) + CHECK_EQUAL( (time_point{ms1} <= time_point{ms1}), true ) + CHECK_EQUAL( (time_point{ms1} <= time_point{ms0}), false ) - // ---------------------------------- - // bool operator>(const time_point&) - REQUIRE_EQUAL( (time_point{ms1} > time_point{ms0}), true ) - REQUIRE_EQUAL( (time_point{ms1} > time_point{ms1}), false ) + // --------------------------------- + // bool operator>(const time_point&) + CHECK_EQUAL( (time_point{ms1} > time_point{ms0}), true ) + CHECK_EQUAL( (time_point{ms1} > time_point{ms1}), false ) // ---------------------------------- // bool operator>=(const time_point&) - REQUIRE_EQUAL( (time_point{ms1} >= time_point{ms1}), true ) - REQUIRE_EQUAL( (time_point{ms0} >= time_point{ms1}), false ) + CHECK_EQUAL( (time_point{ms1} >= time_point{ms1}), true ) + CHECK_EQUAL( (time_point{ms0} >= time_point{ms1}), false ) silence_output(false); EOSIO_TEST_END +// Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(time_point_sec_type_test) - silence_output(true); + silence_output(false); - microseconds ms0{ 0LL}; - microseconds ms1{ 1LL}; - microseconds ms2{-1LL}; - microseconds ms_max{i64max}; + microseconds ms0 { 0LL}; + microseconds ms1 { 1LL}; + microseconds msn1{-1LL}; microseconds ms_min{i64min}; + microseconds ms_max{i64max}; time_point tp0{ms0}; time_point tp1{ms1}; - time_point tp2{ms2}; - time_point tp_max{ms_max}; + time_point tpn1{msn1}; time_point tp_min{ms_min}; + time_point tp_max{ms_max}; - //// time_point_sec()/uint32_t sec_since_epoch()const - REQUIRE_EQUAL( time_point_sec{}.sec_since_epoch(), 0 ) + /// time_point_sec() + //uint32_t sec_since_epoch()const + CHECK_EQUAL( time_point_sec{}.utc_seconds, 0 ) - //// explicit time_point_sec(uint32_t) - REQUIRE_EQUAL( time_point_sec{u32max}.sec_since_epoch(), u32max ) - REQUIRE_EQUAL( time_point_sec{u32max + 1}.sec_since_epoch(), 0 ) + /// explicit time_point_sec(uint32_t) + CHECK_EQUAL( time_point_sec{u32min}.utc_seconds, 0 ) + CHECK_EQUAL( time_point_sec{u32max}.utc_seconds, u32max ) + CHECK_EQUAL( time_point_sec{u32max + 1}.utc_seconds, 0 ) - //// time_point_sec(const time_point&) - REQUIRE_EQUAL( time_point_sec{tp0}.sec_since_epoch(), 0LL / 1000000LL ) - REQUIRE_EQUAL( time_point_sec{tp1}.sec_since_epoch(), 1LL / 1000000LL ) - REQUIRE_EQUAL( time_point_sec{tp2}.sec_since_epoch(), -1LL / 1000000LL ) - REQUIRE_EQUAL( time_point_sec{tp_max}.sec_since_epoch(), i64max / 1000000LL ) - REQUIRE_EQUAL( time_point_sec{tp_min}.sec_since_epoch(), i64min / 1000000LL ) - - // ------------------------------- - // static time_point_sec maximum() - REQUIRE_EQUAL( time_point_sec{}.maximum().utc_seconds, 0xFFFFFFFF ) - REQUIRE_EQUAL( time_point_sec{}.maximum().utc_seconds != 1, true ) + /// time_point_sec(const time_point&) + CHECK_EQUAL( time_point_sec{tp0}.utc_seconds, ms0._count / 1000000 ) + CHECK_EQUAL( time_point_sec{tp1}.utc_seconds, ms1._count / 1000000 ) + CHECK_EQUAL( time_point_sec{tpn1}.utc_seconds, msn1._count / 1000000 ) // --------------------------- // static time_point_sec min() - REQUIRE_EQUAL( time_point_sec{}.min().utc_seconds, 0 ) - REQUIRE_EQUAL( time_point_sec{}.min().utc_seconds != 1, true ) + CHECK_EQUAL( time_point_sec{}.min().utc_seconds == 0, true ) + CHECK_EQUAL( time_point_sec{}.min().utc_seconds != 1, true ) + + // ------------------------------- + // static time_point_sec maximum() + CHECK_EQUAL( time_point_sec{}.maximum().utc_seconds == 0xFFFFFFFF, true ) + CHECK_EQUAL( time_point_sec{}.maximum().utc_seconds != 1, true ) // -------------------------- // operator time_point()const - REQUIRE_EQUAL( (time_point{time_point_sec{tp0}}.elapsed.count()), 0 ) - REQUIRE_EQUAL( (time_point{time_point_sec{tp1}}.elapsed.count()), 0 ) - REQUIRE_EQUAL( (time_point{time_point_sec{tp2}}.elapsed.count()), 0 ) - REQUIRE_EQUAL( (time_point{time_point_sec{tp_max}}.elapsed.count() / 1000000), ms_max.count() / 1000000 ) - REQUIRE_EQUAL( (time_point{time_point_sec{tp_min}}.elapsed.count() / 1000000), ms_min.count() / 1000000 ) + CHECK_EQUAL( time_point_sec{u32min}.operator time_point(), time_point{seconds(u32min)} ) + CHECK_EQUAL( time_point_sec{u32max}.operator time_point(), time_point{seconds(u32max)} ) // ------------------------------------------- // time_point_sec operator=(const time_point&) - REQUIRE_EQUAL( (time_point_sec{} = time_point{tp0}), time_point_sec{} ) - REQUIRE_EQUAL( (time_point_sec{} = time_point{tp1}), time_point_sec{} ) - REQUIRE_EQUAL( (time_point_sec{} = time_point{tp_max}), time_point_sec{tp_max} ) + CHECK_EQUAL( (time_point_sec{} = tp0), time_point_sec{} ) + CHECK_EQUAL( (time_point_sec{} = tp1), time_point_sec{} ) + CHECK_EQUAL( (time_point_sec{} = tp_max), time_point_sec{tp_max} ) // --------------------------------------- // time_point_sec operator+(uint32_t)const - REQUIRE_EQUAL( (time_point_sec{} + u32min), time_point_sec{u32min} ) - REQUIRE_EQUAL( (time_point_sec{} + u32max), time_point_sec{u32max} ) + CHECK_EQUAL( (time_point_sec{} + u32min), time_point_sec{u32min} ) + CHECK_EQUAL( (time_point_sec{} + u32max), time_point_sec{u32max} ) // ----------------------------------------------------------------------- // friend time_point operator+(const time_point_sec&, const microseconds&) - REQUIRE_EQUAL( (time_point_sec{} + ms1), time_point{tp1} ) - REQUIRE_EQUAL( (time_point_sec{} + ms2), time_point{tp2} ) + CHECK_EQUAL( (time_point_sec{0} + microseconds{ 1000000LL}), time_point{microseconds{ 1000000LL}} ) + CHECK_EQUAL( (time_point_sec{0} + microseconds{-1000000LL}), time_point{microseconds{-1000000LL}} ) + CHECK_EQUAL( (time_point_sec{1} + microseconds{ 1000000LL}), time_point{microseconds{ 2000000LL}} ) + CHECK_EQUAL( (time_point_sec{1} + microseconds{-1000000LL}), time_point{microseconds{ 0LL}} ) - // --------------------------------------- - // time_point_sec operator-(uint32_t)const - REQUIRE_EQUAL( (time_point_sec{u32max} - u32max), time_point_sec{} ) - REQUIRE_EQUAL( (time_point_sec{u32min} - 1UL), time_point_sec{u32max} ) - // ----------------------------------------------------------------------- // friend time_point operator-(const time_point_sec&, const microseconds&) - // REQUIRE_EQUAL( (time_point_sec{u32max} - microseconds{i32max}), time_point{u32max-i32max} ) - // REQUIRE_EQUAL( (time_point_sec{u32min} - microseconds{}), time_point{} ) // new + CHECK_EQUAL( (time_point_sec{0} - microseconds{ 1000000LL}), time_point{microseconds{-1000000LL}} ) + CHECK_EQUAL( (time_point_sec{0} - microseconds{-1000000LL}), time_point{microseconds{ 1000000LL}} ) + CHECK_EQUAL( (time_point_sec{1} - microseconds{ 1000000LL}), time_point{microseconds{ 0LL}} ) + CHECK_EQUAL( (time_point_sec{1} - microseconds{-1000000LL}), time_point{microseconds{ 2000000LL}} ) - // // --------------------------------------------------------------------------- - // // friend microseconds operator-(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) - // REQUIRE_EQUAL( (time_point_sec{} - time_point_sec{}), microseconds{} ) // new + // --------------------------------------------------------------------------- + // friend microseconds operator-(const time_point_sec&, const time_point_sec&) + CHECK_EQUAL( (time_point_sec{0} - time_point_sec{0}), microseconds{ 0LL} ) + CHECK_EQUAL( (time_point_sec{0} - time_point_sec{1}), microseconds{-1000000LL} ) + CHECK_EQUAL( (time_point_sec{1} - time_point_sec{0}), microseconds{ 1000000LL} ) + CHECK_EQUAL( (time_point_sec{1} - time_point_sec{1}), microseconds{ 0LL} ) - // // ----------------------------------------------------------------------- - // // friend microseconds operator-(const time_point&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) - // REQUIRE_EQUAL( (time_point{} - time_point_sec{}), microseconds{} ) // new - - // // ------------------------------------ - // // time_point_sec& operator+=(uint32_t) - // REQUIRE_EQUAL( (time_point_sec{} += i64max), time_point_sec{i64max} ) - // REQUIRE_EQUAL( (time_point_sec{} += i64max), time_point_sec{i64max} ) // new - - // // ---------------------------------------- - // // time_point_sec& operator+=(microseconds) - // REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64max}), time_point_sec{tp_max} ) - // REQUIRE_EQUAL( (time_point_sec{} += microseconds{i64max}), time_point_sec{tp_max} ) // new - - // // ------------------------------------------ - // // time_point_sec& operator+=(time_point_sec) - // REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64max}), time_point_sec{i64max} ) - // REQUIRE_EQUAL( (time_point_sec{} += time_point_sec{i64max}), time_point_sec{i64max} ) // new - - // // ------------------------------------ - // // time_point_sec& operator-=(uint32_t) - // REQUIRE_EQUAL( (time_point_sec{i64max} -= i64max), time_point_sec{} ) - // REQUIRE_EQUAL( (time_point_sec{i64max} -= i64max), time_point_sec{} ) // new - - // // ---------------------------------------- - // // time_point_sec& operator-=(microseconds) - // REQUIRE_EQUAL( (time_point_sec{i64max} -= microseconds{i64max}), time_point_sec{i64max - (i64max / 1000000LL)} ) - // REQUIRE_EQUAL( (time_point_sec{i64max} -= microseconds{i64max}), time_point_sec{i64max - (i64max / 1000000LL)} ) // new - - // // ------------------------------------------ - // // time_point_sec& operator-=(time_point_sec) - // REQUIRE_EQUAL( (time_point_sec{i64max} -= time_point_sec{i64max}), time_point_sec{} ) - // REQUIRE_EQUAL( (time_point_sec{i64max} -= time_point_sec{i64max}), time_point_sec{} ) // new - - // // -------------------------------------------------------------------- - // // friend bool operator==(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp0} == time_point{tp0}), true ) // new - - // // -------------------------------------------------------------------- - // // friend bool operator!=(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp_max} != time_point{tp0}), true ) // new - - // // ------------------------------------------------------------------- - // // friend bool operator<(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) // new - - // // -------------------------------------------------------------------- - // // friend bool operator<=(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp0} < time_point{tp_max}), true ) // new - - // // ------------------------------------------------------------------- - // // friend bool operator>(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp_max} > time_point{tp0}), true ) // new - - // // -------------------------------------------------------------------- - // // friend bool operator>=(const time_point_sec&, const time_point_sec&) - // REQUIRE_EQUAL( (time_point_sec{tp_max} >= time_point{tp_max}), true ) - // REQUIRE_EQUAL( (time_point_sec{tp_max} >= time_point{tp_max}), true ) // new + // ----------------------------------------------------------------------- + // friend microseconds operator-(const time_point&, const time_point_sec&) + CHECK_EQUAL( (time_point{microseconds{0}} - time_point_sec{0}), microseconds{ 0LL} ) + CHECK_EQUAL( (time_point{microseconds{0}} - time_point_sec{1}), microseconds{-1000000LL} ) + CHECK_EQUAL( (time_point{microseconds{1}} - time_point_sec{0}), microseconds{ 1LL} ) + CHECK_EQUAL( (time_point{microseconds{1}} - time_point_sec{1}), microseconds{ -999999LL} ) + + // ------------------------------------ + // time_point_sec& operator+=(uint32_t) + CHECK_EQUAL( (time_point_sec{0} += u32min), time_point_sec{u32min} ) + CHECK_EQUAL( (time_point_sec{0} += u32max), time_point_sec{u32max} ) + + // ---------------------------------------- + // time_point_sec& operator+=(microseconds) + CHECK_EQUAL( (time_point_sec{0} += microseconds{ 1LL}), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{0} += microseconds{1000000LL}), time_point_sec{1} ) + CHECK_EQUAL( (time_point_sec{1} += microseconds{ 1LL}), time_point_sec{1} ) + CHECK_EQUAL( (time_point_sec{1} += microseconds{1000000LL}), time_point_sec{2} ) + + // ------------------------------------------ + // time_point_sec& operator+=(time_point_sec) + CHECK_EQUAL( (time_point_sec{0} += time_point_sec{0}), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{0} += time_point_sec{1}), time_point_sec{1} ) + CHECK_EQUAL( (time_point_sec{1} += time_point_sec{0}), time_point_sec{1} ) + CHECK_EQUAL( (time_point_sec{1} += time_point_sec{1}), time_point_sec{2} ) + + // ------------------------------------ + // time_point_sec& operator-=(uint32_t) + CHECK_EQUAL( (time_point_sec{u32min} -= u32min), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{u32max} -= u32max), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{u32max} -= u32min), time_point_sec{u32max} ) + + // ---------------------------------------- + // time_point_sec& operator-=(microseconds) + CHECK_EQUAL( (time_point_sec{0} -= microseconds{ 1LL}), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{1} -= microseconds{1000000LL}), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{2} -= microseconds{1000000LL}), time_point_sec{1} ) + CHECK_EQUAL( (time_point_sec{3} -= microseconds{1000000LL}), time_point_sec{2} ) + + // ------------------------------------------ + // time_point_sec& operator-=(time_point_sec) + CHECK_EQUAL( (time_point_sec{0} -= time_point_sec{0}), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{1} -= time_point_sec{1}), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{2} -= time_point_sec{1}), time_point_sec{1} ) + CHECK_EQUAL( (time_point_sec{3} -= time_point_sec{1}), time_point_sec{2} ) + + // --------------------------------------- + // time_point_sec operator+(uint32_t)const + CHECK_EQUAL( (time_point_sec{0} + u32min), time_point_sec{u32min} ) + CHECK_EQUAL( (time_point_sec{0} + u32max), time_point_sec{u32max} ) + + // --------------------------------------- + // time_point_sec operator-(uint32_t)const + CHECK_EQUAL( (time_point_sec{u32min} - u32min), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{u32max} - u32max), time_point_sec{0} ) + CHECK_EQUAL( (time_point_sec{u32max} - u32min), time_point_sec{u32max} ) + + // -------------------------------------------------------------------- + // friend bool operator==(const time_point_sec&, const time_point_sec&) + CHECK_EQUAL( (time_point_sec{1} == time_point_sec{1}), true ) + CHECK_EQUAL( (time_point_sec{0} == time_point_sec{1}), false ) + + // -------------------------------------------------------------------- + // friend bool operator!=(const time_point_sec&, const time_point_sec&) + CHECK_EQUAL( (time_point_sec{1} != time_point_sec{1}), false ) + CHECK_EQUAL( (time_point_sec{0} != time_point_sec{1}), true ) + + // ------------------------------------------------------------------- + // friend bool operator<(const time_point_sec&, const time_point_sec&) + CHECK_EQUAL( (time_point_sec{0} < time_point_sec{1}), true ) + CHECK_EQUAL( (time_point_sec{1} < time_point_sec{1}), false ) + + // -------------------------------------------------------------------- + // friend bool operator<=(const time_point_sec&, const time_point_sec&) + CHECK_EQUAL( (time_point_sec{1} <= time_point_sec{1}), true ) + CHECK_EQUAL( (time_point_sec{2} <= time_point_sec{1}), false ) + + // ------------------------------------------------------------------- + // friend bool operator>(const time_point_sec&, const time_point_sec&) + CHECK_EQUAL( (time_point_sec{1} > time_point_sec{0}), true ) + CHECK_EQUAL( (time_point_sec{1} > time_point_sec{1}), false ) + + // -------------------------------------------------------------------- + // friend bool operator>=(const time_point_sec&, const time_point_sec&) + CHECK_EQUAL( (time_point_sec{1} >= time_point_sec{1}), true ) + CHECK_EQUAL( (time_point_sec{1} >= time_point_sec{2}), false ) silence_output(false); EOSIO_TEST_END +// Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(block_timestamp_type_test) silence_output(false); - microseconds ms0{ 0LL}; - microseconds ms1{ 1LL}; - microseconds ms2{-1LL}; - microseconds ms_max{i64max}; - microseconds ms_min{i64min}; + static const int64_t bt_epoch{946684800000LL}; - time_point tp0{ms0}; - time_point tp1{ms1}; - time_point tp2{ms2}; - time_point tp_max{ms_max}; - time_point tp_min{ms_min}; + static const microseconds ms0{bt_epoch*1000}; + static const microseconds ms1{bt_epoch*1000+500000}; + static const microseconds ms2{bt_epoch*1000+1000000}; + static const microseconds ms_max{i64max}; - time_point_sec tps0{tp0}; - time_point_sec tps1{tp1}; - time_point_sec tps2{tp2}; - time_point_sec tps_max{tp_max}; - time_point_sec tps_min{tp_min}; - - //// explicit block_timestamp(uint32_t) - REQUIRE_EQUAL( block_timestamp{}.slot == block_timestamp{0}.slot, true ) - - // NOTE: No invariant established. `block_timestamp` gets wonky with certain inputs. - // Steps to reproduce: - // microseconds ms0{0LL}; - // time_point tp0{ms0}; - // block_timestamp{tp0}; - // eosio::print_f("%", block_timestamp.slot); // output: 18446744071816182016 - // Reason for erros: - // set_time_point(time_point{microseconds{0LL}}); - // int64_t micro_since_epoch = t.time_since_epoch().count(); // 0 - // int64_t msec_since_epoch = micro_since_epoch / 1000; // 0 / 1000 = 0 - // slot = uint32_t(( msec_since_epoch - block_timestamp_epoch ) / int64_t(block_interval_ms)); // 0 - 946684800000ll / 500 - - //// block_timestamp(const time_point&) - REQUIRE_EQUAL( block_timestamp{tp0}.slot == block_timestamp{0}.slot, true ) + static const time_point tp0{ms0}; + static const time_point tp1{ms1}; + static const time_point tp2{ms2}; + static const time_point tp_max{ms_max}; + + static const time_point_sec tps0{tp0}; + static const time_point_sec tps1{tp1}; + static const time_point_sec tps2{tp2}; + static const time_point_sec tps_max{u32max}; - //// block_timestamp(const time_point_sec&) - REQUIRE_EQUAL( block_timestamp{tps0}.slot == block_timestamp{0}.slot, true ) + /// explicit block_timestamp(uint32_t) + CHECK_EQUAL( block_timestamp{}.slot, 0 ) + CHECK_EQUAL( block_timestamp{u32min}.slot, u32min ) + CHECK_EQUAL( block_timestamp{u32max}.slot, u32max ) + + /// block_timestamp(const time_point&) + CHECK_EQUAL( block_timestamp{tp0}.slot, 0 ) + CHECK_EQUAL( block_timestamp{tp1}.slot, 1 ) + CHECK_EQUAL( block_timestamp{tp2}.slot, 2 ) + + /// block_timestamp(const time_point_sec&) + CHECK_EQUAL( block_timestamp{tps0}.slot, 0 ) + CHECK_EQUAL( block_timestamp{tps1}.slot, 1 ) + CHECK_EQUAL( block_timestamp{tps2}.slot, 2 ) + + // eosio::print(block_timestamp{tp0}.slot); + // eosio::print(block_timestamp{tp1}.slot); + // eosio::print(block_timestamp{tp2}.slot); + // eosio::print("\n"); + // eosio::print(block_timestamp{tps0}.slot); + // eosio::print(block_timestamp{tps1}.slot); + // eosio::print(block_timestamp{tps2}.slot); + // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+15000000}}}}.slot); + // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+20000000}}}}.slot); + // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+25000000}}}}.slot); + // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+30000000}}}}.slot); // -------------------------------- // static block_timestamp maximum() - REQUIRE_EQUAL( block_timestamp{}.maximum(), block_timestamp{0xFFFF} ) + CHECK_EQUAL( block_timestamp{}.maximum() == block_timestamp{0xFFFF}, true ) + CHECK_EQUAL( block_timestamp{}.maximum() != block_timestamp{0}, true ) // ---------------------------- // static block_timestamp min() - REQUIRE_EQUAL( block_timestamp{}.min(), block_timestamp{0} ) + CHECK_EQUAL( block_timestamp{}.min() == block_timestamp{0}, true ) + CHECK_EQUAL( block_timestamp{}.min() != block_timestamp{0xFFFF}, true ) // ---------------------- // block_timestamp next() - REQUIRE_EQUAL( (block_timestamp{}.next()), block_timestamp{1} ) - - // NOTE: This function will never throw unless the value lands on `0` - // For example: - // block_timestamp{std::numeric_limits::max(); - // REQUIRE_ASSERT( "block timestamp overflow", ([]() {block_timestamp{std::numeric_limits::max()-1}.next();}) ); - - // ---------------------------------------------------------- - // time_point to_time_point()const/operator time_point()const - block_timestamp bt0{tps0}; - block_timestamp bt1{tps1}; - block_timestamp bt2{tps2}; - block_timestamp bt_max{tps_max}; - block_timestamp bt_min{tps_min}; - - REQUIRE_EQUAL( (bt0.to_time_point()), static_cast(tps0) ) - REQUIRE_EQUAL( (bt1.to_time_point()), static_cast(tps1) ) - REQUIRE_EQUAL( (bt2.to_time_point()), static_cast(tps2) ) - REQUIRE_EQUAL( (bt_max.to_time_point()), static_cast(tps_max) ) - REQUIRE_EQUAL( (bt_min.to_time_point()), static_cast(tps_min) ) + CHECK_EQUAL( (block_timestamp{0}.next()), block_timestamp{1} ) + CHECK_EQUAL( (block_timestamp{1}.next()), block_timestamp{2} ) + CHECK_EQUAL( (block_timestamp{2}.next()), block_timestamp{3} ) + + REQUIRE_ASSERT( "block timestamp overflow", [](){block_timestamp{u32max}.next();} ) + + // ------------------------------- + // time_point to_time_point()const + + // -------------------------- + // operator time_point()const // -------------------------------------- // void set_time_point(const time_point&) - // REQUIRE_EQUAL( bt0.set_time_point{tp0}, ) // ------------------------------------------ // void set_time_point(const time_point_sec&) // --------------------------------- // void operator=(const time_point&) + // CHECK_EQUAL( (block_timestamp{} = tp0), block_timestamp{} ) + // CHECK_EQUAL( (block_timestamp{} = tp1), block_timestamp{} ) + // CHECK_EQUAL( (block_timestamp{} = tp_max), block_timestamp{} ) // --------------------------------------- // bool operator==(const block_timestamp&) - REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) - REQUIRE_EQUAL( bt0 == block_timestamp{tps0}, true ) // new - + CHECK_EQUAL( block_timestamp{1} == block_timestamp{1}, true ) + CHECK_EQUAL( block_timestamp{0} == block_timestamp{1}, false ) + + // --------------------------------------- // bool operator!=(const block_timestamp&) - REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) - REQUIRE_EQUAL( bt_max != block_timestamp{tps1}, true ) // new - + CHECK_EQUAL( block_timestamp{1} != block_timestamp{1}, false ) + CHECK_EQUAL( block_timestamp{0} != block_timestamp{1}, true ) + + // -------------------------------------- // bool operator<(const block_timestamp&) - REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) - REQUIRE_EQUAL( bt0 < block_timestamp{tps_max}, true ) // new - + CHECK_EQUAL( block_timestamp{0} < block_timestamp{1}, true ) + CHECK_EQUAL( block_timestamp{1} < block_timestamp{1}, false ) + + // --------------------------------------- // bool operator<=(const block_timestamp&) - REQUIRE_EQUAL( bt_max <= block_timestamp{tps_max}, true ) - REQUIRE_EQUAL( bt_max <= block_timestamp{tps_max}, true ) // new - + CHECK_EQUAL( block_timestamp{1} <= block_timestamp{1}, true ) + CHECK_EQUAL( block_timestamp{2} <= block_timestamp{1}, false ) + + // --------------------------------------- // bool operator>(const block_timestamp&) - REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) - REQUIRE_EQUAL( bt_max > block_timestamp{tps0}, true ) // new - + CHECK_EQUAL( block_timestamp{1} > block_timestamp{0}, true ) + CHECK_EQUAL( block_timestamp{1} > block_timestamp{1}, false ) + + // --------------------------------------- // bool operator>=(const block_timestamp&) - REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) - REQUIRE_EQUAL( bt_max >= block_timestamp{bt_max}, true ) // new + CHECK_EQUAL( block_timestamp{1} >= block_timestamp{1}, true ) + CHECK_EQUAL( block_timestamp{1} >= block_timestamp{2}, false ) silence_output(false); EOSIO_TEST_END @@ -461,7 +498,7 @@ EOSIO_TEST_END int main(int argc, char* argv[]) { // EOSIO_TEST(microseconds_type_test); // EOSIO_TEST(time_point_type_test); - EOSIO_TEST(time_point_sec_type_test); - // EOSIO_TEST(block_timestamp_type_test); + // EOSIO_TEST(time_point_sec_type_test); + EOSIO_TEST(block_timestamp_type_test); return has_failed(); } diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index dd67efbc71..8ce491df26 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -7,13 +7,15 @@ #include #include +using std::numeric_limits; + using eosio::datastream; -const uint32_t u32min = std::numeric_limits::min(); // 0 -const uint32_t u32max = std::numeric_limits::max(); // 18446744073709551615 +const uint32_t u32min = numeric_limits::min(); // 0 +const uint32_t u32max = numeric_limits::max(); // 18446744073709551615 -const int32_t i32min = std::numeric_limits::min(); // -9223372036854775808 -const int32_t i32max = std::numeric_limits::max(); // 9223372036854775807 +const int32_t i32min = numeric_limits::min(); // -9223372036854775808 +const int32_t i32max = numeric_limits::max(); // 9223372036854775807 // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(unsigned_int_type_test) From c87404bf1b3bf6b93d025cc1c275646d40d1a395 Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 15 Feb 2019 15:02:45 -0500 Subject: [PATCH 41/51] Finish `time_tests` and `varint_tests` --- tests/unit/datastream_tests.cpp | 2 + tests/unit/eosio_tests.cpp | 39 ------ tests/unit/print_tests.cpp | 158 ---------------------- tests/unit/time_tests.cpp | 61 ++++----- tests/unit/varint_tests.cpp | 230 +++++++++++++++----------------- 5 files changed, 134 insertions(+), 356 deletions(-) delete mode 100644 tests/unit/eosio_tests.cpp delete mode 100644 tests/unit/print_tests.cpp diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index aabe151c17..b8f8781bbf 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -12,6 +12,8 @@ // TODO: Make parenthesis consistent // TODO: Make sure to check the CMake files // TODO: Make the appropriate variabels const and/or static +// TODO: Make sure to adjust the correct min max values +// TODO: Make sure all output is on it's appropriate value // Is this a good convention? // microseconds ms0{ 0LL}; // microseconds ms1{ 1LL}; diff --git a/tests/unit/eosio_tests.cpp b/tests/unit/eosio_tests.cpp deleted file mode 100644 index a452d19751..0000000000 --- a/tests/unit/eosio_tests.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file - * @copyright defined in eosio.cdt/LICENSE.txt - */ - -#include -#include - -namespace using_no_macros { -class [[eosio::contract]] con { - - struct [[eosio::table]] tab {}; - - [[eosio::action]] void act() {} -}; -} - -namespace using_macros { -CONTRACT con { - - TABLE tab {}; - - ACTION act() {} -}; -} - -// Definitions in `eosio.cdt/libraries/eosiolib/eosio.hpp` -EOSIO_TEST_BEGIN(eosio_test) - silence_output(false); - -// REQUIRE_EQUAL( typeid(using_no_macros::con).name() == typeid(using_macros::con).name(), true ) - - silence_output(false); -EOSIO_TEST_END - -int main(int argc, char* argv[]) { - EOSIO_TEST(eosio_test); - return has_failed(); -} diff --git a/tests/unit/print_tests.cpp b/tests/unit/print_tests.cpp deleted file mode 100644 index 16cd5c9890..0000000000 --- a/tests/unit/print_tests.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/** - * @file - * @copyright defined in eosio.cdt/LICENSE.txt - */ - -#include -#include -#include -#include -#include - -using eosio::name; -using eosio::print; - -// EOSIO_TEST_BEGIN(print_test) -// silence_output(true); -// CHECK_PRINT("27", [](){ eosio::print((uint8_t)27); }); -// CHECK_PRINT("34", [](){ eosio::print((int)34); }); -// CHECK_PRINT([](std::string s){return s[0] == 'a';}, [](){ eosio::print((char)'a'); }); -// CHECK_PRINT([](std::string s){return s[0] == 'b';}, [](){ eosio::print((int8_t)'b'); }); -// CHECK_PRINT("202", [](){ eosio::print((unsigned int)202); }); -// CHECK_PRINT("-202", [](){ eosio::print((int)-202); }); -// CHECK_PRINT("707", [](){ eosio::print((unsigned long)707); }); -// CHECK_PRINT("-707", [](){ eosio::print((long)-707); }); -// CHECK_PRINT("909", [](){ eosio::print((unsigned long long)909); }); -// CHECK_PRINT("-909", [](){ eosio::print((long long)-909); }); -// CHECK_PRINT("404", [](){ eosio::print((uint32_t)404); }); -// CHECK_PRINT("-404", [](){ eosio::print((int32_t)-404); }); -// CHECK_PRINT("404000000", [](){ eosio::print((uint64_t)404000000); }); -// CHECK_PRINT("-404000000", [](){ eosio::print((int64_t)-404000000); }); -// CHECK_PRINT("0x0066000000000000", [](){ eosio::print((uint128_t)102); }); -// CHECK_PRINT("0xffffff9affffffffffffffffffffffff", [](){ eosio::print((int128_t)-102); }); -// silence_output(false); -// EOSIO_TEST_END - -EOSIO_TEST_BEGIN(print_test) - silence_output(false); - - // ------------------------------ - // inline void print(const char*) - // std::string s{}; - // char c{}; - // CHECK_PRINT( "", [&](){eosio::print(s.c_str());} ); - - // Not understanding how this test isn't passing - // for(unsigned short i{}; i < 256; ++i) { - // c = i; - // s = static_cast(i); - // CHECK_PRINT( [&](std::string temp){return temp == std::string{c};}, [&](){eosio::print(s.c_str());} ); - - - // eosio::print(c); - // eosio::print("\n"); - // eosio::print(s); - // eosio::print("\n"); - // } - - // Not understanding how this test isn't passing - // Using 70 as an arbitrary string length - // for(unsigned short i{}; i < 70; ++i) { - // s = std::string(i, 'i'); - // CHECK_PRINT( [&](std::string temp){return temp == std::string(i, 'i');}, [&](){eosio::print(s.c_str());} ); - // } - - // ---------------------------------------------------------------------------------- - // template >::value && - // std::is_signed>::value, int> = 0> - // inline void print(T) - - // ---------------------------------------------------------------------------------- - // template >::value && - // std::is_signed>::value, int> = 0> - // inline void print(T) - - // ------------------------ - // inline void print(float) - // float f{}; - // for(float i{}; i < 1; f += 0.01) { - // CHECK_PRINT( [&](std::string){return std::to_string{f};}, [&](){eosio::print(f);} ); - // } - - // s = ; - // CHECK_PRINT( "", [&](){eosio::print(s.c_str());} ); - - // ------------------------- - // inline void print(double) - - // ------------------------------ - // inline void print(long double) - - // --------------------- - // template - // inline void print(const fixed_bytes&) - - // --------------------- - // template - // inline void print( fixed_bytes&) - - // ----------------------- - // inline void print(name) -// CHECK_PRINT( [](const std::string& s) {print("HI\n", s.size(), "\n", s, "\n", std_out.index, "\n"); return false;}, []() {print(name{"1"});} ) -// CHECK_PRINT( [](const std::string& s) {print("HI\n", s.size(), "\n", s, "\n", std_out.index, "\n"); return false;}, []() {print(name{"1"});} ) -// print("------\n"); - CHECK_PRINT( "1", []() {print(name{"1"});} ) - // REQUIRE_PRINT( "5", []() {print(name{"5"});} ) - // REQUIRE_PRINT( "a", []() {print(name{"a"});} ) - // REQUIRE_PRINT( "z", []() {print(name{"z"});} ) - - // REQUIRE_PRINT( "abc", []() {print(name{"abc"});} ) - // REQUIRE_PRINT( "123", []() {print(name{"123"});} ) - - // REQUIRE_PRINT( ".abc", []() {print(name{".abc"});} ) - // REQUIRE_PRINT( ".........abc", []() {print(name{".........abc"});} ) - // REQUIRE_PRINT( "123.", []() {print(name{"123."});} ) - // REQUIRE_PRINT( "123.........", []() {print(name{"123........."});} ) - // REQUIRE_PRINT( ".a.b.c.1.2.3.", []() {print(name{".a.b.c.1.2.3."});} ) - - // REQUIRE_PRINT( "abc.123", []() {print(name{"abc.123"});} ) - // REQUIRE_PRINT( "123.abc", []() {print(name{"123.abc"});} ) - - // REQUIRE_PRINT( "12345abcdefgj", []() {print(name{"12345abcdefgj"});} ) - // REQUIRE_PRINT( "hijklmnopqrsj", []() {print(name{"hijklmnopqrsj"});} ) - // REQUIRE_PRINT( "tuvwxyz.1234j", []() {print(name{"tuvwxyz.1234j"});} ) - - // REQUIRE_PRINT( "111111111111j", []() {print(name{"111111111111j"});} ) - // REQUIRE_PRINT( "555555555555j", []() {print(name{"555555555555j"});} ) - // REQUIRE_PRINT( "aaaaaaaaaaaaj", []() {print(name{"aaaaaaaaaaaaj"});} ) - // REQUIRE_PRINT( "zzzzzzzzzzzzj", []() {print(name{"zzzzzzzzzzzzj"});} ) - - // ------------------------------------- - // inline void print(eosio::symbol_code) - - // ------------------------------------------------------------------------------------------ - // template>::value, int> = 0> - // inline void print(T&&) - - // -------------------------------- - // inline void print_f(const char*) - - // ----------------------------------------- - // template - // inline void print_f( const char*, Arg, Args...) - - // ---------------------------------------- - // template - // void print( Arg&&, Args&&...) - - // -------------------- - // template - // inline iostream& operator<<(iostream&, const T&) - - silence_output(false); -EOSIO_TEST_END - -int main(int argc, char* argv[]) { - EOSIO_TEST(print_test); - return has_failed(); -} diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index 6d3092c287..186f350ac9 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -26,7 +26,7 @@ const uint32_t u32max = numeric_limits::max(); // 4294967295 // Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(microseconds_type_test) -silence_output(false); +silence_output(true); /// explicit microseconds(uint64_t)/int64_t count() CHECK_EQUAL( microseconds{}._count, 0ULL ) @@ -126,7 +126,7 @@ EOSIO_TEST_END // Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(time_point_type_test) - silence_output(false); + silence_output(true); microseconds ms0 { 0LL}; microseconds ms1 { 1LL}; @@ -213,7 +213,7 @@ EOSIO_TEST_END // Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(time_point_sec_type_test) - silence_output(false); + silence_output(true); microseconds ms0 { 0LL}; microseconds ms1 { 1LL}; @@ -253,8 +253,8 @@ EOSIO_TEST_BEGIN(time_point_sec_type_test) // -------------------------- // operator time_point()const - CHECK_EQUAL( time_point_sec{u32min}.operator time_point(), time_point{seconds(u32min)} ) - CHECK_EQUAL( time_point_sec{u32max}.operator time_point(), time_point{seconds(u32max)} ) + CHECK_EQUAL( time_point_sec{u32min}.operator time_point(), time_point{microseconds{static_cast(u32min)*1000000}} ) + CHECK_EQUAL( time_point_sec{u32max}.operator time_point(), time_point{microseconds{static_cast(u32max)*1000000}} ) // ------------------------------------------- // time_point_sec operator=(const time_point&) @@ -380,7 +380,7 @@ EOSIO_TEST_END // Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(block_timestamp_type_test) - silence_output(false); + silence_output(true); static const int64_t bt_epoch{946684800000LL}; @@ -397,7 +397,6 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) static const time_point_sec tps0{tp0}; static const time_point_sec tps1{tp1}; static const time_point_sec tps2{tp2}; - static const time_point_sec tps_max{u32max}; /// explicit block_timestamp(uint32_t) CHECK_EQUAL( block_timestamp{}.slot, 0 ) @@ -405,26 +404,14 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) CHECK_EQUAL( block_timestamp{u32max}.slot, u32max ) /// block_timestamp(const time_point&) + // void set_time_point(const time_point&) CHECK_EQUAL( block_timestamp{tp0}.slot, 0 ) CHECK_EQUAL( block_timestamp{tp1}.slot, 1 ) CHECK_EQUAL( block_timestamp{tp2}.slot, 2 ) /// block_timestamp(const time_point_sec&) - CHECK_EQUAL( block_timestamp{tps0}.slot, 0 ) - CHECK_EQUAL( block_timestamp{tps1}.slot, 1 ) - CHECK_EQUAL( block_timestamp{tps2}.slot, 2 ) - - // eosio::print(block_timestamp{tp0}.slot); - // eosio::print(block_timestamp{tp1}.slot); - // eosio::print(block_timestamp{tp2}.slot); - // eosio::print("\n"); - // eosio::print(block_timestamp{tps0}.slot); - // eosio::print(block_timestamp{tps1}.slot); - // eosio::print(block_timestamp{tps2}.slot); - // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+15000000}}}}.slot); - // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+20000000}}}}.slot); - // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+25000000}}}}.slot); - // eosio::print(block_timestamp{time_point_sec{time_point{microseconds{bt_epoch*1000+30000000}}}}.slot); + // void set_time_point(const time_point_sec&) + // Infeasible constructor? // -------------------------------- // static block_timestamp maximum() @@ -446,21 +433,25 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) // ------------------------------- // time_point to_time_point()const - + CHECK_EQUAL( block_timestamp{1}.to_time_point(), time_point{microseconds{(1*500+bt_epoch)*1000}} ) + CHECK_EQUAL( block_timestamp{2}.to_time_point(), time_point{microseconds{(2*500+bt_epoch)*1000}} ) + CHECK_EQUAL( block_timestamp{3}.to_time_point(), time_point{microseconds{(3*500+bt_epoch)*1000}} ) + // -------------------------- // operator time_point()const - - // -------------------------------------- - // void set_time_point(const time_point&) - - // ------------------------------------------ - // void set_time_point(const time_point_sec&) + CHECK_EQUAL( block_timestamp{1}.operator time_point(), time_point{microseconds{(1*500+bt_epoch)*1000}} ) + CHECK_EQUAL( block_timestamp{2}.operator time_point(), time_point{microseconds{(2*500+bt_epoch)*1000}} ) + CHECK_EQUAL( block_timestamp{3}.operator time_point(), time_point{microseconds{(3*500+bt_epoch)*1000}} ) // --------------------------------- // void operator=(const time_point&) - // CHECK_EQUAL( (block_timestamp{} = tp0), block_timestamp{} ) - // CHECK_EQUAL( (block_timestamp{} = tp1), block_timestamp{} ) - // CHECK_EQUAL( (block_timestamp{} = tp_max), block_timestamp{} ) + block_timestamp opequal{}; + opequal = tp0; + CHECK_EQUAL( opequal, block_timestamp{tp0} ) + opequal = tp1; + CHECK_EQUAL( opequal, block_timestamp{tp1} ) + opequal = tp2; + CHECK_EQUAL( opequal, block_timestamp{tp2} ) // --------------------------------------- // bool operator==(const block_timestamp&) @@ -496,9 +487,9 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) EOSIO_TEST_END int main(int argc, char* argv[]) { - // EOSIO_TEST(microseconds_type_test); - // EOSIO_TEST(time_point_type_test); - // EOSIO_TEST(time_point_sec_type_test); + EOSIO_TEST(microseconds_type_test); + EOSIO_TEST(time_point_type_test); + EOSIO_TEST(time_point_sec_type_test); EOSIO_TEST(block_timestamp_type_test); return has_failed(); } diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index 8ce491df26..9b5791e8df 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -12,30 +12,30 @@ using std::numeric_limits; using eosio::datastream; const uint32_t u32min = numeric_limits::min(); // 0 -const uint32_t u32max = numeric_limits::max(); // 18446744073709551615 +const uint32_t u32max = numeric_limits::max(); // 4294967295 -const int32_t i32min = numeric_limits::min(); // -9223372036854775808 -const int32_t i32max = numeric_limits::max(); // 9223372036854775807 +const int32_t i32min = numeric_limits::min(); // -2147483648 +const int32_t i32max = numeric_limits::max(); // 2147483647 // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(unsigned_int_type_test) silence_output(true); - + /// unsigned_int(uint32_t) - REQUIRE_EQUAL( unsigned_int{}.value, 0 ) - REQUIRE_EQUAL( unsigned_int{u32min}.value, 0 ) - REQUIRE_EQUAL( unsigned_int{u32max}.value, 18446744073709551615 ) + CHECK_EQUAL( unsigned_int{}.value, 0 ) + CHECK_EQUAL( unsigned_int{u32min}.value, 0 ) + CHECK_EQUAL( unsigned_int{u32max}.value, 4294967295 ) /// unsigned_int(T) - REQUIRE_EQUAL( unsigned_int{uint8_t{0}}.value, 0 ) - REQUIRE_EQUAL( unsigned_int{uint16_t{1}}.value, 1 ) - REQUIRE_EQUAL( unsigned_int{uint32_t{2}}.value, 2 ) - REQUIRE_EQUAL( unsigned_int{uint64_t{3}}.value, 3 ) + CHECK_EQUAL( unsigned_int{uint8_t{0}}.value, 0 ) + CHECK_EQUAL( unsigned_int{uint16_t{1}}.value, 1 ) + CHECK_EQUAL( unsigned_int{uint32_t{2}}.value, 2 ) + CHECK_EQUAL( unsigned_int{uint64_t{3}}.value, 3 ) // ----------------- // operator T()const - REQUIRE_EQUAL( unsigned_int{0}.operator bool(), false ) - REQUIRE_EQUAL( unsigned_int{1}.operator bool(), true ) + CHECK_EQUAL( unsigned_int{0}.operator bool(), false ) + CHECK_EQUAL( unsigned_int{1}.operator bool(), true ) // --------------------------------- // unsigned_int& operator=(uint32_t) @@ -43,101 +43,102 @@ EOSIO_TEST_BEGIN(unsigned_int_type_test) unsigned_int ui1{42}; ui0 = ui1; - REQUIRE_EQUAL( ui0 == ui1, true ) + CHECK_EQUAL( ui0 == ui1, true ) // ------------------------------------------------------------ // friend bool operator==(const unsigned_int&, const uint32_t&) - REQUIRE_EQUAL( unsigned_int{42} == uint32_t{42}, true ) - REQUIRE_EQUAL( unsigned_int{42} == uint32_t{43}, false ) + CHECK_EQUAL( unsigned_int{42} == uint32_t{42}, true ) + CHECK_EQUAL( unsigned_int{42} == uint32_t{43}, false ) // ------------------------------------------------------------ // friend bool operator==(const uint32_t&, const unsigned_int&) - REQUIRE_EQUAL( uint32_t{42} == unsigned_int{42}, true ) - REQUIRE_EQUAL( uint32_t{43} == unsigned_int{42}, false ) + CHECK_EQUAL( uint32_t{42} == unsigned_int{42}, true ) + CHECK_EQUAL( uint32_t{43} == unsigned_int{42}, false ) // ---------------------------------------------------------------- // friend bool operator==(const unsigned_int&, const unsigned_int&) - REQUIRE_EQUAL( unsigned_int{42} == unsigned_int{42}, true ) - REQUIRE_EQUAL( unsigned_int{42} == unsigned_int{43}, false ) + CHECK_EQUAL( unsigned_int{42} == unsigned_int{42}, true ) + CHECK_EQUAL( unsigned_int{42} == unsigned_int{43}, false ) // ------------------------------------------------------------ // friend bool operator!=(const unsigned_int&, const uint32_t&) - REQUIRE_EQUAL( unsigned_int{42} != uint32_t{42}, false ) - REQUIRE_EQUAL( unsigned_int{42} != uint32_t{43}, true ) + CHECK_EQUAL( unsigned_int{42} != uint32_t{42}, false ) + CHECK_EQUAL( unsigned_int{42} != uint32_t{43}, true ) // ------------------------------------------------------------ // friend bool operator!=(const uint32_t&, const unsigned_int&) - REQUIRE_EQUAL( uint32_t{42} != unsigned_int{42}, false ) - REQUIRE_EQUAL( uint32_t{43} != unsigned_int{42}, true ) + CHECK_EQUAL( uint32_t{42} != unsigned_int{42}, false ) + CHECK_EQUAL( uint32_t{43} != unsigned_int{42}, true ) // ---------------------------------------------------------------- // friend bool operator!=(const unsigned_int&, const unsigned_int&) - REQUIRE_EQUAL( unsigned_int{42} != unsigned_int{42}, false ) - REQUIRE_EQUAL( unsigned_int{42} != unsigned_int{43}, true ) + CHECK_EQUAL( unsigned_int{42} != unsigned_int{42}, false ) + CHECK_EQUAL( unsigned_int{42} != unsigned_int{43}, true ) // ------------------------------------------------------------ // friend bool operator< (const unsigned_int&, const uint32_t&) - REQUIRE_EQUAL( unsigned_int{42} < uint32_t{42}, false ) - REQUIRE_EQUAL( unsigned_int{42} < uint32_t{43}, true ) + CHECK_EQUAL( unsigned_int{42} < uint32_t{42}, false ) + CHECK_EQUAL( unsigned_int{42} < uint32_t{43}, true ) // ------------------------------------------------------------ // friend bool operator< (const uint32_t&, const unsigned_int&) - REQUIRE_EQUAL( uint32_t{42} < unsigned_int{42}, false ) - REQUIRE_EQUAL( uint32_t{42} < unsigned_int{43}, true ) + CHECK_EQUAL( uint32_t{42} < unsigned_int{42}, false ) + CHECK_EQUAL( uint32_t{42} < unsigned_int{43}, true ) // ---------------------------------------------------------------- // friend bool operator< (const unsigned_int&, const unsigned_int&) - REQUIRE_EQUAL( unsigned_int{42} < unsigned_int{42}, false ) - REQUIRE_EQUAL( unsigned_int{42} < unsigned_int{43}, true ) + CHECK_EQUAL( unsigned_int{42} < unsigned_int{42}, false ) + CHECK_EQUAL( unsigned_int{42} < unsigned_int{43}, true ) // ------------------------------------------------------------ // friend bool operator>=(const unsigned_int&, const uint32_t&) - REQUIRE_EQUAL( unsigned_int{42} >= uint32_t{42}, true ) - REQUIRE_EQUAL( unsigned_int{42} >= uint32_t{43}, false ) + CHECK_EQUAL( unsigned_int{42} >= uint32_t{42}, true ) + CHECK_EQUAL( unsigned_int{42} >= uint32_t{43}, false ) // ------------------------------------------------------------ // friend bool operator>=(const uint32_t&, const unsigned_int&) - REQUIRE_EQUAL( uint32_t{42} >= unsigned_int{42}, true ) - REQUIRE_EQUAL( uint32_t{42} >= unsigned_int{43}, false ) + CHECK_EQUAL( uint32_t{42} >= unsigned_int{42}, true ) + CHECK_EQUAL( uint32_t{42} >= unsigned_int{43}, false ) // ---------------------------------------------------------------- // friend bool operator>=(const unsigned_int&, const unsigned_int&) - REQUIRE_EQUAL( unsigned_int{42} >= unsigned_int{42}, true ) - REQUIRE_EQUAL( unsigned_int{42} >= unsigned_int{43}, false ) + CHECK_EQUAL( unsigned_int{42} >= unsigned_int{42}, true ) + CHECK_EQUAL( unsigned_int{42} >= unsigned_int{43}, false ) // --------------------------------------------------------------- // friend DataStream& operator<<(DataStream&, const unsigned_int&) // friend DataStream& operator>>(DataStream&, unsigned_int&) - char buffer[8]{}; - datastream ds{buffer, 8}; + static constexpr uint16_t buffer_size{256}; + char datastream_buffer[buffer_size]; // Buffer for the datastream to point to + + datastream ds{datastream_buffer, buffer_size}; const unsigned_int cui{42}; unsigned_int ui{}; ds << cui; ds.seekp(0); ds >> ui; - CHECK_EQUAL( cui == ui, true) - + silence_output(false); EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(signed_int_type_test) - silence_output(false); + silence_output(true); /// signed_int(uint32_t) - REQUIRE_EQUAL( signed_int{}.value, 0 ) - REQUIRE_EQUAL( signed_int{i32min}.value, -9223372036854775808 ) - REQUIRE_EQUAL( signed_int{i32max}.value, 9223372036854775807 ) + CHECK_EQUAL( signed_int{}.value, 0 ) + CHECK_EQUAL( signed_int{i32min}.value, -2147483648 ) + CHECK_EQUAL( signed_int{i32max}.value, 2147483647 ) // ----------------------- // operator int32_t()const - REQUIRE_EQUAL( signed_int{}.operator int32_t(), 0 ) - REQUIRE_EQUAL( signed_int{i32min}.operator int32_t(), -9223372036854775808 ) - REQUIRE_EQUAL( signed_int{i32max}.operator int32_t(), 9223372036854775807 ) + CHECK_EQUAL( signed_int{}.operator int32_t(), 0 ) + CHECK_EQUAL( signed_int{i32min}.operator int32_t(), -2147483648 ) + CHECK_EQUAL( signed_int{i32max}.operator int32_t(), 2147483647 ) - // --------------------- + // -------------------------------- // signed_int& operator=(const T&) signed_int i0{}; signed_int i1{}; @@ -154,124 +155,105 @@ EOSIO_TEST_BEGIN(signed_int_type_test) i2 = i32; i3 = i64; - REQUIRE_EQUAL( i0.value, 0 ) - REQUIRE_EQUAL( i1.value, 1 ) - REQUIRE_EQUAL( i2.value, 2 ) - REQUIRE_EQUAL( i3.value, 3 ) + CHECK_EQUAL( i0.value, 0 ) + CHECK_EQUAL( i1.value, 1 ) + CHECK_EQUAL( i2.value, 2 ) + CHECK_EQUAL( i3.value, 3 ) // -------------------------- // signed_int operator++(int) signed_int post_inc0{0}; signed_int post_inc1{1}; - REQUIRE_EQUAL( post_inc0++.value, 0 ) - REQUIRE_EQUAL( post_inc1++.value, 1 ) + CHECK_EQUAL( post_inc0++.value, 0 ) + CHECK_EQUAL( post_inc1++.value, 1 ) // ------------------------ // signed_int& operator++() signed_int pre_inc0{0}; signed_int pre_inc1{1}; - REQUIRE_EQUAL( ++pre_inc0.value, 1 ) - REQUIRE_EQUAL( ++pre_inc1.value, 2 ) + CHECK_EQUAL( ++pre_inc0.value, 1 ) + CHECK_EQUAL( ++pre_inc1.value, 2 ) // ------------------------------------------------------------ // friend bool operator==(const signed_int&, const uint32_t&) - REQUIRE_EQUAL( signed_int{42} == int32_t{42}, true ) - REQUIRE_EQUAL( signed_int{42} == int32_t{43}, false ) + CHECK_EQUAL( signed_int{42} == int32_t{42}, true ) + CHECK_EQUAL( signed_int{42} == int32_t{43}, false ) - // ------------------------------------------------------------ + // ---------------------------------------------------------- // friend bool operator==(const uint32_t&, const signed_int&) - REQUIRE_EQUAL( int32_t{42} == signed_int{42}, true ) - REQUIRE_EQUAL( int32_t{43} == signed_int{42}, false ) + CHECK_EQUAL( int32_t{42} == signed_int{42}, true ) + CHECK_EQUAL( int32_t{43} == signed_int{42}, false ) - // ---------------------------------------------------------------- + // ------------------------------------------------------------ // friend bool operator==(const signed_int&, const signed_int&) - REQUIRE_EQUAL( signed_int{42} == signed_int{42}, true ) - REQUIRE_EQUAL( signed_int{42} == signed_int{43}, false ) + CHECK_EQUAL( signed_int{42} == signed_int{42}, true ) + CHECK_EQUAL( signed_int{42} == signed_int{43}, false ) - // ------------------------------------------------------------ + // ---------------------------------------------------------- // friend bool operator!=(const signed_int&, const uint32_t&) - REQUIRE_EQUAL( signed_int{42} != int32_t{42}, false ) - REQUIRE_EQUAL( signed_int{42} != int32_t{43}, true ) + CHECK_EQUAL( signed_int{42} != int32_t{42}, false ) + CHECK_EQUAL( signed_int{42} != int32_t{43}, true ) - // ------------------------------------------------------------ + // ---------------------------------------------------------- // friend bool operator!=(const uint32_t&, const signed_int&) - REQUIRE_EQUAL( int32_t{42} != signed_int{42}, false ) - REQUIRE_EQUAL( int32_t{43} != signed_int{42}, true ) + CHECK_EQUAL( int32_t{42} != signed_int{42}, false ) + CHECK_EQUAL( int32_t{43} != signed_int{42}, true ) - // ---------------------------------------------------------------- + // ------------------------------------------------------------ // friend bool operator!=(const signed_int&, const signed_int&) - REQUIRE_EQUAL( signed_int{42} != signed_int{42}, false ) - REQUIRE_EQUAL( signed_int{42} != signed_int{43}, true ) + CHECK_EQUAL( signed_int{42} != signed_int{42}, false ) + CHECK_EQUAL( signed_int{42} != signed_int{43}, true ) - // ------------------------------------------------------------ + // ---------------------------------------------------------- // friend bool operator< (const signed_int&, const uint32_t&) - REQUIRE_EQUAL( signed_int{42} < int32_t{42}, false ) - REQUIRE_EQUAL( signed_int{42} < int32_t{43}, true ) + CHECK_EQUAL( signed_int{42} < int32_t{42}, false ) + CHECK_EQUAL( signed_int{42} < int32_t{43}, true ) - // ------------------------------------------------------------ + // ---------------------------------------------------------- // friend bool operator< (const uint32_t&, const signed_int&) - REQUIRE_EQUAL( int32_t{42} < signed_int{42}, false ) - REQUIRE_EQUAL( int32_t{42} < signed_int{43}, true ) + CHECK_EQUAL( int32_t{42} < signed_int{42}, false ) + CHECK_EQUAL( int32_t{42} < signed_int{43}, true ) - // ---------------------------------------------------------------- + // ------------------------------------------------------------ // friend bool operator< (const signed_int&, const signed_int&) - REQUIRE_EQUAL( signed_int{42} < signed_int{42}, false ) - REQUIRE_EQUAL( signed_int{42} < signed_int{43}, true ) + CHECK_EQUAL( signed_int{42} < signed_int{42}, false ) + CHECK_EQUAL( signed_int{42} < signed_int{43}, true ) - // ------------------------------------------------------------ + // ---------------------------------------------------------- // friend bool operator>=(const signed_int&, const uint32_t&) - REQUIRE_EQUAL( signed_int{42} >= int32_t{42}, true ) - REQUIRE_EQUAL( signed_int{42} >= int32_t{43}, false ) + CHECK_EQUAL( signed_int{42} >= int32_t{42}, true ) + CHECK_EQUAL( signed_int{42} >= int32_t{43}, false ) - // ------------------------------------------------------------ + // ---------------------------------------------------------- // friend bool operator>=(const uint32_t&, const signed_int&) - REQUIRE_EQUAL( int32_t{42} >= signed_int{42}, true ) - REQUIRE_EQUAL( int32_t{42} >= signed_int{43}, false ) + CHECK_EQUAL( int32_t{42} >= signed_int{42}, true ) + CHECK_EQUAL( int32_t{42} >= signed_int{43}, false ) - // ---------------------------------------------------------------- + // ------------------------------------------------------------ // friend bool operator>=(const signed_int&, const signed_int&) - REQUIRE_EQUAL( signed_int{42} >= signed_int{42}, true ) - REQUIRE_EQUAL( signed_int{42} >= signed_int{43}, false ) - - // ----------------------------- - // friend DataStream& operator<<(DataStream&, const signed_int&) - const signed_int ci{0x7F}; - - char buffer0[10]{}; - datastream ds0{buffer0, 10}; - -eosio::print(ci.value, "\n"); - ds0 << ci; -eosio::print(static_cast(buffer0[0]), " "); -eosio::print(static_cast(buffer0[1]), "\n"); - signed_int temp{}; - ds0 >> temp; -eosio::print(static_cast(buffer0[0]), " "); -eosio::print(static_cast(buffer0[1]), "\n"); -eosio::print(temp.value, "\n"); - - // REQUIRE_EQUAL( ci == temp, true ) - - // REQUIRE_EQUAL( memcmp(expected_buffer0, buffer0, 2), 0 ) + CHECK_EQUAL( signed_int{42} >= signed_int{42}, true ) + CHECK_EQUAL( signed_int{42} >= signed_int{43}, false ) - // ----------------------------- - // template + // ---------------------------------------------------------------- + // friend DataStream& operator<<(DataStream& ds, const signed_int&) // friend DataStream& operator>>(DataStream&, signed_int&) - signed_int i{}; - const char expected_buffer1[1]{0x7F}; - - char buffer1[1]{0x7F}; - datastream ds1{buffer1, 1}; + static constexpr uint16_t buffer_size{256}; + char datastream_buffer[buffer_size]; // Buffer for the datastream to point to - ds1 >> i; + datastream ds{datastream_buffer, buffer_size}; - REQUIRE_EQUAL( memcmp(expected_buffer1, buffer1, 1), 0 ) + const signed_int csi{-42}; + signed_int si{}; + ds << csi; + ds.seekp(0); + ds >> si; + CHECK_EQUAL( csi == si, true) silence_output(false); EOSIO_TEST_END int main(int argc, char* argv[]) { - // EOSIO_TEST(unsigned_int_type_test) + EOSIO_TEST(unsigned_int_type_test) EOSIO_TEST(signed_int_type_test); return has_failed(); } From c717257b683e20eb8f9ca7ca56821b3a75896ded Mon Sep 17 00:00:00 2001 From: johndebord Date: Fri, 15 Feb 2019 19:05:38 -0500 Subject: [PATCH 42/51] Continuation of `fixed_bytes_tests.cpp` --- tests/unit/datastream_tests.cpp | 2 + tests/unit/fixed_bytes_tests.cpp | 280 +++++++++++++++---------------- 2 files changed, 135 insertions(+), 147 deletions(-) diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index b8f8781bbf..cd06e300ea 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -14,6 +14,8 @@ // TODO: Make the appropriate variabels const and/or static // TODO: Make sure to adjust the correct min max values // TODO: Make sure all output is on it's appropriate value +// TODO: Make sure it is readable for all future people (and myself) reading the code +// TODO: Organize functions in the most logical order, even if it's not the order in which they are defined // Is this a good convention? // microseconds ms0{ 0LL}; // microseconds ms1{ 1LL}; diff --git a/tests/unit/fixed_bytes_tests.cpp b/tests/unit/fixed_bytes_tests.cpp index 1dc23a538a..c55a449d8c 100644 --- a/tests/unit/fixed_bytes_tests.cpp +++ b/tests/unit/fixed_bytes_tests.cpp @@ -3,13 +3,14 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include + #include #include -using eosio::fixed_bytes; using std::array; -using namespace eosio::native; +using eosio::fixed_bytes; // Definitions in `eosio.cdt/libraries/eosiolib/fixed_bytes.hpp` EOSIO_TEST_BEGIN(fixed_bytes_test) @@ -17,57 +18,40 @@ EOSIO_TEST_BEGIN(fixed_bytes_test) /// constexpr fixed_bytes() // static constexpr size_t padded_bytes() -REQUIRE_EQUAL( fixed_bytes<20>{}.padded_bytes(), 12 ) -REQUIRE_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) -REQUIRE_EQUAL( fixed_bytes<64>{}.padded_bytes(), 0 ) - -// eosio::print(fixed_bytes<20>{}.num_words()); -// eosio::print(fixed_bytes<32>{}.num_words()); -// eosio::print(fixed_bytes<64>{}.num_words(), "\n"); - -// eosio::print(fixed_bytes<20>{array{}}.num_words()); -// eosio::print(fixed_bytes<32>{array{}}.num_words()); -// eosio::print(fixed_bytes<64>{array{}}.num_words(), "\n"); +CHECK_EQUAL( fixed_bytes<20>{}.padded_bytes(), 12 ) +CHECK_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) +CHECK_EQUAL( fixed_bytes<64>{}.padded_bytes(), 0 ) /// fixed_bytes(const std::array&) // static constexpr size_t num_words() -REQUIRE_EQUAL( (fixed_bytes<20>{array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32>{array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64>{array{}}.num_words()), 4 ) - -// /// fixed_bytes(const std::array&) -REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -REQUIRE_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -// eosio::print(fixed_bytes<20>{std::array{}}.num_words()); -// eosio::print(fixed_bytes<32>{std::array{}}.num_words()); -// eosio::print(fixed_bytes<64>{std::array{}}.num_words()); -// eosio::print(fixed_bytes<128>{std::array{}}.num_words()); -// eosio::print(fixed_bytes<256>{std::array{}}.num_words()); -// REQUIRE_EQUAL( (fixed_bytes<32>{std::array{}}.num_words()), 1 ) -// REQUIRE_EQUAL( (fixed_bytes<64>{std::array{}}.num_words()), 1 ) -// REQUIRE_EQUAL( (fixed_bytes<128>{std::array{}}.num_words()), 1 ) +CHECK_EQUAL( (fixed_bytes<20>{array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32>{array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64>{array{}}.num_words()), 4 ) + +/// fixed_bytes(const std::array&) +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) /// fixed_bytes(const Word(&arr)[NumWords]) const uint8_t arr8[1]{}; @@ -75,51 +59,50 @@ const uint16_t arr16[1]{}; const uint32_t arr32[1]{}; const uint64_t arr64[1]{}; -REQUIRE_EQUAL( (fixed_bytes<20> {arr8}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {arr8}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {arr8}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{arr8}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{arr8}.num_words()), 16 ) - -REQUIRE_EQUAL( (fixed_bytes<20> {arr16}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {arr16}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {arr16}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{arr16}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{arr16}.num_words()), 16 ) - -REQUIRE_EQUAL( (fixed_bytes<20> {arr32}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {arr32}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {arr32}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{arr32}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{arr32}.num_words()), 16 ) - -REQUIRE_EQUAL( (fixed_bytes<20> {arr64}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<32> {arr64}.num_words()), 2 ) -REQUIRE_EQUAL( (fixed_bytes<64> {arr64}.num_words()), 4 ) -REQUIRE_EQUAL( (fixed_bytes<128>{arr64}.num_words()), 8 ) -REQUIRE_EQUAL( (fixed_bytes<256>{arr64}.num_words()), 16 ) - +CHECK_EQUAL( (fixed_bytes<20> {arr8}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {arr8}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {arr8}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{arr8}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{arr8}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {arr16}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {arr16}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {arr16}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{arr16}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{arr16}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {arr32}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {arr32}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {arr32}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{arr32}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{arr32}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {arr64}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {arr64}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {arr64}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{arr64}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{arr64}.num_words()), 16 ) + +// ------------------------------------------- // make_from_word_sequence(FirstWord, Rest...) array arr{1ULL,2ULL,3ULL,4ULL}; fixed_bytes<32> fb1{fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)}; fixed_bytes<32> fb2{arr}; -REQUIRE_EQUAL( (fixed_bytes<20>::make_from_word_sequence(1ULL)), - (fixed_bytes<20>{array{1ULL}}) ) -REQUIRE_EQUAL( (fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)), - (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}) ) +CHECK_EQUAL( (fixed_bytes<20>::make_from_word_sequence(1ULL, 2ULL)), + (fixed_bytes<20>{array{1ULL, 2ULL}}) ) -// // const auto& get_array()const -// REQUIRE_EQUAL( (fixed_bytes<0>{}.get_array()), (std::array{}) ) -// REQUIRE_EQUAL( (fixed_bytes<1>{}.get_array()), (std::array{}) ) -// REQUIRE_EQUAL( (fixed_bytes<32>{}.get_array()), (std::array{}) ) -// REQUIRE_EQUAL( (fixed_bytes<512>{}.get_array()), (std::array{}) ) +CHECK_EQUAL( (fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)), + (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}) ) -// eosio::print((fixed_bytes<2>{1}.data())); -// eosio::print("\n"); -// eosio::print(*((uint128_t*){})); -// eosio::print("\n"); +// ---------------------------- +// const auto& get_array()const +// CHECK_EQUAL( (fixed_bytes<0>{}.get_array()), (std::array{}) ) +// CHECK_EQUAL( (fixed_bytes<1>{}.get_array()), (std::array{}) ) +// CHECK_EQUAL( (fixed_bytes<32>{}.get_array()), (std::array{}) ) +// CHECK_EQUAL( (fixed_bytes<512>{}.get_array()), (std::array{}) ) +// ----------- // auto data() // std::array arr0{}; // std::array arr1{}; @@ -130,71 +113,74 @@ REQUIRE_EQUAL( (fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3UL // fixed_bytes<32> fb2{arr2}; // fixed_bytes<512> fb3{arr3}; -// REQUIRE_EQUAL( fb0.data(), arr0.data() ) -// REQUIRE_EQUAL( (fixed_bytes<0>{}.data()), uint128_t*(std::array{0}) ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.data(), 1 ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.data(), 2 ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.data(), 32 ) - -// // auto data()const -// REQUIRE_EQUAL( fixed_bytes<0>{}.data(), 0 ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.data(), 1 ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.data(), 2 ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.data(), 32 ) - -// // auto size()const -// REQUIRE_EQUAL( fixed_bytes<0>{}.size(), 0 ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.size(), 1 ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.size(), 2 ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.size(), 32 ) - -// // std::array extract_as_byte_array()const -// REQUIRE_EQUAL( fixed_bytes<0>{}.extract_as_byte_array(), 0 ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.extract_as_byte_array(), 1 ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.extract_as_byte_array(), 2 ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.extract_as_byte_array(), 32 ) - -// // template -// // bool operator==(const fixed_bytes, const fixed_bytes) -// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) - -// // template +// CHECK_EQUAL( fb0.data(), arr0.data() ) +// CHECK_EQUAL( (fixed_bytes<0>{}.data()), uint128_t*(std::array{0}) ) +// CHECK_EQUAL( fixed_bytes<1>{}.data(), 1 ) +// CHECK_EQUAL( fixed_bytes<32>{}.data(), 2 ) +// CHECK_EQUAL( fixed_bytes<512>{}.data(), 32 ) + +// ---------------- +// auto data()const +// CHECK_EQUAL( fixed_bytes<0>{}.data(), 0 ) +// CHECK_EQUAL( fixed_bytes<1>{}.data(), 1 ) +// CHECK_EQUAL( fixed_bytes<32>{}.data(), 2 ) +// CHECK_EQUAL( fixed_bytes<512>{}.data(), 32 ) + +// ---------------- +// auto size()const +// CHECK_EQUAL( fixed_bytes<0>{}.size(), 0 ) +// CHECK_EQUAL( fixed_bytes<1>{}.size(), 1 ) +// CHECK_EQUAL( fixed_bytes<32>{}.size(), 2 ) +// CHECK_EQUAL( fixed_bytes<512>{}.size(), 32 ) + +// ------------------------------------------------------ +// std::array extract_as_byte_array()const +// CHECK_EQUAL( fixed_bytes<0>{}.extract_as_byte_array(), 0 ) +// CHECK_EQUAL( fixed_bytes<1>{}.extract_as_byte_array(), 1 ) +// CHECK_EQUAL( fixed_bytes<32>{}.extract_as_byte_array(), 2 ) +// CHECK_EQUAL( fixed_bytes<512>{}.extract_as_byte_array(), 32 ) + +// ---------- +// template<> +// const fixed_bytes +// bool operator==(const fixed_bytes, const fixed_bytes) +// CHECK_EQUAL( fixed_bytes<1>{1ULL} == fixed_bytes<1>{1ULL}, true ) +// CHECK_EQUAL( fixed_bytes<1>{} == fixed_bytes<1>{}, false ) + +// // ---------- +// // template<> +// // const fixed_bytes // // bool operator!=(const fixed_bytes, const fixed_bytes) -// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) +// CHECK_EQUAL( fixed_bytes<1>{} != fixed_bytes<1>{}, true ) +// CHECK_EQUAL( fixed_bytes<1>{} != fixed_bytes<1>{}, false ) -// // template -// // bool operator>(const fixed_bytes, const fixed_bytes) -// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) - -// // template +// // ---------- +// // template<> +// // const fixed_bytes // // bool operator<(const fixed_bytes, const fixed_bytes) -// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) - -// // template -// // bool operator>=(const fixed_bytes, const fixed_bytes) -// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) +// CHECK_EQUAL( fixed_bytes<1>{} < fixed_bytes<1>{}, true ) +// CHECK_EQUAL( fixed_bytes<1>{} < fixed_bytes<1>{}, false ) -// // template +// // ---------- +// // template<> +// // const fixed_bytes // // bool operator<=(const fixed_bytes, const fixed_bytes) -// REQUIRE_EQUAL( fixed_bytes<0>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<1>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<32>{}.num_words(), true ) -// REQUIRE_EQUAL( fixed_bytes<512>{}.num_words(), true ) +// CHECK_EQUAL( fixed_bytes<1>{} <= fixed_bytes<1>{}, true ) +// CHECK_EQUAL( fixed_bytes<1>{} <= fixed_bytes<1>{}, false ) + +// // ---------- +// // template<> +// // const fixed_bytes +// // bool operator>(const fixed_bytes, const fixed_bytes) +// CHECK_EQUAL( fixed_bytes<1>{} > fixed_bytes<1>{}, true ) +// CHECK_EQUAL( fixed_bytes<1>{} > fixed_bytes<1>{}, false ) + +// // ---------- +// // template<> +// // const fixed_bytes +// // bool operator>=(const fixed_bytes, const fixed_bytes) +// CHECK_EQUAL( fixed_bytes<1>{} >= fixed_bytes<1>{}, true ) +// CHECK_EQUAL( fixed_bytes<1>{} >= fixed_bytes<1>{}, false ) silence_output(false); EOSIO_TEST_END From 61df4168a1a52f76a30307a5a0901d97e3b85d5d Mon Sep 17 00:00:00 2001 From: johndebord Date: Tue, 19 Feb 2019 02:24:07 -0500 Subject: [PATCH 43/51] Finish tests and get them production ready --- tests/CMakeLists.txt | 20 +- tests/unit/CMakeLists.txt | 36 +++- tests/unit/asset_tests.cpp | 186 ++++++++++------- tests/unit/binary_extension_tests.cpp | 263 ++++++++++++------------ tests/unit/crypto_tests.cpp | 16 +- tests/unit/datastream_tests.cpp | 203 +++++++++---------- tests/unit/fixed_bytes_tests.cpp | 280 +++++++++++++------------- tests/unit/name_tests.cpp | 114 +++++------ tests/unit/rope_tests.cpp | 74 +++++++ tests/unit/serialize_tests.cpp | 12 +- tests/unit/symbol_tests.cpp | 63 +++--- tests/unit/system_tests.cpp | 22 +- tests/unit/time_tests.cpp | 70 +++---- tests/unit/varint_tests.cpp | 84 ++++---- 14 files changed, 788 insertions(+), 655 deletions(-) create mode 100644 tests/unit/rope_tests.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2a4aa692ca..b72f132859 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,8 +1,14 @@ -set(unit_test_dir ${CMAKE_BINARY_DIR}/EosioWasmTests-prefix/src/EosioWasmTests-build) +set( unit_test_dir ${CMAKE_BINARY_DIR}/EosioWasmTests-prefix/src/EosioWasmTests-build ) -add_test(asset_tests ${unit_test_dir}/asset_tests) -add_test(name_tests ${unit_test_dir}/name_tests) -add_test(symbol_tests ${unit_test_dir}/symbol_tests) -add_test(system_tests ${unit_test_dir}/system_tests) -add_test(time_tests ${unit_test_dir}/time_tests) -add_test(varint_tests ${unit_test_dir}/varint_tests) +add_test( asset_tests ${unit_test_dir}/asset_tests ) +add_test( binary_extension_tests ${unit_test_dir}/binary_extension_tests ) +add_test( crypto_tests ${unit_test_dir}/crypto_tests ) +add_test( datastream_tests ${unit_test_dir}/datastream_tests ) +add_test( fixed_bytes_tests ${unit_test_dir}/fixed_bytes_tests ) +add_test( name_tests ${unit_test_dir}/name_tests ) +add_test( rope_tests ${unit_test_dir}/rope_tests ) +add_test( serialize_tests ${unit_test_dir}/serialize_tests ) +add_test( symbol_tests ${unit_test_dir}/symbol_tests ) +add_test( system_tests ${unit_test_dir}/system_tests ) +add_test( time_tests ${unit_test_dir}/time_tests ) +add_test( varint_tests ${unit_test_dir}/varint_tests ) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index e104a31f42..82c2ae9df2 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -1,12 +1,28 @@ -list(APPEND CMAKE_MODULE_PATH ${EOSIO_CDT_BIN}) -include(EosioCDTMacros) +list( APPEND CMAKE_MODULE_PATH ${EOSIO_CDT_BIN} ) +include( EosioCDTMacros ) -add_native_executable(asset_tests asset_tests.cpp) -add_native_executable(name_tests name_tests.cpp) -add_native_executable(symbol_tests symbol_tests.cpp) -add_native_executable(system_tests system_tests.cpp) -add_native_executable(time_tests time_tests.cpp) -add_native_executable(varint_tests varint_tests.cpp) +add_native_executable( asset_tests asset_tests.cpp ) +add_native_executable( binary_extension_tests binary_extension_tests.cpp ) +add_native_executable( crypto_tests crypto_tests.cpp ) +add_native_executable( datastream_tests datastream_tests.cpp ) +add_native_executable( fixed_bytes_tests fixed_bytes_tests.cpp ) +add_native_executable( name_tests name_tests.cpp ) +add_native_executable( rope_tests rope_tests.cpp ) +add_native_executable( serialize_tests serialize_tests.cpp ) +add_native_executable( symbol_tests symbol_tests.cpp ) +add_native_executable( system_tests system_tests.cpp ) +add_native_executable( time_tests time_tests.cpp ) +add_native_executable( varint_tests varint_tests.cpp ) -add_dependencies(name_tests EosioTools) -add_dependencies(system_tests EosioTools) +add_dependencies( name_tests EosioTools ) +add_dependencies( binary_extension_tests EosioTools ) +add_dependencies( crypto_tests EosioTools ) +add_dependencies( datastream_tests EosioTools ) +add_dependencies( fixed_bytes_tests EosioTools ) +add_dependencies( name_tests EosioTools ) +add_dependencies( rope_tests EosioTools ) +add_dependencies( serialize_tests EosioTools ) +add_dependencies( symbol_tests EosioTools ) +add_dependencies( system_tests EosioTools ) +add_dependencies( time_tests EosioTools ) +add_dependencies( varint_tests EosioTools ) diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 5e3527202b..12ba492344 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -3,29 +3,33 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include + #include #include +using std::string; + using eosio::name; using eosio::symbol; using eosio::extended_symbol; using eosio::asset; using eosio::extended_asset; -const int64_t asset_mask{(1LL << 62) - 1}; -const int64_t asset_min{-asset_mask}; // -4611686018427387903 -const int64_t asset_max{ asset_mask}; // 4611686018427387903 +static constexpr int64_t asset_mask{(1LL << 62) - 1}; +static constexpr int64_t asset_min{-asset_mask}; // -4611686018427387903 +static constexpr int64_t asset_max{ asset_mask}; // 4611686018427387903 // Definitions in `eosio.cdt/libraries/eosiolib/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) silence_output(true); - symbol s0{"A",0}; - symbol s1{"Z",0}; - symbol s2{"AAAAAAA",0}; - symbol s3{"ZZZZZZZ",0}; - symbol sym_no_prec{"SYMBOLL",0}; // Symbol with no precision - symbol sym_prec{"SYMBOLL",63}; // Symbol with precision + static constexpr symbol s0{"A", 0}; + static constexpr symbol s1{"Z", 0}; + static constexpr symbol s2{"AAAAAAA", 0}; + static constexpr symbol s3{"ZZZZZZZ", 0}; + static constexpr symbol sym_no_prec{"SYMBOLL", 0}; // Symbol with no precision + static constexpr symbol sym_prec{"SYMBOLL", 63}; // Symbol with precision //// constexpr asset() CHECK_EQUAL( asset{}.amount, 0ULL ) @@ -43,10 +47,9 @@ EOSIO_TEST_BEGIN(asset_type_test) // Note: there is an invariant established for `asset` that is not enforced for `symbol` // For example: - // valid code: - // `symbol{};` - // throws "invalid symbol name": - // `asset{{}, symbol{}};` + // `symbol{};` // valid code + // `asset{{}, symbol{}};` // throws "invalid symbol name" + CHECK_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{0LL}};}) ) CHECK_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{1LL}};}) ) CHECK_ASSERT( "invalid symbol name", ([]() {asset{0LL, symbol{16639ULL}};}) ) @@ -87,70 +90,103 @@ EOSIO_TEST_BEGIN(asset_type_test) // ------------------------ // void set_amount(int64_t) - asset a{0LL, sym_no_prec}; - CHECK_EQUAL( (a.set_amount(0LL), a.amount), 0LL ) - CHECK_EQUAL( (a.set_amount(1LL), a.amount), 1LL ) - CHECK_EQUAL( (a.set_amount(asset_min), a.amount), asset_min ) - CHECK_EQUAL( (a.set_amount(asset_max), a.amount), asset_max ) + asset asset_set_amount{0LL, sym_no_prec}; + CHECK_EQUAL( (asset_set_amount.set_amount(0LL), asset_set_amount.amount), 0LL ) + CHECK_EQUAL( (asset_set_amount.set_amount(1LL), asset_set_amount.amount), 1LL ) + CHECK_EQUAL( (asset_set_amount.set_amount(asset_min), asset_set_amount.amount), asset_min ) + CHECK_EQUAL( (asset_set_amount.set_amount(asset_max), asset_set_amount.amount), asset_max ) CHECK_ASSERT( "magnitude of asset amount must be less than 2^62", ( [&]() { - a.set_amount(asset_min - 1); + asset_set_amount.set_amount(asset_min - 1); }) ) CHECK_ASSERT( "magnitude of asset amount must be less than 2^62", ( [&]() { - a.set_amount(asset_max + 1); + asset_set_amount.set_amount(asset_max + 1); }) ) // --------------------- // std::to_string()const - // Note that printing an `asset` is limited to a precision of 63 + // Note: + // Printing an `asset` is limited to a precision of 63 // This will trigger an error: - // `asset{int64_t{1LL}, symbol{"SYMBOLL", 64}}.print();` - // output: "Floating point exception: ..." + // `asset{int64_t{1LL}, symbol{"SYMBOLL", 64}}.print();` // output: "Floating point exception: ..." - // This produces weird behavior when the `precision` is 0: - // `asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.print();` - // output: "1. SYMBOLL" + // Note: + // This produces odd behavior when the `precision` is 0: + // `asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.print();` // output: "1. SYMBOLL" - // Note that `assets` with negative amounts with 0 `precision` print have the odd behavior of printing two '-' characters: + // Note: + // That `assets` with negative amounts with 0 `precision` print two '-' characters + CHECK_EQUAL( (asset{0LL, sym_no_prec}.to_string()), "0. SYMBOLL" ) CHECK_EQUAL( (asset{-0LL, sym_no_prec}.to_string()), "0. SYMBOLL" ) - CHECK_EQUAL( (asset{0LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) - CHECK_EQUAL( (asset{-0LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) + CHECK_EQUAL( (asset{0LL, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) + CHECK_EQUAL( (asset{-0LL, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) CHECK_EQUAL( (asset{1LL, sym_no_prec}.to_string()), "1. SYMBOLL" ) CHECK_EQUAL( (asset{-1LL, sym_no_prec}.to_string()), "--1. SYMBOLL" ) - CHECK_EQUAL( (asset{1LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) - CHECK_EQUAL( (asset{-1LL, sym_prec}.to_string()), "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) + CHECK_EQUAL( (asset{1LL, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) + CHECK_EQUAL( (asset{-1LL, sym_prec}.to_string()), + "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) CHECK_EQUAL( (asset{asset_min, sym_no_prec}.to_string()), "--4611686018427387903. SYMBOLL" ) CHECK_EQUAL( (asset{asset_max, sym_no_prec}.to_string()), "4611686018427387903. SYMBOLL" ) - CHECK_EQUAL( (asset{asset_min, sym_prec}.to_string()), "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) - CHECK_EQUAL( (asset{asset_max, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) + CHECK_EQUAL( (asset{asset_min, sym_prec}.to_string()), + "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) + CHECK_EQUAL( (asset{asset_max, sym_prec}.to_string()), + "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) - // Note: uncomment once print checking has been resolved // ----------------- // void print()const + // Note: + // Uncomment once print checking has been resolved + // CHECK_PRINT( "0. SYMBOLL", [&](){asset{0LL, sym_no_prec}.print();} ); // CHECK_PRINT( "0. SYMBOLL", [&](){asset{-0LL, sym_no_prec}.print();} ); - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", [&](){asset{0LL, sym_prec}.print();} ); - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", [&](){asset{-0LL, sym_prec}.print();} ); + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( + // [&]() { + // asset{0LL, sym_prec}.print(); + // }); + // ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( + // [&]() { + // asset{-0LL, sym_prec}.print(); + // }); + // ) // CHECK_PRINT( "1. SYMBOLL", [&](){asset{1LL, sym_no_prec}.print();} ); // CHECK_PRINT( "--1. SYMBOLL", [&](){asset{-1LL, sym_no_prec}.print();} ); - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", [&](){asset{1LL, sym_prec}.print();} ); - // CHECK_PRINT( "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", [&](){asset{-1LL, sym_prec}.print();} ); + // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( + // [&]() { + // asset{1LL, sym_prec}.print(); + // }); + // ) + // CHECK_PRINT( "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( + // [&]() { + // asset{-1LL, sym_prec}.print(); + // }); + // ) // CHECK_PRINT( "--4611686018427387903. SYMBOLL", [&](){asset{asset_min, sym_no_prec}.print();} ); // CHECK_PRINT( "4611686018427387903. SYMBOLL", [&](){asset{asset_max, sym_no_prec}.print();} ); - // CHECK_PRINT( "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", [&](){asset{asset_min, sym_prec}.print();} ); - // CHECK_PRINT( "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", [&](){asset{asset_max, sym_prec}.print();} ); + // CHECK_PRINT( "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( + // [&]() { + // asset{asset_min, sym_prec}.print(); + // }); + // ) + // CHECK_PRINT( "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( + // [&]() { + // asset{asset_max, sym_prec}.print(); + // }); + // ) - // How are these `assets` constructed? Isn't `uint8_t precision` supposed to be constexpr when constructing a symbol? for( uint8_t precision{0}; precision < 64; ++precision ) { CHECK_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ) @@ -237,29 +273,34 @@ EOSIO_TEST_BEGIN(asset_type_test) }) ) - // ----------------------------------------------------------------------------------------------------------------------------- - // friend asset operator/(const asset&, int64_t)/asset& operator/=(int64_t)/friend int64_t operator/(const asset&, const asset&) + // --------------------------------------------- + // friend asset operator/(const asset&, int64_t) CHECK_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount), (asset{ 0LL, sym_no_prec}) ) CHECK_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}) ) CHECK_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec}.amount), (asset{-2LL, sym_no_prec}) ) CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{ 0LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount), (asset{ 0LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{ 1LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{ 4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount), (asset{-2LL, sym_no_prec}) ) - CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{-2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) - + // ---------------------------------------------------- + // friend int64_t operator/(const asset&, const asset&) CHECK_EQUAL( (asset{ 0LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 0LL, sym_no_prec}.amount) ) CHECK_EQUAL( (asset{ 1LL, sym_no_prec} / asset{ 1LL, sym_no_prec} ), (asset{ 1LL, sym_no_prec}.amount) ) CHECK_EQUAL( (asset{ 4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{ 2LL, sym_no_prec} ), (asset{-2LL, sym_no_prec}.amount) ) CHECK_EQUAL( (asset{-4LL, sym_no_prec} / asset{-2LL, sym_no_prec} ), (asset{ 2LL, sym_no_prec}.amount) ) + // --------------------------------- + // friend asset& operator/=(int64_t) + CHECK_EQUAL( (asset{ 0LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount), (asset{ 0LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 1LL, sym_no_prec} /= asset{ 1LL, sym_no_prec}.amount), (asset{ 1LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{ 4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{ 2LL, sym_no_prec}.amount), (asset{-2LL, sym_no_prec}) ) + CHECK_EQUAL( (asset{-4LL, sym_no_prec} /= asset{-2LL, sym_no_prec}.amount), (asset{ 2LL, sym_no_prec}) ) + CHECK_ASSERT( "divide by zero", ([&]() {asset{1LL, sym_no_prec} /= 0;}) ) - // Note that there is no invariant established here when adding or setting the `amount` + // Note: + // There is no invariant established here when adding or setting the `amount` CHECK_ASSERT( "signed division overflow", ( []() { asset a{}; @@ -276,7 +317,7 @@ EOSIO_TEST_BEGIN(asset_type_test) // -------------------------------------------------- // friend bool operator==(const asset&, const asset&) - CHECK_EQUAL( (asset{{}, sym_no_prec} == asset{{}, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{0LL, sym_no_prec} == asset{0LL, sym_no_prec} ), true ) CHECK_EQUAL( (asset{asset_min, sym_no_prec} == asset{asset_min, sym_no_prec} ), true ) CHECK_EQUAL( (asset{asset_max, sym_no_prec} == asset{asset_max, sym_no_prec} ), true ) @@ -293,21 +334,21 @@ EOSIO_TEST_BEGIN(asset_type_test) // ------------------------------------------------- // friend bool operator<(const asset&, const asset&) - CHECK_EQUAL( (asset{{}, sym_no_prec} < asset{1LL, sym_no_prec} ), true ) + CHECK_EQUAL( (asset{0LL, sym_no_prec} < asset{1LL, sym_no_prec} ), true ) // -------------------------------------------------- // friend bool operator<=(const asset&, const asset&) - CHECK_EQUAL( ( asset{{}, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{0LL, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) CHECK_EQUAL( ( asset{1LL, sym_no_prec} <= asset{1LL, sym_no_prec} ), true ) // ------------------------------------------------- // friend bool operator>(const asset&, const asset&) - CHECK_EQUAL( ( asset{1LL, sym_no_prec} > asset{{}, sym_no_prec} ), true ) - CHECK_EQUAL( ( asset{{}, sym_no_prec} > asset{1LL, sym_no_prec} ), false ) + CHECK_EQUAL( ( asset{1LL, sym_no_prec} > asset{0LL, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{0LL, sym_no_prec} > asset{1LL, sym_no_prec} ), false ) // -------------------------------------------------- // friend bool operator>=( const asset&, const asset&) - CHECK_EQUAL( ( asset{1LL, sym_no_prec} >= asset{{}, sym_no_prec} ), true ) + CHECK_EQUAL( ( asset{1LL, sym_no_prec} >= asset{0LL, sym_no_prec} ), true ) CHECK_EQUAL( ( asset{1LL, sym_no_prec} >= asset{1LL, sym_no_prec} ), true ) silence_output(false); @@ -317,22 +358,22 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(extended_asset_type_test) silence_output(true); - symbol sym_no_prec{"SYMBOLL",0}; - symbol sym_prec{"SYMBOLL",63}; + static constexpr symbol sym_no_prec{"SYMBOLL",0}; + static constexpr symbol sym_prec{"SYMBOLL",63}; - extended_symbol ex_sym_no_prec{sym_no_prec, name{"eosioaccountj"}}; - extended_symbol ex_sym_prec{sym_prec, name{"eosioaccountj"}}; + static constexpr extended_symbol ext_sym_no_prec{sym_no_prec, name{"eosioaccountj"}}; + static constexpr extended_symbol ext_sym_prec{sym_prec, name{"eosioaccountj"}}; - asset asset_no_prec{0LL, sym_no_prec}; - asset asset_prec{0LL, sym_prec}; + static const asset asset_no_prec{0LL, sym_no_prec}; + static const asset asset_prec{0LL, sym_prec}; //// extended_asset() CHECK_EQUAL( extended_asset{}.quantity, asset{} ) - CHECK_EQUAL( extended_asset{}.contract, name{} ) + CHECK_EQUAL( extended_asset{}.contract, name{} ) //// extended_asset(int64_t, extended_symbol) - CHECK_EQUAL( (extended_asset{{},ex_sym_no_prec}.quantity), (asset{0LL, sym_no_prec}) ) - CHECK_EQUAL( (extended_asset{{},ex_sym_no_prec}.contract), (name{"eosioaccountj"}) ) + CHECK_EQUAL( (extended_asset{{},ext_sym_no_prec}.quantity), (asset{0LL, sym_no_prec}) ) + CHECK_EQUAL( (extended_asset{{},ext_sym_no_prec}.contract), (name{"eosioaccountj"}) ) //// extended_asset(asset, name) CHECK_EQUAL( (extended_asset{asset_no_prec, name{"eosioaccountj"}}.quantity), (asset{ 0LL, sym_no_prec}) ) @@ -340,15 +381,18 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // ------------------------------------------ // extended_symbol get_extended_symbol()const - CHECK_EQUAL( (extended_asset{{},ex_sym_no_prec}.get_extended_symbol()), (ex_sym_no_prec) ) - CHECK_EQUAL( (extended_asset{{},ex_sym_prec}.get_extended_symbol()), (ex_sym_prec) ) + CHECK_EQUAL( (extended_asset{{},ext_sym_no_prec}.get_extended_symbol()), (ext_sym_no_prec) ) + CHECK_EQUAL( (extended_asset{{},ext_sym_prec}.get_extended_symbol()), (ext_sym_prec) ) - // Note: uncomment once print checking has been resolved - // Note that if there is no precision, there will be odd output: - // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` - // output: "0. A@1" // ----------------- // void print()const + // Note: + // Uncomment once print checking has been resolved + + // Note: + // That if there is no precision, there will be odd output: + // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` // output: "0. A@1" + // CHECK_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) // CHECK_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) // CHECK_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index 2649dd3742..cd5ea2dc02 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -6,218 +6,221 @@ #include #include +using std::in_place; +using std::move; + using eosio::binary_extension; // Definitions in `eosio.cdt/libraries/eosiolib/binary_extension.hpp` EOSIO_TEST_BEGIN(binary_extension_test) - silence_output(false); + silence_output(true); - /// constexpr binary_extension() + //// constexpr binary_extension() // constexpr bool has_value()const - REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) + CHECK_EQUAL( (binary_extension{}.has_value()), false ) - REQUIRE_EQUAL( (binary_extension{}.has_value()), false ) + CHECK_EQUAL( (binary_extension{}.has_value()), false ) - /// constexpr binary_extension(const T&) - REQUIRE_EQUAL( (binary_extension{'c'}.has_value()), true ) + //// constexpr binary_extension(const T&) + CHECK_EQUAL( (binary_extension{'c'}.has_value()), true ) - REQUIRE_EQUAL( (binary_extension{"abcd"}.has_value()), true ) + CHECK_EQUAL( (binary_extension{"abcd"}.has_value()), true ) - /// constexpr binary_extension(T&&) - REQUIRE_EQUAL( (binary_extension{std::move('c')}.has_value()), true ) + //// constexpr binary_extension(T&&) + CHECK_EQUAL( (binary_extension{move('c')}.has_value()), true ) - REQUIRE_EQUAL( (binary_extension{std::move("abcd")}.has_value()), true ) + CHECK_EQUAL( (binary_extension{move("abcd")}.has_value()), true ) - /// constexpr binary_extension(std::in_place_t, Args&&...) - REQUIRE_EQUAL( (binary_extension{std::in_place, 'c'}.has_value()), true ) /// How do I pack more args into this?? /// + //// constexpr binary_extension(in_place_t, Args&&...) + CHECK_EQUAL( (binary_extension{in_place, 'c'}.has_value()), true ) - REQUIRE_EQUAL( (binary_extension{std::in_place, "abcd"}.has_value()), true ) /// How do I pack more args into this?? /// + CHECK_EQUAL( (binary_extension{in_place, "abcd"}.has_value()), true ) - /// constexpr binary_extension(const binary_extension&) - binary_extension bin_copy_char_lval0{'c'}; - binary_extension bin_copy_char_lval1{bin_copy_char_lval0}; - REQUIRE_EQUAL( bin_copy_char_lval0.value(), bin_copy_char_lval1.value() ) - REQUIRE_EQUAL( bin_copy_char_lval0.has_value(), bin_copy_char_lval1.has_value() ) + //// constexpr binary_extension(const binary_extension&) + static const binary_extension cbe_ctor_char_lval0{'c'}; + static const binary_extension cbe_ctor_char_lval1{cbe_ctor_char_lval0}; + CHECK_EQUAL( cbe_ctor_char_lval0.value(), cbe_ctor_char_lval1.value() ) + CHECK_EQUAL( cbe_ctor_char_lval0.has_value(), cbe_ctor_char_lval1.has_value() ) - binary_extension bin_copy_str_lval0{"abcd"}; - binary_extension bin_copy_str_lval1{bin_copy_str_lval0}; - REQUIRE_EQUAL( bin_copy_str_lval0.value(), bin_copy_str_lval1.value() ) - REQUIRE_EQUAL( bin_copy_str_lval0.has_value(), bin_copy_str_lval1.has_value() ) + static const binary_extension cbe_ctor_str_lval0{"abcd"}; + static const binary_extension cbe_ctor_str_lval1{cbe_ctor_str_lval0}; + CHECK_EQUAL( cbe_ctor_str_lval0.value(), cbe_ctor_str_lval1.value() ) + CHECK_EQUAL( cbe_ctor_str_lval0.has_value(), cbe_ctor_str_lval1.has_value() ) - /// constexpr binary_extension(binary_extension&&) - binary_extension bin_copy_char_rval0{'c'}; - REQUIRE_EQUAL( bin_copy_char_rval0.has_value(), true ) - binary_extension bin_copy_char_rval1{std::move(bin_copy_char_rval0)}; - REQUIRE_EQUAL( bin_copy_char_rval0.has_value(), false ) - REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){bin_copy_char_rval0.value();} ) + //// constexpr binary_extension(binary_extension&&) + binary_extension be_ctor_char_rval0{'c'}; + CHECK_EQUAL( be_ctor_char_rval0.has_value(), true ) + binary_extension be_ctor_char_rval1{move(be_ctor_char_rval0)}; + CHECK_EQUAL( be_ctor_char_rval0.has_value(), false ) + CHECK_ASSERT( "cannot get value of empty binary_extension", [&](){be_ctor_char_rval0.value();} ) - binary_extension bin_copy_str_rval0{"abcd"}; - REQUIRE_EQUAL( bin_copy_str_rval0.has_value(), true ) - binary_extension bin_copy_str_rval1{std::move(bin_copy_str_rval0)}; - REQUIRE_EQUAL( bin_copy_str_rval0.has_value(), false ) - REQUIRE_ASSERT( "cannot get value of empty binary_extension", [&](){bin_copy_str_rval0.value();} ) + binary_extension be_ctor_str_rval0{"abcd"}; + CHECK_EQUAL( be_ctor_str_rval0.has_value(), true ) + binary_extension be_ctor_str_rval1{move(be_ctor_str_rval0)}; + CHECK_EQUAL( be_ctor_str_rval0.has_value(), false ) + CHECK_ASSERT( "cannot get value of empty binary_extension", [&](){be_ctor_str_rval0.value();} ) // ------------------- // ~binary_extension() - binary_extension bin_destruct_char{'c'}; - REQUIRE_EQUAL( bin_destruct_char.has_value(), true ) - bin_destruct_char.~binary_extension(); - REQUIRE_EQUAL( bin_destruct_char.has_value(), false ) + static const binary_extension be_destruct_char{'c'}; + CHECK_EQUAL( be_destruct_char.has_value(), true ) + be_destruct_char.~binary_extension(); + CHECK_EQUAL( be_destruct_char.has_value(), false ) - binary_extension bin_destruct_str{"abcd"}; - REQUIRE_EQUAL( bin_destruct_str.has_value(), true ) - bin_destruct_str.~binary_extension(); - REQUIRE_EQUAL( bin_destruct_str.has_value(), false ) + static const binary_extension be_destruct_str{"abcd"}; + CHECK_EQUAL( be_destruct_str.has_value(), true ) + be_destruct_str.~binary_extension(); + CHECK_EQUAL( be_destruct_str.has_value(), false ) // --------------------------------------- // constexpr explicit operator bool()const - REQUIRE_EQUAL( (binary_extension{}.operator bool()), false ) - REQUIRE_EQUAL( (binary_extension{0}.operator bool()), true ) - REQUIRE_EQUAL( (binary_extension{'c'}.operator bool()), true ) + CHECK_EQUAL( (binary_extension{}.operator bool()), false ) + CHECK_EQUAL( (binary_extension{0}.operator bool()), true ) + CHECK_EQUAL( (binary_extension{'c'}.operator bool()), true ) - REQUIRE_EQUAL( (binary_extension{}.operator bool()), false ) - REQUIRE_EQUAL( (binary_extension{nullptr}.operator bool()), true ) - REQUIRE_EQUAL( (binary_extension{"abcd"}.operator bool()), true ) + CHECK_EQUAL( (binary_extension{}.operator bool()), false ) + CHECK_EQUAL( (binary_extension{nullptr}.operator bool()), true ) + CHECK_EQUAL( (binary_extension{"abcd"}.operator bool()), true ) // --------------------- // constexpr T& value()& - binary_extension bin_char_value0{'c'}; - REQUIRE_EQUAL( (bin_char_value0.value() == 'c'), true ) - REQUIRE_EQUAL( (bin_char_value0.value() == 'd'), false ) + binary_extension be_char_value{'c'}; + CHECK_EQUAL( (be_char_value.value() == 'c'), true ) + CHECK_EQUAL( (be_char_value.value() == 'd'), false ) - binary_extension bin_str_value0{"abcd"}; - REQUIRE_EQUAL( (bin_str_value0.value() == "abcd"), true ) - REQUIRE_EQUAL( (bin_str_value0.value() == "efgh"), false ) + binary_extension be_str_value{"abcd"}; + CHECK_EQUAL( (be_str_value.value() == "abcd"), true ) + CHECK_EQUAL( (be_str_value.value() == "efgh"), false ) // -------------------------------- // constexpr const T& value()const& - binary_extension bin_const_char_value0{'c'}; - REQUIRE_EQUAL( (bin_const_char_value0.value() == 'c'), true ) - REQUIRE_EQUAL( (bin_const_char_value0.value() == 'd'), false ) + static const binary_extension cbe_char_value{'c'}; + CHECK_EQUAL( (cbe_char_value.value() == 'c'), true ) + CHECK_EQUAL( (cbe_char_value.value() == 'd'), false ) - binary_extension bin_const_str_value0{"abcd"}; - REQUIRE_EQUAL( (bin_const_str_value0.value() == "abcd"), true ) - REQUIRE_EQUAL( (bin_const_str_value0.value() == "efgh"), false ) + static const binary_extension cbe_str_value{"abcd"}; + CHECK_EQUAL( (cbe_str_value.value() == "abcd"), true ) + CHECK_EQUAL( (cbe_str_value.value() == "efgh"), false ) // --------------------------------------------------------------------------------------- - // constexpr auto value_or(U&&) -> std::enable_if_t::value, T&>& - binary_extension bin_temp_value_or{}; - uint8_t some_value = bin_temp_value_or.value_or(uint8_t{0x63}); - REQUIRE_EQUAL( some_value, 0x63 ) + // constexpr auto value_or(U&&) -> enable_if_t::value, T&>& + binary_extension be_val_or{}; + uint8_t ui8_val = be_val_or.value_or(uint8_t{0x63}); + CHECK_EQUAL( ui8_val, 0x63 ) // -------------------------- // constexpr T&& value_or()&& - REQUIRE_EQUAL( binary_extension{'c'}.value_or(), 'c' ) + CHECK_EQUAL( binary_extension{'c'}.value_or(), 'c' ) - binary_extension bin_value_or_rval{"abcd"}; - REQUIRE_EQUAL( bin_value_or_rval.value_or(), "abcd" ) + binary_extension be_val_or_rval{"abcd"}; + CHECK_EQUAL( be_val_or_rval.value_or(), "abcd" ) // ------------------------------------- // constexpr const T&& value_or()const&& - REQUIRE_EQUAL( binary_extension{'c'}.value_or(), 'c' ) + CHECK_EQUAL( binary_extension{'c'}.value_or(), 'c' ) - binary_extension bin_const_value_or_rval{"abcd"}; - REQUIRE_EQUAL( bin_const_value_or_rval.value_or(), "abcd" ) + static const binary_extension cbe_val_or_rval{"abcd"}; + CHECK_EQUAL( cbe_val_or_rval.value_or(), "abcd" ) // ----------------------- // constexpr T value_or()& - binary_extension bin_char_value_or_val{'c'}; - REQUIRE_EQUAL( bin_char_value_or_val.value_or(), 'c' ) + binary_extension be_char_val_or{'c'}; + CHECK_EQUAL( be_char_val_or.value_or(), 'c' ) - binary_extension bin_value_or_val{"abcd"}; - REQUIRE_EQUAL( bin_value_or_val.value_or(), "abcd" ) + binary_extension be_str_val_or{"abcd"}; + CHECK_EQUAL( be_str_val_or.value_or(), "abcd" ) // ---------------------------- // constexpr T value_or()const& - binary_extension bin_char_const_value_or_val{'c'}; - REQUIRE_EQUAL( bin_char_const_value_or_val.value_or(), 'c' ) + static const binary_extension cbe_char_val_or{'c'}; + CHECK_EQUAL( cbe_char_val_or.value_or(), 'c' ) - binary_extension bin_const_value_or_val{"abcd"}; - REQUIRE_EQUAL( bin_const_value_or_val.value_or(), "abcd" ) + static const binary_extension cbe_str_val_or{"abcd"}; + CHECK_EQUAL( cbe_str_val_or.value_or(), "abcd" ) // ------------------------- // constexpr T* operator->() - binary_extension bin_char_arrow_op{'c'}; - REQUIRE_EQUAL( *bin_char_arrow_op.operator->() == 'c', true ) - REQUIRE_EQUAL( *bin_char_arrow_op.operator->() != 'd', true ) + binary_extension be_char_mem_op{'c'}; + CHECK_EQUAL( *be_char_mem_op.operator->() == 'c', true ) + CHECK_EQUAL( *be_char_mem_op.operator->() != 'd', true ) - binary_extension bin_arrow_op{"abcd"}; - REQUIRE_EQUAL( *bin_arrow_op.operator->() == "abcd", true ) - REQUIRE_EQUAL( *bin_arrow_op.operator->() != "efgh", true ) + binary_extension be_str_mem_op{"abcd"}; + CHECK_EQUAL( *be_str_mem_op.operator->() == "abcd", true ) + CHECK_EQUAL( *be_str_mem_op.operator->() != "efgh", true ) // ------------------------------------ // constexpr const T* operator->()const - binary_extension bin_char_const_arrow_op{'c'}; - REQUIRE_EQUAL( *bin_char_const_arrow_op.operator->() == 'c', true ) - REQUIRE_EQUAL( *bin_char_const_arrow_op.operator->() != 'd', true ) + static const binary_extension cbe_char_mem_op{'c'}; + CHECK_EQUAL( *cbe_char_mem_op.operator->() == 'c', true ) + CHECK_EQUAL( *cbe_char_mem_op.operator->() != 'd', true ) - binary_extension bin_const_arrow_op{"abcd"}; - REQUIRE_EQUAL( *bin_const_arrow_op.operator->() == "abcd", true ) - REQUIRE_EQUAL( *bin_const_arrow_op.operator->() != "efgh", true ) + static const binary_extension cbe_str_arrow_op{"abcd"}; + CHECK_EQUAL( *cbe_str_arrow_op.operator->() == "abcd", true ) + CHECK_EQUAL( *cbe_str_arrow_op.operator->() != "efgh", true ) // ------------------------- // constexpr T& operator*()& - binary_extension bin_char_star_op{'c'}; - REQUIRE_EQUAL( bin_char_star_op.operator*() == 'c', true ) - REQUIRE_EQUAL( bin_char_star_op.operator*() != 'd', true ) + binary_extension be_char_lval_dref_op{'c'}; + CHECK_EQUAL( be_char_lval_dref_op.operator*() == 'c', true ) + CHECK_EQUAL( be_char_lval_dref_op.operator*() != 'd', true ) - binary_extension bin_star_op{"abcd"}; - REQUIRE_EQUAL( bin_star_op.operator*() == "abcd", true ) - REQUIRE_EQUAL( bin_star_op.operator*() != "efgh", true ) + binary_extension be_str_lval_dref_op{"abcd"}; + CHECK_EQUAL( be_str_lval_dref_op.operator*() == "abcd", true ) + CHECK_EQUAL( be_str_lval_dref_op.operator*() != "efgh", true ) // ------------------------------------ // constexpr const T& operator*()const& - binary_extension bin_char_const_star_op{'c'}; - REQUIRE_EQUAL( bin_char_const_star_op.operator*() == 'c', true ) - REQUIRE_EQUAL( bin_char_const_star_op.operator*() != 'd', true ) + static const binary_extension cbe_char_lval_dref_op{'c'}; + CHECK_EQUAL( cbe_char_lval_dref_op.operator*() == 'c', true ) + CHECK_EQUAL( cbe_char_lval_dref_op.operator*() != 'd', true ) - binary_extension bin_const_star_op{"abcd"}; - REQUIRE_EQUAL( bin_const_star_op.operator*() == "abcd", true ) - REQUIRE_EQUAL( bin_const_star_op.operator*() != "efgh", true ) + static const binary_extension cbe_str_lval_dref_op{"abcd"}; + CHECK_EQUAL( cbe_str_lval_dref_op.operator*() == "abcd", true ) + CHECK_EQUAL( cbe_str_lval_dref_op.operator*() != "efgh", true ) // --------------------------- // constexpr T&& operator*()&& - binary_extension bin_char_rval_star_op{'c'}; - REQUIRE_EQUAL( std::move(bin_char_rval_star_op.operator*()) == 'c', true ) - REQUIRE_EQUAL( std::move(bin_char_rval_star_op.operator*()) != 'd', true ) + binary_extension be_char_rval_dref_op{'c'}; + CHECK_EQUAL( move(be_char_rval_dref_op.operator*()) == 'c', true ) + CHECK_EQUAL( move(be_char_rval_dref_op.operator*()) != 'd', true ) - binary_extension bin_str_rval_star_op{"abcd"}; - REQUIRE_EQUAL( std::move(bin_str_rval_star_op.operator*()) == "abcd", true ) - REQUIRE_EQUAL( std::move(bin_str_rval_star_op.operator*()) != "efgh", true ) + binary_extension be_str_rval_dref_op{"abcd"}; + CHECK_EQUAL( move(be_str_rval_dref_op.operator*()) == "abcd", true ) + CHECK_EQUAL( move(be_str_rval_dref_op.operator*()) != "efgh", true ) // -------------------------------------- // constexpr const T&& operator*()const&& - binary_extension bin_const_char_rval_star_op{'c'}; - REQUIRE_EQUAL( std::move(bin_const_char_rval_star_op.operator*()) == 'c', true ) - REQUIRE_EQUAL( std::move(bin_const_char_rval_star_op.operator*()) != 'd', true ) + static const binary_extension cbe_char_rval_dref_op{'c'}; + CHECK_EQUAL( move(cbe_char_rval_dref_op.operator*()) == 'c', true ) + CHECK_EQUAL( move(cbe_char_rval_dref_op.operator*()) != 'd', true ) - binary_extension bin_const_str_rval_star_op{"abcd"}; - REQUIRE_EQUAL( std::move(bin_const_str_rval_star_op.operator*()) == "abcd", true ) - REQUIRE_EQUAL( std::move(bin_const_str_rval_star_op.operator*()) != "efgh", true ) + static const binary_extension cbe_str_rval_dref_op{"abcd"}; + CHECK_EQUAL( move(cbe_str_rval_dref_op.operator*()) == "abcd", true ) + CHECK_EQUAL( move(cbe_str_rval_dref_op.operator*()) != "efgh", true ) // ---------------------------- // T& emplace(Args&& ... args)& - binary_extension bin_char_emplace{'c'}; - bin_char_emplace.emplace(std::move('d')); - REQUIRE_EQUAL( bin_char_emplace.value() == 'd', true ) - REQUIRE_EQUAL( bin_char_emplace.value() != 'c', true ) + binary_extension be_char_emplace{'c'}; + be_char_emplace.emplace(move('d')); + CHECK_EQUAL( be_char_emplace.value() == 'd', true ) + CHECK_EQUAL( be_char_emplace.value() != 'c', true ) - binary_extension bin_str_emplace{"abcd"}; - bin_str_emplace.emplace(std::move("efgh")); - REQUIRE_EQUAL( bin_str_emplace.value() == "efgh", true ) - REQUIRE_EQUAL( bin_str_emplace.value() != "abcd", true ) + binary_extension be_str_emplace{"abcd"}; + be_str_emplace.emplace(move("efgh")); + CHECK_EQUAL( be_str_emplace.value() == "efgh", true ) + CHECK_EQUAL( be_str_emplace.value() != "abcd", true ) // ------------ // void reset() - binary_extension bin_char_reset{'c'}; - REQUIRE_EQUAL( bin_char_reset.has_value(), true ) - bin_char_reset.reset(); - REQUIRE_EQUAL( bin_char_reset.has_value(), false ) - - binary_extension bin_str_reset{"abcd"}; - REQUIRE_EQUAL( bin_str_reset.has_value(), true ) - bin_str_reset.reset(); - REQUIRE_EQUAL( bin_str_reset.has_value(), false ) + binary_extension be_char_reset{'c'}; + CHECK_EQUAL( be_char_reset.has_value(), true ) + be_char_reset.reset(); + CHECK_EQUAL( be_char_reset.has_value(), false ) + + binary_extension be_str_reset{"abcd"}; + CHECK_EQUAL( be_str_reset.has_value(), true ) + be_str_reset.reset(); + CHECK_EQUAL( be_str_reset.has_value(), false ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/crypto_tests.cpp b/tests/unit/crypto_tests.cpp index b909e64124..e1f7ca436c 100644 --- a/tests/unit/crypto_tests.cpp +++ b/tests/unit/crypto_tests.cpp @@ -15,13 +15,13 @@ EOSIO_TEST_BEGIN(public_key_type_test) // ----------------------------------------------------- // bool operator==(const public_key&, const public_key&) - CHECK_EQUAL( (public_key{0, std::array{}} == public_key{0, std::array{}}), true ) - CHECK_EQUAL( (public_key{0, std::array{1}} == public_key{0, std::array{}}), false ) + CHECK_EQUAL( (public_key{0, std::array{}} == public_key{0, std::array{}}), true ) + CHECK_EQUAL( (public_key{0, std::array{1}} == public_key{0, std::array{}}), false ) // ----------------------------------------------------- // bool operator!=(const public_key&, const public_key&) - CHECK_EQUAL( (public_key{0, std::array{}} != public_key{0, std::array{}}), false ) - CHECK_EQUAL( (public_key{0, std::array{1}} != public_key{0, std::array{}}), true ) + CHECK_EQUAL( (public_key{0, std::array{}} != public_key{0, std::array{}}), false ) + CHECK_EQUAL( (public_key{0, std::array{1}} != public_key{0, std::array{}}), true ) silence_output(false); EOSIO_TEST_END @@ -32,13 +32,13 @@ EOSIO_TEST_BEGIN(signature_type_test) // --------------------------------------------------- // bool operator==(const signature&, const signature&) - CHECK_EQUAL( (signature{0, std::array{}} == signature{0, std::array{}}), true ) - CHECK_EQUAL( (signature{0, std::array{1}} == signature{0, std::array{}}), false ) + CHECK_EQUAL( (signature{0, std::array{}} == signature{0, std::array{}}), true ) + CHECK_EQUAL( (signature{0, std::array{1}} == signature{0, std::array{}}), false ) // --------------------------------------------------- // bool operator!=(const signature&, const signature&) - CHECK_EQUAL( (signature{0, std::array{1}} != signature{0, std::array{}}), true ) - CHECK_EQUAL( (signature{0, std::array{}} != signature{0, std::array{}}), false ) + CHECK_EQUAL( (signature{0, std::array{1}} != signature{0, std::array{}}), true ) + CHECK_EQUAL( (signature{0, std::array{}} != signature{0, std::array{}}), false ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index cd06e300ea..032e3c30c5 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -1,27 +1,3 @@ -// TODO: Make costructor comments across all files consistant `///` make them three slashes -// TODO: Get rid of redundant tests -// TODO: Organize all tests in the most logical way -// TODO: Figure out a logical ordering of temporary helper variables -// TODO: Should I extern all global variables? -// TODO: Line up everything -// TODO: Make all parenthesis consistent in the `REQUIRE` expressions -// TODO: Change all `REQUIRE`'s to `CHECK`'s -// TODO: Chnage the uint32_t max to the real value in all files -// TODO: Check to make sure that the uint32_t changes didn't break anything for all files -// TODO: Make sure to remove all explicit `std::` in statements -// TODO: Make parenthesis consistent -// TODO: Make sure to check the CMake files -// TODO: Make the appropriate variabels const and/or static -// TODO: Make sure to adjust the correct min max values -// TODO: Make sure all output is on it's appropriate value -// TODO: Make sure it is readable for all future people (and myself) reading the code -// TODO: Organize functions in the most logical order, even if it's not the order in which they are defined -// Is this a good convention? -// microseconds ms0{ 0LL}; -// microseconds ms1{ 1LL}; -// microseconds ms2{-1LL}; -// microseconds ms_min{i64min}; -// microseconds ms_max{i64max}; /** * @file * @copyright defined in eosio.cdt/LICENSE.txt @@ -46,13 +22,17 @@ using std::array; using std::begin; +using std::deque; using std::end; using std::fill; using std::list; using std::map; +using std::optional; +using std::pair; using std::set; using std::string; using std::tuple; +using std::variant; using std::vector; using eosio::binary_extension; @@ -263,7 +243,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // T (primitive) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const int cprim{10}; + static constexpr int cprim{10}; int prim{}; ds << cprim; ds.seekp(0); @@ -278,7 +258,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const non_prim_test cnonprim{10}; + static constexpr non_prim_test cnonprim{10}; non_prim_test nonprim{}; ds << cnonprim; ds.seekp(0); @@ -289,7 +269,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // T[] (primitive) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const int cprim_array[10]{0,1,2,3,4,5,6,7,8,9}; + static constexpr int cprim_array[10]{0,1,2,3,4,5,6,7,8,9}; int prim_array[10]{}; ds << cprim_array; CHECK_EQUAL( ds.tellp(), 41 ) @@ -310,7 +290,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const non_prim_array_test cnon_prim_array[10]{0,1,2,3,4,5,6,7,8,9}; + static constexpr non_prim_array_test cnon_prim_array[10]{0,1,2,3,4,5,6,7,8,9}; non_prim_array_test non_prim_array[10]{}; ds << cnon_prim_array; CHECK_EQUAL( ds.tellp(), 41 ) @@ -327,7 +307,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // bool ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const bool cboolean{true}; + static constexpr bool cboolean{true}; bool boolean{}; ds << cboolean; ds.seekp(0); @@ -338,20 +318,21 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::array ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const array ca{'a','b','c','d','e','f','g','h','i'}; - std::array a{}; - ds << ca; + static constexpr array carr{'a','b','c','d','e','f','g','h','i'}; + array arr{}; + ds << carr; ds.seekp(0); - ds >> a; - CHECK_EQUAL( ca, a ) - - /// Note: uncomment once issue has been resolved /// + ds >> arr; + CHECK_EQUAL( carr, arr ) + // ---------- // std::deque + // Note: + // Uncomment once issue has been resolved // ds.seekp(0); // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const std::deque cd{'a','b','c','d','e','f','g','h','i' }; - // std::deque d{}; + // const deque cd{'a','b','c','d','e','f','g','h','i' }; + // deque d{}; // ds << cd; // ds.seekp(0); // ds >> d; // Fails here @@ -361,8 +342,8 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::list ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::list cl{'a','b','c','d','e','f','g','h','i' }; - std::list l{}; + static const list cl{'a','b','c','d','e','f','g','h','i' }; + list l{}; ds << cl; ds.seekp(0); ds >> l; @@ -372,19 +353,19 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::map ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const map cchar_map{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; - map char_map{}; - ds << cchar_map; + static const map cm{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; + map m{}; + ds << cm; ds.seekp(0); - ds >> char_map; - CHECK_EQUAL( cchar_map, char_map ) + ds >> m; + CHECK_EQUAL( cm, m ) // ------------- // std::optional ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::optional co{'c'}; - std::optional o{}; + static const optional co{'c'}; + optional o{}; ds << co; ds.seekp(0); ds >> o; @@ -394,8 +375,8 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::pair ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::pair cp{'c', 42}; - std::pair p{}; + static const pair cp{'c', 42}; + pair p{}; ds << cp; ds.seekp(0); ds >> p; @@ -405,18 +386,18 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::set ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const set cchar_set{'a','b','c','d','e','f','g','h','i'}; - set char_set{}; - ds << cchar_set; + static const set cs{'a','b','c','d','e','f','g','h','i'}; + set s{}; + ds << cs; ds.seekp(0); - ds >> char_set; - CHECK_EQUAL( cchar_set, char_set ) + ds >> s; + CHECK_EQUAL( cs, s ) // ----------- // std::string ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const string cstr {"abcdefghi"}; + static const string cstr {"abcdefghi"}; string str{}; ds << cstr; ds.seekp(0); @@ -427,7 +408,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::tuple ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const tuple ctup{"abcefghi",42,'a'}; + static const tuple ctup{"abcefghi",42,'a'}; tuple tup{}; ds << ctup; ds.seekp(0); @@ -438,12 +419,12 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // std::variant ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const std::variant v0{1024}; - std::variant v1{}; - ds << v0; + static const variant cv{1024}; + variant v{}; + ds << cv; ds.seekp(0); - ds >> v1; - CHECK_EQUAL( v0, v1 ) + ds >> v; + CHECK_EQUAL( cv, v ) // ----------- // std::vector @@ -453,21 +434,21 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const vector ctest_vec{{'a'},{'b'},{'c'},{'d'},{'e'},{'f'},{'g'},{'h'},{'i'}}; - vector test_vec{}; - ds << ctest_vec; + static const vector cvec{{'a'},{'b'},{'c'},{'d'},{'e'},{'f'},{'g'},{'h'},{'i'}}; + vector vec{}; + ds << cvec; ds.seekp(0); - ds >> test_vec; + ds >> vec; - for (int i{0}; i < ctest_vec.size(); ++i) { - CHECK_EQUAL( ctest_vec[i].val, test_vec[i].val ) + for (int i{0}; i < cvec.size(); ++i) { + CHECK_EQUAL( cvec[i].val, vec[i].val ) } // ----------------- // std::vector ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const vector cchar_vec{'a','b','c','d','e','f','g','h','i'}; + static const vector cchar_vec{'a','b','c','d','e','f','g','h','i'}; vector char_vec{}; ds << cchar_vec; ds.seekp(0); @@ -478,7 +459,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // capi_checksum160 ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const capi_checksum160 ccheck160 {0x01,0x02,0x03,0x04}; + static const capi_checksum160 ccheck160 {0x01,0x02,0x03,0x04}; capi_checksum160 check160{}; ds << ccheck160; ds.seekp(0); @@ -489,7 +470,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // capi_checksum256 ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const capi_checksum256 ccheck256 {0x01,0x02,0x03,0x04}; + static const capi_checksum256 ccheck256 {0x01,0x02,0x03,0x04}; capi_checksum256 check256{}; ds << ccheck256; ds.seekp(0); @@ -500,7 +481,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // capi_checksum512 ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const capi_checksum512 ccheck512 {0x01,0x02,0x03,0x04}; + static const capi_checksum512 ccheck512 {0x01,0x02,0x03,0x04}; capi_checksum512 check512{}; ds << ccheck512; ds.seekp(0); @@ -511,7 +492,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // capi_public_key ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const capi_public_key c_cpubkey{'a','b','c','d','e','f','g','h','i'}; + static const capi_public_key c_cpubkey{'a','b','c','d','e','f','g','h','i'}; capi_public_key c_pubkey{}; ds << c_cpubkey; ds.seekp(0); @@ -522,7 +503,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // eosio::binary_extension ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const binary_extension cbe_char{'c'}; + static const binary_extension cbe_char{'c'}; binary_extension cb_char{}; ds << cbe_char; ds.seekp(0); @@ -538,30 +519,27 @@ EOSIO_TEST_BEGIN(datastream_stream_test) ds >> be_int1; CHECK_EQUAL( be_int1.value(), 42 ) - const be_test bet{}; // For default value ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const binary_extension cbe_btest{bet}; - binary_extension cb_btest{}; - ds << cbe_btest; + static const be_test be_default{}; // Default constructing object + static const binary_extension cbe_test{be_default}; + binary_extension cb_test{}; + ds << cbe_test; ds.seekp(0); - ds >> cb_btest; - CHECK_EQUAL( cbe_btest.value().val, cb_btest.value().val ) + ds >> cb_test; + CHECK_EQUAL( cbe_test.value().val, cb_test.value().val ) ds.seekp(256); fill(begin(datastream_buffer), end(datastream_buffer), 0); - binary_extension cb_default_test{}; - ds >> cb_default_test; - CHECK_EQUAL( 42, cb_default_test.value_or().val ) - - const binary_extension cbe_none{}; - CHECK_ASSERT( "cannot get value of empty binary_extension", [&](){cbe_none.value();} ) + binary_extension be_default_test{}; + ds >> be_default_test; + CHECK_EQUAL( 42, be_default_test.value_or().val ) // ------------------ // eosio::fixed_bytes ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const fixed_bytes<32> cfb{fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)}; + static const fixed_bytes<32> cfb{fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)}; fixed_bytes<32> fb{}; ds << cfb; ds.seekp(0); @@ -572,7 +550,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // eosio::ignore ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const ignore cig{}; + static const ignore cig{}; ignore ig; ds << cig; CHECK_EQUAL( ds.tellp(), 0 ) @@ -583,18 +561,18 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // eosio::ignore_wrapper ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const ignore_wrapper ciw{'c'}; - char iw; - ds << ciw; + static const ignore_wrapper cigw{'c'}; + char igw; + ds << cigw; ds.seekp(0); - ds >> iw; - CHECK_EQUAL( ciw.value, iw ) + ds >> igw; + CHECK_EQUAL( cigw.value, igw ) // ----------------- // eosio::public_key ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const public_key cpubkey{{},'a','b','c','d','e','f','g','h','i'}; + static const public_key cpubkey{{},'a','b','c','d','e','f','g','h','i'}; public_key pubkey{}; ds << cpubkey; ds.seekp(0); @@ -605,7 +583,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // eosio::signature ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const signature csig{{},'a','b','c','d','e','f','g','h','i'}; + static const signature csig{{},'a','b','c','d','e','f','g','h','i'}; signature sig{}; ds << csig; ds.seekp(0); @@ -616,26 +594,26 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // eosio::symbol ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const symbol sym_no_prec{"SYMBOLL", 0}; + static const symbol csym_no_prec{"SYMBOLL", 0}; symbol sym{}; - ds << sym_no_prec; + ds << csym_no_prec; ds.seekp(0); ds >> sym; - CHECK_EQUAL( sym_no_prec, sym ) + CHECK_EQUAL( csym_no_prec, sym ) ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const symbol sym_prec{"SYMBOLL", 255}; - ds << sym_prec; + static const symbol csym_prec{"SYMBOLL", 255}; + ds << csym_prec; ds.seekp(0); ds >> sym; - CHECK_EQUAL( sym_prec, sym ) + CHECK_EQUAL( csym_prec, sym ) // ------------------ // eosio::symbol_code ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const symbol_code csc{"SYMBOLL"}; + static const symbol_code csc{"SYMBOLL"}; symbol_code sc{}; ds << csc; ds.seekp(0); @@ -646,7 +624,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // boost::container::flat_map ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const flat_map cchar_flat_map{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; + static const flat_map cchar_flat_map{{'a',97}, {'b',98}, {'c',99}, {'d',100}}; flat_map char_flat_map{}; ds << cchar_flat_map; ds.seekp(0); @@ -657,7 +635,7 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // boost::container::flat_set ds.seekp(0); fill(begin(datastream_buffer), end(datastream_buffer), 0); - const flat_set cchar_flat_set{'a','b','c','d','e','f','g','h','i'}; + static const flat_set cchar_flat_set{'a','b','c','d','e','f','g','h','i'}; flat_set char_flat_set{}; ds << cchar_flat_set; ds.seekp(0); @@ -673,23 +651,23 @@ EOSIO_TEST_BEGIN(misc_datastream_test) // --------------------------- // vector pack(const T&) - const string pack_s{"abcdefghi"}; - std::vector pack_res{}; - pack_res = pack(pack_s); - CHECK_EQUAL( memcmp(pack_s.data(), pack_res.data()+1, pack_s.size()), 0 ) + static const string pack_str{"abcdefghi"}; + vector pack_res{}; + pack_res = pack(pack_str); + CHECK_EQUAL( memcmp(pack_str.data(), pack_res.data()+1, pack_str.size()), 0 ) // -------------------------- // size_t pack_size(const T&) int pack_size_i{42}; double pack_size_d{3.14}; string pack_size_s{"abcdefghi"};; - CHECK_EQUAL( pack_size(pack_size_i), 4 ) - CHECK_EQUAL( pack_size(pack_size_d), 8 ) + CHECK_EQUAL( pack_size(pack_size_i), 4 ) + CHECK_EQUAL( pack_size(pack_size_d), 8 ) CHECK_EQUAL( pack_size(pack_size_s), 10 ) // ----------------------------- // T unpack(const char*, size_t) - const char unpack_source_buffer[9]{'a','b','c','d','e','f','g','h','i'}; + static const char unpack_source_buffer[9]{'a','b','c','d','e','f','g','h','i'}; char unpack_ch{}; for (uint8_t i = 0; i < 9; ++i) { unpack_ch = unpack(unpack_source_buffer+i, 9); @@ -698,7 +676,7 @@ EOSIO_TEST_BEGIN(misc_datastream_test) // ----------------------------- // T unpack(const vector&) - const vector unpack_source_vec{'a','b','c','d','e','f','g','h','i'}; + static const vector unpack_source_vec{'a','b','c','d','e','f','g','h','i'}; for (uint8_t i = 0; i < 9; ++i) { unpack_ch = unpack(unpack_source_buffer+i, 9); CHECK_EQUAL( unpack_source_buffer[i], unpack_ch ) @@ -708,7 +686,8 @@ EOSIO_TEST_BEGIN(misc_datastream_test) EOSIO_TEST_END int main(int argc, char* argv[]) { - // Note: uncomment once segfaulting during `memcpy` has been resolved + // Note: + // Uncomment once segfaulting during `memcpy` has been resolved // EOSIO_TEST(datastream_test); EOSIO_TEST(datastream_specialization_test); EOSIO_TEST(datastream_stream_test); diff --git a/tests/unit/fixed_bytes_tests.cpp b/tests/unit/fixed_bytes_tests.cpp index c55a449d8c..0416f0e0df 100644 --- a/tests/unit/fixed_bytes_tests.cpp +++ b/tests/unit/fixed_bytes_tests.cpp @@ -5,8 +5,8 @@ #include -#include #include +#include using std::array; @@ -14,173 +14,167 @@ using eosio::fixed_bytes; // Definitions in `eosio.cdt/libraries/eosiolib/fixed_bytes.hpp` EOSIO_TEST_BEGIN(fixed_bytes_test) - silence_output(false); + silence_output(true); -/// constexpr fixed_bytes() +//// constexpr fixed_bytes() // static constexpr size_t padded_bytes() CHECK_EQUAL( fixed_bytes<20>{}.padded_bytes(), 12 ) CHECK_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) CHECK_EQUAL( fixed_bytes<64>{}.padded_bytes(), 0 ) +CHECK_EQUAL( fixed_bytes<128>{}.padded_bytes(), 0 ) +CHECK_EQUAL( fixed_bytes<256>{}.padded_bytes(), 0 ) -/// fixed_bytes(const std::array&) +//// fixed_bytes(const array&) // static constexpr size_t num_words() -CHECK_EQUAL( (fixed_bytes<20>{array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32>{array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64>{array{}}.num_words()), 4 ) - -/// fixed_bytes(const std::array&) -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -/// fixed_bytes(const Word(&arr)[NumWords]) -const uint8_t arr8[1]{}; -const uint16_t arr16[1]{}; -const uint32_t arr32[1]{}; -const uint64_t arr64[1]{}; - -CHECK_EQUAL( (fixed_bytes<20> {arr8}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {arr8}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {arr8}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{arr8}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{arr8}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {arr16}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {arr16}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {arr16}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{arr16}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{arr16}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {arr32}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {arr32}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {arr32}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{arr32}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{arr32}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {arr64}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {arr64}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {arr64}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{arr64}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{arr64}.num_words()), 16 ) +CHECK_EQUAL( (fixed_bytes<20>{array::word_t, 2>{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32>{array::word_t, 2>{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64>{array::word_t, 4>{}}.num_words()), 4 ) + +//// fixed_bytes(const std::array&) +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + +//// fixed_bytes(const Word(&arr)[NumWords]) +static constexpr uint8_t ctor_arr8[1]{}; +static constexpr uint16_t ctor_arr16[1]{}; +static constexpr uint32_t ctor_arr32[1]{}; +static constexpr uint64_t ctor_arr64[1]{}; + +CHECK_EQUAL( (fixed_bytes<20> {ctor_arr8}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {ctor_arr8}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {ctor_arr8}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{ctor_arr8}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{ctor_arr8}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {ctor_arr16}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {ctor_arr16}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {ctor_arr16}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{ctor_arr16}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{ctor_arr16}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {ctor_arr32}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {ctor_arr32}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {ctor_arr32}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{ctor_arr32}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{ctor_arr32}.num_words()), 16 ) + +CHECK_EQUAL( (fixed_bytes<20> {ctor_arr64}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<32> {ctor_arr64}.num_words()), 2 ) +CHECK_EQUAL( (fixed_bytes<64> {ctor_arr64}.num_words()), 4 ) +CHECK_EQUAL( (fixed_bytes<128>{ctor_arr64}.num_words()), 8 ) +CHECK_EQUAL( (fixed_bytes<256>{ctor_arr64}.num_words()), 16 ) // ------------------------------------------- // make_from_word_sequence(FirstWord, Rest...) -array arr{1ULL,2ULL,3ULL,4ULL}; -fixed_bytes<32> fb1{fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)}; -fixed_bytes<32> fb2{arr}; -CHECK_EQUAL( (fixed_bytes<20>::make_from_word_sequence(1ULL, 2ULL)), - (fixed_bytes<20>{array{1ULL, 2ULL}}) ) +// Possible discrepancy between two `make_from_word_sequence` and array constructor +// CHECK_EQUAL( (fixed_bytes<20>::make_from_word_sequence(1ULL, 2ULL)), +// (fixed_bytes<20>{array{1ULL, 2ULL}}) ) CHECK_EQUAL( (fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)), - (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}) ) + (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}) ) + +// ------------------------------------------------- +// array extract_as_byte_array()const +static constexpr array extract_arr{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04 }; + +CHECK_EQUAL( (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}.extract_as_byte_array()), extract_arr ) // ---------------------------- // const auto& get_array()const -// CHECK_EQUAL( (fixed_bytes<0>{}.get_array()), (std::array{}) ) -// CHECK_EQUAL( (fixed_bytes<1>{}.get_array()), (std::array{}) ) -// CHECK_EQUAL( (fixed_bytes<32>{}.get_array()), (std::array{}) ) -// CHECK_EQUAL( (fixed_bytes<512>{}.get_array()), (std::array{}) ) +CHECK_EQUAL( (fixed_bytes<0>{}.get_array()), (array{}) ) +CHECK_EQUAL( (fixed_bytes<1>{}.get_array()), (array{}) ) +CHECK_EQUAL( (fixed_bytes<32>{}.get_array()), (array{}) ) +CHECK_EQUAL( (fixed_bytes<512>{}.get_array()), (array{}) ) // ----------- // auto data() -// std::array arr0{}; -// std::array arr1{}; -// std::array arr2{}; -// std::array arr3{}; -// fixed_bytes<0> fb0(arr0); -// fixed_bytes<1> fb1{arr1}; -// fixed_bytes<32> fb2{arr2}; -// fixed_bytes<512> fb3{arr3}; - -// CHECK_EQUAL( fb0.data(), arr0.data() ) -// CHECK_EQUAL( (fixed_bytes<0>{}.data()), uint128_t*(std::array{0}) ) -// CHECK_EQUAL( fixed_bytes<1>{}.data(), 1 ) -// CHECK_EQUAL( fixed_bytes<32>{}.data(), 2 ) -// CHECK_EQUAL( fixed_bytes<512>{}.data(), 32 ) +static constexpr fixed_bytes<0> fb_data0{}; +static constexpr fixed_bytes<1> fb_data1{}; +static constexpr fixed_bytes<32> fb_data2{}; +static constexpr fixed_bytes<512> fb_data3{}; + +CHECK_EQUAL( fb_data0.data(), fb_data0.get_array().data() ) +CHECK_EQUAL( fb_data1.data(), fb_data1.get_array().data() ) +CHECK_EQUAL( fb_data2.data(), fb_data2.get_array().data() ) +CHECK_EQUAL( fb_data3.data(), fb_data3.get_array().data() ) // ---------------- // auto data()const -// CHECK_EQUAL( fixed_bytes<0>{}.data(), 0 ) -// CHECK_EQUAL( fixed_bytes<1>{}.data(), 1 ) -// CHECK_EQUAL( fixed_bytes<32>{}.data(), 2 ) -// CHECK_EQUAL( fixed_bytes<512>{}.data(), 32 ) +static constexpr fixed_bytes<0> cfb_data0{}; +static constexpr fixed_bytes<1> cfb_data1{}; +static constexpr fixed_bytes<32> cfb_data2{}; +static constexpr fixed_bytes<512> cfb_data3{}; + +CHECK_EQUAL( cfb_data0.data(), cfb_data0.get_array().data() ) +CHECK_EQUAL( cfb_data1.data(), cfb_data1.get_array().data() ) +CHECK_EQUAL( cfb_data2.data(), cfb_data2.get_array().data() ) +CHECK_EQUAL( cfb_data3.data(), cfb_data3.get_array().data() ) // ---------------- // auto size()const -// CHECK_EQUAL( fixed_bytes<0>{}.size(), 0 ) -// CHECK_EQUAL( fixed_bytes<1>{}.size(), 1 ) -// CHECK_EQUAL( fixed_bytes<32>{}.size(), 2 ) -// CHECK_EQUAL( fixed_bytes<512>{}.size(), 32 ) - -// ------------------------------------------------------ -// std::array extract_as_byte_array()const -// CHECK_EQUAL( fixed_bytes<0>{}.extract_as_byte_array(), 0 ) -// CHECK_EQUAL( fixed_bytes<1>{}.extract_as_byte_array(), 1 ) -// CHECK_EQUAL( fixed_bytes<32>{}.extract_as_byte_array(), 2 ) -// CHECK_EQUAL( fixed_bytes<512>{}.extract_as_byte_array(), 32 ) - -// ---------- -// template<> -// const fixed_bytes -// bool operator==(const fixed_bytes, const fixed_bytes) -// CHECK_EQUAL( fixed_bytes<1>{1ULL} == fixed_bytes<1>{1ULL}, true ) -// CHECK_EQUAL( fixed_bytes<1>{} == fixed_bytes<1>{}, false ) - -// // ---------- -// // template<> -// // const fixed_bytes -// // bool operator!=(const fixed_bytes, const fixed_bytes) -// CHECK_EQUAL( fixed_bytes<1>{} != fixed_bytes<1>{}, true ) -// CHECK_EQUAL( fixed_bytes<1>{} != fixed_bytes<1>{}, false ) - -// // ---------- -// // template<> -// // const fixed_bytes -// // bool operator<(const fixed_bytes, const fixed_bytes) -// CHECK_EQUAL( fixed_bytes<1>{} < fixed_bytes<1>{}, true ) -// CHECK_EQUAL( fixed_bytes<1>{} < fixed_bytes<1>{}, false ) - -// // ---------- -// // template<> -// // const fixed_bytes -// // bool operator<=(const fixed_bytes, const fixed_bytes) -// CHECK_EQUAL( fixed_bytes<1>{} <= fixed_bytes<1>{}, true ) -// CHECK_EQUAL( fixed_bytes<1>{} <= fixed_bytes<1>{}, false ) - -// // ---------- -// // template<> -// // const fixed_bytes -// // bool operator>(const fixed_bytes, const fixed_bytes) -// CHECK_EQUAL( fixed_bytes<1>{} > fixed_bytes<1>{}, true ) -// CHECK_EQUAL( fixed_bytes<1>{} > fixed_bytes<1>{}, false ) - -// // ---------- -// // template<> -// // const fixed_bytes -// // bool operator>=(const fixed_bytes, const fixed_bytes) -// CHECK_EQUAL( fixed_bytes<1>{} >= fixed_bytes<1>{}, true ) -// CHECK_EQUAL( fixed_bytes<1>{} >= fixed_bytes<1>{}, false ) +CHECK_EQUAL( fixed_bytes<0>{}.size(), 0 ) +CHECK_EQUAL( fixed_bytes<1>{}.size(), 1 ) +CHECK_EQUAL( fixed_bytes<32>{}.size(), 2 ) +CHECK_EQUAL( fixed_bytes<512>{}.size(), 32 ) + +// --------------------------------------------------------------------------- +// friend bool operator== <>(const fixed_bytes, const fixed_bytes) +static const fixed_bytes<32> fb_cmp0{fixed_bytes<32>::make_from_word_sequence(0ULL,0ULL,0ULL,0ULL)}; +static const fixed_bytes<32> fb_cmp1{fixed_bytes<32>::make_from_word_sequence(1ULL,1ULL,1ULL,1ULL)}; +static const fixed_bytes<32> fb_cmp2{fixed_bytes<32>::make_from_word_sequence(2ULL,2ULL,2ULL,2ULL)}; + +CHECK_EQUAL( fb_cmp1 == fb_cmp1, true ) +CHECK_EQUAL( fb_cmp0 == fb_cmp1, false ) + +// --------------------------------------------------------------------------- +// friend bool operator!= <>(const fixed_bytes, const fixed_bytes) +CHECK_EQUAL( fb_cmp1 != fb_cmp1, false ) +CHECK_EQUAL( fb_cmp0 != fb_cmp1, true ) + +// --------------------------------------------------------------------------- +// friend bool operator< <>(const fixed_bytes, const fixed_bytes) +CHECK_EQUAL( fb_cmp0 < fb_cmp1, true ) +CHECK_EQUAL( fb_cmp1 < fb_cmp1, false ) + +// --------------------------------------------------------------------------- +// friend bool operator<= <>(const fixed_bytes, const fixed_bytes) +CHECK_EQUAL( fb_cmp1 <= fb_cmp1, true ) +CHECK_EQUAL( fb_cmp2 <= fb_cmp1, false ) + +// --------------------------------------------------------------------------- +// friend bool operator> <>(const fixed_bytes, const fixed_bytes) +CHECK_EQUAL( fb_cmp1 > fb_cmp0, true ) +CHECK_EQUAL( fb_cmp1 > fb_cmp1, false ) + +// --------------------------------------------------------------------------- +// friend bool operator>= <>(const fixed_bytes, const fixed_bytes) +CHECK_EQUAL( fb_cmp1 >= fb_cmp1, true ) +CHECK_EQUAL( fb_cmp1 >= fb_cmp2, false ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 6ed7714226..7fbab91262 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -3,13 +3,19 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include +#include + #include #include +using std::numeric_limits; +using std::string; + using eosio::name; -const uint64_t u64min = std::numeric_limits::min(); // 0ULL -const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL +static constexpr uint64_t u64min = numeric_limits::min(); // 0ULL +static constexpr uint64_t u64max = numeric_limits::max(); // 18446744073709551615ULL // Definitions in `eosio.cdt/libraries/eosiolib/name.hpp` EOSIO_TEST_BEGIN(name_type_test) @@ -28,8 +34,9 @@ EOSIO_TEST_BEGIN(name_type_test) CHECK_EQUAL( name{name::raw{1ULL}}.value, 1ULL ) CHECK_EQUAL( name{name::raw{u64max}}.value, u64max ) - // Note that these are the exact `uint64_t` value representations of the given string - //// constexpr explicit name(std::string_view) + //// constexpr explicit name(string_view) + // Note: + // These are the exact `uint64_t` value representations of the given string CHECK_EQUAL( name{"1"}.value, 576460752303423488ULL ) CHECK_EQUAL( name{"5"}.value, 2882303761517117440ULL ) CHECK_EQUAL( name{"a"}.value, 3458764513820540928ULL ) @@ -122,12 +129,6 @@ EOSIO_TEST_BEGIN(name_type_test) CHECK_EQUAL( name{"eosioaccou.nj"}.suffix(), name{"nj"} ) CHECK_EQUAL( name{"eosioaccoun.j"}.suffix(), name{"j"} ) - // Note that this case is ignored because '.' characters at the end of a name are ignored - // Make functions perfect mirrors of eachother (01/07/2019) - // `print_f("Value of suffix: %\n", name{"eosioaccounj."}.suffix() );` - // `print_f("Value of expected suffix: \n" );` - // `eosio_assert( name{"eosioaccounj."}.suffix() == name{""}, "name.suffix() != \"\"" );` - CHECK_EQUAL( name{"e.o.s.i.o.a.c"}.suffix(), name{"c"} ) CHECK_EQUAL( name{"eos.ioa.cco"}.suffix(), name{"cco"} ) @@ -176,56 +177,57 @@ EOSIO_TEST_BEGIN(name_type_test) // char* write_as_string(char*, char*)const char buffer[13]{}; - std::string test_str{"1"}; - name{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - - name{test_str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - - // Note that any '.' characters at the end of a name are ignored - name{test_str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); + string str{"1"}; + name{str}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "5"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "a"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "z"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + + name{str = "abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "123"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + + // Note: + // Any '.' characters at the end of a name are ignored + name{str = ".abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = ".........abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "123."}.write_as_string( buffer, buffer + sizeof(buffer) ); CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) - name{test_str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); + name{str = "123........."}.write_as_string( buffer, buffer + sizeof(buffer) ); CHECK_EQUAL( memcmp("123", buffer, 3), 0 ) - name{test_str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); + name{str = ".a.b.c.1.2.3."}.write_as_string( buffer, buffer + sizeof(buffer) ); CHECK_EQUAL( memcmp(".a.b.c.1.2.3", buffer, 12), 0 ) - name{test_str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - - name{test_str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - - name{test_str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - name{test_str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); - CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - - // ---------------------------- - // std::string to_string()const + name{str = "abc.123"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "123.abc"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + + name{str = "12345abcdefgj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "hijklmnopqrsj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "tuvwxyz.1234j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + + name{str = "111111111111j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "555555555555j"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "aaaaaaaaaaaaj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + name{str = "zzzzzzzzzzzzj"}.write_as_string( buffer, buffer + sizeof(buffer) ); + CHECK_EQUAL( memcmp(str.c_str(), buffer, strlen(str.c_str())), 0 ) + + // ----------------------- + // string to_string()const CHECK_EQUAL( name{"1"}.to_string(), "1" ) CHECK_EQUAL( name{"5"}.to_string(), "5" ) CHECK_EQUAL( name{"a"}.to_string(), "a" ) diff --git a/tests/unit/rope_tests.cpp b/tests/unit/rope_tests.cpp new file mode 100644 index 0000000000..b6ef61814d --- /dev/null +++ b/tests/unit/rope_tests.cpp @@ -0,0 +1,74 @@ +/** + * @file + * @copyright defined in eosio.cdt/LICENSE.txt + */ + +#include +#include +#include +#include + +using namespace eosio::native; + +EOSIO_TEST_BEGIN(rope_test) + silence_output(false); + eosio::rope r("test string 0"); + r += ", test string 1"; + r += ", test string 2"; + r += ", test string 3"; + r += ", test string 4"; + r += ", test string 5"; + r += ", test string 6"; + r += ", test string 7"; + + std::string s("test string 0"); + s += ", test string 1"; + s += ", test string 2"; + s += ", test string 3"; + s += ", test string 4"; + s += ", test string 5"; + s += ", test string 6"; + s += ", test string 7"; + + eosio::rope r2 = r + eosio::rope("lhs") + eosio::rope("rhs") + "some string"; + std::string s2 = s + std::string("lhs") + std::string("rhs") + "some string"; + + r2 += eosio::rope("rvalue +="); + s2 += std::string("rvalue +="); + + r2 = r2 + r2; + s2 = s2 + s2; + + r2 += "the end"; + s2 += "the end"; + + eosio::rope r3(r2); + std::string s3(s2); + + REQUIRE_EQUAL(s.compare(std::string(r.c_str())), 0); + REQUIRE_EQUAL(s2.compare(std::string(r2.c_str())), 0); + REQUIRE_EQUAL(s3.compare(std::string(r3.c_str())), 0); + + REQUIRE_EQUAL(s.length(), r.length()); + REQUIRE_EQUAL(s2.length(), r2.length()); + REQUIRE_EQUAL(s3.length(), r3.length()); + + for (int i=0; i < s.length(); i++) { + REQUIRE_EQUAL(s[i], r[i]); + } + + for (int i=0; i < s2.length(); i++) { + REQUIRE_EQUAL(s2[i], r2[i]); + } + + for (int i=0; i < s3.length(); i++) { + REQUIRE_EQUAL(s3[i], r3[i]); + } + + silence_output(false); +EOSIO_TEST_END + +int main(int argc, char** argv) { + EOSIO_TEST(rope_test); + return has_failed(); +} diff --git a/tests/unit/serialize_tests.cpp b/tests/unit/serialize_tests.cpp index d7f2aca3a9..ed1d2af4e9 100644 --- a/tests/unit/serialize_tests.cpp +++ b/tests/unit/serialize_tests.cpp @@ -3,6 +3,12 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include +#include +#include +#include +#include + #include #include #include @@ -55,7 +61,7 @@ EOSIO_TEST_BEGIN(serialize_test) datastream ds_expected{ds_expected_buffer, buffer_size}; // Testing base structures - const B b{'c'}; + static constexpr B b{'c'}; B bb; ds_expected << b.c; ds << b; @@ -70,7 +76,7 @@ EOSIO_TEST_BEGIN(serialize_test) fill(begin(ds_expected_buffer), end(ds_expected_buffer), 0); // Testing derived structures - const D1 d1{'c', 42}; + static constexpr D1 d1{'c', 42}; D1 dd1; ds_expected << d1.c << d1.i; ds << d1; @@ -86,7 +92,7 @@ EOSIO_TEST_BEGIN(serialize_test) fill(begin(ds_expected_buffer), end(ds_expected_buffer), 0); // Testing multiple layers of derived structures - const D2 d2{'c', 42, {1.1, 2.2}}; + static const D2 d2{'c', 42, {1.1, 2.2}}; D1 dd2; ds_expected << d2.c << d2.i << d2.v; ds << d2; diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index 6c1265b4e4..defdeca315 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -3,16 +3,22 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include +#include + #include #include +using std::numeric_limits; +using std::string; + using eosio::name; using eosio::symbol_code; using eosio::symbol; using eosio::extended_symbol; -const uint64_t u64min = std::numeric_limits::min(); // 0ULL -const uint64_t u64max = std::numeric_limits::max(); // 18446744073709551615ULL +static constexpr uint64_t u64min = numeric_limits::min(); // 0ULL +static constexpr uint64_t u64max = numeric_limits::max(); // 18446744073709551615ULL // Definitions in `eosio.cdt/libraries/eosiolib/symbol.hpp` EOSIO_TEST_BEGIN(symbol_code_type_test) @@ -27,7 +33,7 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) CHECK_EQUAL( symbol_code{1ULL}.raw(), 1ULL ) CHECK_EQUAL( symbol_code{u64max}.raw(), u64max ) - //// constexpr explicit symbol_code(std::string_view str) + //// constexpr explicit symbol_code(string_view str) CHECK_EQUAL( symbol_code{"A"}.raw(), 65ULL ) CHECK_EQUAL( symbol_code{"Z"}.raw(), 90ULL ) CHECK_EQUAL( symbol_code{"AAAAAAA"}.raw(), 18367622009667905ULL ) @@ -76,7 +82,7 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) // char* write_as_string(char*, char*)const char buffer[7]{}; - std::string test_str{"A"}; + string test_str{"A"}; symbol_code{test_str}.write_as_string( buffer, buffer + sizeof(buffer) ); CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) symbol_code{test_str = "Z"}.write_as_string( buffer, buffer + sizeof(buffer) ); @@ -86,8 +92,8 @@ EOSIO_TEST_BEGIN(symbol_code_type_test) symbol_code{test_str = "ZZZZZZZ"}.write_as_string( buffer, buffer + sizeof(buffer) ); CHECK_EQUAL( memcmp(test_str.c_str(), buffer, strlen(test_str.c_str())), 0 ) - // ---------------------------- - // std::string to_string()const + // ----------------------- + // string to_string()const CHECK_EQUAL( symbol_code{"A"}.to_string(), "A" ) CHECK_EQUAL( symbol_code{"Z"}.to_string(), "Z" ) CHECK_EQUAL( symbol_code{"AAAAAAA"}.to_string(), "AAAAAAA" ) @@ -121,10 +127,10 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(symbol_type_test) silence_output(true); - symbol_code sc0{"A"}; - symbol_code sc1{"Z"}; - symbol_code sc2{"AAAAAAA"}; - symbol_code sc3{"ZZZZZZZ"}; + static constexpr symbol_code sc0{"A"}; + static constexpr symbol_code sc1{"Z"}; + static constexpr symbol_code sc2{"AAAAAAA"}; + static constexpr symbol_code sc3{"ZZZZZZZ"}; //// constexpr symbol() // constexpr uint64_t raw()const @@ -135,8 +141,9 @@ EOSIO_TEST_BEGIN(symbol_type_test) CHECK_EQUAL( symbol{1ULL}.raw(), 1ULL ) CHECK_EQUAL( symbol{u64max}.raw(), u64max ) - //// constexpr symbol(std::string_view, uint8_t) - // Note that unless constructed with `initializer_list`, precision does not check for wrap-around + //// constexpr symbol(string_view, uint8_t) + // Note: + // Unless constructed with `initializer_list`, precision does not check for wrap-around CHECK_EQUAL( (symbol{sc0, 0}.raw()), 16640ULL ) CHECK_EQUAL( (symbol{sc1, 0}.raw()), 23040ULL ) CHECK_EQUAL( (symbol{sc2, 0}.raw()), 4702111234474983680ULL ) @@ -188,10 +195,11 @@ EOSIO_TEST_BEGIN(symbol_type_test) CHECK_EQUAL( (symbol{"SYMBOLL", 0}.operator bool()), true ) CHECK_EQUAL( (!symbol{"", 0}.operator bool()), true ) CHECK_EQUAL( (!symbol{"SYMBOLL", 0}.operator bool()), false ) - - // Note: uncomment once print checking has been resolved + // --------------------- // void print(bool)const + // Note: + // Uncomment once print checking has been resolved // CHECK_PRINT( "0,A", [&](){symbol{"A", 0}.print(true);} ); // CHECK_PRINT( "0,Z", [&](){symbol{"Z", 0}.print(true);} ); // CHECK_PRINT( "255,AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(true);} ); @@ -225,19 +233,19 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(extended_symbol_type_test) silence_output(true); - name n0{"1"}; - name n1{"5"}; - name n2{"a"}; - name n3{"z"}; - name n4{"111111111111j"}; - name n5{"555555555555j"}; - name n6{"aaaaaaaaaaaaj"}; - name n7{"zzzzzzzzzzzzj"}; + static constexpr name n0{"1"}; + static constexpr name n1{"5"}; + static constexpr name n2{"a"}; + static constexpr name n3{"z"}; + static constexpr name n4{"111111111111j"}; + static constexpr name n5{"555555555555j"}; + static constexpr name n6{"aaaaaaaaaaaaj"}; + static constexpr name n7{"zzzzzzzzzzzzj"}; - symbol s0{"A",0}; - symbol s1{"Z",0}; - symbol s2{"AAAAAAA",255}; - symbol s3{"ZZZZZZZ",255}; + static constexpr symbol s0{"A",0}; + static constexpr symbol s1{"Z",0}; + static constexpr symbol s2{"AAAAAAA",255}; + static constexpr symbol s3{"ZZZZZZZ",255}; //// constexpr extended_symbol() // constexpr name get_symbol() @@ -263,9 +271,10 @@ EOSIO_TEST_BEGIN(extended_symbol_type_test) CHECK_EQUAL( (extended_symbol{s3, n6}.get_contract().value), 3570337562653461615ULL ) CHECK_EQUAL( (extended_symbol{s3, n7}.get_contract().value), u64max ) - // Note: uncomment once print checking has been resolved // --------------------- // void print(bool)const + // Note: + // Uncomment once print checking has been resolved // CHECK_PRINT( "0,A@1", [&](){extended_symbol{s0, n0}.print(true);} ); // CHECK_PRINT( "0,A@5", [&](){extended_symbol{s0, n1}.print(true);} ); // CHECK_PRINT( "0,Z@a", [&](){extended_symbol{s1, n2}.print(true);} ); diff --git a/tests/unit/system_tests.cpp b/tests/unit/system_tests.cpp index 90ec77f79a..4e568115f7 100644 --- a/tests/unit/system_tests.cpp +++ b/tests/unit/system_tests.cpp @@ -8,9 +8,11 @@ #include #include -using eosio::check; +using std::move; using std::string; +using eosio::check; + // Definitions in `eosio.cdt/libraries/eosiolib/system.hpp` EOSIO_TEST_BEGIN(system_test) silence_output(true); @@ -19,26 +21,22 @@ EOSIO_TEST_BEGIN(system_test) // inline void check(bool, const char*) CHECK_ASSERT( "asserted", []() { const char* str{"asserted"}; check(false, str);} ); - // ------------------------------------------- - // inline void check(bool, const std::string&) + // -------------------------------------- + // inline void check(bool, const string&) CHECK_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, str);} ); - // -------------------------------------------- - // inline void check(bool, const std::string&&) - CHECK_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, std::move(str));} ); + // --------------------------------------- + // inline void check(bool, const string&&) + CHECK_ASSERT( "asserted", []() { const string str{"asserted"}; check(false, move(str));} ); // -------------------------------------------- // inline void check(bool, const char*, size_t) CHECK_ASSERT( "assert", []() { const char* str{"asserted"}; check(false, str, 6);} ); - // --------------------------------------------------- - // inline void check(bool, const std::string&, size_t) + // ---------------------------------------------- + // inline void check(bool, const string&, size_t) CHECK_ASSERT( "assert", []() { const string str{"asserted"}; check(false, str, 6);} ); - // Note: this function will not work when given `0` as the `uint64_t` value. - // Maybe some invariants should be established as to what is acceptable and what isn't. - // For example: - // CHECK_ASSERT("0", []() { check(false, 0);} ); // --------------------------------- // inline void check(bool, uint64_t) CHECK_ASSERT("1", []() { check(false, 1);} ); diff --git a/tests/unit/time_tests.cpp b/tests/unit/time_tests.cpp index 186f350ac9..7c9971db07 100644 --- a/tests/unit/time_tests.cpp +++ b/tests/unit/time_tests.cpp @@ -18,17 +18,17 @@ using eosio::time_point; using eosio::time_point_sec; using eosio::block_timestamp; -const int64_t i64min = numeric_limits::min(); // -9223372036854775808 -const int64_t i64max = numeric_limits::max(); // 9223372036854775807 +static constexpr int64_t i64min = numeric_limits::min(); // -9223372036854775808 +static constexpr int64_t i64max = numeric_limits::max(); // 9223372036854775807 -const uint32_t u32min = numeric_limits::min(); // 0 -const uint32_t u32max = numeric_limits::max(); // 4294967295 +static constexpr uint32_t u32min = numeric_limits::min(); // 0 +static constexpr uint32_t u32max = numeric_limits::max(); // 4294967295 // Definitions in `eosio.cdt/libraries/eosiolib/time.hpp` EOSIO_TEST_BEGIN(microseconds_type_test) silence_output(true); - /// explicit microseconds(uint64_t)/int64_t count() + //// explicit microseconds(uint64_t)/int64_t count() CHECK_EQUAL( microseconds{}._count, 0ULL ) CHECK_EQUAL( microseconds{i64max}._count, i64max ) CHECK_EQUAL( microseconds{i64min}._count, i64min ) @@ -128,13 +128,13 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(time_point_type_test) silence_output(true); - microseconds ms0 { 0LL}; - microseconds ms1 { 1LL}; - microseconds msn1{-1LL}; - microseconds ms_min{i64min}; - microseconds ms_max{i64max}; + static const microseconds ms0 { 0LL}; + static const microseconds ms1 { 1LL}; + static const microseconds msn1{-1LL}; + static const microseconds ms_min{i64min}; + static const microseconds ms_max{i64max}; - /// explicit time_point(microseconds) + //// explicit time_point(microseconds) // microseconds& time_since_epoch() CHECK_EQUAL( time_point{ms0}.time_since_epoch(), ms0 ) CHECK_EQUAL( time_point{ms1}.time_since_epoch(), ms1 ) @@ -215,28 +215,28 @@ EOSIO_TEST_END EOSIO_TEST_BEGIN(time_point_sec_type_test) silence_output(true); - microseconds ms0 { 0LL}; - microseconds ms1 { 1LL}; - microseconds msn1{-1LL}; - microseconds ms_min{i64min}; - microseconds ms_max{i64max}; + static const microseconds ms0 { 0LL}; + static const microseconds ms1 { 1LL}; + static const microseconds msn1{-1LL}; + static const microseconds ms_min{i64min}; + static const microseconds ms_max{i64max}; - time_point tp0{ms0}; - time_point tp1{ms1}; - time_point tpn1{msn1}; - time_point tp_min{ms_min}; - time_point tp_max{ms_max}; + static const time_point tp0{ms0}; + static const time_point tp1{ms1}; + static const time_point tpn1{msn1}; + static const time_point tp_min{ms_min}; + static const time_point tp_max{ms_max}; - /// time_point_sec() + //// time_point_sec() //uint32_t sec_since_epoch()const CHECK_EQUAL( time_point_sec{}.utc_seconds, 0 ) - /// explicit time_point_sec(uint32_t) + //// explicit time_point_sec(uint32_t) CHECK_EQUAL( time_point_sec{u32min}.utc_seconds, 0 ) CHECK_EQUAL( time_point_sec{u32max}.utc_seconds, u32max ) CHECK_EQUAL( time_point_sec{u32max + 1}.utc_seconds, 0 ) - /// time_point_sec(const time_point&) + //// time_point_sec(const time_point&) CHECK_EQUAL( time_point_sec{tp0}.utc_seconds, ms0._count / 1000000 ) CHECK_EQUAL( time_point_sec{tp1}.utc_seconds, ms1._count / 1000000 ) CHECK_EQUAL( time_point_sec{tpn1}.utc_seconds, msn1._count / 1000000 ) @@ -398,18 +398,18 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) static const time_point_sec tps1{tp1}; static const time_point_sec tps2{tp2}; - /// explicit block_timestamp(uint32_t) + //// explicit block_timestamp(uint32_t) CHECK_EQUAL( block_timestamp{}.slot, 0 ) CHECK_EQUAL( block_timestamp{u32min}.slot, u32min ) CHECK_EQUAL( block_timestamp{u32max}.slot, u32max ) - /// block_timestamp(const time_point&) + //// block_timestamp(const time_point&) // void set_time_point(const time_point&) CHECK_EQUAL( block_timestamp{tp0}.slot, 0 ) CHECK_EQUAL( block_timestamp{tp1}.slot, 1 ) CHECK_EQUAL( block_timestamp{tp2}.slot, 2 ) - /// block_timestamp(const time_point_sec&) + //// block_timestamp(const time_point_sec&) // void set_time_point(const time_point_sec&) // Infeasible constructor? @@ -429,7 +429,7 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) CHECK_EQUAL( (block_timestamp{1}.next()), block_timestamp{2} ) CHECK_EQUAL( (block_timestamp{2}.next()), block_timestamp{3} ) - REQUIRE_ASSERT( "block timestamp overflow", [](){block_timestamp{u32max}.next();} ) + CHECK_ASSERT( "block timestamp overflow", [](){block_timestamp{u32max}.next();} ) // ------------------------------- // time_point to_time_point()const @@ -445,13 +445,13 @@ EOSIO_TEST_BEGIN(block_timestamp_type_test) // --------------------------------- // void operator=(const time_point&) - block_timestamp opequal{}; - opequal = tp0; - CHECK_EQUAL( opequal, block_timestamp{tp0} ) - opequal = tp1; - CHECK_EQUAL( opequal, block_timestamp{tp1} ) - opequal = tp2; - CHECK_EQUAL( opequal, block_timestamp{tp2} ) + block_timestamp bt_equal_op{}; + bt_equal_op = tp0; + CHECK_EQUAL( bt_equal_op, block_timestamp{tp0} ) + bt_equal_op = tp1; + CHECK_EQUAL( bt_equal_op, block_timestamp{tp1} ) + bt_equal_op = tp2; + CHECK_EQUAL( bt_equal_op, block_timestamp{tp2} ) // --------------------------------------- // bool operator==(const block_timestamp&) diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index 9b5791e8df..5c4756f664 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -3,6 +3,8 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include + #include #include #include @@ -11,22 +13,22 @@ using std::numeric_limits; using eosio::datastream; -const uint32_t u32min = numeric_limits::min(); // 0 -const uint32_t u32max = numeric_limits::max(); // 4294967295 +static constexpr uint32_t u32min = numeric_limits::min(); // 0 +static constexpr uint32_t u32max = numeric_limits::max(); // 4294967295 -const int32_t i32min = numeric_limits::min(); // -2147483648 -const int32_t i32max = numeric_limits::max(); // 2147483647 +static constexpr int32_t i32min = numeric_limits::min(); // -2147483648 +static constexpr int32_t i32max = numeric_limits::max(); // 2147483647 // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(unsigned_int_type_test) - silence_output(true); + silence_output(false); - /// unsigned_int(uint32_t) + //// unsigned_int(uint32_t) CHECK_EQUAL( unsigned_int{}.value, 0 ) CHECK_EQUAL( unsigned_int{u32min}.value, 0 ) CHECK_EQUAL( unsigned_int{u32max}.value, 4294967295 ) - /// unsigned_int(T) + //// unsigned_int(T) CHECK_EQUAL( unsigned_int{uint8_t{0}}.value, 0 ) CHECK_EQUAL( unsigned_int{uint16_t{1}}.value, 1 ) CHECK_EQUAL( unsigned_int{uint32_t{2}}.value, 2 ) @@ -39,10 +41,10 @@ EOSIO_TEST_BEGIN(unsigned_int_type_test) // --------------------------------- // unsigned_int& operator=(uint32_t) - unsigned_int ui0{}; - unsigned_int ui1{42}; + static const unsigned_int ui0{42}; + unsigned_int ui1{}; - ui0 = ui1; + ui1 = ui0; CHECK_EQUAL( ui0 == ui1, true ) // ------------------------------------------------------------ @@ -113,7 +115,7 @@ EOSIO_TEST_BEGIN(unsigned_int_type_test) datastream ds{datastream_buffer, buffer_size}; - const unsigned_int cui{42}; + static const unsigned_int cui{42}; unsigned_int ui{}; ds << cui; ds.seekp(0); @@ -125,9 +127,9 @@ EOSIO_TEST_END // Defined in `eosio.cdt/libraries/eosiolib/varint.hpp` EOSIO_TEST_BEGIN(signed_int_type_test) - silence_output(true); + silence_output(false); - /// signed_int(uint32_t) + //// signed_int(uint32_t) CHECK_EQUAL( signed_int{}.value, 0 ) CHECK_EQUAL( signed_int{i32min}.value, -2147483648 ) CHECK_EQUAL( signed_int{i32max}.value, 2147483647 ) @@ -140,39 +142,39 @@ EOSIO_TEST_BEGIN(signed_int_type_test) // -------------------------------- // signed_int& operator=(const T&) - signed_int i0{}; - signed_int i1{}; - signed_int i2{}; - signed_int i3{}; - - int8_t i8{0}; - int16_t i16{1}; - int32_t i32{2}; - int64_t i64{3}; - - i0 = i8; - i1 = i16; - i2 = i32; - i3 = i64; - - CHECK_EQUAL( i0.value, 0 ) - CHECK_EQUAL( i1.value, 1 ) - CHECK_EQUAL( i2.value, 2 ) - CHECK_EQUAL( i3.value, 3 ) + static const int8_t i8{0}; + static const int16_t i16{1}; + static const int32_t i32{2}; + static const int64_t i64{3}; + + signed_int si0{}; + signed_int si1{}; + signed_int si2{}; + signed_int si3{}; + + si0 = i8; + si1 = i16; + si2 = i32; + si3 = i64; + + CHECK_EQUAL( si0.value, 0 ) + CHECK_EQUAL( si1.value, 1 ) + CHECK_EQUAL( si2.value, 2 ) + CHECK_EQUAL( si3.value, 3 ) // -------------------------- // signed_int operator++(int) - signed_int post_inc0{0}; - signed_int post_inc1{1}; - CHECK_EQUAL( post_inc0++.value, 0 ) - CHECK_EQUAL( post_inc1++.value, 1 ) + signed_int si_post_inc0{0}; + signed_int si_post_inc1{1}; + CHECK_EQUAL( si_post_inc0++.value, 0 ) + CHECK_EQUAL( si_post_inc1++.value, 1 ) // ------------------------ // signed_int& operator++() - signed_int pre_inc0{0}; - signed_int pre_inc1{1}; - CHECK_EQUAL( ++pre_inc0.value, 1 ) - CHECK_EQUAL( ++pre_inc1.value, 2 ) + signed_int si_pre_inc0{0}; + signed_int si_pre_inc1{1}; + CHECK_EQUAL( ++si_pre_inc0.value, 1 ) + CHECK_EQUAL( ++si_pre_inc1.value, 2 ) // ------------------------------------------------------------ // friend bool operator==(const signed_int&, const uint32_t&) @@ -242,7 +244,7 @@ EOSIO_TEST_BEGIN(signed_int_type_test) datastream ds{datastream_buffer, buffer_size}; - const signed_int csi{-42}; + static const signed_int csi{-42}; signed_int si{}; ds << csi; ds.seekp(0); From 0de3c410b2f4057e0faa818f3e5cbfc4ef5c557f Mon Sep 17 00:00:00 2001 From: Bucky Kittinger Date: Wed, 20 Feb 2019 18:17:01 -0500 Subject: [PATCH 44/51] added llp64 fixes and fixed sse issues --- libraries/libc/musl | 2 +- tests/CMakeLists.txt | 1 + tests/unit/CMakeLists.txt | 3 +- tests/unit/asset_tests.cpp | 152 ++++++------ tests/unit/datastream_tests.cpp | 6 +- tests/unit/fixed_bytes_tests.cpp | 318 +++++++++++++------------- tests/unit/name_tests.cpp | 8 +- tests/unit/print_tests.cpp | 2 +- tools/include/compiler_options.hpp.in | 16 +- 9 files changed, 252 insertions(+), 256 deletions(-) diff --git a/libraries/libc/musl b/libraries/libc/musl index ab09461e6e..1fe3e67a0f 160000 --- a/libraries/libc/musl +++ b/libraries/libc/musl @@ -1 +1 @@ -Subproject commit ab09461e6e411f0c6d28ae0fac049602c936df49 +Subproject commit 1fe3e67a0f98abac43f1a613fd294d0e2808476d diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4af9ecad3d..6108af4e2a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,6 +5,7 @@ add_test( datastream_tests ${CMAKE_BINARY_DIR}/tests/unit/datastream_tests ) add_test( fixed_bytes_tests ${CMAKE_BINARY_DIR}/tests/unit/fixed_bytes_tests ) add_test( name_tests ${CMAKE_BINARY_DIR}/tests/unit/name_tests ) add_test( rope_tests ${CMAKE_BINARY_DIR}/tests/unit/rope_tests ) +add_test( print_tests ${CMAKE_BINARY_DIR}/tests/unit/print_tests ) add_test( serialize_tests ${CMAKE_BINARY_DIR}/tests/unit/serialize_tests ) add_test( symbol_tests ${CMAKE_BINARY_DIR}/tests/unit/symbol_tests ) add_test( system_tests ${CMAKE_BINARY_DIR}/tests/unit/system_tests ) diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 5ce28f0a67..589b6f9259 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -11,7 +11,8 @@ add_native_executable( rope_tests rope_tests.cpp ) add_native_executable( serialize_tests serialize_tests.cpp ) add_native_executable( symbol_tests symbol_tests.cpp ) add_native_executable( system_tests system_tests.cpp ) -add_native_executable(rope_tests rope_tests.cpp) +add_native_executable( rope_tests rope_tests.cpp ) +add_native_executable( print_tests print_tests.cpp ) add_native_executable( time_tests time_tests.cpp ) add_native_executable( varint_tests varint_tests.cpp ) diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index ebf3899a79..a53d40f963 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -22,7 +22,7 @@ static constexpr int64_t asset_max{ asset_mask}; // 4611686018427387903 // Definitions in `eosio.cdt/libraries/eosio/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) - silence_output(true); + silence_output(false); static constexpr symbol s0{"A", 0}; static constexpr symbol s1{"Z", 0}; @@ -148,44 +148,44 @@ EOSIO_TEST_BEGIN(asset_type_test) // Note: // Uncomment once print checking has been resolved - // CHECK_PRINT( "0. SYMBOLL", [&](){asset{0LL, sym_no_prec}.print();} ); - // CHECK_PRINT( "0. SYMBOLL", [&](){asset{-0LL, sym_no_prec}.print();} ); - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( - // [&]() { - // asset{0LL, sym_prec}.print(); - // }); - // ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( - // [&]() { - // asset{-0LL, sym_prec}.print(); - // }); - // ) - - // CHECK_PRINT( "1. SYMBOLL", [&](){asset{1LL, sym_no_prec}.print();} ); - // CHECK_PRINT( "--1. SYMBOLL", [&](){asset{-1LL, sym_no_prec}.print();} ); - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( - // [&]() { - // asset{1LL, sym_prec}.print(); - // }); - // ) - // CHECK_PRINT( "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( - // [&]() { - // asset{-1LL, sym_prec}.print(); - // }); - // ) - - // CHECK_PRINT( "--4611686018427387903. SYMBOLL", [&](){asset{asset_min, sym_no_prec}.print();} ); - // CHECK_PRINT( "4611686018427387903. SYMBOLL", [&](){asset{asset_max, sym_no_prec}.print();} ); - // CHECK_PRINT( "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( - // [&]() { - // asset{asset_min, sym_prec}.print(); - // }); - // ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( - // [&]() { - // asset{asset_max, sym_prec}.print(); - // }); - // ) + CHECK_PRINT( "0. SYMBOLL", [&](){asset{0LL, sym_no_prec}.print();} ); + CHECK_PRINT( "0. SYMBOLL", [&](){asset{-0LL, sym_no_prec}.print();} ); + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( + [&]() { + asset{0LL, sym_prec}.print(); + }) + ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( + [&]() { + asset{-0LL, sym_prec}.print(); + }) + ) + + CHECK_PRINT( "1. SYMBOLL", [&](){asset{1LL, sym_no_prec}.print();} ); + CHECK_PRINT( "--1. SYMBOLL", [&](){asset{-1LL, sym_no_prec}.print();} ); + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( + [&]() { + asset{1LL, sym_prec}.print(); + }) + ) + CHECK_PRINT( "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( + [&]() { + asset{-1LL, sym_prec}.print(); + }) + ) + + CHECK_PRINT( "--4611686018427387903. SYMBOLL", [&](){asset{asset_min, sym_no_prec}.print();} ); + CHECK_PRINT( "4611686018427387903. SYMBOLL", [&](){asset{asset_max, sym_no_prec}.print();} ); + CHECK_PRINT( "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( + [&]() { + asset{asset_min, sym_prec}.print(); + }) + ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( + [&]() { + asset{asset_max, sym_prec}.print(); + }) + ) for( uint8_t precision{0}; precision < 64; ++precision ) { CHECK_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), @@ -393,43 +393,47 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // That if there is no precision, there will be odd output: // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` // output: "0. A@1" - // CHECK_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) - // CHECK_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) - // CHECK_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) - // CHECK_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ) + CHECK_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) + CHECK_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) + CHECK_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) + CHECK_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ) - // CHECK_PRINT( "1.1 A@1", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"1"}}.print();} ) - // CHECK_PRINT( "1.1 A@5", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"5"}}.print();} ) - // CHECK_PRINT( "1.1 Z@a", [](){extended_asset{asset{int64_t{11}, symbol{"Z", 1}}, name{"a"}}.print();} ) - // CHECK_PRINT( "1.1 Z@z", [](){extended_asset{asset{int64_t{11}, symbol{"Z", 1}}, name{"z"}}.print();} ) + CHECK_PRINT( "1.1 A@1", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"1"}}.print();} ) + CHECK_PRINT( "1.1 A@5", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"5"}}.print();} ) + CHECK_PRINT( "1.1 Z@a", [](){extended_asset{asset{int64_t{11}, symbol{"Z", 1}}, name{"a"}}.print();} ) + CHECK_PRINT( "1.1 Z@z", [](){extended_asset{asset{int64_t{11}, symbol{"Z", 1}}, name{"z"}}.print();} ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 A@1", - // [](){extended_asset{asset{int64_t{11}, symbol{"A", 63}}, name{"1"}}.print();} ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 A@5", - // [](){extended_asset{asset{int64_t{11}, symbol{"A", 63}}, name{"5"}}.print();} ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 Z@a", - // [](){extended_asset{asset{int64_t{11}, symbol{"Z", 63}}, name{"a"}}.print();} ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 Z@z", - // [](){extended_asset{asset{int64_t{11}, symbol{"Z", 63}}, name{"z"}}.print();} ) - - // CHECK_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - // CHECK_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - // CHECK_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - // CHECK_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) - - // CHECK_PRINT( "1.1 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - // CHECK_PRINT( "1.1 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - // CHECK_PRINT( "1.1 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - // CHECK_PRINT( "1.1 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) - - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@111111111111j", - // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@555555555555j", - // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@aaaaaaaaaaaaj", - // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - // CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@zzzzzzzzzzzzj", - // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 A@1", + [](){extended_asset{asset{int64_t{11}, symbol{"A", 63}}, name{"1"}}.print();} ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 A@5", + [](){extended_asset{asset{int64_t{11}, symbol{"A", 63}}, name{"5"}}.print();} ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 Z@a", + [](){extended_asset{asset{int64_t{11}, symbol{"Z", 63}}, name{"a"}}.print();} ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 Z@z", + [](){extended_asset{asset{int64_t{11}, symbol{"Z", 63}}, name{"z"}}.print();} ) + + CHECK_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + CHECK_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + CHECK_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + CHECK_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + silence_output(false); + eosio::print("WHAT\n"); + extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print(); + eosio::print("\n"); + silence_output(false); + //CHECK_PRINT( "1.1 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + //CHECK_PRINT( "1.1 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + //CHECK_PRINT( "1.1 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + //CHECK_PRINT( "1.1 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + + //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@111111111111j", + // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@555555555555j", + // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@aaaaaaaaaaaaj", + // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@zzzzzzzzzzzzj", + // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) // ------------------------------- // extended_asset operator-()const diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 29933738e2..8242fb445e 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -83,12 +83,10 @@ EOSIO_TEST_BEGIN(datastream_test) // T pos()const CHECK_EQUAL( ds.pos(), datastream_buffer+256 ) - CHECK_EQUAL( ds.pos(), datastream_buffer ) // inline bool seekp(size_t) ds.seekp(0); CHECK_EQUAL( ds.pos(), datastream_buffer ) - CHECK_EQUAL( ds.pos(), datastream_buffer+256 ) // inline bool write(const char*, size_t) fill(begin(buffer), end(buffer), 1); // Fill `buffer` with a new set of values @@ -613,9 +611,7 @@ EOSIO_TEST_BEGIN(misc_datastream_test) EOSIO_TEST_END int main(int argc, char* argv[]) { - // Note: - // Uncomment once segfaulting during `memcpy` has been resolved - // EOSIO_TEST(datastream_test); + EOSIO_TEST(datastream_test); EOSIO_TEST(datastream_specialization_test); EOSIO_TEST(datastream_stream_test); EOSIO_TEST(misc_datastream_test); diff --git a/tests/unit/fixed_bytes_tests.cpp b/tests/unit/fixed_bytes_tests.cpp index 346d03ada0..965ed86362 100644 --- a/tests/unit/fixed_bytes_tests.cpp +++ b/tests/unit/fixed_bytes_tests.cpp @@ -16,165 +16,165 @@ using eosio::fixed_bytes; EOSIO_TEST_BEGIN(fixed_bytes_test) silence_output(true); -//// constexpr fixed_bytes() -// static constexpr size_t padded_bytes() -CHECK_EQUAL( fixed_bytes<20>{}.padded_bytes(), 12 ) -CHECK_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) -CHECK_EQUAL( fixed_bytes<64>{}.padded_bytes(), 0 ) -CHECK_EQUAL( fixed_bytes<128>{}.padded_bytes(), 0 ) -CHECK_EQUAL( fixed_bytes<256>{}.padded_bytes(), 0 ) - -//// fixed_bytes(const array&) -// static constexpr size_t num_words() -CHECK_EQUAL( (fixed_bytes<20>{array::word_t, 2>{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32>{array::word_t, 2>{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64>{array::word_t, 4>{}}.num_words()), 4 ) - -//// fixed_bytes(const std::array&) -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) - -//// fixed_bytes(const Word(&arr)[NumWords]) -static constexpr uint8_t ctor_arr8[1]{}; -static constexpr uint16_t ctor_arr16[1]{}; -static constexpr uint32_t ctor_arr32[1]{}; -static constexpr uint64_t ctor_arr64[1]{}; - -CHECK_EQUAL( (fixed_bytes<20> {ctor_arr8}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {ctor_arr8}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {ctor_arr8}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{ctor_arr8}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{ctor_arr8}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {ctor_arr16}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {ctor_arr16}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {ctor_arr16}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{ctor_arr16}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{ctor_arr16}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {ctor_arr32}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {ctor_arr32}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {ctor_arr32}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{ctor_arr32}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{ctor_arr32}.num_words()), 16 ) - -CHECK_EQUAL( (fixed_bytes<20> {ctor_arr64}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<32> {ctor_arr64}.num_words()), 2 ) -CHECK_EQUAL( (fixed_bytes<64> {ctor_arr64}.num_words()), 4 ) -CHECK_EQUAL( (fixed_bytes<128>{ctor_arr64}.num_words()), 8 ) -CHECK_EQUAL( (fixed_bytes<256>{ctor_arr64}.num_words()), 16 ) - -// ------------------------------------------- -// make_from_word_sequence(FirstWord, Rest...) - -// Possible discrepancy between two `make_from_word_sequence` and array constructor -// CHECK_EQUAL( (fixed_bytes<20>::make_from_word_sequence(1ULL, 2ULL)), -// (fixed_bytes<20>{array{1ULL, 2ULL}}) ) - -CHECK_EQUAL( (fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)), - (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}) ) - -// ------------------------------------------------- -// array extract_as_byte_array()const -static constexpr array extract_arr{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04 }; - -CHECK_EQUAL( (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}.extract_as_byte_array()), extract_arr ) - -// ---------------------------- -// const auto& get_array()const -CHECK_EQUAL( (fixed_bytes<0>{}.get_array()), (array{}) ) -CHECK_EQUAL( (fixed_bytes<1>{}.get_array()), (array{}) ) -CHECK_EQUAL( (fixed_bytes<32>{}.get_array()), (array{}) ) -CHECK_EQUAL( (fixed_bytes<512>{}.get_array()), (array{}) ) - -// ----------- -// auto data() -static constexpr fixed_bytes<0> fb_data0{}; -static constexpr fixed_bytes<1> fb_data1{}; -static constexpr fixed_bytes<32> fb_data2{}; -static constexpr fixed_bytes<512> fb_data3{}; - -CHECK_EQUAL( fb_data0.data(), fb_data0.get_array().data() ) -CHECK_EQUAL( fb_data1.data(), fb_data1.get_array().data() ) -CHECK_EQUAL( fb_data2.data(), fb_data2.get_array().data() ) -CHECK_EQUAL( fb_data3.data(), fb_data3.get_array().data() ) - -// ---------------- -// auto data()const -static constexpr fixed_bytes<0> cfb_data0{}; -static constexpr fixed_bytes<1> cfb_data1{}; -static constexpr fixed_bytes<32> cfb_data2{}; -static constexpr fixed_bytes<512> cfb_data3{}; - -CHECK_EQUAL( cfb_data0.data(), cfb_data0.get_array().data() ) -CHECK_EQUAL( cfb_data1.data(), cfb_data1.get_array().data() ) -CHECK_EQUAL( cfb_data2.data(), cfb_data2.get_array().data() ) -CHECK_EQUAL( cfb_data3.data(), cfb_data3.get_array().data() ) - -// ---------------- -// auto size()const -CHECK_EQUAL( fixed_bytes<0>{}.size(), 0 ) -CHECK_EQUAL( fixed_bytes<1>{}.size(), 1 ) -CHECK_EQUAL( fixed_bytes<32>{}.size(), 2 ) -CHECK_EQUAL( fixed_bytes<512>{}.size(), 32 ) - -// --------------------------------------------------------------------------- -// friend bool operator== <>(const fixed_bytes, const fixed_bytes) -static const fixed_bytes<32> fb_cmp0{fixed_bytes<32>::make_from_word_sequence(0ULL,0ULL,0ULL,0ULL)}; -static const fixed_bytes<32> fb_cmp1{fixed_bytes<32>::make_from_word_sequence(1ULL,1ULL,1ULL,1ULL)}; -static const fixed_bytes<32> fb_cmp2{fixed_bytes<32>::make_from_word_sequence(2ULL,2ULL,2ULL,2ULL)}; - -CHECK_EQUAL( fb_cmp1 == fb_cmp1, true ) -CHECK_EQUAL( fb_cmp0 == fb_cmp1, false ) - -// --------------------------------------------------------------------------- -// friend bool operator!= <>(const fixed_bytes, const fixed_bytes) -CHECK_EQUAL( fb_cmp1 != fb_cmp1, false ) -CHECK_EQUAL( fb_cmp0 != fb_cmp1, true ) - -// --------------------------------------------------------------------------- -// friend bool operator< <>(const fixed_bytes, const fixed_bytes) -CHECK_EQUAL( fb_cmp0 < fb_cmp1, true ) -CHECK_EQUAL( fb_cmp1 < fb_cmp1, false ) - -// --------------------------------------------------------------------------- -// friend bool operator<= <>(const fixed_bytes, const fixed_bytes) -CHECK_EQUAL( fb_cmp1 <= fb_cmp1, true ) -CHECK_EQUAL( fb_cmp2 <= fb_cmp1, false ) - -// --------------------------------------------------------------------------- -// friend bool operator> <>(const fixed_bytes, const fixed_bytes) -CHECK_EQUAL( fb_cmp1 > fb_cmp0, true ) -CHECK_EQUAL( fb_cmp1 > fb_cmp1, false ) - -// --------------------------------------------------------------------------- -// friend bool operator>= <>(const fixed_bytes, const fixed_bytes) -CHECK_EQUAL( fb_cmp1 >= fb_cmp1, true ) -CHECK_EQUAL( fb_cmp1 >= fb_cmp2, false ) + //// constexpr fixed_bytes() + // static constexpr size_t padded_bytes() + CHECK_EQUAL( fixed_bytes<20>{}.padded_bytes(), 12 ) + CHECK_EQUAL( fixed_bytes<32>{}.padded_bytes(), 0 ) + CHECK_EQUAL( fixed_bytes<64>{}.padded_bytes(), 0 ) + CHECK_EQUAL( fixed_bytes<128>{}.padded_bytes(), 0 ) + CHECK_EQUAL( fixed_bytes<256>{}.padded_bytes(), 0 ) + + //// fixed_bytes(const array&) + // static constexpr size_t num_words() + CHECK_EQUAL( (fixed_bytes<20>{array::word_t, 2>{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32>{array::word_t, 2>{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64>{array::word_t, 4>{}}.num_words()), 4 ) + + //// fixed_bytes(const std::array&) + CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + + CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + + CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + + CHECK_EQUAL( (fixed_bytes<20> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {array{}}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {array{}}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{array{}}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{array{}}.num_words()), 16 ) + + //// fixed_bytes(const Word(&arr)[NumWords]) + static constexpr uint8_t ctor_arr8[1]{}; + static constexpr uint16_t ctor_arr16[1]{}; + static constexpr uint32_t ctor_arr32[1]{}; + static constexpr uint64_t ctor_arr64[1]{}; + + CHECK_EQUAL( (fixed_bytes<20> {ctor_arr8}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {ctor_arr8}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {ctor_arr8}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{ctor_arr8}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{ctor_arr8}.num_words()), 16 ) + + CHECK_EQUAL( (fixed_bytes<20> {ctor_arr16}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {ctor_arr16}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {ctor_arr16}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{ctor_arr16}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{ctor_arr16}.num_words()), 16 ) + + CHECK_EQUAL( (fixed_bytes<20> {ctor_arr32}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {ctor_arr32}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {ctor_arr32}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{ctor_arr32}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{ctor_arr32}.num_words()), 16 ) + + CHECK_EQUAL( (fixed_bytes<20> {ctor_arr64}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<32> {ctor_arr64}.num_words()), 2 ) + CHECK_EQUAL( (fixed_bytes<64> {ctor_arr64}.num_words()), 4 ) + CHECK_EQUAL( (fixed_bytes<128>{ctor_arr64}.num_words()), 8 ) + CHECK_EQUAL( (fixed_bytes<256>{ctor_arr64}.num_words()), 16 ) + + // ------------------------------------------- + // make_from_word_sequence(FirstWord, Rest...) + + // Possible discrepancy between two `make_from_word_sequence` and array constructor + // CHECK_EQUAL( (fixed_bytes<20>::make_from_word_sequence(1ULL, 2ULL)), + // (fixed_bytes<20>{array{1ULL, 2ULL}}) ) + + CHECK_EQUAL( (fixed_bytes<32>::make_from_word_sequence(1ULL,2ULL,3ULL,4ULL)), + (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}) ) + + // ------------------------------------------------- + // array extract_as_byte_array()const + static constexpr array extract_arr{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04 }; + + CHECK_EQUAL( (fixed_bytes<32>{array{1ULL,2ULL,3ULL,4ULL}}.extract_as_byte_array()), extract_arr ) + + // ---------------------------- + // const auto& get_array()const + CHECK_EQUAL( (fixed_bytes<0>{}.get_array()), (array{}) ) + CHECK_EQUAL( (fixed_bytes<1>{}.get_array()), (array{}) ) + CHECK_EQUAL( (fixed_bytes<32>{}.get_array()), (array{}) ) + CHECK_EQUAL( (fixed_bytes<512>{}.get_array()), (array{}) ) + + // ----------- + // auto data() + static constexpr fixed_bytes<0> fb_data0{}; + static constexpr fixed_bytes<1> fb_data1{}; + static constexpr fixed_bytes<32> fb_data2{}; + static constexpr fixed_bytes<512> fb_data3{}; + + CHECK_EQUAL( fb_data0.data(), fb_data0.get_array().data() ) + CHECK_EQUAL( fb_data1.data(), fb_data1.get_array().data() ) + CHECK_EQUAL( fb_data2.data(), fb_data2.get_array().data() ) + CHECK_EQUAL( fb_data3.data(), fb_data3.get_array().data() ) + + // ---------------- + // auto data()const + static constexpr fixed_bytes<0> cfb_data0{}; + static constexpr fixed_bytes<1> cfb_data1{}; + static constexpr fixed_bytes<32> cfb_data2{}; + static constexpr fixed_bytes<512> cfb_data3{}; + + CHECK_EQUAL( cfb_data0.data(), cfb_data0.get_array().data() ) + CHECK_EQUAL( cfb_data1.data(), cfb_data1.get_array().data() ) + CHECK_EQUAL( cfb_data2.data(), cfb_data2.get_array().data() ) + CHECK_EQUAL( cfb_data3.data(), cfb_data3.get_array().data() ) + + // ---------------- + // auto size()const + CHECK_EQUAL( fixed_bytes<0>{}.size(), 0 ) + CHECK_EQUAL( fixed_bytes<1>{}.size(), 1 ) + CHECK_EQUAL( fixed_bytes<32>{}.size(), 2 ) + CHECK_EQUAL( fixed_bytes<512>{}.size(), 32 ) + + // --------------------------------------------------------------------------- + // friend bool operator== <>(const fixed_bytes, const fixed_bytes) + static const fixed_bytes<32> fb_cmp0{fixed_bytes<32>::make_from_word_sequence(0ULL,0ULL,0ULL,0ULL)}; + static const fixed_bytes<32> fb_cmp1{fixed_bytes<32>::make_from_word_sequence(1ULL,1ULL,1ULL,1ULL)}; + static const fixed_bytes<32> fb_cmp2{fixed_bytes<32>::make_from_word_sequence(2ULL,2ULL,2ULL,2ULL)}; + + CHECK_EQUAL( fb_cmp1 == fb_cmp1, true ) + CHECK_EQUAL( fb_cmp0 == fb_cmp1, false ) + + // --------------------------------------------------------------------------- + // friend bool operator!= <>(const fixed_bytes, const fixed_bytes) + CHECK_EQUAL( fb_cmp1 != fb_cmp1, false ) + CHECK_EQUAL( fb_cmp0 != fb_cmp1, true ) + + // --------------------------------------------------------------------------- + // friend bool operator< <>(const fixed_bytes, const fixed_bytes) + CHECK_EQUAL( fb_cmp0 < fb_cmp1, true ) + CHECK_EQUAL( fb_cmp1 < fb_cmp1, false ) + + // --------------------------------------------------------------------------- + // friend bool operator<= <>(const fixed_bytes, const fixed_bytes) + CHECK_EQUAL( fb_cmp1 <= fb_cmp1, true ) + CHECK_EQUAL( fb_cmp2 <= fb_cmp1, false ) + + // --------------------------------------------------------------------------- + // friend bool operator> <>(const fixed_bytes, const fixed_bytes) + CHECK_EQUAL( fb_cmp1 > fb_cmp0, true ) + CHECK_EQUAL( fb_cmp1 > fb_cmp1, false ) + + // --------------------------------------------------------------------------- + // friend bool operator>= <>(const fixed_bytes, const fixed_bytes) + CHECK_EQUAL( fb_cmp1 >= fb_cmp1, true ) + CHECK_EQUAL( fb_cmp1 >= fb_cmp2, false ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index 38b6a8dc56..cdda07d40b 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -45,10 +45,10 @@ EOSIO_TEST_BEGIN(name_type_test) CHECK_EQUAL( name{"abc"}.value, 3589368903014285312ULL ) CHECK_EQUAL( name{"123"}.value, 614178399182651392ULL ) - CHECK_EQUAL( name{".abc"}.value, 112167778219196416ULL ) - CHECK_EQUAL( name{".........abc"}.value, 102016ULL ) - CHECK_EQUAL( name{"123."}.value, 614178399182651392ULL ) - CHECK_EQUAL( name{"123........."}.value, 614178399182651392ULL ) + CHECK_EQUAL( name{".abc"}.value, 112167778219196416ULL ) + CHECK_EQUAL( name{".........abc"}.value, 102016ULL ) + CHECK_EQUAL( name{"123."}.value, 614178399182651392ULL ) + CHECK_EQUAL( name{"123........."}.value, 614178399182651392ULL ) CHECK_EQUAL( name{".a.b.c.1.2.3."}.value, 108209673814966320ULL ) CHECK_EQUAL( name{"abc.123"}.value, 3589369488740450304ULL ) diff --git a/tests/unit/print_tests.cpp b/tests/unit/print_tests.cpp index e188a6c297..ccaf61a44b 100644 --- a/tests/unit/print_tests.cpp +++ b/tests/unit/print_tests.cpp @@ -4,7 +4,7 @@ using namespace eosio::native; EOSIO_TEST_BEGIN(print_test) - silence_output(true); + silence_output(false); CHECK_PRINT("27", [](){ eosio::print((uint8_t)27); }); CHECK_PRINT("34", [](){ eosio::print((int)34); }); CHECK_PRINT([](std::string s){return s[0] == 'a';}, [](){ eosio::print((char)'a'); }); diff --git a/tools/include/compiler_options.hpp.in b/tools/include/compiler_options.hpp.in index 703cd29f81..2b4233acc0 100644 --- a/tools/include/compiler_options.hpp.in +++ b/tools/include/compiler_options.hpp.in @@ -346,26 +346,20 @@ static void GetCompDefaults(std::vector& copts) { copts.emplace_back("--target=wasm32"); copts.emplace_back("-ffreestanding"); } else { + copts.emplace_back("-Wunused-command-line-argument"); #ifdef __APPLE__ copts.emplace_back("--target=x86_64-unknown-darwin-macho"); copts.emplace_back("-mmacosx-version-min=10.13"); copts.emplace_back("-fno-stack-protector"); - copts.emplace_back("-m64"); #else copts.emplace_back("--target=x86_64-unknown-linux-gnu"); - copts.emplace_back("-m64"); #endif - if (!allow_sse_opt) { - copts.emplace_back("-Wno-error=unused-command-line-argument"); - copts.emplace_back("-fno-builtin"); - copts.emplace_back("-mno-sse"); - copts.emplace_back("-mno-sse2"); - copts.emplace_back("-mno-mmx"); - copts.emplace_back("-mno-3dnow"); - } + copts.emplace_back("-m64"); if (!fasm_opt) { + copts.emplace_back("-fno-builtin"); + copts.emplace_back("-mstackrealign"); copts.emplace_back("-DEOSIO_NATIVE"); - copts.emplace_back("-DLP64"); + copts.emplace_back("-DLLP64"); } } From bb32d452869b43f3a8a56c7ca35f33904e86c922 Mon Sep 17 00:00:00 2001 From: Bucky Kittinger Date: Wed, 20 Feb 2019 18:21:45 -0500 Subject: [PATCH 45/51] remove unneeded code --- tests/unit/asset_tests.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index a53d40f963..436190d1f5 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -416,11 +416,6 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) CHECK_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) CHECK_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) CHECK_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) - silence_output(false); - eosio::print("WHAT\n"); - extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print(); - eosio::print("\n"); - silence_output(false); //CHECK_PRINT( "1.1 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) //CHECK_PRINT( "1.1 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) //CHECK_PRINT( "1.1 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) From f29d37e44483c225db071c5ae723fe6ae66d8cbb Mon Sep 17 00:00:00 2001 From: Bucky Kittinger Date: Wed, 20 Feb 2019 21:49:38 -0500 Subject: [PATCH 46/51] use size_t and uint64_t for malloc instead of uint32_t and int32_t --- libraries/eosiolib/malloc.cpp | 114 +++++++++++++++++----------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/libraries/eosiolib/malloc.cpp b/libraries/eosiolib/malloc.cpp index 7b14d52d25..3adc4bb6fd 100644 --- a/libraries/eosiolib/malloc.cpp +++ b/libraries/eosiolib/malloc.cpp @@ -18,11 +18,11 @@ namespace eosio { extern "C" uintptr_t __get_heap_base(); void* sbrk(size_t num_bytes) { - constexpr uint32_t NBPPL2 = 16U; - constexpr uint32_t NBBP = 65536U; + constexpr size_t NBPPL2 = 16U; + constexpr size_t NBBP = 65536U; static bool initialized; - static uint32_t sbrk_bytes; + static size_t sbrk_bytes; if(!initialized) { sbrk_bytes = CURRENT_MEMORY * NBBP; initialized = true; @@ -31,14 +31,14 @@ namespace eosio { if(num_bytes > INT32_MAX) return reinterpret_cast(-1); - const uint32_t prev_num_bytes = sbrk_bytes; - const uint32_t current_pages = CURRENT_MEMORY; + const size_t prev_num_bytes = sbrk_bytes; + const size_t current_pages = CURRENT_MEMORY; // round the absolute value of num_bytes to an alignment boundary num_bytes = (num_bytes + 7U) & ~7U; // update the number of bytes allocated, and compute the number of pages needed - const uint32_t num_desired_pages = (sbrk_bytes + num_bytes + NBBP - 1) >> NBPPL2; + const size_t num_desired_pages = (sbrk_bytes + num_bytes + NBBP - 1) >> NBPPL2; if(num_desired_pages > current_pages) { if (GROW_MEMORY(num_desired_pages - current_pages) == -1) @@ -78,22 +78,22 @@ namespace eosio { memory* next_active_heap() { - constexpr uint32_t wasm_page_size = 64*1024; + constexpr size_t wasm_page_size = 64*1024; memory* const current_memory = _available_heaps + _active_heap; - const uint32_t current_memory_size = reinterpret_cast(sbrk(0)); - if(static_cast(current_memory_size) < 0) + const size_t current_memory_size = reinterpret_cast(sbrk(0)); + if(static_cast(current_memory_size) < 0) return nullptr; //grab up to the end of the current WASM memory page provided that it has 1KiB remaining, otherwise // grow to end of next page - uint32_t heap_adj; + size_t heap_adj; if(current_memory_size % wasm_page_size <= wasm_page_size-1024) heap_adj = (current_memory_size + wasm_page_size) - (current_memory_size % wasm_page_size) - current_memory_size; else heap_adj = (current_memory_size + wasm_page_size*2) - (current_memory_size % (wasm_page_size*2)) - current_memory_size; char* new_memory_start = reinterpret_cast(sbrk(heap_adj)); - if(reinterpret_cast(new_memory_start) == -1) { + if(reinterpret_cast(new_memory_start) == -1) { // ensure that any remaining unallocated memory gets cleaned up current_memory->cleanup_remaining(); ++_active_heap; @@ -114,7 +114,7 @@ namespace eosio { return next; } - void* malloc(uint32_t size) + void* malloc(size_t size) { if (size == 0) return nullptr; @@ -153,7 +153,7 @@ namespace eosio { if (buffer == nullptr) { - const uint32_t end_free_heap = _active_free_heap; + const size_t end_free_heap = _active_free_heap; do { @@ -171,7 +171,7 @@ namespace eosio { return buffer; } - void* realloc(void* ptr, uint32_t size) + void* realloc(void* ptr, size_t size) { if (size == 0) { @@ -182,7 +182,7 @@ namespace eosio { adjust_to_mem_block(size); char* realloc_ptr = nullptr; - uint32_t orig_ptr_size = 0; + size_t orig_ptr_size = 0; if (ptr != nullptr) { char* const char_ptr = static_cast(ptr); @@ -204,7 +204,7 @@ namespace eosio { if (new_alloc == nullptr) return nullptr; - const uint32_t copy_size = (size < orig_ptr_size) ? size : orig_ptr_size; + const size_t copy_size = (size < orig_ptr_size) ? size : orig_ptr_size; if (copy_size > 0) { memcpy(new_alloc, ptr, copy_size); @@ -230,9 +230,9 @@ namespace eosio { } } - void adjust_to_mem_block(uint32_t& size) + void adjust_to_mem_block(size_t& size) { - const uint32_t remainder = (size + _size_marker) & _rem_mem_block_mask; + const size_t remainder = (size + _size_marker) & _rem_mem_block_mask; if (remainder > 0) { size += _mem_block - remainder; @@ -249,32 +249,32 @@ namespace eosio { { } - void init(char* const mem_heap, uint32_t size) + void init(char* const mem_heap, size_t size) { _heap_size = size; _heap = mem_heap; } - uint32_t is_init() const + size_t is_init() const { return _heap != nullptr; } - uint32_t is_in_heap(const char* const ptr) const + size_t is_in_heap(const char* const ptr) const { const char* const end_of_buffer = _heap + _heap_size; const char* const first_ptr_of_buffer = _heap + _size_marker; return ptr >= first_ptr_of_buffer && ptr < end_of_buffer; } - uint32_t is_capacity_remaining() const + size_t is_capacity_remaining() const { return _offset + _size_marker < _heap_size; } - char* malloc(uint32_t size) + char* malloc(size_t size) { - uint32_t used_up_size = _offset + size + _size_marker; + size_t used_up_size = _offset + size + _size_marker; if (used_up_size > _heap_size) { return nullptr; @@ -286,7 +286,7 @@ namespace eosio { return new_buff.ptr(); } - char* malloc_from_freed(uint32_t size) + char* malloc_from_freed(size_t size) { eosio::check(_offset == _heap_size, "malloc_from_freed was designed to only be called after _heap was completely allocated"); @@ -311,7 +311,7 @@ namespace eosio { return nullptr; } - char* realloc_in_place(char* const ptr, uint32_t size, uint32_t* orig_ptr_size) + char* realloc_in_place(char* const ptr, size_t size, size_t* orig_ptr_size) { const char* const end_of_buffer = _heap + _heap_size; @@ -371,13 +371,13 @@ namespace eosio { return; // take the remaining memory and act like it was allocated - const uint32_t size = _heap_size - _offset - _size_marker; + const size_t size = _heap_size - _offset - _size_marker; buffer_ptr new_buff(&_heap[_offset + _size_marker], size, _heap + _heap_size); _offset = _heap_size; new_buff.mark_free(); } - bool expand_memory(char* exp_mem, uint32_t size) + bool expand_memory(char* exp_mem, size_t size) { if (_heap + _heap_size != exp_mem) return false; @@ -393,19 +393,19 @@ namespace eosio { public: buffer_ptr(void* ptr, const char* const heap_end) : _ptr(static_cast(ptr)) - , _size(*reinterpret_cast(static_cast(ptr) - _size_marker) & ~_alloc_memory_mask) + , _size(*reinterpret_cast(static_cast(ptr) - _size_marker) & ~_alloc_memory_mask) , _heap_end(heap_end) { } - buffer_ptr(void* ptr, uint32_t buff_size, const char* const heap_end) + buffer_ptr(void* ptr, size_t buff_size, const char* const heap_end) : _ptr(static_cast(ptr)) , _heap_end(heap_end) { size(buff_size); } - uint32_t size() const + size_t size() const { return _size; } @@ -419,11 +419,11 @@ namespace eosio { return next; } - void size(uint32_t val) + void size(size_t val) { // keep the same state (allocated or free) as was set before - const uint32_t memory_state = *reinterpret_cast(_ptr - _size_marker) & _alloc_memory_mask; - *reinterpret_cast(_ptr - _size_marker) = val | memory_state; + const size_t memory_state = *reinterpret_cast(_ptr - _size_marker) & _alloc_memory_mask; + *reinterpret_cast(_ptr - _size_marker) = val | memory_state; _size = val; } @@ -439,39 +439,39 @@ namespace eosio { void mark_alloc() { - *reinterpret_cast(_ptr - _size_marker) |= _alloc_memory_mask; + *reinterpret_cast(_ptr - _size_marker) |= _alloc_memory_mask; } void mark_free() { - *reinterpret_cast(_ptr - _size_marker) &= ~_alloc_memory_mask; + *reinterpret_cast(_ptr - _size_marker) &= ~_alloc_memory_mask; } bool is_alloc() const { - return *reinterpret_cast(_ptr - _size_marker) & _alloc_memory_mask; + return *reinterpret_cast(_ptr - _size_marker) & _alloc_memory_mask; } - bool merge_contiguous_if_available(uint32_t needed_size) + bool merge_contiguous_if_available(size_t needed_size) { return merge_contiguous(needed_size, true); } - bool merge_contiguous(uint32_t needed_size) + bool merge_contiguous(size_t needed_size) { return merge_contiguous(needed_size, false); } private: - bool merge_contiguous(uint32_t needed_size, bool all_or_nothing) + bool merge_contiguous(size_t needed_size, bool all_or_nothing) { // do not bother if there isn't contiguious space to allocate - if( all_or_nothing && uint32_t(_heap_end - _ptr) < needed_size ) + if( all_or_nothing && size_t(_heap_end - _ptr) < needed_size ) return false; - uint32_t possible_size = _size; + size_t possible_size = _size; while (possible_size < needed_size && (_ptr + possible_size < _heap_end)) { - const uint32_t next_mem_flag_size = *reinterpret_cast(_ptr + possible_size); + const size_t next_mem_flag_size = *reinterpret_cast(_ptr + possible_size); // if ALLOCed then done with contiguous free memory if (next_mem_flag_size & _alloc_memory_mask) break; @@ -483,12 +483,12 @@ namespace eosio { return false; // combine - const uint32_t new_size = possible_size < needed_size ? possible_size : needed_size; + const size_t new_size = possible_size < needed_size ? possible_size : needed_size; size(new_size); if (possible_size > needed_size) { - const uint32_t freed_size = possible_size - needed_size - _size_marker; + const size_t freed_size = possible_size - needed_size - _size_marker; buffer_ptr freed_remainder(_ptr + needed_size + _size_marker, freed_size, _heap_end); freed_remainder.mark_free(); } @@ -497,28 +497,28 @@ namespace eosio { } char* _ptr; - uint32_t _size; + size_t _size; const char* const _heap_end; }; - uint32_t _heap_size; + size_t _heap_size; char* _heap; - uint32_t _offset; + size_t _offset; }; - static const uint32_t _size_marker = sizeof(uint32_t); + static const size_t _size_marker = sizeof(size_t); // allocate memory in 8 char blocks - static const uint32_t _mem_block = 8; - static const uint32_t _rem_mem_block_mask = _mem_block - 1; - static const uint32_t _initial_heap_size = 8192;//32768; + static const size_t _mem_block = 8; + static const size_t _rem_mem_block_mask = _mem_block - 1; + static const size_t _initial_heap_size = 8192;//32768; // if sbrk is not called outside of this file, then this is the max times we can call it - static const uint32_t _heaps_size = 16; + static const size_t _heaps_size = 16; char _initial_heap[_initial_heap_size]; memory _available_heaps[_heaps_size]; - uint32_t _heaps_actual_size; - uint32_t _active_heap; - uint32_t _active_free_heap; - static const uint32_t _alloc_memory_mask = uint32_t(1) << 31; + size_t _heaps_actual_size; + size_t _active_heap; + size_t _active_free_heap; + static const size_t _alloc_memory_mask = size_t(1) << 31; }; memory_manager memory_heap; From 33bf11ac50c66ab6b1ef240dbabb1ce2d38ae4ec Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 21 Feb 2019 12:04:04 -0500 Subject: [PATCH 47/51] Add conr2d's changes and update `asset_tests` --- libraries/eosiolib/asset.hpp | 8 ++- libraries/eosiolib/core/eosio/asset.hpp | 8 ++- tests/unit/asset_tests.cpp | 92 +++++++++++++------------ 3 files changed, 57 insertions(+), 51 deletions(-) diff --git a/libraries/eosiolib/asset.hpp b/libraries/eosiolib/asset.hpp index e0b36da253..0062250973 100644 --- a/libraries/eosiolib/asset.hpp +++ b/libraries/eosiolib/asset.hpp @@ -345,9 +345,11 @@ namespace eosio { change /= 10; } char str[p+32]; - const char* fmt = negative ? "-%lld.%s %s" : "%lld.%s %s"; - snprintf(str, sizeof(str), fmt, - (int64_t)(amount/p10), fraction, symbol.code().to_string().c_str()); + snprintf(str, sizeof(str), "%lld%s%s %s", + (int64_t)(amount/p10), + (fraction[0]) ? "." : "", + fraction, + symbol.code().to_string().c_str()); return {str}; } diff --git a/libraries/eosiolib/core/eosio/asset.hpp b/libraries/eosiolib/core/eosio/asset.hpp index 7b2b75f08a..e1509cabae 100644 --- a/libraries/eosiolib/core/eosio/asset.hpp +++ b/libraries/eosiolib/core/eosio/asset.hpp @@ -344,9 +344,11 @@ namespace eosio { change /= 10; } char str[p+32]; - const char* fmt = negative ? "-%lld.%s %s" : "%lld.%s %s"; - snprintf(str, sizeof(str), fmt, - (int64_t)(amount/p10), fraction, symbol.code().to_string().c_str()); + snprintf(str, sizeof(str), "%lld%s%s %s", + (int64_t)(amount/p10), + (fraction[0]) ? "." : "", + fraction, + symbol.code().to_string().c_str()); return {str}; } diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 436190d1f5..6c91b582f4 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -22,7 +22,7 @@ static constexpr int64_t asset_max{ asset_mask}; // 4611686018427387903 // Definitions in `eosio.cdt/libraries/eosio/asset.hpp` EOSIO_TEST_BEGIN(asset_type_test) - silence_output(false); + silence_output(true); static constexpr symbol s0{"A", 0}; static constexpr symbol s1{"Z", 0}; @@ -122,24 +122,24 @@ EOSIO_TEST_BEGIN(asset_type_test) // Note: // That `assets` with negative amounts with 0 `precision` print two '-' characters - CHECK_EQUAL( (asset{0LL, sym_no_prec}.to_string()), "0. SYMBOLL" ) - CHECK_EQUAL( (asset{-0LL, sym_no_prec}.to_string()), "0. SYMBOLL" ) - CHECK_EQUAL( (asset{0LL, sym_prec}.to_string()), + CHECK_EQUAL( (asset{ 0LL, sym_no_prec}.to_string()), "0 SYMBOLL" ) + CHECK_EQUAL( (asset{-0LL, sym_no_prec}.to_string()), "0 SYMBOLL" ) + CHECK_EQUAL( (asset{ 0LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) CHECK_EQUAL( (asset{-0LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL" ) - CHECK_EQUAL( (asset{1LL, sym_no_prec}.to_string()), "1. SYMBOLL" ) - CHECK_EQUAL( (asset{-1LL, sym_no_prec}.to_string()), "--1. SYMBOLL" ) - CHECK_EQUAL( (asset{1LL, sym_prec}.to_string()), + CHECK_EQUAL( (asset{ 1LL, sym_no_prec}.to_string()), "1 SYMBOLL" ) + CHECK_EQUAL( (asset{-1LL, sym_no_prec}.to_string()), "-1 SYMBOLL" ) + CHECK_EQUAL( (asset{ 1LL, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) CHECK_EQUAL( (asset{-1LL, sym_prec}.to_string()), - "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) - - CHECK_EQUAL( (asset{asset_min, sym_no_prec}.to_string()), "--4611686018427387903. SYMBOLL" ) - CHECK_EQUAL( (asset{asset_max, sym_no_prec}.to_string()), "4611686018427387903. SYMBOLL" ) + "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL" ) + + CHECK_EQUAL( (asset{asset_min, sym_no_prec}.to_string()), "-4611686018427387903 SYMBOLL" ) + CHECK_EQUAL( (asset{asset_max, sym_no_prec}.to_string()), "4611686018427387903 SYMBOLL" ) CHECK_EQUAL( (asset{asset_min, sym_prec}.to_string()), - "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) + "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) CHECK_EQUAL( (asset{asset_max, sym_prec}.to_string()), "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL" ) @@ -148,8 +148,8 @@ EOSIO_TEST_BEGIN(asset_type_test) // Note: // Uncomment once print checking has been resolved - CHECK_PRINT( "0. SYMBOLL", [&](){asset{0LL, sym_no_prec}.print();} ); - CHECK_PRINT( "0. SYMBOLL", [&](){asset{-0LL, sym_no_prec}.print();} ); + CHECK_PRINT( "0 SYMBOLL", [&](){asset{0LL, sym_no_prec}.print();} ); + CHECK_PRINT( "0 SYMBOLL", [&](){asset{-0LL, sym_no_prec}.print();} ); CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( [&]() { asset{0LL, sym_prec}.print(); @@ -161,22 +161,22 @@ EOSIO_TEST_BEGIN(asset_type_test) }) ) - CHECK_PRINT( "1. SYMBOLL", [&](){asset{1LL, sym_no_prec}.print();} ); - CHECK_PRINT( "--1. SYMBOLL", [&](){asset{-1LL, sym_no_prec}.print();} ); - CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( + CHECK_PRINT( "1 SYMBOLL", [&](){asset{1LL, sym_no_prec}.print();} ); + CHECK_PRINT( "-1 SYMBOLL", [&](){asset{-1LL, sym_no_prec}.print();} ); + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( [&]() { asset{1LL, sym_prec}.print(); }) ) - CHECK_PRINT( "-0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000001 SYMBOLL", ( [&]() { asset{-1LL, sym_prec}.print(); }) ) - CHECK_PRINT( "--4611686018427387903. SYMBOLL", [&](){asset{asset_min, sym_no_prec}.print();} ); - CHECK_PRINT( "4611686018427387903. SYMBOLL", [&](){asset{asset_max, sym_no_prec}.print();} ); - CHECK_PRINT( "-0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( + CHECK_PRINT( "-4611686018427387903 SYMBOLL", [&](){asset{asset_min, sym_no_prec}.print();} ); + CHECK_PRINT( "4611686018427387903 SYMBOLL", [&](){asset{asset_max, sym_no_prec}.print();} ); + CHECK_PRINT( "0.000000000000000000000000000000000000000000004611686018427387903 SYMBOLL", ( [&]() { asset{asset_min, sym_prec}.print(); }) @@ -186,8 +186,9 @@ EOSIO_TEST_BEGIN(asset_type_test) asset{asset_max, sym_prec}.print(); }) ) - - for( uint8_t precision{0}; precision < 64; ++precision ) { + + // Printing symbols at every level of precision + for( uint8_t precision{1}; precision < 64; ++precision ) { CHECK_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ) } @@ -393,10 +394,10 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // That if there is no precision, there will be odd output: // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` // output: "0. A@1" - CHECK_PRINT( "0. A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) - CHECK_PRINT( "0. A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) - CHECK_PRINT( "0. Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) - CHECK_PRINT( "0. Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ) + CHECK_PRINT( "0 A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) + CHECK_PRINT( "0 A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) + CHECK_PRINT( "0 Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) + CHECK_PRINT( "0 Z@z", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"z"}}.print();} ) CHECK_PRINT( "1.1 A@1", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"1"}}.print();} ) CHECK_PRINT( "1.1 A@5", [](){extended_asset{asset{int64_t{11}, symbol{"A", 1}}, name{"5"}}.print();} ) @@ -412,24 +413,25 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 Z@z", [](){extended_asset{asset{int64_t{11}, symbol{"Z", 63}}, name{"z"}}.print();} ) - CHECK_PRINT( "0. AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - CHECK_PRINT( "0. AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - CHECK_PRINT( "0. ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - CHECK_PRINT( "0. ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) - //CHECK_PRINT( "1.1 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - //CHECK_PRINT( "1.1 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - //CHECK_PRINT( "1.1 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - //CHECK_PRINT( "1.1 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) - - //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@111111111111j", - // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@555555555555j", - // [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@aaaaaaaaaaaaj", - // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - //CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@zzzzzzzzzzzzj", - // [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) - + CHECK_PRINT( "0 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + CHECK_PRINT( "0 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{0}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + CHECK_PRINT( "0 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + CHECK_PRINT( "0 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + + // CHECK_PRINT( "1.1 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + // CHECK_PRINT( "1.1 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + // CHECK_PRINT( "1.1 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + // CHECK_PRINT( "1.1 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@111111111111j", + [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 63}}, name{"111111111111j"}}.print();} ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@555555555555j", + [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 63}}, name{"555555555555j"}}.print();} ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@aaaaaaaaaaaaj", + [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 63}}, name{"aaaaaaaaaaaaj"}}.print();} ) + CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 ZZZZZZZ@zzzzzzzzzzzzj", + [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 63}}, name{"zzzzzzzzzzzzj"}}.print();} ) + // ------------------------------- // extended_asset operator-()const CHECK_EQUAL( (-extended_asset{asset{ 0, sym_no_prec}, {}}.quantity), (extended_asset{asset_no_prec, {}}.quantity) ) From 107fbb2590e87f8c4e8fcb698b370934eb4ff9df Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 21 Feb 2019 13:54:40 -0500 Subject: [PATCH 48/51] Clean up --- tests/unit/asset_tests.cpp | 51 ++++++++++++++++----------------- tests/unit/datastream_tests.cpp | 18 ++++++------ tests/unit/name_tests.cpp | 3 +- tests/unit/symbol_tests.cpp | 10 +++---- 4 files changed, 40 insertions(+), 42 deletions(-) diff --git a/tests/unit/asset_tests.cpp b/tests/unit/asset_tests.cpp index 6c91b582f4..065001d656 100644 --- a/tests/unit/asset_tests.cpp +++ b/tests/unit/asset_tests.cpp @@ -114,13 +114,6 @@ EOSIO_TEST_BEGIN(asset_type_test) // Printing an `asset` is limited to a precision of 63 // This will trigger an error: // `asset{int64_t{1LL}, symbol{"SYMBOLL", 64}}.print();` // output: "Floating point exception: ..." - - // Note: - // This produces odd behavior when the `precision` is 0: - // `asset{int64_t{1LL}, symbol{"SYMBOLL", 0}}.print();` // output: "1. SYMBOLL" - - // Note: - // That `assets` with negative amounts with 0 `precision` print two '-' characters CHECK_EQUAL( (asset{ 0LL, sym_no_prec}.to_string()), "0 SYMBOLL" ) CHECK_EQUAL( (asset{-0LL, sym_no_prec}.to_string()), "0 SYMBOLL" ) @@ -145,9 +138,6 @@ EOSIO_TEST_BEGIN(asset_type_test) // ----------------- // void print()const - // Note: - // Uncomment once print checking has been resolved - CHECK_PRINT( "0 SYMBOLL", [&](){asset{0LL, sym_no_prec}.print();} ); CHECK_PRINT( "0 SYMBOLL", [&](){asset{-0LL, sym_no_prec}.print();} ); CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000000 SYMBOLL", ( @@ -187,7 +177,7 @@ EOSIO_TEST_BEGIN(asset_type_test) }) ) - // Printing symbols at every level of precision + // Printing symbols at every level of precision, starting at a precision of `1` for( uint8_t precision{1}; precision < 64; ++precision ) { CHECK_EQUAL( (asset{0LL, symbol{"SYMBOLL", precision}}.to_string()), (std::string(std::string("0.") + std::string(precision, '0') + std::string(" SYMBOLL"))) ) @@ -324,7 +314,8 @@ EOSIO_TEST_BEGIN(asset_type_test) CHECK_ASSERT( "comparison of assets with different symbols is not allowed", ( []() { - asset{{}, symbol{"SYMBOLL", 0}} == asset{{}, symbol{"LLOBMYS", 0}}; + bool b{asset{{}, symbol{"SYMBOLL", 0}} == asset{{}, symbol{"LLOBMYS", 0}}}; + return b; }) ) @@ -387,13 +378,6 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // ----------------- // void print()const - // Note: - // Uncomment once print checking has been resolved - - // Note: - // That if there is no precision, there will be odd output: - // `extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();` // output: "0. A@1" - CHECK_PRINT( "0 A@1", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"1"}}.print();} ) CHECK_PRINT( "0 A@5", [](){extended_asset{asset{int64_t{0}, symbol{"A", 0}}, name{"5"}}.print();} ) CHECK_PRINT( "0 Z@a", [](){extended_asset{asset{int64_t{0}, symbol{"Z", 0}}, name{"a"}}.print();} ) @@ -418,10 +402,10 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) CHECK_PRINT( "0 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) CHECK_PRINT( "0 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{0}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) - // CHECK_PRINT( "1.1 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) - // CHECK_PRINT( "1.1 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) - // CHECK_PRINT( "1.1 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) - // CHECK_PRINT( "1.1 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) + CHECK_PRINT( "11 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"111111111111j"}}.print();} ) + CHECK_PRINT( "11 AAAAAAA@555555555555j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 0}}, name{"555555555555j"}}.print();} ) + CHECK_PRINT( "11 ZZZZZZZ@aaaaaaaaaaaaj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"aaaaaaaaaaaaj"}}.print();} ) + CHECK_PRINT( "11 ZZZZZZZ@zzzzzzzzzzzzj", [](){extended_asset{asset{int64_t{11}, symbol{"ZZZZZZZ", 0}}, name{"zzzzzzzzzzzzj"}}.print();} ) CHECK_PRINT( "0.000000000000000000000000000000000000000000000000000000000000011 AAAAAAA@111111111111j", [](){extended_asset{asset{int64_t{11}, symbol{"AAAAAAA", 63}}, name{"111111111111j"}}.print();} ) @@ -513,17 +497,32 @@ EOSIO_TEST_BEGIN(extended_asset_type_test) // ------------------------------------------------------------------- // friend bool operator<(const extended_asset&, const extended_asset&) CHECK_EQUAL( (extended_asset{asset_no_prec, name{}} < extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ) - CHECK_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} < extended_asset{{}, name{"eosioaccountj"}};}) ) + CHECK_ASSERT( "type mismatch", ( + [&]() { + bool b{extended_asset{{}, name{}} < extended_asset{{}, name{"eosioaccountj"}}}; + return b; + }) + ) // -------------------------------------------------------------------- // friend bool operator<=(const extended_asset&, const extended_asset&) CHECK_EQUAL( (extended_asset{asset_no_prec, name{}} <= extended_asset{asset{ 1LL, sym_no_prec}, {}}), true ); - CHECK_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} <= extended_asset{{}, name{"eosioaccountj"}};}) ) + CHECK_ASSERT( "type mismatch", ( + [&]() { + bool b{extended_asset{{}, name{}} <= extended_asset{{}, name{"eosioaccountj"}}}; + return b; + }) + ) // -------------------------------------------------------------------- // friend bool operator>=(const extended_asset&, const extended_asset&) CHECK_EQUAL( (extended_asset{asset{ 1LL, sym_no_prec}, {}} >= extended_asset{asset_no_prec, name{}}), true ); - CHECK_ASSERT( "type mismatch", ([&]() {extended_asset{{}, name{}} >= extended_asset{{}, name{"eosioaccountj"}};}) ) + CHECK_ASSERT( "type mismatch", ( + [&]() { + bool b{extended_asset{{}, name{}} >= extended_asset{{}, name{"eosioaccountj"}}}; + return b; + }) + ) silence_output(false); EOSIO_TEST_END diff --git a/tests/unit/datastream_tests.cpp b/tests/unit/datastream_tests.cpp index 8242fb445e..cdb2fc6c31 100644 --- a/tests/unit/datastream_tests.cpp +++ b/tests/unit/datastream_tests.cpp @@ -318,16 +318,14 @@ EOSIO_TEST_BEGIN(datastream_stream_test) // ---------- // std::deque - // Note: - // Uncomment once issue has been resolved - // ds.seekp(0); - // fill(begin(datastream_buffer), end(datastream_buffer), 0); - // const deque cd{'a','b','c','d','e','f','g','h','i' }; - // deque d{}; - // ds << cd; - // ds.seekp(0); - // ds >> d; // Fails here - // CHECK_EQUAL( cd, d ) + ds.seekp(0); + fill(begin(datastream_buffer), end(datastream_buffer), 0); + const deque cd{'a','b','c','d','e','f','g','h','i' }; + deque d{}; + ds << cd; + ds.seekp(0); + ds >> d; + CHECK_EQUAL( cd, d ) // --------- // std::list diff --git a/tests/unit/name_tests.cpp b/tests/unit/name_tests.cpp index cdda07d40b..d26a1e29e5 100644 --- a/tests/unit/name_tests.cpp +++ b/tests/unit/name_tests.cpp @@ -175,7 +175,8 @@ EOSIO_TEST_BEGIN(name_type_test) // ---------------------------------------- // char* write_as_string(char*, char*)const - char buffer[13]{}; + static constexpr uint8_t buffer_size{32}; + char buffer[buffer_size]{}; string str{"1"}; name{str}.write_as_string( buffer, buffer + sizeof(buffer) ); diff --git a/tests/unit/symbol_tests.cpp b/tests/unit/symbol_tests.cpp index b6b250ffdc..0d0bde5a54 100644 --- a/tests/unit/symbol_tests.cpp +++ b/tests/unit/symbol_tests.cpp @@ -199,11 +199,11 @@ EOSIO_TEST_BEGIN(symbol_type_test) // --------------------- // void print(bool)const // Note: - // Uncomment once print checking has been resolved - // CHECK_PRINT( "0,A", [&](){symbol{"A", 0}.print(true);} ); - // CHECK_PRINT( "0,Z", [&](){symbol{"Z", 0}.print(true);} ); - // CHECK_PRINT( "255,AAAAAAA", [&](){symbol{"AAAAAAA", 255}.print(true);} ); - // CHECK_PRINT( "255,ZZZZZZZ", [&](){symbol{"ZZZZZZZ", 255}.print(true);} ); + // This function prints the length of the symbol at the very end + CHECK_PRINT( "0,A1", [&](){symbol{"A", 0}.print(true);} ); + CHECK_PRINT( "0,Z1", [&](){symbol{"Z", 0}.print(true);} ); + CHECK_PRINT( "255,AAAAAAA7", [&](){symbol{"AAAAAAA", 255}.print(true);} ); + CHECK_PRINT( "255,ZZZZZZZ7", [&](){symbol{"ZZZZZZZ", 255}.print(true);} ); // -------------------------------------------------------------- // friend constexpr bool operator==(const symbol&, const symbol&) From e969ad97aef08b2c97666d4345a034828f51ca46 Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 21 Feb 2019 14:54:46 -0500 Subject: [PATCH 49/51] Fix to silence conversion warning --- tests/unit/binary_extension_tests.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index ca89681148..e322c98fa0 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -3,11 +3,14 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ +#include + #include #include using std::in_place; using std::move; +using std::string; using eosio::binary_extension; @@ -88,7 +91,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) CHECK_EQUAL( (be_char_value.value() == 'c'), true ) CHECK_EQUAL( (be_char_value.value() == 'd'), false ) - binary_extension be_str_value{"abcd"}; + binary_extension be_str_value{const_cast("abcd")}; CHECK_EQUAL( (be_str_value.value() == "abcd"), true ) CHECK_EQUAL( (be_str_value.value() == "efgh"), false ) @@ -102,7 +105,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) CHECK_EQUAL( (cbe_str_value.value() == "abcd"), true ) CHECK_EQUAL( (cbe_str_value.value() == "efgh"), false ) - // --------------------------------------------------------------------------------------- + // ----------------------------------------------------------------------------- // constexpr auto value_or(U&&) -> enable_if_t::value, T&>& binary_extension be_val_or{}; uint8_t ui8_val = be_val_or.value_or(uint8_t{0x63}); @@ -112,7 +115,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) // constexpr T&& value_or()&& CHECK_EQUAL( binary_extension{'c'}.value_or(), 'c' ) - binary_extension be_val_or_rval{"abcd"}; + binary_extension be_val_or_rval{const_cast("abcd")}; CHECK_EQUAL( be_val_or_rval.value_or(), "abcd" ) // ------------------------------------- @@ -127,7 +130,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) binary_extension be_char_val_or{'c'}; CHECK_EQUAL( be_char_val_or.value_or(), 'c' ) - binary_extension be_str_val_or{"abcd"}; + binary_extension be_str_val_or{const_cast("abcd")}; CHECK_EQUAL( be_str_val_or.value_or(), "abcd" ) // ---------------------------- @@ -144,7 +147,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) CHECK_EQUAL( *be_char_mem_op.operator->() == 'c', true ) CHECK_EQUAL( *be_char_mem_op.operator->() != 'd', true ) - binary_extension be_str_mem_op{"abcd"}; + binary_extension be_str_mem_op{const_cast("abcd")}; CHECK_EQUAL( *be_str_mem_op.operator->() == "abcd", true ) CHECK_EQUAL( *be_str_mem_op.operator->() != "efgh", true ) @@ -164,7 +167,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) CHECK_EQUAL( be_char_lval_dref_op.operator*() == 'c', true ) CHECK_EQUAL( be_char_lval_dref_op.operator*() != 'd', true ) - binary_extension be_str_lval_dref_op{"abcd"}; + binary_extension be_str_lval_dref_op{const_cast("abcd")}; CHECK_EQUAL( be_str_lval_dref_op.operator*() == "abcd", true ) CHECK_EQUAL( be_str_lval_dref_op.operator*() != "efgh", true ) @@ -184,7 +187,7 @@ EOSIO_TEST_BEGIN(binary_extension_test) CHECK_EQUAL( move(be_char_rval_dref_op.operator*()) == 'c', true ) CHECK_EQUAL( move(be_char_rval_dref_op.operator*()) != 'd', true ) - binary_extension be_str_rval_dref_op{"abcd"}; + binary_extension be_str_rval_dref_op{const_cast("abcd")}; CHECK_EQUAL( move(be_str_rval_dref_op.operator*()) == "abcd", true ) CHECK_EQUAL( move(be_str_rval_dref_op.operator*()) != "efgh", true ) From 25ecd4c365ae397a61e2d094c2a52baa146c6aae Mon Sep 17 00:00:00 2001 From: John DeBord Date: Thu, 21 Feb 2019 14:56:06 -0500 Subject: [PATCH 50/51] Remove `string` dependency --- tests/unit/binary_extension_tests.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unit/binary_extension_tests.cpp b/tests/unit/binary_extension_tests.cpp index e322c98fa0..057c8c3b23 100644 --- a/tests/unit/binary_extension_tests.cpp +++ b/tests/unit/binary_extension_tests.cpp @@ -3,14 +3,11 @@ * @copyright defined in eosio.cdt/LICENSE.txt */ -#include - #include #include using std::in_place; using std::move; -using std::string; using eosio::binary_extension; From e5be3b22ee4b7322efc922a6146de4acc47b9544 Mon Sep 17 00:00:00 2001 From: johndebord Date: Thu, 21 Feb 2019 15:17:17 -0500 Subject: [PATCH 51/51] Expound upon `signed_int` test --- tests/unit/varint_tests.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/unit/varint_tests.cpp b/tests/unit/varint_tests.cpp index 3c382a60a0..0eceda3122 100644 --- a/tests/unit/varint_tests.cpp +++ b/tests/unit/varint_tests.cpp @@ -11,8 +11,9 @@ using std::numeric_limits; -using namespace eosio; using eosio::datastream; +using eosio::unsigned_int; +using eosio::signed_int; static constexpr uint32_t u32min = numeric_limits::min(); // 0 static constexpr uint32_t u32max = numeric_limits::max(); // 4294967295 @@ -121,7 +122,7 @@ EOSIO_TEST_BEGIN(unsigned_int_type_test) ds << cui; ds.seekp(0); ds >> ui; - CHECK_EQUAL( cui == ui, true) + CHECK_EQUAL( cui, ui) silence_output(false); EOSIO_TEST_END @@ -246,11 +247,15 @@ EOSIO_TEST_BEGIN(signed_int_type_test) datastream ds{datastream_buffer, buffer_size}; static const signed_int csi{-42}; - signed_int si{}; - ds << csi; + signed_int a{44}, b{(1<<30)+2}, c{-35}, d{-(1<<30)-2}; // Small+, Small-, Large+, Large- + signed_int aa, bb, cc, dd; + ds << a << b << c << d; ds.seekp(0); - ds >> si; - CHECK_EQUAL( csi == si, true) + ds >> aa >> bb >> cc >> dd; + CHECK_EQUAL( a, aa ) + CHECK_EQUAL( b, bb ) + CHECK_EQUAL( c, cc ) + CHECK_EQUAL( d, dd ) silence_output(false); EOSIO_TEST_END