Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
name: Go

name: tests
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Setup
uses: actions/setup-go@v2
with:
go-version: 1.15

- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 1

- name: Dependencies
run: go mod download

- name: Lint
run: |
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh -s v1.31.0
./bin/golangci-lint run

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
id: go
- name: Test
run: go test -race -cover -coverprofile=coverage.txt -covermode=atomic ./...

- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Coverage
uses: codecov/codecov-action@v1.0.13
with:
file: ./coverage.txt
flags: unittests
name: codecov-umbrella
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/StudioSol/set

go 1.12

require github.com/smartystreets/goconvey v1.6.4
require github.com/stretchr/testify v1.6.1
25 changes: 12 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
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/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 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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=
3 changes: 2 additions & 1 deletion linkedhashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package set
import (
"fmt"
"hash/fnv"
"log"
)

type entry struct {
Expand Down Expand Up @@ -147,7 +148,7 @@ func (l *linkedHashMap) hash(key interface{}) uint64 {
h := fnv.New64()
_, err := h.Write([]byte(fmt.Sprintf("%#v", key)))
if err != nil {
// noop
log.Print(err)
}
return h.Sum64()
}
Expand Down
116 changes: 56 additions & 60 deletions linkedhashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,82 @@ import (
"math/rand"
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/require"
)

func TestGet(t *testing.T) {
Convey("Given LinkedHashSet.Get", t, func() {
Convey("When the key exists", func() {
set := newLinkedHashMap()
value := rand.Int()
set.Put("test", value)
Convey("It should return the expected value", func() {
So(set.Get("test"), ShouldEqual, value)
})
})
t.Run("When the key exists", func(t *testing.T) {
set := newLinkedHashMap()
value := rand.Int()
set.Put("test", value)
require.Equal(t, value, set.Get("test"))
})

Convey("When the key not exists", func() {
set := newLinkedHashMap()
Convey("It should return an empty value", func() {
result := set.Get("bla")
So(result, ShouldEqual, nil)
})
})
t.Run("When the key not exists", func(t *testing.T) {
set := newLinkedHashMap()
result := set.Get("bla")
require.Nil(t, result)
})
}


func TestPut(t *testing.T) {
Convey("Given LinkedHashSet.Put", t, func() {
Convey("When an invalid key is given", func() {
set := newLinkedHashMap()
t.Run("valid key", func(t *testing.T) {
set := newLinkedHashMap()
value := rand.Int()
set.Put("test", value)
require.Equal(t, value, set.Get("test"))
})

value := rand.Int()
set.Put(nil, value)
t.Run("invalid key", func(t *testing.T) {
set := newLinkedHashMap()

value := rand.Int()
set.Put(nil, value)

So(set.Length(), ShouldEqual, 0)
})
require.Equal(t, 0, set.Length())
})
}

func TestRemove(t *testing.T) {
Convey("Given a valid list of numbers", t, func() {
testNumbers := []int{1,2,3}
testNumbers := []int{1, 2, 3}
t.Run("first value", func(t *testing.T) {
set := newLinkedHashMap()
for _, number := range testNumbers {
set.Put(number, number)
}

set.Remove(1)
require.Equal(t, 2, set.Length())
require.Nil(t, set.Get(1))
})

t.Run("last value", func(t *testing.T) {
set := newLinkedHashMap()
for _, number := range testNumbers {
set.Put(number, number)
}

set.Remove(3)
require.Equal(t, 2, set.Length())
require.Nil(t, set.Get(3))
})

t.Run("middle value", func(t *testing.T) {
set := newLinkedHashMap()
for _, number := range testNumbers {
set.Put(number, number)
}
Convey("When first value is removed", func() {
setCopy := *set
setCopy.Remove(1)
Convey("It should remove the correct value", func() {
So(setCopy.Length(), ShouldEqual, 2)
So(setCopy.Get(1), ShouldBeNil)
})
})
Convey("When last value is removed", func() {
setCopy := *set
setCopy.Remove(3)
Convey("It should remove the correct value", func() {
So(setCopy.Length(), ShouldEqual, 2)
So(setCopy.Get(3), ShouldBeNil)
})
})
Convey("When a middle value is removed", func() {
setCopy := *set
setCopy.Remove(2)
Convey("It should remove the correct value", func() {
So(setCopy.Length(), ShouldEqual, 2)
So(setCopy.Get(2), ShouldBeNil)
})
})

set.Remove(2)
require.Equal(t, 2, set.Length())
require.Nil(t, set.Get(2))
})
Convey("Given a list with single value", t, func() {

t.Run("single value", func(t *testing.T) {
set := newLinkedHashMap()
set.Put(1, 1)
Convey("When the values is removed", func() {
setCopy := *set
setCopy.Remove(1)
Convey("It should remove the correct value", func() {
So(setCopy.Length(), ShouldEqual, 0)
So(setCopy.Get(1), ShouldBeNil)
})
})
set.Remove(1)
require.Equal(t, 0, set.Length())
require.Nil(t, set.Get(1))
})
}
Loading