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
|
package rangemap_test
import (
"jinx/pkg/libs/rangemap"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAppendAndGet(t *testing.T) {
rm := rangemap.New[string]()
rm.AppendToLast(0, "a")
rm.AppendToLast(1, "b")
rm.AppendToLast(4, "c")
assert.Equal(t, "a", *rm.Get(0))
assert.Equal(t, "b", *rm.Get(1))
assert.Equal(t, "c", *rm.Get(2))
assert.Equal(t, "c", *rm.Get(3))
assert.Equal(t, "c", *rm.Get(4))
}
func TestEmpty(t *testing.T) {
rm := rangemap.New[int]()
assert.Nil(t, rm.Get(0))
assert.Nil(t, rm.Get(1))
assert.Nil(t, rm.Get(2))
assert.Nil(t, rm.Get(-1))
}
func TestNoOverlap(t *testing.T) {
rm := rangemap.New[string]()
rm.Set(0, 2, "a")
rm.Set(3, 3, "b")
rm.Set(4, 7, "c")
assert.Equal(t, "a", *rm.Get(0))
assert.Equal(t, "a", *rm.Get(1))
assert.Equal(t, "a", *rm.Get(2))
assert.Equal(t, "b", *rm.Get(3))
assert.Equal(t, "c", *rm.Get(4))
assert.Equal(t, "c", *rm.Get(5))
assert.Equal(t, "c", *rm.Get(6))
assert.Equal(t, "c", *rm.Get(7))
assert.Nil(t, rm.Get(8))
assert.Equal(t, "[{0..2: a}, {3..3: b}, {4..7: c}, {8..-1}]", rm.String())
}
func TestOverlap(t *testing.T) {
rm := rangemap.New[string]()
rm.Set(0, 2, "a")
rm.Set(3, 5, "b")
rm.Set(6, 10, "c")
rm.Set(4, 8, "x")
assert.Equal(t, "[{0..2: a}, {3..3: b}, {4..8: x}, {9..10: c}, {11..-1}]", rm.String())
}
func TestOverwrite(t *testing.T) {
rm := rangemap.New[string]()
rm.Set(0, 2, "a")
rm.Set(3, 5, "b")
rm.Set(6, 10, "c")
rm.Set(3, 9, "x")
assert.Equal(t, "[{0..2: a}, {3..9: x}, {10..10: c}, {11..-1}]", rm.String())
}
func TestOverwriteInBetween(t *testing.T) {
rm := rangemap.New[string]()
rm.Set(0, 2, "a")
rm.Set(3, 4, "b")
rm.Set(5, 5, "c")
rm.Set(6, 10, "d")
rm.Set(3, 9, "x")
assert.Equal(t, "[{0..2: a}, {3..9: x}, {10..10: d}, {11..-1}]", rm.String())
}
func TestPerfectFit(t *testing.T) {
rm := rangemap.New[string]()
rm.Set(0, 2, "a")
rm.Set(5, 6, "b")
rm.Set(3, 4, "x")
assert.Equal(t, "[{0..2: a}, {3..4: x}, {5..6: b}, {7..-1}]", rm.String())
}
func TestSpaceBetween(t *testing.T) {
rm := rangemap.New[string]()
rm.Set(0, 2, "a")
rm.Set(9, 9, "b")
rm.Set(4, 6, "x")
// Since our search can never fail, we pad the space with a sentinel value.
assert.Equal(t, "[{0..2: a}, {3..3}, {4..6: x}, {7..8}, {9..9: b}, {10..-1}]", rm.String())
}
|