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
8 changes: 8 additions & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Contributors

- [Bruno Souza](https://github.com/brunomvsouza)
- Project design and first version
- [Guilherme Hübner ](https://github.com/guilhermehubner)
- Code improvements
- [Rodrigo Brito](https://github.com/rodrigo-brito)
- Test coverage improvements
6 changes: 3 additions & 3 deletions linkedhashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type entry struct {
after *entry
}

// linkedHashMap stores data in key-value pairs while mantaining insertion order
// linkedHashMap stores data in key-value pairs while maintaining insertion order
//
// - Uses a doubly linked list to mantain insertion order
// - Uses a doubly linked list to maintain insertion order
type linkedHashMap struct {
table map[uint64]*entry
header *entry
Expand Down Expand Up @@ -152,7 +152,7 @@ func (l *linkedHashMap) hash(key interface{}) uint64 {
return h.Sum64()
}

func newlinkedHashMap() *linkedHashMap {
func newLinkedHashMap() *linkedHashMap {
return &linkedHashMap{
table: make(map[uint64]*entry),
}
Expand Down
89 changes: 89 additions & 0 deletions linkedhashmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package set

import (
"math/rand"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

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)
})
})

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)
})
})
})
}


func TestPut(t *testing.T) {
Convey("Given LinkedHashSet.Put", t, func() {
Convey("When an invalid key is given", func() {
set := newLinkedHashMap()

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

So(set.Length(), ShouldEqual, 0)
})
})
}

func TestRemove(t *testing.T) {
Convey("Given a valid list of numbers", t, func() {
testNumbers := []int{1,2,3}
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)
})
})
})
Convey("Given a list with single value", t, func() {
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)
})
})
})
}
4 changes: 2 additions & 2 deletions linkedhashsetint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package set
//
// - Does not allow storing duplicated values
// - Does not allow storing nil values
// - Mantains insertion order over iteration
// - Maintains insertion order over iteration
type LinkedHashSetINT64 struct {
linkedHashMap *linkedHashMap
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (l *LinkedHashSetINT64) InArray(search int64) bool {
// NewLinkedHashSetINT64 returns a new LinkedHashSetINT64 with the provided items
func NewLinkedHashSetINT64(ints ...int64) *LinkedHashSetINT64 {
lhm := &LinkedHashSetINT64{
linkedHashMap: newlinkedHashMap(),
linkedHashMap: newLinkedHashMap(),
}
if len(ints) > 0 {
lhm.Add(ints...)
Expand Down
35 changes: 17 additions & 18 deletions linkedhashsetint64_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package set_test
package set

import (
"testing"

"github.com/StudioSol/set"
. "github.com/smartystreets/goconvey/convey"
)

const giangSliceLength = 100000
const giantSliceLength = 100000

var giantINT64Slice = make([]int64, giangSliceLength)
var giantINT64Slice = make([]int64, giantSliceLength)

func init() {
for i := 0; i < giangSliceLength; i++ {
for i := 0; i < giantSliceLength; i++ {
giantINT64Slice[i] = int64(i + 1)
}
}

func TestLinkedHashSetINT64Add(t *testing.T) {
Convey("Given LinkedHashSetINT64.Add", t, func() {
Convey("It should not store elements that are already on the Set", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
set.Add(0, 0)
set.Add(0)
So(set.Length(), ShouldEqual, 1)
})
Convey("It should store elements with the correct constraints", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
set.Add(0, 1, 2, 99, 93, 32, 00, 01, 2)
So(set.Length(), ShouldEqual, 6)
})
Expand All @@ -36,7 +35,7 @@ func TestLinkedHashSetINT64Add(t *testing.T) {
func TestLinkedHashSetINT64Remove(t *testing.T) {
Convey("Given LinkedHashSetINT64.Remove", t, func() {
Convey("When a big list is given", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
set.Add(giantINT64Slice...)
Convey("It should remove elements from a Set", func() {
// first element
Expand All @@ -52,7 +51,7 @@ func TestLinkedHashSetINT64Remove(t *testing.T) {
})
})
Convey("When list with one item is given", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
set.Add(1)
Convey("It should remove the element from the set", func() {
set.Remove(1)
Expand All @@ -65,7 +64,7 @@ func TestLinkedHashSetINT64Remove(t *testing.T) {
func TestLinkedHashSetINT64Iter(t *testing.T) {
Convey("Given LinkedHashSetINT64.Iter", t, func() {
Convey("It should iterate over all elements of the set respecting the insertion order", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
set.Add(giantINT64Slice...)
var (
i int
Expand All @@ -79,15 +78,15 @@ func TestLinkedHashSetINT64Iter(t *testing.T) {
i++
}
So(somethingWentWrong, ShouldBeFalse)
So(i, ShouldEqual, giangSliceLength)
So(i, ShouldEqual, giantSliceLength)
})
})
}

func TestLinkedHashSetINT64Length(t *testing.T) {
Convey("Given LinkedHashSetINT64.Length", t, func() {
Convey("It should return the correct length of the Set", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
set.Add(0, 1, 2, 99, 93, 32, 00, 01, 2)
So(set.Length(), ShouldEqual, 6)
set.Remove(1)
Expand All @@ -99,7 +98,7 @@ func TestLinkedHashSetINT64Length(t *testing.T) {
})

Convey("It should return the correct length of the Set no matter the length of the Set", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
set.Add(giantINT64Slice...)
So(set.Length(), ShouldEqual, len(giantINT64Slice))
})
Expand All @@ -109,21 +108,21 @@ func TestLinkedHashSetINT64Length(t *testing.T) {
func TestInArray(t *testing.T) {
Convey("Given LinkedHashSetINT64.InArray", t, func() {
Convey("When the element is in the list", func() {
set := set.NewLinkedHashSetINT64(2, 4, 6, 8)
set := NewLinkedHashSetINT64(2, 4, 6, 8)
So(set.InArray(2), ShouldBeTrue)
So(set.InArray(4), ShouldBeTrue)
So(set.InArray(6), ShouldBeTrue)
So(set.InArray(8), ShouldBeTrue)
})
Convey("When the element is not in the list", func() {
set := set.NewLinkedHashSetINT64(2, 4, 6, 8)
set := NewLinkedHashSetINT64(2, 4, 6, 8)
So(set.InArray(1), ShouldBeFalse)
So(set.InArray(3), ShouldBeFalse)
So(set.InArray(5), ShouldBeFalse)
So(set.InArray(7), ShouldBeFalse)
})
Convey("When the list is empty", func() {
set := set.NewLinkedHashSetINT64()
set := NewLinkedHashSetINT64()
So(set.InArray(1), ShouldBeFalse)
So(set.InArray(3), ShouldBeFalse)
So(set.InArray(5), ShouldBeFalse)
Expand All @@ -136,7 +135,7 @@ func TestAsSlice(t *testing.T) {
Convey("Given LinkedHashSetINT64.AsSlice", t, func() {
Convey("It should return the correct slice", func() {
expectedArray := []int64{2, 4, 6, 8}
set := set.NewLinkedHashSetINT64(expectedArray...)
set := NewLinkedHashSetINT64(expectedArray...)

array := set.AsSlice()
So(array, ShouldHaveLength, len(expectedArray))
Expand All @@ -152,7 +151,7 @@ func TestAsInterface(t *testing.T) {
Convey("Given LinkedHashSetINT64.AsInterface", t, func() {
Convey("It should return the correct slice", func() {
expectedArray := []int64{2, 4, 6, 8}
set := set.NewLinkedHashSetINT64(expectedArray...)
set := NewLinkedHashSetINT64(expectedArray...)

array := set.AsInterface()
So(array, ShouldHaveLength, len(expectedArray))
Expand Down
4 changes: 2 additions & 2 deletions linkedhashsetstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package set
//
// - Does not allow storing duplicated values
// - Does not allow storing nil values
// - Mantains insertion order over iteration
// - Maintains insertion order over iteration
type LinkedHashSetString struct {
linkedHashMap *linkedHashMap
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (l *LinkedHashSetString) InArray(search string) bool {
// NewLinkedHashSetString returns a new LinkedHashSetString with the provided items
func NewLinkedHashSetString(strings ...string) *LinkedHashSetString {
lhm := &LinkedHashSetString{
linkedHashMap: newlinkedHashMap(),
linkedHashMap: newLinkedHashMap(),
}
if len(strings) > 0 {
lhm.Add(strings...)
Expand Down
Loading