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

eosiolib Unit/Regression Tests #424

Merged
merged 55 commits into from
Feb 21, 2019
Merged
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
e703cc3
Add more `name` tests
Dec 28, 2018
f236e89
Merge branch 'release/1.5.x' into cdt-tests
Jan 2, 2019
5d83272
Start tests
Jan 2, 2019
ceab265
Merge remote-tracking branch 'upstream/larryk85-patch-1' into cdt-tests
Jan 3, 2019
cd1def0
Continuation of `name` type tests
Jan 3, 2019
3c16d75
Continuation of `name` type tests
Jan 4, 2019
c0703b0
Start `symbol` type tests
Jan 7, 2019
a536473
Start `asset` type tests
Jan 8, 2019
dfa7b9d
Continuation of `asset` type tests
Jan 9, 2019
09c8222
Continuation of `asset` type tests
Jan 11, 2019
86397cf
Continuation of `asset` type tests
Jan 11, 2019
0b458b2
Start `microseconds`, `time_point`, and `time_point_sec` type tests
Jan 15, 2019
39845b0
Refactor tests to be more succinct
Jan 17, 2019
e65b747
Continue refactor of tests for clarity and templates for future tests
Jan 18, 2019
897627f
Start varint and print tests
Jan 23, 2019
b486c4d
Update `CMakeLists.txt` files
Jan 24, 2019
65783b9
Add `fixed_bytes` and `crypto` tests
Jan 25, 2019
f5bed23
Add `datastream` tests
Jan 28, 2019
62d5140
Continuation of `datastream` tests
Jan 28, 2019
63c1d9a
Progress on `datastream` tests and `varint` tests almost done
Jan 28, 2019
253297a
Continuing work on `fixed_bytes` tests
Jan 29, 2019
751f9fe
Add `binary_extension` tests
Jan 29, 2019
0aab645
Continuation
Jan 30, 2019
b5d8b43
Continuation of `datastream` tests
Jan 30, 2019
f35f6af
Tidy up tests;
Jan 31, 2019
5e4b003
Continuation of `datastream` tests
Feb 1, 2019
80e9fae
Continuation of `datastream` tests
Feb 4, 2019
23f1734
Continuation of `datastream` tests
Feb 4, 2019
e0484fa
Fix alignment issue
Feb 4, 2019
3e44872
Finishing up `datastream` tests
Feb 5, 2019
1c2935a
Tidy up `datastream` tests
Feb 6, 2019
7993563
Start trying to touch-up `varint` tests
Feb 6, 2019
6140d28
Fix build with `fno-builtins` flag
Feb 7, 2019
d2accfe
Finishing up `datastream` tests
Feb 7, 2019
bef0fca
Delete .#CMakeLists.txt
johndebord Feb 7, 2019
3ccea4d
More edits on `datastream` tests
Feb 8, 2019
14a2e1c
Merge branch 'cdt-tests' of github.com:EOSIO/eosio.cdt into cdt-tests
Feb 8, 2019
bd76516
Finish `datastream` tests
Feb 8, 2019
2bcd19a
Finish `datastream` tests
Feb 8, 2019
0cc99ce
Spruce-up tests
Feb 8, 2019
e1deefb
Fix for `__bzero` behavior
Feb 11, 2019
677d158
Finish `serialize_tests`
Feb 11, 2019
7cb0f54
Nearly finished with `time_tests`; with various other edits
Feb 14, 2019
c87404b
Finish `time_tests` and `varint_tests`
Feb 15, 2019
c717257
Continuation of `fixed_bytes_tests.cpp`
Feb 16, 2019
61df416
Finish tests and get them production ready
Feb 19, 2019
4c2c6b0
merged release/1.6.x
larryk85 Feb 19, 2019
0de3c41
added llp64 fixes and fixed sse issues
larryk85 Feb 20, 2019
bb32d45
remove unneeded code
larryk85 Feb 20, 2019
f29d37e
use size_t and uint64_t for malloc instead of uint32_t and int32_t
larryk85 Feb 21, 2019
33bf11a
Add conr2d's changes and update `asset_tests`
Feb 21, 2019
107fbb2
Clean up
Feb 21, 2019
e969ad9
Fix to silence conversion warning
Feb 21, 2019
25ecd4c
Remove `string` dependency
johndebord Feb 21, 2019
e5be3b2
Expound upon `signed_int` test
Feb 21, 2019
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
Prev Previous commit
Next Next commit
Continuation of datastream tests
johndebord committed Jan 28, 2019
commit 62d514091f2765d7ae73bd490c1f9c0c5c70dc1c
325 changes: 259 additions & 66 deletions tests/unit/datastream_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,108 +1,301 @@
#include <eosio/native/tester.hpp>
#include <eosiolib/datastream.hpp>
#include <list>
#include <vector>

#include <eosiolib/print.hpp>

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<uint32_t> ds{0, 256}`??
/// datastream(T, size_t)
datastream<char*> 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<uint32_t> ds{0, 256}`??
/// datastream(T, size_t)
datastream<const char*> 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<uint32_t> ds{0, 256}`??
/// datastream(T, size_t)
datastream<size_t> 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<char> l{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', };
eosio::print(sizeof(unsigned_int));

datastream<char*> 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<char> um_ds(std::begin(datastream_buffer), std::end(datastream_buffer));
// std::vector<char> 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<size_t>::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();
}