| 1 |
// Copyright 2026 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hmaps |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
) |
| 21 |
|
| 22 |
func TestOrdered(t *testing.T) { |
| 23 |
c := qt.New(t) |
| 24 |
|
| 25 |
m := NewOrdered[string, int]() |
| 26 |
m.Set("a", 1) |
| 27 |
m.Set("b", 2) |
| 28 |
m.Set("c", 3) |
| 29 |
|
| 30 |
c.Assert(m.Keys(), qt.DeepEquals, []string{"a", "b", "c"}) |
| 31 |
c.Assert(m.Values(), qt.DeepEquals, []int{1, 2, 3}) |
| 32 |
|
| 33 |
v, found := m.Get("b") |
| 34 |
c.Assert(found, qt.Equals, true) |
| 35 |
c.Assert(v, qt.Equals, 2) |
| 36 |
|
| 37 |
m.Set("b", 22) |
| 38 |
c.Assert(m.Keys(), qt.DeepEquals, []string{"a", "b", "c"}) |
| 39 |
c.Assert(m.Values(), qt.DeepEquals, []int{1, 22, 3}) |
| 40 |
|
| 41 |
m.Delete("b") |
| 42 |
|
| 43 |
c.Assert(m.Keys(), qt.DeepEquals, []string{"a", "c"}) |
| 44 |
c.Assert(m.Values(), qt.DeepEquals, []int{1, 3}) |
| 45 |
} |
| 46 |
|
| 47 |
func TestOrderedHash(t *testing.T) { |
| 48 |
c := qt.New(t) |
| 49 |
|
| 50 |
m := NewOrdered[string, int]() |
| 51 |
m.Set("a", 1) |
| 52 |
m.Set("b", 2) |
| 53 |
m.Set("c", 3) |
| 54 |
|
| 55 |
h1, err := m.Hash() |
| 56 |
c.Assert(err, qt.IsNil) |
| 57 |
|
| 58 |
m.Set("d", 4) |
| 59 |
|
| 60 |
h2, err := m.Hash() |
| 61 |
c.Assert(err, qt.IsNil) |
| 62 |
|
| 63 |
c.Assert(h1, qt.Not(qt.Equals), h2) |
| 64 |
|
| 65 |
m = NewOrdered[string, int]() |
| 66 |
m.Set("b", 2) |
| 67 |
m.Set("a", 1) |
| 68 |
m.Set("c", 3) |
| 69 |
|
| 70 |
h3, err := m.Hash() |
| 71 |
c.Assert(err, qt.IsNil) |
| 72 |
// Order does not matter. |
| 73 |
c.Assert(h1, qt.Equals, h3) |
| 74 |
} |
| 75 |
|
| 76 |
func TestOrderedNil(t *testing.T) { |
| 77 |
c := qt.New(t) |
| 78 |
|
| 79 |
var m *Ordered[string, int] |
| 80 |
|
| 81 |
m.Set("a", 1) |
| 82 |
c.Assert(m.Keys(), qt.IsNil) |
| 83 |
c.Assert(m.Values(), qt.IsNil) |
| 84 |
v, found := m.Get("a") |
| 85 |
c.Assert(found, qt.Equals, false) |
| 86 |
c.Assert(v, qt.Equals, 0) |
| 87 |
m.Delete("a") |
| 88 |
var b bool |
| 89 |
m.Range(func(k string, v int) bool { |
| 90 |
b = true |
| 91 |
return true |
| 92 |
}) |
| 93 |
c.Assert(b, qt.Equals, false) |
| 94 |
c.Assert(m.Len(), qt.Equals, 0) |
| 95 |
c.Assert(m.Clone(), qt.IsNil) |
| 96 |
h, err := m.Hash() |
| 97 |
c.Assert(err, qt.IsNil) |
| 98 |
c.Assert(h, qt.Equals, uint64(0)) |
| 99 |
} |