Broke the virtual machine unit test out into a separate Boost auto test case.

This commit is contained in:
Carl Allendorph 2014-04-19 10:55:20 -07:00
parent bf4865adaf
commit 16c7fe5dad
2 changed files with 31 additions and 53 deletions

View File

@ -45,11 +45,10 @@ BOOST_AUTO_TEST_CASE(basic_tests)
std::cout << sha3(s.out()) << std::endl;*/ std::cout << sha3(s.out()) << std::endl;*/
int r = 0; int r = 0;
r += vmTest();
// r += daggerTest(); // r += daggerTest();
// r += stateTest(); // r += stateTest();
// r += peerTest(argc, argv); // r += peerTest(argc, argv);
BOOST_REQUIRE(!r); // BOOST_REQUIRE(!r);
} }

81
vm.cpp
View File

@ -27,12 +27,13 @@
#include <Log.h> #include <Log.h>
#include <Instruction.h> #include <Instruction.h>
#include "JsonSpiritHeaders.h" #include "JsonSpiritHeaders.h"
#include <boost/test/unit_test.hpp>
using namespace std; using namespace std;
using namespace json_spirit; using namespace json_spirit;
using namespace eth; using namespace eth;
namespace eth namespace eth { namespace test {
{
class FakeExtVM: public ExtVMFace class FakeExtVM: public ExtVMFace
{ {
@ -311,37 +312,16 @@ public:
bytes thisTxData; bytes thisTxData;
}; };
#define CREATE_TESTS 0 void doTests(json_spirit::mValue& v, bool _fillin)
template <> class UnitTest<1>
{
public:
int operator()()
{ {
json_spirit::mValue v;
#if CREATE_TESTS
string s = asString(contents("../../cpp-ethereum/test/vmtests.json"));
json_spirit::read_string(s, v);
bool passed = doTests(v, true);
cout << json_spirit::write_string(v, true) << endl;
#else
string s = asString(contents("../../tests/vmtests.json"));
json_spirit::read_string(s, v);
bool passed = doTests(v, false);
#endif
return passed ? 0 : 1;
}
bool doTests(json_spirit::mValue& v, bool _fillin)
{
bool passed = true;
for (auto& i: v.get_obj()) for (auto& i: v.get_obj())
{ {
cnote << i.first; cnote << i.first;
mObject& o = i.second.get_obj(); mObject& o = i.second.get_obj();
VM vm; VM vm;
FakeExtVM fev; eth::test::FakeExtVM fev;
fev.importEnv(o["env"].get_obj()); fev.importEnv(o["env"].get_obj());
fev.importState(o["pre"].get_obj()); fev.importState(o["pre"].get_obj());
@ -365,37 +345,40 @@ public:
} }
else else
{ {
FakeExtVM test; eth::test::FakeExtVM test;
test.importState(o["post"].get_obj()); test.importState(o["post"].get_obj());
test.importTxs(o["txs"].get_array()); test.importTxs(o["txs"].get_array());
int i = 0; int i = 0;
for (auto const& d: o["out"].get_array()) for (auto const& d: o["out"].get_array())
{ {
if (output[i] != FakeExtVM::toInt(d)) BOOST_CHECK_MESSAGE( output[i] == FakeExtVM::toInt(d), "Output byte [" << i << "] different!");
{
cwarn << "Test failed: output byte" << i << "different.";
passed = false;
}
++i; ++i;
} }
BOOST_CHECK( test.addresses == fev.addresses);
if (test.addresses != fev.addresses) BOOST_CHECK( test.txs == fev.txs );
{
cwarn << "Test failed: state different.";
passed = false;
}
if (test.txs != fev.txs)
{
cwarn << "Test failed: tx list different:";
cwarn << test.txs;
cwarn << fev.txs;
passed = false;
}
} }
} }
return passed;
} }
} } // Namespace Close
BOOST_AUTO_TEST_CASE(vm_tests)
{
try
{
cnote << "Testing VM...";
json_spirit::mValue v;
string s = asString(contents("../../tests/vmtests.json"));
BOOST_REQUIRE_MESSAGE( s.length() > 0, "Contents of 'vmtests.json' is empty. Have you cloned the 'tests' repo branch develop?" );
json_spirit::read_string(s, v);
eth::test::doTests(v, false);
}
catch( std::exception& e)
{
BOOST_ERROR("Failed VM Test with Exception: " << e.what());
}
}
#if 0
string makeTestCase() string makeTestCase()
{ {
json_spirit::mObject o; json_spirit::mObject o;
@ -427,9 +410,5 @@ public:
} }
int vmTest()
{
cnote << "Testing VM...";
return UnitTest<1>()();
}
#endif