Skip to content

Commit 2144cfa

Browse files
committed
test(span): More tests and docs for span lib
1 parent 941b56b commit 2144cfa

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

angch/span/span.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func (s Spans[T]) Add(from, to int, content T) Spans[T] {
1818
return newspan
1919
}
2020

21+
// AddCompress add a new span, sort and compress existing one and returns a *new*
22+
// Spans[T]. Make sure you assign or reassign the new Spans. Can't help it, generics
2123
func (s Spans[T]) AddCompress(from, to int, content T) Spans[T] {
2224
// FIXME, this func hasn't been fixed to make sure content is the same before merging
2325
from, to = min(from, to), max(from, to)

angch/span/span_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package span
22

33
import (
4+
"log"
45
"reflect"
56
"testing"
67
)
@@ -48,3 +49,36 @@ func TestAddInt(t *testing.T) {
4849
})
4950
}
5051
}
52+
53+
func TestSpans_Contains(t *testing.T) {
54+
55+
// Test spans based on AoC 2025-05
56+
spans := NewSpans[bool]()
57+
spans = spans.AddCompress(3, 5, true)
58+
spans = spans.AddCompress(10, 14, true)
59+
spans = spans.AddCompress(16, 20, true)
60+
spans = spans.AddCompress(12, 18, true)
61+
62+
tests := []struct {
63+
name string // description of this test case
64+
// Named input parameters for target function.
65+
i int
66+
want bool
67+
}{
68+
{"1", 1, false},
69+
{"5", 5, true},
70+
{"8", 8, false},
71+
{"11", 11, true},
72+
{"17", 17, true},
73+
{"32", 32, false},
74+
}
75+
for _, tt := range tests {
76+
t.Run(tt.name, func(t *testing.T) {
77+
log.Println(spans)
78+
got := spans.Contains(tt.i)
79+
if got != tt.want {
80+
t.Errorf("Contains() = %v, want %v", got, tt.want)
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)