Skip to content

Commit

Permalink
Change char array to string in output_stream to add possibility to ex…
Browse files Browse the repository at this point in the history
…tend buffer.
  • Loading branch information
Michal Lesiak committed Nov 4, 2022
1 parent 4471baa commit e62ade2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
24 changes: 13 additions & 11 deletions libraries/native/native/eosio/crt.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#pragma once
#include <setjmp.h>

#include <string>

namespace eosio { namespace cdt {
enum output_stream_kind {
std_out,
std_err,
none
};
struct output_stream {
static constexpr size_t max_size = 1024 * 2;
char output[max_size] = {};
size_t index = 0;
std::string to_string()const { return std::string((const char*)output, index); }
const char* get()const { return output; }
void push(char c) {
if(index < max_size - 1)
output[index++] = c;
}
void clear() { index = 0; }
class output_stream {
static constexpr size_t initial_size = 1024 * 4;
std::string output;

public:
output_stream() { output.reserve(initial_size); }
std::string to_string() const { return output; }
const char* get() const { return output.c_str(); }
size_t index() const { return output.size(); }
void push(char c) { output.push_back(c); }
void clear() { output.clear(); }
};
}} //ns eosio::cdt

Expand Down
4 changes: 2 additions & 2 deletions libraries/native/native/eosio/tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ template <size_t N, typename F, typename... Args>
inline bool expect_assert(bool check, const std::string& li, const char (&expected)[N], F&& func, Args... args) {
return expect_assert(check, li,
[&](const std::string& s) {
return std_err.index == N-1 &&
return std_err.index() == N-1 &&
memcmp(expected, s.c_str(), N-1) == 0; }, func, args...);
}

Expand All @@ -75,7 +75,7 @@ template <size_t N, typename F, typename... Args>
inline bool expect_print(bool check, const std::string& li, const char (&expected)[N], F&& func, Args... args) {
return expect_print(check, li,
[&](const std::string& s) {
return std_out.index == N-1 &&
return std_out.index() == N-1 &&
memcmp(expected, s.c_str(), N-1) == 0; }, func, args...);

}
Expand Down
14 changes: 8 additions & 6 deletions tests/unit/crt_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@ EOSIO_TEST_BEGIN(output_stream_push)
const char* msg = "abc";
_prints(msg, eosio::cdt::output_stream_kind::std_err);
CHECK_EQUAL(std_err.to_string(), "abc");
CHECK_EQUAL(std_err.index(), 3);

std_err.clear();
const char* msg2 = "";
_prints(msg2, eosio::cdt::output_stream_kind::std_err);
CHECK_EQUAL(std_err.to_string(), "");
CHECK_EQUAL(std_err.index(), 0);

std_err.clear();
EOSIO_TEST_END

EOSIO_TEST_BEGIN(output_stream_push_overflow)
std_err.clear();
const auto max_size = std_err.max_size;
const auto initial_capacity = std_err.to_string().capacity();
CHECK_EQUAL(std_err.index(), 0);

char msg[max_size+1] = {};
for (auto i = 0; i< max_size; ++i)
msg[i] = 'x';
std::string large_msg('x', initial_capacity + 1);

_prints(msg, eosio::cdt::output_stream_kind::std_err);
CHECK_EQUAL(std_err.to_string().size(), max_size - 1);
_prints(large_msg.c_str(), eosio::cdt::output_stream_kind::std_err);
CHECK_EQUAL(std_err.to_string().capacity() > initial_capacity, true);
CHECK_EQUAL(std_err.index(), large_msg.size());

std_err.clear();
EOSIO_TEST_END
Expand Down

0 comments on commit e62ade2

Please sign in to comment.