From a2f6a1747018942c540eaf382c87107febb006b4 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 28 Feb 2014 12:55:30 +0000 Subject: [PATCH] Tests. --- crypto.cpp | 1 + dagger.cpp | 1 + hexPrefix.cpp | 69 +++++++++++++++++++++--------------- main.cpp | 18 +++++----- rlp.cpp | 98 +++++++++++++++++++++++++++++++-------------------- state.cpp | 2 ++ trie.cpp | 54 ++++++++++++++++++++++++++++ vm.cpp | 3 +- 8 files changed, 171 insertions(+), 75 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index 3d565dd73..8a57e1d57 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -30,6 +30,7 @@ using namespace eth; int cryptoTest() { + cnote << "Testing Crypto..."; secp256k1_start(); KeyPair p(Secret(fromUserHex("3ecb44df2159c26e0f995712d4f39b6f6e499b40749b1cf1246c37f9516cb6a4"))); diff --git a/dagger.cpp b/dagger.cpp index cc22baa13..8f7df77d3 100644 --- a/dagger.cpp +++ b/dagger.cpp @@ -28,6 +28,7 @@ using namespace eth; int daggerTest() { + cnote << "Testing Dagger..."; // Test dagger { auto s = steady_clock::now(); diff --git a/hexPrefix.cpp b/hexPrefix.cpp index a579f3f02..10b839a75 100644 --- a/hexPrefix.cpp +++ b/hexPrefix.cpp @@ -20,38 +20,51 @@ * Main test functions. */ +#include +#include "../json_spirit/json_spirit_reader_template.h" +#include "../json_spirit/json_spirit_writer_template.h" #include "TrieCommon.h" using namespace std; using namespace eth; +namespace js = json_spirit; + +namespace eth +{ + +template <> class UnitTest<3> +{ +public: + int operator()() + { + js::mValue v; + string s = asString(contents("../../tests/hexencodetest.json")); + js::read_string(s, v); + bool passed = true; + for (auto& i: v.get_obj()) + { + js::mObject& o = i.second.get_obj(); + cnote << i.first; + bytes v; + for (auto& i: o["seq"].get_array()) + v.push_back(i.get_int()); + auto e = hexPrefixEncode(v, o["term"].get_bool()); + if (!o["out"].is_null() && o["out"].get_str() != asHex(e)) + { + cwarn << "Test failed."; + cwarn << "Test says:" << o["out"].get_str(); + cwarn << "Impl says:" << asHex(e); + passed = false; + } + } + return passed ? 0 : 1; + } + +}; + +} int hexPrefixTest() { - /* - * Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1 - * [0,0,1,2,3,4,5] 0x10012345 - * [0,1,2,3,4,5] 0x00012345 - * [1,2,3,4,5] 0x112345 - * [0,0,1,2,3,4] 0x00001234 - * [0,1,2,3,4] 0x101234 - * [1,2,3,4] 0x001234 - * [0,0,1,2,3,4,5,T] 0x30012345 - * [0,0,1,2,3,4,T] 0x20001234 - * [0,1,2,3,4,5,T] 0x20012345 - * [1,2,3,4,5,T] 0x312345 - * [1,2,3,4,T] 0x201234 - */ - assert(asHex(hexPrefixEncode({0, 0, 1, 2, 3, 4, 5}, false)) == "10012345"); - assert(asHex(hexPrefixEncode({0, 1, 2, 3, 4, 5}, false)) == "00012345"); - assert(asHex(hexPrefixEncode({1, 2, 3, 4, 5}, false)) == "112345"); - assert(asHex(hexPrefixEncode({0, 0, 1, 2, 3, 4}, false)) == "00001234"); - assert(asHex(hexPrefixEncode({0, 1, 2, 3, 4}, false)) == "101234"); - assert(asHex(hexPrefixEncode({1, 2, 3, 4}, false)) == "001234"); - assert(asHex(hexPrefixEncode({0, 0, 1, 2, 3, 4, 5}, true)) == "30012345"); - assert(asHex(hexPrefixEncode({0, 0, 1, 2, 3, 4}, true)) == "20001234"); - assert(asHex(hexPrefixEncode({0, 1, 2, 3, 4, 5}, true)) == "20012345"); - assert(asHex(hexPrefixEncode({1, 2, 3, 4, 5}, true)) == "312345"); - assert(asHex(hexPrefixEncode({1, 2, 3, 4}, true)) == "201234"); - - return 0; + cnote << "Testing Hex-Prefix-Encode..."; + return UnitTest<3>()(); } - diff --git a/main.cpp b/main.cpp index ed0ea76e7..311a43701 100644 --- a/main.cpp +++ b/main.cpp @@ -42,14 +42,16 @@ int main(int, char**) std::cout << asHex(s.out()) << std::endl; std::cout << sha3(s.out()) << std::endl;*/ - hexPrefixTest(); - rlpTest(); - trieTest(); - daggerTest(); - cryptoTest(); - vmTest(); -// stateTest(); -// peerTest(argc, argv); + int r = 0; + r += hexPrefixTest(); + r += rlpTest(); + r += trieTest(); + r += vmTest(); + r += cryptoTest(); // TODO: Put in tests repo. +// r += daggerTest(); +// r += stateTest(); +// r += peerTest(argc, argv); + assert(!r); return 0; } diff --git a/rlp.cpp b/rlp.cpp index 744c3d33c..bc8fb6031 100644 --- a/rlp.cpp +++ b/rlp.cpp @@ -20,48 +20,70 @@ * RLP test functions. */ +#include +#include "../json_spirit/json_spirit_reader_template.h" +#include "../json_spirit/json_spirit_writer_template.h" #include using namespace std; using namespace eth; +namespace js = json_spirit; + +namespace eth +{ + +template <> class UnitTest<2> +{ +public: + static void buildRLP(js::mValue& _v, RLPStream& _rlp) + { + if (_v.type() == js::array_type) + { + RLPStream s; + for (auto& i: _v.get_array()) + buildRLP(i, s); + _rlp.appendList(s.out()); + } + else if (_v.type() == js::int_type) + _rlp.append(_v.get_uint64()); + else if (_v.type() == js::str_type) + { + auto s = _v.get_str(); + if (s.size() && s[0] == '#') + _rlp.append(bigint(s.substr(1))); + else + _rlp.append(s); + } + } + + int operator()() + { + js::mValue v; + string s = asString(contents("../../tests/rlptest.json")); + js::read_string(s, v); + bool passed = true; + for (auto& i: v.get_obj()) + { + js::mObject& o = i.second.get_obj(); + cnote << i.first; + RLPStream s; + buildRLP(o["in"], s); + if (!o["out"].is_null() && o["out"].get_str() != asHex(s.out())) + { + cwarn << "Test failed."; + cwarn << "Test says:" << o["out"].get_str(); + cwarn << "Impl says:" << asHex(s.out()); + passed = false; + } + } + return passed ? 0 : 1; + } + +}; + +} int rlpTest() { - // int of value 15 - assert(RLP("\x0f") == 15); - assert(asString(rlp(15)) == "\x0f"); - - // 3-character string - assert(RLP("\x83""dog") == "dog"); - assert(asString(rlp("dog")) == "\x83""dog"); - - // 2-item list - string twoItemListString = "\xc5\x0f\x83""dog"; - RLP twoItemList(twoItemListString); - assert(twoItemList.itemCount() == 2); - assert(twoItemList[0] == 15); - assert(twoItemList[1] == "dog"); - assert(asString(rlpList(15, "dog")) == "\xc5\x0f\x83""dog"); - - // null - assert(RLP("\x80") == ""); - assert(asString(rlp("")) == "\x80"); - - // 1-byte (8-bit) int - assert(RLP("\x81\x80") == 128); - assert(asString(rlp(128)) == "\x81\x80"); - - // 2-byte (16-bit) int - assert(RLP("\x82\x01\x01") == 257); - assert(asString(rlp(257)) == "\x82\x01\x01"); - - // 32-byte (256-bit) int - assert(RLP("\xa0\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f") == bigint("0x100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")); - assert(asString(rlp(bigint("0x100102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"))) == "\xa0\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"); - - // 56-character string. - assert(RLP("\xb8\x38""Lorem ipsum dolor sit amet, consectetur adipisicing elit") == "Lorem ipsum dolor sit amet, consectetur adipisicing elit"); - assert(asString(rlp("Lorem ipsum dolor sit amet, consectetur adipisicing elit")) == "\xb8\x38""Lorem ipsum dolor sit amet, consectetur adipisicing elit"); - - return 0; + cnote << "Testing RLP..."; + return UnitTest<2>()(); } - diff --git a/state.cpp b/state.cpp index a3ea2aea2..60d68b936 100644 --- a/state.cpp +++ b/state.cpp @@ -30,6 +30,8 @@ using namespace eth; int stateTest() { + cnote << "Testing State..."; + KeyPair me = sha3("Gav Wood"); KeyPair myMiner = sha3("Gav's Miner"); // KeyPair you = sha3("123"); diff --git a/trie.cpp b/trie.cpp index 5cce3d0f4..26a072600 100644 --- a/trie.cpp +++ b/trie.cpp @@ -20,6 +20,9 @@ * Trie test functions. */ +#include +#include "../json_spirit/json_spirit_reader_template.h" +#include "../json_spirit/json_spirit_writer_template.h" #include #include #include @@ -27,6 +30,53 @@ using namespace std; using namespace eth; +namespace js = json_spirit; + +namespace eth +{ + +unsigned fac(unsigned _i) { return _i > 2 ? _i * fac(_i - 1) : _i; } + +template <> class UnitTest<4> +{ +public: + int operator()() + { + js::mValue v; + string s = asString(contents("../../tests/trietest.json")); + js::read_string(s, v); + bool passed = true; + for (auto& i: v.get_obj()) + { + js::mObject& o = i.second.get_obj(); + cnote << i.first; + vector> ss; + for (auto& i: o["in"].get_obj()) + ss.push_back(make_pair(i.first, i.second.get_str())); + for (unsigned j = 0; j < fac(ss.size()); ++j) + { + next_permutation(ss.begin(), ss.end()); + BasicMap m; + GenericTrieDB t(&m); + t.init(); + for (auto const& k: ss) + t.insert(k.first, k.second); + if (!o["root"].is_null() && o["root"].get_str() != asHex(t.root().asArray())) + { + cwarn << "Test failed on permutation " << j; + cwarn << "Test says:" << o["root"].get_str(); + cwarn << "Impl says:" << asHex(t.root().asArray()); + passed = false; + } + } + } + return passed ? 0 : 1; + } + +}; + +} + inline h256 stringMapHash256(StringMap const& _s) { return hash256(_s); @@ -34,6 +84,10 @@ inline h256 stringMapHash256(StringMap const& _s) int trieTest() { + cnote << "Testing Trie..."; + return UnitTest<4>()(); + + // More tests... { BasicMap m; GenericTrieDB t(&m); diff --git a/vm.cpp b/vm.cpp index 1ba54d30b..55299f906 100644 --- a/vm.cpp +++ b/vm.cpp @@ -343,7 +343,7 @@ public: Transactions txs; }; -#define CREATE_TESTS 1 +#define CREATE_TESTS 0 template <> class UnitTest<1> { @@ -446,6 +446,7 @@ public: int vmTest() { + cnote << "Testing VM..."; return UnitTest<1>()(); }