aboutsummaryrefslogtreecommitdiffstats
path: root/ethutil/trie_test.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-06-30 19:08:00 +0800
committerobscuren <geffobscura@gmail.com>2014-06-30 19:08:00 +0800
commit5a86892ecbd68c3d466cb1ef282c4cb81300abce (patch)
treede51bb2eeda7ae39de026491ff200173ebd454d0 /ethutil/trie_test.go
parent8151858e70e19996d05928e4c63d36cd4847daa8 (diff)
downloaddexon-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar
dexon-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar.gz
dexon-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar.bz2
dexon-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar.lz
dexon-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar.xz
dexon-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar.zst
dexon-5a86892ecbd68c3d466cb1ef282c4cb81300abce.zip
Using remote for test cases
Diffstat (limited to 'ethutil/trie_test.go')
-rw-r--r--ethutil/trie_test.go108
1 files changed, 94 insertions, 14 deletions
diff --git a/ethutil/trie_test.go b/ethutil/trie_test.go
index 2937b1525..3ee955b11 100644
--- a/ethutil/trie_test.go
+++ b/ethutil/trie_test.go
@@ -1,7 +1,12 @@
package ethutil
import (
+ "bytes"
+ "encoding/hex"
+ "encoding/json"
"fmt"
+ "io/ioutil"
+ "net/http"
"reflect"
"testing"
)
@@ -171,23 +176,98 @@ func TestTriePurge(t *testing.T) {
}
}
-func TestTrieIt(t *testing.T) {
- _, trie := New()
+func h(str string) string {
+ d, err := hex.DecodeString(str)
+ if err != nil {
+ panic(err)
+ }
+
+ return string(d)
+}
- data := [][]string{
- {"do", "verb"},
- {"ether", "wookiedoo"},
- {"horse", "stallion"},
- {"shaman", "horse"},
- {"doge", "coin"},
- {"ether", ""},
- {"dog", "puppy"},
- {"shaman", ""},
+func get(in string) (out string) {
+ if len(in) > 2 && in[:2] == "0x" {
+ out = h(in[2:])
+ } else {
+ out = in
}
- for _, item := range data {
- trie.Update(item[0], item[1])
+ return
+}
+
+type Test struct {
+ Name string
+ In map[string]string
+ Root string
+}
+
+func CreateTest(name string, data []byte) (Test, error) {
+ t := Test{Name: name}
+ err := json.Unmarshal(data, &t)
+ if err != nil {
+ return Test{}, fmt.Errorf("%v", err)
}
- fmt.Printf("root %x", trie.Root)
+ return t, nil
+}
+
+func CreateTests(uri string, cb func(Test)) {
+ resp, err := http.Get(uri)
+ if err != nil {
+ panic(err)
+ }
+ defer resp.Body.Close()
+
+ data, err := ioutil.ReadAll(resp.Body)
+
+ var objmap map[string]*json.RawMessage
+ err = json.Unmarshal(data, &objmap)
+ if err != nil {
+ panic(err)
+ }
+
+ for name, testData := range objmap {
+ test, err := CreateTest(name, *testData)
+ if err != nil {
+ panic(err)
+ }
+
+ cb(test)
+ }
+}
+
+func TestRemote(t *testing.T) {
+ CreateTests("https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json", func(test Test) {
+ _, trie := New()
+ for key, value := range test.In {
+ trie.Update(get(key), get(value))
+ }
+ fmt.Printf("%-15s: %x\n", test.Name, trie.Root)
+
+ a := NewValue(h(test.Root)).Bytes()
+ b := NewValue(trie.Root).Bytes()
+ if bytes.Compare(a, b) != 0 {
+ t.Errorf("%-10s: %x %x", test.Name, a, b)
+ }
+ })
+}
+
+func TestTrieReplay(t *testing.T) {
+ CreateTests("https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json", func(test Test) {
+ _, trie := New()
+ for key, value := range test.In {
+ trie.Update(get(key), get(value))
+ }
+
+ _, trie2 := New()
+ trie.NewIterator().Each(func(key string, v *Value) {
+ trie2.Update(key, string(v.Str()))
+ })
+
+ a := NewValue(trie.Root).Bytes()
+ b := NewValue(trie2.Root).Bytes()
+ if bytes.Compare(a, b) != 0 {
+ t.Errorf("root %x %x\n", trie.Root, trie2.Root)
+ }
+ })
}