diff options
author | Gav Wood <i@gavwood.com> | 2014-02-28 20:55:30 +0800 |
---|---|---|
committer | Gav Wood <i@gavwood.com> | 2014-02-28 20:55:30 +0800 |
commit | a2f6a1747018942c540eaf382c87107febb006b4 (patch) | |
tree | b04ee107e53a221f5749d6645eea4c3efe2ba929 /trie.cpp | |
parent | 857b9f9bf9345397a686fd5b35034e94cfde33e0 (diff) | |
download | dexon-solidity-a2f6a1747018942c540eaf382c87107febb006b4.tar.gz dexon-solidity-a2f6a1747018942c540eaf382c87107febb006b4.tar.zst dexon-solidity-a2f6a1747018942c540eaf382c87107febb006b4.zip |
Tests.
Diffstat (limited to 'trie.cpp')
-rw-r--r-- | trie.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -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); |