Skip to content

Commit

Permalink
Update Go versions we test against, update dependencies. (#5)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Hopkins <ahopkins@ziprecruiter.com>
  • Loading branch information
aaron42net and Aaron Hopkins committed Jun 28, 2022
1 parent cd36c9c commit 52a2bac
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
go: ['1.13', '1.14', '1.15', '1.16']
go: ['1.15', '1.16', '1.17', '1.18']

steps:
- uses: actions/setup-go@v2
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/go-test.yml~
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: go-test
on:
push:
pull_request:
schedule:
- cron: '55 4 20 * *'

jobs:

test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
go: ['1.14', '1.15', '1.16', '1.17']

steps:
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v2
- run: go test -v -short -race -coverprofile=profile.cov ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: profile.cov
flag-name: Go-${{ matrix.go }}
parallel: true

# notifies that all test jobs are finished.
finish:
needs: test
runs-on: ubuntu-latest
steps:
- uses: shogo82148/actions-goveralls@v1
with:
parallel-finished: true
10 changes: 2 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module github.com/die-net/intervals

go 1.17
go 1.14

require github.com/stretchr/testify v1.7.0

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
require github.com/stretchr/testify v1.7.5
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 29 additions & 2 deletions intervals.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Package intervals provides an insertion-sort based solution to the merge
// overlapping intervals problem.
// Package intervals provides a fast insertion-sort based solution to the
// merge overlapping intervals problem.
package intervals

import (
"sort"
)

// Interval represents range of the form [Start, End), which contains all
// integer x in Start <= x < End.
type Interval struct {
Start int64
End int64
}

// Empty returns true if Start is less than end, false if they are equal,
// and panics if Start is greater than End.
func (v Interval) Empty() bool {
if v.Start < v.End {
return false
Expand All @@ -23,8 +27,31 @@ func (v Interval) Empty() bool {
return true
}

// Intervals is an ordered representation of a slice of Interval.
type Intervals []Interval

// Search will return the Interval in the given Intervals containing the
// value of off and true if found. Otherwise, it will return an empty
// Interval and false.
func (ivs Intervals) Search(off int64) (Interval, bool) {
// Skip search if we definitely don't contain offset.
if len(ivs) == 0 || ivs[0].Start > off || ivs[len(ivs)-1].End < off {
return Interval{}, false
}

// Find the first interval that matches, whose end is at least off.
i := sort.Search(len(ivs), func(i int) bool { return ivs[i].End >= off })
if i < len(ivs) && ivs[i].Start <= off {
return ivs[i], true
}

return Interval{}, false
}

// Insert adds a given Interval to Intervals, possibly inserting in the
// correct order between two intervals, extending an existing interval, or
// compacting existing intervals as necessary. ivs must be correctly
// ordered. An empty v will result in a noop, and an invalid v may panic.
func (ivs Intervals) Insert(v Interval) Intervals {
if v.Empty() {
return ivs
Expand Down
30 changes: 27 additions & 3 deletions intervals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,31 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

func TestExpected(t *testing.T) {
func TestSearchExpected(t *testing.T) {
tests := []struct {
intervals Intervals
offset int64
expected Interval
ok bool
}{
{intervals: Intervals{{0, 2}, {4, 5}}, offset: -1, expected: Interval{0, 0}, ok: false},
{intervals: Intervals{{0, 2}, {4, 5}}, offset: 0, expected: Interval{0, 2}, ok: true},
{intervals: Intervals{{0, 2}, {4, 5}}, offset: 1, expected: Interval{0, 2}, ok: true},
{intervals: Intervals{{0, 2}, {4, 5}}, offset: 2, expected: Interval{0, 2}, ok: true},
{intervals: Intervals{{0, 2}, {4, 5}}, offset: 3, expected: Interval{0, 0}, ok: false},
{intervals: Intervals{{0, 2}, {4, 5}}, offset: 4, expected: Interval{4, 5}, ok: true},
{intervals: Intervals{{0, 2}, {4, 5}}, offset: 5, expected: Interval{4, 5}, ok: true},
{intervals: Intervals{{0, 2}, {4, 5}}, offset: 6, expected: Interval{0, 0}, ok: false},
}

for _, test := range tests {
v, ok := test.intervals.Search(test.offset)
assert.Equal(t, test.expected, v)
assert.Equal(t, test.ok, ok)
}
}

func TestInsertExpected(t *testing.T) {
tests := []struct {
inserts Intervals
expected Intervals
Expand Down Expand Up @@ -43,7 +67,7 @@ func TestExpected(t *testing.T) {
}
}

func TestRandom(t *testing.T) {
func TestInsertRandom(t *testing.T) {
for n := 0; n < 1000; n++ {
vs := Intervals{}
count := rand.Intn(10) + 1
Expand Down Expand Up @@ -86,7 +110,7 @@ func TestRandom(t *testing.T) {
}
}

func TestPanic(t *testing.T) {
func TestInsertPanic(t *testing.T) {
// End before start should panic.
assert.Panics(t, func() { Intervals{}.Insert(Interval{2, 1}) })
}
Expand Down

0 comments on commit 52a2bac

Please sign in to comment.