diff options
| author | Jonas Knobloch <jonas.knobloch@t-online.de> | 2026-09-11 18:38:13 +0200 |
|---|---|---|
| committer | Jonas Knobloch <jonas.knobloch@t-online.de> | 2026-09-11 18:38:13 +0200 |
| commit | 75e581b3bc19a73d0c1f48b32f2e58121c9741c1 (patch) | |
| tree | 897e2f24db3374d58ba36dcee67879cc7cd4956c /dict/dict_test.go | |
| parent | 330c6387962b197c5ca7a8051d4864feaecc0f25 (diff) | |
| parent | f998fee7427403698f9262d1e92d1a307c652094 (diff) | |
Merge remote-tracking branch 'origin/wip-entropy' into wip-frequency
Diffstat (limited to 'dict/dict_test.go')
| -rw-r--r-- | dict/dict_test.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/dict/dict_test.go b/dict/dict_test.go new file mode 100644 index 0000000..c5fb717 --- /dev/null +++ b/dict/dict_test.go @@ -0,0 +1,78 @@ +package dict + +import ( + "fmt" + "slices" + "strings" + "testing" +) + +func TestDict_Sort(t *testing.T) { + d := NewDict[string]() + + d.Set("foo", "foo") + d.Set("bar", "bar") + d.Set("baz", "baz") + + d.Sort(func(a, b Entry[string]) int { + return strings.Compare(a.Key, b.Key) + }) + + expected := []Entry[string]{ + {"bar", "bar"}, + {"baz", "baz"}, + {"foo", "foo"}, + } + + verifyMap(t, d) + + if !slices.Equal(d.s, expected) { + t.Fatalf("expected %v but got %v", expected, d.s) + } +} + +func verifyMap(t *testing.T, dict *Dict[string]) { + if len(dict.m) != len(dict.s) { + t.Fatalf("length missmatch") + } + + for i, s := range dict.s { + v, ok := dict.m[s.Key] + + if !ok { + t.Fatalf("unknown key %s", s) + } + + if v != i { + t.Errorf("expected %d but got %d\n", i, v) + } + } +} + +func BenchmarkDict_Get(b *testing.B) { + d := NewDict[int]() + + for i := 0; i < 1000; i++ { + d.Set(fmt.Sprintf("key_%d", i), i) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = d.Get("key_500") + } +} + +func BenchmarkDict_GetBytes(b *testing.B) { + d := NewDict[int]() + + for i := 0; i < 1000; i++ { + d.Set(fmt.Sprintf("key_%d", i), i) + } + + keyBytes := []byte("key_500") + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _, _ = d.GetBytes(keyBytes) + } +} |
