Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

More fixes to native tester macros - develop #660

Merged
merged 3 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions libraries/eosiolib/core/eosio/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,42 @@ namespace eosio {
static constexpr size_t npos = -1;

template <size_t N>
constexpr string(const char (&str)[N]) : _size{N-1}, _capacity{_size}, _begin{str}
{ }
constexpr string(const char (&str)[N])
: _size{N-1}
, _capacity{_size}
, _begin{str}
{
}

constexpr string() : _size{0}, _capacity{0}, _begin{""}
{ }
constexpr string()
: _size{0}
, _capacity{0}
, _begin{""}
{
}

constexpr string(const char* str, const size_t n) : _size{n}, _capacity{_size*2} {
constexpr string(const char* str, const size_t n)
: _size{n}
, _capacity{_size*2}
{
char* begin{new char[_capacity]};
memcpy(begin, str, _size);
_begin = begin;
}

constexpr string(const size_t n, const char c) : _size{n}, _capacity{_size*2} {
constexpr string(const size_t n, const char c)
: _size{n}
, _capacity{_size*2}
{
char* begin{new char[_capacity]};
memset(begin, c, _size);
_begin = begin;
}

constexpr string(const string& str, const size_t pos, const size_t n) : _size{n}, _capacity{_size*2} {
constexpr string(const string& str, const size_t pos, const size_t n)
: _size{n}
, _capacity{_size*2}
{
if (n == string::npos || str._size < pos+n) {
_size = str._size;
_capacity = _size*2;
Expand All @@ -48,14 +65,20 @@ namespace eosio {
clone(_size, _capacity, str.data()+pos);
}

constexpr string(const string& str) : _size{str._size}, _capacity{str._capacity} {
constexpr string(const string& str)
: _size{str._size}
, _capacity{str._capacity}
{
if (str.is_literal())
_begin = std::get<const char*>(str._begin);
else
clone(str._size, str._capacity, str.data());
}

constexpr string(string&& str) : _size{str._size}, _capacity{str._capacity} {
constexpr string(string&& str)
: _size{str._size}
, _capacity{str._capacity}
{
if (str.is_literal())
_begin = std::get<const char*>(str._begin);
else
Expand Down Expand Up @@ -255,7 +278,7 @@ namespace eosio {
void pop_back() {
if (_size == 0)
return;
--_size;
resize(--_size);
}

string substr(size_t pos = 0, size_t len = npos) const {
Expand Down Expand Up @@ -319,6 +342,7 @@ namespace eosio {
if (is_literal())
clone(_size, _capacity, std::get<const char*>(_begin));
memmove(std::get<uptr>(_begin).get()+pos+len, std::get<uptr>(_begin).get(), len);
resize(_size);

return *this;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/eosiolib/core/eosio/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ namespace eosio {
char buffer[7];
auto end = code().write_as_string( buffer, buffer + sizeof(buffer) );
if( buffer < end )
::eosio::print( buffer, (end-buffer) );
printl( buffer, (end-buffer) );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions libraries/native/native/eosio/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ inline bool expect_print(bool check, const std::string& li, Pred&& pred, F&& fun
if (!check)
eosio::check(passed, std::string("error : wrong print message {"+li+"}").c_str());
if (!passed)
eosio::print("error : wrong print message9 {"+li+"}\n");
eosio::print("error : wrong print message {"+li+"}\n");
silence_output(disable_out);
return passed;
}
Expand All @@ -81,13 +81,13 @@ inline bool expect_print(bool check, const std::string& li, const char (&expecte
}

#define CHECK_ASSERT(...) \
___has_failed &= expect_assert(true, std::string(__FILE__)+":"+__func__+":"+(std::to_string(__LINE__)), __VA_ARGS__);
___has_failed |= !expect_assert(true, std::string(__FILE__)+":"+__func__+":"+(std::to_string(__LINE__)), __VA_ARGS__);

#define REQUIRE_ASSERT(...) \
expect_assert(false, std::string(__FILE__)+":"+__func__+":"+(std::to_string(__LINE__)), __VA_ARGS__);

#define CHECK_PRINT(...) \
___has_failed &= expect_print(true, std::string(__FILE__)+":"+__func__+":"+(std::to_string(__LINE__)), __VA_ARGS__);
___has_failed |= !expect_print(true, std::string(__FILE__)+":"+__func__+":"+(std::to_string(__LINE__)), __VA_ARGS__);

#define REQUIRE_PRINT(...) \
expect_print(false, std::string(__FILE__)+":"+__func__+":"+(std::to_string(__LINE__)), __VA_ARGS__);
Expand Down
34 changes: 18 additions & 16 deletions tests/unit/string_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ using eosio::string;

// Definitions found in `eosio.cdt/libraries/eosiolib/core/eosio/string.hpp`
EOSIO_TEST_BEGIN(string_test)
silence_output(false);

//// template <size_t N>
//// string(const char (&str)[N])
{
Expand Down Expand Up @@ -356,7 +354,7 @@ EOSIO_TEST_BEGIN(string_test)
CHECK_EQUAL( eostr.at(0), 'a' )
CHECK_EQUAL( eostr.at(5), 'f' )

CHECK_ASSERT( "eostring::string::at", []() {eostr.at(6);} )
CHECK_ASSERT( "eosio::string::at", []() {eostr.at(6);} )
}

//// const char& at(const size_t n) const
Expand All @@ -365,7 +363,7 @@ EOSIO_TEST_BEGIN(string_test)
CHECK_EQUAL( eostr.at(0), 'a' )
CHECK_EQUAL( eostr.at(5), 'f' )

CHECK_ASSERT( "eostring::string::at const", []() {eostr.at(6);} )
CHECK_ASSERT( "eosio::string::at const", []() {eostr.at(6);} )
}

{
Expand Down Expand Up @@ -796,7 +794,7 @@ EOSIO_TEST_BEGIN(string_test)
{
static const string eostr{"abcdef"};
static char str[7]{};
CHECK_ASSERT( "eostring::string::copy", []() {eostr.copy(str, 1, eostr.size()+1);} )
CHECK_ASSERT( "eosio::string::copy", []() {eostr.copy(str, 1, eostr.size()+1);} )
}

//// string& insert(const size_t pos, const char* str)
Expand Down Expand Up @@ -863,8 +861,8 @@ EOSIO_TEST_BEGIN(string_test)
{
static string eostr{"abcdefg"};
static const char* null_man{nullptr};
CHECK_ASSERT( "eostring::string::insert", []() {eostr.insert(0, null_man, 1);} )
CHECK_ASSERT( "eostring::string::insert", []() {eostr.insert(-1, "ooo", 1);} )
CHECK_ASSERT( "eosio::string::insert", []() {eostr.insert(0, null_man, 1);} )
CHECK_ASSERT( "eosio::string::insert", []() {eostr.insert(-1, "ooo", 1);} )
}

//// string& insert(const size_t pos, const string& str)
Expand Down Expand Up @@ -934,7 +932,7 @@ EOSIO_TEST_BEGIN(string_test)
{
static string eostr{"abcdefg"};
static const string str{"ooo"};
CHECK_ASSERT( "eostring::string::insert", []() {eostr.insert(-1, str);} )
CHECK_ASSERT( "eosio::string::insert", []() {eostr.insert(-1, str);} )
}

{
Expand Down Expand Up @@ -1016,7 +1014,7 @@ EOSIO_TEST_BEGIN(string_test)
{
static string eostr{"abcdefg"};
static string str{"ooo"};
CHECK_ASSERT( "eostring::string::insert", []() {eostr.insert(-1, str);} )
CHECK_ASSERT( "eosio::string::insert", []() {eostr.insert(-1, str);} )
}

{ // Bucky's test for bug he caught; PR #459.
Expand All @@ -1025,7 +1023,7 @@ EOSIO_TEST_BEGIN(string_test)
CHECK_EQUAL( eostr.size(), 6 )
CHECK_EQUAL( eostr.capacity(), 12 )
CHECK_EQUAL( strcmp(eostr.c_str(), "0hello") , 0 )

eostr.insert(0, "h", 1);
CHECK_EQUAL( eostr.size(), 7 )
CHECK_EQUAL( eostr.capacity(), 12 )
Expand Down Expand Up @@ -1119,7 +1117,7 @@ EOSIO_TEST_BEGIN(string_test)

{
static string eostr{"abcdefg"};
CHECK_ASSERT( "eostring::string::erase", []() {eostr.erase(-1, 1);} )
CHECK_ASSERT( "eosio::string::erase", []() {eostr.erase(-1, 1);} )
}

{
Expand Down Expand Up @@ -1232,7 +1230,7 @@ EOSIO_TEST_BEGIN(string_test)

{
static string eostr{"abcdefg"};
CHECK_ASSERT( "eostring::string::erase", []() {eostr.erase(-1, 1);} )
CHECK_ASSERT( "eosio::string::erase", []() {eostr.erase(-1, 1);} )
}

//// string& append(const char* str)
Expand All @@ -1257,7 +1255,7 @@ EOSIO_TEST_BEGIN(string_test)
{
static string eostr{"abcdefg"};
static const char* null_man{nullptr};
CHECK_ASSERT( "eostring::string::append", []() {eostr.append(null_man);} )
CHECK_ASSERT( "eosio::string::append", []() {eostr.append(null_man);} )
}

//// string& append(const string& str)
Expand Down Expand Up @@ -1494,11 +1492,15 @@ EOSIO_TEST_BEGIN(string_test)
ds >> str;
CHECK_EQUAL( cstr, str )
}

silence_output(false);
EOSIO_TEST_END

int main() {
int main(int argc, char* argv[]) {
bool verbose = false;
if( argc >= 2 && std::strcmp( argv[1], "-v" ) == 0 ) {
verbose = true;
}
silence_output(!verbose);

EOSIO_TEST(string_test)
return has_failed();
}
10 changes: 4 additions & 6 deletions tests/unit/symbol_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,10 @@ EOSIO_TEST_BEGIN(symbol_type_test)

// ---------------------
// void print(bool)const
// Note:
// 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);} );
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&)
Expand Down