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()) } func TestAppendRanges(t *testing.T) { rm1 := rangemap.New[string]() rm1.Set(0, 2, "a") rm1.Set(3, 5, "b") rm2 := rangemap.New[string]() rm2.Set(0, 2, "c") rm2.Set(3, 3, "d") assert.Equal(t, "[{0..2: a}, {3..5: b}, {6..-1}]", rm1.String()) assert.Equal(t, "[{0..2: c}, {3..3: d}, {4..-1}]", rm2.String()) rm1.AppendRanges(rm2) assert.Equal(t, "[{0..2: a}, {3..5: b}, {6..8: c}, {9..9: d}, {10..-1}]", rm1.String()) } func TestAppendRangesSpaceBetween(t *testing.T) { rm1 := rangemap.New[string]() rm1.Set(1, 2, "a") rm1.Set(4, 4, "b") rm2 := rangemap.New[string]() rm2.Set(2, 4, "c") rm2.Set(6, 6, "d") rm1.AppendRanges(rm2) assert.Equal(t, "[{0..0}, {1..2: a}, {3..3}, {4..4: b}, {5..6}, {7..9: c}, {10..10}, {11..11: d}, {12..-1}]", rm1.String()) } func TestFill(t *testing.T) { rm := rangemap.New[string]() rm.Set(0, 1, "a") rm.Set(3, 5, "b") rm.Set(9, 10, "c") rm.Fill(2, 10, "x") assert.Equal(t, "[{0..1: a}, {2..2: x}, {3..5: b}, {6..8: x}, {9..10: c}, {11..-1}]", rm.String()) } func TestFillFromMiddle(t *testing.T) { rm := rangemap.New[string]() rm.Set(0, 2, "a") rm.Set(9, 10, "c") rm.Fill(5, 6, "x") assert.Equal(t, "[{0..2: a}, {3..4}, {5..6: x}, {7..8}, {9..10: c}, {11..-1}]", rm.String()) } func TestFillEdge(t *testing.T) { rm := rangemap.New[string]() rm.Set(0, 2, "a") rm.Set(9, 10, "c") rm.Fill(2, 6, "x") assert.Equal(t, "[{0..2: a}, {3..6: x}, {7..8}, {9..10: c}, {11..-1}]", rm.String()) } func TestFillFromEmpty(t *testing.T) { rm := rangemap.New[string]() rm.Fill(2, 4, "x") rm.Fill(8, 10, "x") assert.Equal(t, "[{0..1}, {2..4: x}, {5..7}, {8..10: x}, {11..-1}]", rm.String()) }