Debian Codegraph @

common/hmaps/orderedintset_test.go

raw
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 TestOrderedIntSet(t *testing.T) {
23 c := qt.New(t)
24
25 m := NewOrderedIntSet(2, 1, 3, 7)
26
27 c.Assert(m.Len(), qt.Equals, 4)
28 c.Assert(m.Has(1), qt.Equals, true)
29 c.Assert(m.Has(4), qt.Equals, false)
30 c.Assert(m.String(), qt.Equals, "[2 1 3 7]")
31 m.Set(4)
32 c.Assert(m.Len(), qt.Equals, 5)
33 c.Assert(m.Has(4), qt.Equals, true)
34 c.Assert(m.Next(0), qt.Equals, 1)
35 c.Assert(m.Next(1), qt.Equals, 1)
36 c.Assert(m.Next(2), qt.Equals, 2)
37 c.Assert(m.Next(3), qt.Equals, 3)
38 c.Assert(m.Next(4), qt.Equals, 4)
39 c.Assert(m.Next(7), qt.Equals, 7)
40 c.Assert(m.Next(8), qt.Equals, -1)
41 c.Assert(m.String(), qt.Equals, "[2 1 3 7 4]")
42
43 var nilset *OrderedIntSet
44 c.Assert(nilset.Len(), qt.Equals, 0)
45 c.Assert(nilset.Has(1), qt.Equals, false)
46 c.Assert(nilset.String(), qt.Equals, "[]")
47
48 var collected []int
49 m.ForEachKey(func(key int) bool {
50 collected = append(collected, key)
51 return true
52 })
53 c.Assert(collected, qt.DeepEquals, []int{2, 1, 3, 7, 4})
54 }
55
56 func BenchmarkOrderedIntSet(b *testing.B) {
57 smallSet := NewOrderedIntSet()
58 for i := range 8 {
59 smallSet.Set(i)
60 }
61 mediumSet := NewOrderedIntSet()
62 for i := range 64 {
63 mediumSet.Set(i)
64 }
65 largeSet := NewOrderedIntSet()
66 for i := range 1024 {
67 largeSet.Set(i)
68 }
69
70 b.Run("New", func(b *testing.B) {
71 for b.Loop() {
72 NewOrderedIntSet(1, 2, 3, 4, 5, 6, 7, 8)
73 }
74 })
75
76 b.Run("Has small", func(b *testing.B) {
77 for i := 0; b.Loop(); i++ {
78 smallSet.Has(i % 32)
79 }
80 })
81
82 b.Run("Has medium", func(b *testing.B) {
83 for i := 0; b.Loop(); i++ {
84 mediumSet.Has(i % 32)
85 }
86 })
87
88 b.Run("Next", func(b *testing.B) {
89 for i := 0; b.Loop(); i++ {
90 mediumSet.Next(i % 32)
91 }
92 })
93
94 b.Run("ForEachKey small", func(b *testing.B) {
95 for b.Loop() {
96 smallSet.ForEachKey(func(key int) bool {
97 return true
98 })
99 }
100 })
101
102 b.Run("ForEachKey medium", func(b *testing.B) {
103 for b.Loop() {
104 mediumSet.ForEachKey(func(key int) bool {
105 return true
106 })
107 }
108 })
109
110 b.Run("ForEachKey large", func(b *testing.B) {
111 for b.Loop() {
112 largeSet.ForEachKey(func(key int) bool {
113 return true
114 })
115 }
116 })
117 }