Skip to content

Add test for array/fixed size array support in action data and action return value. #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.5...4.0)

# Sanity check our source directory to make sure that we are not trying to
# generate an in-source build, and to make
Expand Down
2 changes: 1 addition & 1 deletion modules/TestsExternalProject.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (spring_FOUND)
CDTIntegrationTests
SOURCE_DIR "${CMAKE_SOURCE_DIR}/tests/integration"
BINARY_DIR "${CMAKE_BINARY_DIR}/tests/integration"
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${TEST_BUILD_TYPE} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_FRAMEWORK_PATH=${TEST_FRAMEWORK_PATH} -DCMAKE_MODULE_PATH=${TEST_MODULE_PATH} -Deosio_DIR=${eosio_DIR} -DLLVM_DIR=${LLVM_DIR} -DBOOST_ROOT=${BOOST_ROOT}
CMAKE_ARGS -DCMAKE_BUILD_TYPE=${TEST_BUILD_TYPE} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_FRAMEWORK_PATH=${TEST_FRAMEWORK_PATH} -DCMAKE_MODULE_PATH=${TEST_MODULE_PATH} -Dspring_DIR=${spring_DIR} -Deosio_DIR=${eosio_DIR} -DLLVM_DIR=${LLVM_DIR} -DBOOST_ROOT=${BOOST_ROOT}
UPDATE_COMMAND ""
PATCH_COMMAND ""
TEST_COMMAND ""
Expand Down
98 changes: 98 additions & 0 deletions tests/integration/array_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <boost/test/unit_test.hpp>
#include <eosio/testing/tester.hpp>
#include <eosio/chain/abi_serializer.hpp>

#include <fc/variant_object.hpp>

#include <contracts.hpp>

using namespace eosio;
using namespace eosio::testing;
using namespace eosio::chain;
using namespace eosio::testing;
using namespace fc;

using mvo = fc::mutable_variant_object;

BOOST_AUTO_TEST_SUITE(array_tests)

BOOST_FIXTURE_TEST_CASE( std_array_param, tester ) try {
/* ----------- testpa action tests --------------------------------------------------
[[eosio::action]]
void testpa(std::array<int,4> input){
std::array<int,4> arr = input;
for(int i = 0; i < 4; ++i){
eosio::cout << arr[i] << " ";
}
eosio::cout << "\n";
}
-------------------------------------------------------------------------------------- */
create_accounts( { "test"_n } );
produce_block();
set_code( "test"_n, contracts::array_tests_wasm() );
set_abi( "test"_n, contracts::array_tests_abi().data() );
produce_blocks();

auto trace = push_action("test"_n, "testpa"_n, "test"_n, mvo()("input", {1,2,3,4}));
auto& con = trace->action_traces[0].console;
BOOST_REQUIRE_EQUAL(con, std::string("1 2 3 4 \n"));
produce_block();

// size should be correct
// ----------------------
BOOST_CHECK_EXCEPTION( push_action("test"_n, "testpa"_n, "test"_n, mvo()("input", {1,2,3})),
pack_exception,
fc_exception_message_starts_with("Incorrect number of values provided (4) for fixed-size (3) array type"));

produce_block();
} FC_LOG_AND_RETHROW()


BOOST_FIXTURE_TEST_CASE( std_array_return_value, tester ) try {
/* ----------- testre action tests --------------------------------------------------
[[eosio::action]]
std::array<int,4> testre(std::array<int,4> input){
std::array<int,4> arr = input;
for(auto & v : arr) v += 1;
return arr;
}
-------------------------------------------------------------------------------------- */
create_accounts( { "test"_n } );
produce_block();
set_code( "test"_n, contracts::array_tests_wasm() );
set_abi( "test"_n, contracts::array_tests_abi().data() );
produce_blocks();

auto trace = push_action("test"_n, "testre"_n, "test"_n, mvo()("input", {1, 2, 3, 4}));
auto& rv = trace->action_traces[0].return_value;
auto actual = fc::raw::unpack<std::array<int, 4>>(rv);
auto expected = std::array<int, 4>{2, 3, 4, 5};
BOOST_REQUIRE(actual == expected);
} FC_LOG_AND_RETHROW()


BOOST_FIXTURE_TEST_CASE( std_vector_return_value, tester ) try {
/* ----------- testrev action tests --------------------------------------------------
[[eosio::action]]
std::vector<int> testrev(std::vector<int> input){
std::vector<int> vec = input;
for(auto & v : vec) v += 1;
return vec;
}
-------------------------------------------------------------------------------------- */
create_accounts( { "test"_n } );
produce_block();
set_code( "test"_n, contracts::array_tests_wasm() );
set_abi( "test"_n, contracts::array_tests_abi().data() );
produce_blocks();

auto trace = push_action("test"_n, "testrev"_n, "test"_n, mvo()("input", {1, 2, 3, 4}));
auto& rv = trace->action_traces[0].return_value;
auto actual = fc::raw::unpack<std::vector<int>>(rv);
auto expected = std::vector<int>{2, 3, 4, 5};
BOOST_REQUIRE(actual == expected);
} FC_LOG_AND_RETHROW()



BOOST_AUTO_TEST_SUITE_END()
2 changes: 2 additions & 0 deletions tests/integration/contracts.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace eosio::testing {
struct contracts {
static std::vector<uint8_t> array_tests_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/array_tests.wasm"); }
static std::vector<char> array_tests_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/array_tests.abi"); }
static std::vector<uint8_t> malloc_tests_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/malloc_tests.wasm"); }
static std::vector<char> malloc_tests_abi() { return read_abi("${CMAKE_BINARY_DIR}/../unit/test_contracts/malloc_tests.abi"); }
static std::vector<uint8_t> old_malloc_tests_wasm() { return read_wasm("${CMAKE_BINARY_DIR}/../unit/test_contracts/old_malloc_tests.wasm"); }
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/test_contracts/array_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ class [[eosio::contract]] array_tests : public contract {
}
}

// test parameter using std::array
// not supported so far
// test parameter using std::array
[[eosio::action]]
void testpa(std::array<int,4> input){
std::array<int,4> arr = input;
Expand All @@ -76,7 +75,7 @@ class [[eosio::contract]] array_tests : public contract {
eosio::cout << "\n";
}

// test return using std::array, not supported so far
// test parameter and return value using std::array
[[eosio::action]]
// cleos -v push action eosio testre '[[1,2,3,4]]' -p eosio@active
std::array<int,4> testre(std::array<int,4> input){
Expand All @@ -85,6 +84,15 @@ class [[eosio::contract]] array_tests : public contract {
return arr;
}

// test return value using std::array
[[eosio::action]]
// cleos -v push action eosio testre2 '[[1,2,3,4]]' -p eosio@active
std::array<int,4> testre2(std::vector<int> input){
std::array<int,4> arr;
for(size_t i=0; i<4; ++i) arr[i] = input[i+1];
return arr;
}

// test return using std::vector
[[eosio::action]]
// cleos -v push action eosio testrev '[[1,2,3,4]]' -p eosio@active
Expand Down
Loading