Skip to content

Commit 013acac

Browse files
committed
beginnings of unit test for snapshot
1 parent 94510b6 commit 013acac

File tree

6 files changed

+134
-2
lines changed

6 files changed

+134
-2
lines changed

contracts/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_subdirectory(eosio.token)
1212
add_subdirectory(eosio.msig)
1313
add_subdirectory(eosio.sudo)
1414
add_subdirectory(multi_index_test)
15+
add_subdirectory(snapshot_test)
1516
add_subdirectory(eosio.system)
1617
add_subdirectory(identity)
1718
add_subdirectory(stltest)

contracts/multi_index_test/multi_index_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct limit_order {
3030
EOSLIB_SERIALIZE( test_k256, (id)(val) )
3131
};
3232

33-
class multi_index_test {
33+
class snapshot_test {
3434
public:
3535

3636
ACTION(N(multitest), trigger) {
@@ -170,7 +170,7 @@ namespace multi_index_test {
170170
/// The apply method implements the dispatch of events to this contract
171171
void apply( uint64_t /* receiver */, uint64_t code, uint64_t action ) {
172172
require_auth(code);
173-
eosio_assert(eosio::dispatch<multi_index_test, multi_index_test::trigger>(code, action),
173+
eosio_assert(eosio::dispatch<snapshot_test, snapshot_test::trigger>(code, action),
174174
"Could not dispatch");
175175
}
176176
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
file(GLOB ABI_FILES "*.abi")
2+
configure_file("${ABI_FILES}" "${CMAKE_CURRENT_BINARY_DIR}" COPYONLY)
3+
add_wast_executable(TARGET snapshot_test
4+
INCLUDE_FOLDERS "${STANDARD_INCLUDE_FOLDERS}"
5+
LIBRARIES libc++ libc eosiolib
6+
DESTINATION_FOLDER ${CMAKE_CURRENT_BINARY_DIR}
7+
)
8+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "eosio::abi/1.0",
3+
"types": [],
4+
"structs": [{
5+
"name": "increment",
6+
"base": "",
7+
"fields": [
8+
{"name": "value", "type": "uint32" }
9+
]
10+
}
11+
],
12+
"actions": [{
13+
"name": "increment",
14+
"type": "increment",
15+
"ricaridian_contract": ""
16+
}
17+
],
18+
"tables": [],
19+
"ricardian_clauses": [],
20+
"abi_extensions": []
21+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <eosiolib/eosio.hpp>
2+
#include <eosiolib/multi_index.hpp>
3+
4+
using namespace eosio;
5+
6+
namespace snapshot_test {
7+
8+
struct main_record {
9+
uint64_t id;
10+
double index_f64 = 0.0;
11+
long double index_f128 = 0.0L;
12+
uint64_t index_i64 = 0ULL;
13+
uint128_t index_i128 = 0ULL;
14+
key256 index_i256 = key256();
15+
16+
auto primary_key() const { return id; }
17+
18+
auto get_index_f64 () const { return index_f64 ; }
19+
auto get_index_f128 () const { return index_f128; }
20+
auto get_index_i64 () const { return index_i64 ; }
21+
auto get_index_i128 () const { return index_i128; }
22+
const key256& get_index_i256 () const { return index_i256; }
23+
24+
EOSLIB_SERIALIZE( main_record, (id)(index_f64)(index_f128)(index_i64)(index_i128)(index_i256) )
25+
};
26+
27+
struct increment {
28+
increment(): value(0) {}
29+
increment(uint32_t v): value(v) {}
30+
31+
uint32_t value;
32+
33+
EOSLIB_SERIALIZE(increment, (value))
34+
};
35+
36+
using multi_index_type = eosio::multi_index<N(data), main_record,
37+
indexed_by< N(byf ), const_mem_fun<main_record, double ,&main_record::get_index_f64 >>,
38+
indexed_by< N(byff), const_mem_fun<main_record, long double ,&main_record::get_index_f128>>,
39+
indexed_by< N(byi ), const_mem_fun<main_record, uint64_t ,&main_record::get_index_i64 >>,
40+
indexed_by< N(byii), const_mem_fun<main_record, uint128_t ,&main_record::get_index_i128>>,
41+
indexed_by< N(byiiii), const_mem_fun<main_record, const key256& ,&main_record::get_index_i256>>
42+
>;
43+
44+
static void exec( uint64_t self, uint32_t value ) {
45+
multi_index_type data(self, self);
46+
auto current = data.begin( );
47+
if( current == data.end() ) {
48+
data.emplace( self, [&]( auto& r ) {
49+
r.id = value;
50+
r.index_f64 = value;
51+
r.index_f128 = value;
52+
r.index_i64 = value;
53+
r.index_i128 = value;
54+
r.index_i256.data()[0] = value;
55+
});
56+
57+
} else {
58+
data.modify( current, self, [&]( auto& r ) {
59+
r.id += value;
60+
r.index_f64 += value;
61+
r.index_f128 += value;
62+
r.index_i64 += value;
63+
r.index_i128 += value;
64+
r.index_i256.data()[0] += value;
65+
});
66+
}
67+
}
68+
69+
} /// multi_index_test
70+
71+
namespace multi_index_test {
72+
extern "C" {
73+
/// The apply method implements the dispatch of events to this contract
74+
void apply( uint64_t self, uint64_t code, uint64_t action ) {
75+
require_auth(code);
76+
eosio_assert(action == N(increment), "unsupported action");
77+
snapshot_test::exec(self, unpack_action_data<snapshot_test::increment>().value);
78+
}
79+
}
80+
}

unittests/snapshot_tests.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @file
3+
* @copyright defined in eos/LICENSE.txt
4+
*/
5+
6+
#include <boost/test/unit_test.hpp>
7+
#include <eosio/testing/tester.hpp>
8+
9+
using namespace eosio;
10+
using namespace testing;
11+
using namespace chain;
12+
13+
BOOST_AUTO_TEST_SUITE(snapshot_tests)
14+
15+
BOOST_AUTO_TEST_CASE(test_multi_index_snapshot)
16+
{
17+
tester main;
18+
19+
20+
}
21+
22+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)