mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:18:31 +02:00
feat: trie implementation
This commit is contained in:
37
internal/utils/trie/json_test.go
Normal file
37
internal/utils/trie/json_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package trie
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
func TestMarshalUnmarshalJSON(t *testing.T) {
|
||||
trie := NewTrie()
|
||||
data := map[string]any{
|
||||
"foo.bar": 42.12,
|
||||
"foo.baz": "hello",
|
||||
"qwe.rt.yu.io": 123.45,
|
||||
}
|
||||
for k, v := range data {
|
||||
trie.Store(NewKey(k), v)
|
||||
}
|
||||
|
||||
// MarshalJSON
|
||||
bytesFromTrie, err := sonic.Marshal(trie)
|
||||
if err != nil {
|
||||
t.Fatalf("sonic.Marshal error: %v", err)
|
||||
}
|
||||
|
||||
// UnmarshalJSON
|
||||
newTrie := NewTrie()
|
||||
if err := sonic.Unmarshal(bytesFromTrie, newTrie); err != nil {
|
||||
t.Fatalf("UnmarshalJSON error: %v", err)
|
||||
}
|
||||
for k, v := range data {
|
||||
got, ok := newTrie.Get(NewKey(k))
|
||||
if !ok || got != v {
|
||||
t.Errorf("UnmarshalJSON: key %q got %v, want %v", k, got, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user