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

Contract test api #98

Merged
merged 13 commits into from
Jul 26, 2017
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
2 changes: 1 addition & 1 deletion CMakeModules/wasm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ macro(add_wast_target target SOURCE_FILES INCLUDE_FOLDERS DESTINATION_FOLDER)

add_custom_command(OUTPUT ${outfile}.bc
DEPENDS ${infile}
COMMAND ${WASM_CLANG} -emit-llvm -O3 --std=c++14 --target=wasm32 -I ${INCLUDE_FOLDERS} -fno-threadsafe-statics -fno-rtti -fno-exceptions -c ${infile} -o ${outfile}.bc
COMMAND ${WASM_CLANG} -emit-llvm -O3 --std=c++14 --target=wasm32 -fno-builtin -ffreestanding -nostdlib -I ${INCLUDE_FOLDERS} -fno-threadsafe-statics -fno-rtti -fno-exceptions -c ${infile} -o ${outfile}.bc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? Could we document this wasm.cmake file to explain why each and every argument is listed?

IMPLICIT_DEPENDS CXX ${infile}
COMMENT "Building LLVM bitcode ${outfile}.bc"
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
Expand Down
1 change: 1 addition & 0 deletions contracts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(currency)
add_subdirectory(exchange)
add_subdirectory(infinite)
add_subdirectory(test_api)
#add_subdirectory(social)
5 changes: 3 additions & 2 deletions contracts/eoslib/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <eoslib/types.h>
extern "C" {
/**
* @defgroup database Database API
* @brief APIs that store and retreive data on the blockchain
Expand Down Expand Up @@ -160,7 +161,7 @@ int32_t upper_bound_secondary_i128i128( AccountName scope, AccountName code, Tab
int32_t lower_bound_secondary_i128i128( AccountName scope, AccountName code, TableName table,
const void* key, void* data, uint32_t len );

int32_t load_secondary_i128i128( AccountName scope, AccountName code, TableName table, const void* secondary, void* data, uint32_t len );
int32_t load_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );

/**
* @param data - must point to at lest 32 bytes containing {primary,secondary}
Expand All @@ -174,4 +175,4 @@ bool remove_i128i128( AccountName scope, TableName table, const void* data );
bool store_i128i128( AccountName scope, TableName table, const void* data, uint32_t len );

///@} dbi128i128

}
2 changes: 0 additions & 2 deletions contracts/eoslib/db.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

extern "C" {
#include <eoslib/db.h>
}



Expand Down
21 changes: 14 additions & 7 deletions contracts/eoslib/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ namespace eos {
* @ingroup types
*/
static constexpr uint64_t string_to_name( const char* str ) {

uint32_t len = 0;
while( str[len] ) ++len;

uint64_t value = 0;

for( uint32_t i = 0; i <= 12 && i < len; ++i ) {
value <<= 5;
value |= char_to_symbol( str[ len -1 - i ] );
}
for( uint32_t i = 0; i <= 12; ++i ) {
uint64_t c = 0;
if( i < len && i <= 12 ) c = char_to_symbol( str[i] );

if( len >= 13 ) {
value <<= 4;
value |= 0x0f & char_to_symbol( str[ 12 ] );
if( i < 12 ) {
c &= 0x1f;
c <<= 64-5*(i+1);
}
else {
c &= 0x0f;
}

value |= c;
}

return value;
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/test_api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
file(GLOB SOURCE_FILES "*.cpp")
add_wast_target(test_api "${SOURCE_FILES}" "${CMAKE_SOURCE_DIR}/contracts" ${CMAKE_CURRENT_SOURCE_DIR})
55 changes: 55 additions & 0 deletions contracts/test_api/test_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <eoslib/eos.hpp>
#include "test_api.hpp"

extern "C" {

void init() {

}

void apply( unsigned long long code, unsigned long long action ) {

//eos::print("==> CONTRACT: ", code, " ", action, "\n");

//test_types
WASM_TEST_HANDLER(test_types, types_size);
WASM_TEST_HANDLER(test_types, char_to_symbol);
WASM_TEST_HANDLER(test_types, string_to_name);
WASM_TEST_HANDLER(test_types, name_class);

//test_message
WASM_TEST_HANDLER(test_message, read_message);
WASM_TEST_HANDLER(test_message, read_message_to_0);
WASM_TEST_HANDLER(test_message, read_message_to_64k);
WASM_TEST_HANDLER_EX(test_message, require_notice);
WASM_TEST_HANDLER(test_message, require_auth);
WASM_TEST_HANDLER(test_message, assert_false);
WASM_TEST_HANDLER(test_message, assert_true);
WASM_TEST_HANDLER(test_message, now);

//test_print
WASM_TEST_HANDLER(test_print, test_prints);
WASM_TEST_HANDLER(test_print, test_printi);
WASM_TEST_HANDLER(test_print, test_printi128);
WASM_TEST_HANDLER(test_print, test_printn);

//test_math
WASM_TEST_HANDLER(test_math, test_multeq_i128);
WASM_TEST_HANDLER(test_math, test_diveq_i128);
WASM_TEST_HANDLER(test_math, test_diveq_i128_by_0);

//test db
WASM_TEST_HANDLER(test_db, key_i64_general);
WASM_TEST_HANDLER(test_db, key_i64_remove_all);
WASM_TEST_HANDLER(test_db, key_i64_small_load);
WASM_TEST_HANDLER(test_db, key_i64_small_store);
WASM_TEST_HANDLER(test_db, key_i64_store_scope);
WASM_TEST_HANDLER(test_db, key_i64_remove_scope);
WASM_TEST_HANDLER(test_db, key_i64_not_found);
WASM_TEST_HANDLER(test_db, key_i128i128_general);

//unhandled test call
WASM_TEST_ERROR_CODE = WASM_TEST_FAIL;
}

}
96 changes: 96 additions & 0 deletions contracts/test_api/test_api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#pragma once

#define WASM_TEST_ERROR_CODE *((unsigned int *)((1<<16) - 2*sizeof(unsigned int)))
#define WASM_TEST_ERROR_MESSAGE *((unsigned int *)((1<<16) - 1*sizeof(unsigned int)))

#define WASM_TEST_FAIL 0xDEADBEEF
#define WASM_TEST_PASS 0xB0CABACA

#define WASM_ASSERT(m, message) if(!(m)) { WASM_TEST_ERROR_MESSAGE=(unsigned int)message; return WASM_TEST_FAIL; }

typedef unsigned long long u64;
#define WASM_TEST_HANDLER(CLASS, METHOD) \
if( u32(action>>32) == DJBH(#CLASS) && u32(action) == DJBH(#METHOD) ) { \
WASM_TEST_ERROR_CODE = CLASS::METHOD(); \
return; \
}

#define WASM_TEST_HANDLER_EX(CLASS, METHOD) \
if( u32(action>>32) == DJBH(#CLASS) && u32(action) == DJBH(#METHOD) ) { \
WASM_TEST_ERROR_CODE = CLASS::METHOD(code, action); \
return; \
}

typedef unsigned int u32;
static constexpr u32 DJBH(const char* cp)
{
u32 hash = 5381;
while (*cp)
hash = 33 * hash ^ (unsigned char) *cp++;
return hash;
}

#pragma pack(push, 1)
struct dummy_message {
char a; //1
unsigned long long b; //8
int c; //4
};

struct u128_msg {
unsigned __int128 values[3]; //16*3
};
#pragma pack(pop)

static_assert( sizeof(dummy_message) == 13 , "unexpected packing" );
static_assert( sizeof(u128_msg) == 16*3 , "unexpected packing" );

struct test_types {
static unsigned int types_size();
static unsigned int char_to_symbol();
static unsigned int string_to_name();
static unsigned int name_class();
};

struct test_print {
static unsigned int test_prints();
static unsigned int test_printi();
static unsigned int test_printi128();
static unsigned int test_printn();
};

#define DUMMY_MESSAGE_DEFAULT_A 0x45
#define DUMMY_MESSAGE_DEFAULT_B 0xab11cd1244556677
#define DUMMY_MESSAGE_DEFAULT_C 0x7451ae12

struct test_message {

static unsigned int read_message();
static unsigned int read_message_to_0();
static unsigned int read_message_to_64k();
static unsigned int require_notice(unsigned long long code, unsigned long long action);
static unsigned int require_auth();
static unsigned int assert_false();
static unsigned int assert_true();
static unsigned int now();

};

struct test_math {
static unsigned int test_multeq_i128();
static unsigned int test_diveq_i128();
static unsigned int test_diveq_i128_by_0();
};

struct test_db {
static unsigned int key_i64_general();
static unsigned int key_i64_remove_all();
static unsigned int key_i64_small_load();
static unsigned int key_i64_small_store();
static unsigned int key_i64_store_scope();
static unsigned int key_i64_remove_scope();
static unsigned int key_i64_not_found();

static unsigned int key_i128i128_general();
};

Loading