aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGav Wood <i@gavwood.com>2014-02-28 20:55:30 +0800
committerGav Wood <i@gavwood.com>2014-02-28 20:55:30 +0800
commita2f6a1747018942c540eaf382c87107febb006b4 (patch)
treeb04ee107e53a221f5749d6645eea4c3efe2ba929
parent857b9f9bf9345397a686fd5b35034e94cfde33e0 (diff)
downloaddexon-solidity-a2f6a1747018942c540eaf382c87107febb006b4.tar.gz
dexon-solidity-a2f6a1747018942c540eaf382c87107febb006b4.tar.zst
dexon-solidity-a2f6a1747018942c540eaf382c87107febb006b4.zip
Tests.
-rw-r--r--crypto.cpp1
-rw-r--r--dagger.cpp1
-rw-r--r--hexPrefix.cpp69
-rw-r--r--main.cpp18
-rw-r--r--rlp.cpp90
-rw-r--r--state.cpp2
-rw-r--r--trie.cpp54
-rw-r--r--vm.cpp3
8 files changed, 167 insertions, 71 deletions
diff --git a/crypto.cpp b/crypto.cpp
index 3d565dd7..8a57e1d5 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 cc22baa1..8f7df77d 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 a579f3f0..10b839a7 100644
--- a/hexPrefix.cpp
+++ b/hexPrefix.cpp
@@ -20,38 +20,51 @@
* Main test functions.
*/
+#include <fstream>
+#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;
-int hexPrefixTest()
+namespace eth
+{
+
+template <> class UnitTest<3>
{
- /*
- * 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;
+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()
+{
+ cnote << "Testing Hex-Prefix-Encode...";
+ return UnitTest<3>()();
+}
diff --git a/main.cpp b/main.cpp
index ed0ea76e..311a4370 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 744c3d33..bc8fb603 100644
--- a/rlp.cpp
+++ b/rlp.cpp
@@ -20,48 +20,70 @@
* RLP test functions.
*/
+#include <fstream>
+#include "../json_spirit/json_spirit_reader_template.h"
+#include "../json_spirit/json_spirit_writer_template.h"
#include <RLP.h>
using namespace std;
using namespace eth;
+namespace js = json_spirit;
-int rlpTest()
+namespace eth
{
- // 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");
+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);
+ }
+ }
- // 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");
+ 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;
+ }
- // 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;
}
+int rlpTest()
+{
+ cnote << "Testing RLP...";
+ return UnitTest<2>()();
+}
diff --git a/state.cpp b/state.cpp
index a3ea2aea..60d68b93 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 5cce3d0f..26a07260 100644
--- a/trie.cpp
+++ b/trie.cpp
@@ -20,6 +20,9 @@
* Trie test functions.
*/
+#include <fstream>
+#include "../json_spirit/json_spirit_reader_template.h"
+#include "../json_spirit/json_spirit_writer_template.h"
#include <random>
#include <TrieHash.h>
#include <TrieDB.h>
@@ -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<pair<string, string>> 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<BasicMap> 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<BasicMap> t(&m);
diff --git a/vm.cpp b/vm.cpp
index 1ba54d30..55299f90 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>()();
}