GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bitset/bitset_iter_test.go at master · bits-and-blooms/bitset

Expand file tree

/

bitset_iter_test.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
118 lines (102 loc) · 2.35 KB
/

bitset_iter_test.go

Copy path

File metadata and controls

118 lines (102 loc) · 2.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//go:build go1.23
// +build go1.23
package bitset
import (
"math/rand"
"testing"
)
func TestIter(t *testing.T) {
var b BitSet
b.Set(0).Set(3).Set(5).Set(6).Set(63).Set(64).Set(65).Set(127).Set(128).Set(1000000)
// Expected values that should be set
expected := []uint{0, 3, 5, 6, 63, 64, 65, 127, 128, 1000000}
got := make([]uint, 0)
// Collect all values from iterator
for i := range b.EachSet() {
got = append(got, i)
}
// Test 1: Check length matches expected
if len(got) != len(expected) {
t.Errorf("Expected %d elements, got %d", len(expected), len(got))
}
// Test 2: Check all expected values are present and in correct order
for i, want := range expected {
if i >= len(got) {
t.Errorf("Missing expected value %d at position %d", want, i)
continue
}
if got[i] != want {
t.Errorf("At position %d: expected %d, got %d", i, want, got[i])
}
}
// Test 3: Check no extra values
if len(got) > len(expected) {
t.Errorf("Got extra values: %v", got[len(expected):])
}
}
func TestIterEarlyStop(t *testing.T) {
var b BitSet
b.Set(2).Set(7).Set(90)
got := make([]uint, 0)
for i := range b.EachSet() {
got = append(got, i)
if len(got) == 2 {
break
}
}
if len(got) != 2 || got[0] != 2 || got[1] != 7 {
t.Errorf("Expected [2 7], got %v", got)
}
}
func TestIterMatchesAppendTo(t *testing.T) {
r := rand.New(rand.NewSource(42))
for _, density := range []float64{0, 0.01, 0.5, 1} {
var b BitSet
for i := uint(0); i < 4096; i++ {
if r.Float64() < density {
b.Set(i)
}
}
want := b.AppendTo(nil)
got := make([]uint, 0, len(want))
for i := range b.EachSet() {
got = append(got, i)
}
if len(got) != len(want) {
t.Fatalf("density %v: got %d values, want %d", density, len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("density %v: position %d: got %d, want %d", density, i, got[i], want[i])
}
}
}
}
func BenchmarkIter(b *testing.B) {
b.StopTimer()
s := New(10000)
for i := 0; i < 10000; i += 3 {
s.Set(uint(i))
}
b.StartTimer()
for j := 0; j < b.N; j++ {
c := uint(0)
for range s.EachSet() {
c++
}
}
}
func BenchmarkNonInter(b *testing.B) {
b.StopTimer()
s := New(10000)
for i := 0; i < 10000; i += 3 {
s.Set(uint(i))
}
b.StartTimer()
for j := 0; j < b.N; j++ {
c := uint(0)
for i, e := s.NextSet(0); e; i, e = s.NextSet(i + 1) {
c++
}
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.