diff options
author | obscuren <obscuren@obscura.com> | 2014-01-01 10:06:39 +0800 |
---|---|---|
committer | obscuren <obscuren@obscura.com> | 2014-01-01 10:06:39 +0800 |
commit | 276fa6c799b08bc41efd2d26a83eef678e8c943b (patch) | |
tree | b5b72e11bc8f37e5d4d295dc7220f4a870b84978 /trie_test.go | |
parent | f17930eb4661721cd0e1b92765448c589441907b (diff) | |
download | go-tangerine-276fa6c799b08bc41efd2d26a83eef678e8c943b.tar go-tangerine-276fa6c799b08bc41efd2d26a83eef678e8c943b.tar.gz go-tangerine-276fa6c799b08bc41efd2d26a83eef678e8c943b.tar.bz2 go-tangerine-276fa6c799b08bc41efd2d26a83eef678e8c943b.tar.lz go-tangerine-276fa6c799b08bc41efd2d26a83eef678e8c943b.tar.xz go-tangerine-276fa6c799b08bc41efd2d26a83eef678e8c943b.tar.zst go-tangerine-276fa6c799b08bc41efd2d26a83eef678e8c943b.zip |
Working Trie
Diffstat (limited to 'trie_test.go')
-rw-r--r-- | trie_test.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/trie_test.go b/trie_test.go new file mode 100644 index 000000000..dac2333c9 --- /dev/null +++ b/trie_test.go @@ -0,0 +1,69 @@ +package main + +import ( + "testing" + "encoding/hex" +) + +type MemDatabase struct { + db map[string][]byte +} + +func NewMemDatabase() (*MemDatabase, error) { + db := &MemDatabase{db: make(map[string][]byte)} + + return db, nil +} + +func (db *MemDatabase) Put(key []byte, value []byte) { + db.db[string(key)] = value +} + +func (db *MemDatabase) Get(key []byte) ([]byte, error) { + return db.db[string(key)], nil +} + +func TestTriePut(t *testing.T) { + db, err := NewMemDatabase() + trie := NewTrie(db, "") + + if err != nil { + t.Error("Error starting db") + } + + key := trie.Put([]byte("testing node")) + + data, err := db.Get(key) + if err != nil { + t.Error("Nothing at node") + } + + s, _ := Decode(data, 0) + if str, ok := s.([]byte); ok { + if string(str) != "testing node" { + t.Error("Wrong value node", str) + } + } else { + t.Error("Invalid return type") + } +} + +func TestTrieUpdate(t *testing.T) { + db, err := NewMemDatabase() + trie := NewTrie(db, "") + + if err != nil { + t.Error("Error starting db") + } + + + trie.Update("doe", "reindeer") + trie.Update("dog", "puppy") + trie.Update("dogglesworth", "cat") + root := hex.EncodeToString([]byte(trie.root)) + req := "e378927bfc1bd4f01a2e8d9f59bd18db8a208bb493ac0b00f93ce51d4d2af76c" + if root != req { + t.Error("trie.root do not match, expected", req, "got", root) + } +} + |