diff --git a/linkedhashmap.go b/linkedhashmap.go index 04a375a..e12131c 100644 --- a/linkedhashmap.go +++ b/linkedhashmap.go @@ -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 @@ -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), } diff --git a/linkedhashmap_test.go b/linkedhashmap_test.go new file mode 100644 index 0000000..fa0823a --- /dev/null +++ b/linkedhashmap_test.go @@ -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) + }) + }) + }) +} diff --git a/linkedhashsetint64.go b/linkedhashsetint64.go index f7ef4b6..677f63a 100644 --- a/linkedhashsetint64.go +++ b/linkedhashsetint64.go @@ -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 } @@ -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...) diff --git a/linkedhashsetint64_test.go b/linkedhashsetint64_test.go index 4e20239..9405260 100644 --- a/linkedhashsetint64_test.go +++ b/linkedhashsetint64_test.go @@ -1,18 +1,17 @@ -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) } } @@ -20,13 +19,13 @@ func init() { 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) }) @@ -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 @@ -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) @@ -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 @@ -79,7 +78,7 @@ func TestLinkedHashSetINT64Iter(t *testing.T) { i++ } So(somethingWentWrong, ShouldBeFalse) - So(i, ShouldEqual, giangSliceLength) + So(i, ShouldEqual, giantSliceLength) }) }) } @@ -87,7 +86,7 @@ func TestLinkedHashSetINT64Iter(t *testing.T) { 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) @@ -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)) }) @@ -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) @@ -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)) @@ -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)) diff --git a/linkedhashsetstring.go b/linkedhashsetstring.go index de6c55a..edbce8d 100644 --- a/linkedhashsetstring.go +++ b/linkedhashsetstring.go @@ -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 } @@ -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...) diff --git a/linkedhashsetstring_test.go b/linkedhashsetstring_test.go index 8e89fb2..72df621 100644 --- a/linkedhashsetstring_test.go +++ b/linkedhashsetstring_test.go @@ -1,17 +1,16 @@ -package set_test +package set import ( "strconv" "testing" - "github.com/StudioSol/set" . "github.com/smartystreets/goconvey/convey" ) -var giantStringSlice = make([]string, giangSliceLength) +var giantStringSlice = make([]string, giantSliceLength) func init() { - for i := 0; i < giangSliceLength; i++ { + for i := 0; i < giantSliceLength; i++ { giantStringSlice[i] = strconv.Itoa(i + 1) } } @@ -19,13 +18,13 @@ func init() { func TestLinkedHashSetStringAdd(t *testing.T) { Convey("Given LinkedHashSetString.Add", t, func() { Convey("It should not store elements that are already on the Set", func() { - set := set.NewLinkedHashSetString() + set := NewLinkedHashSetString() set.Add("0", "0") set.Add("0") So(set.Length(), ShouldEqual, 1) }) Convey("It should store elements with the correct constraints", func() { - set := set.NewLinkedHashSetString() + set := NewLinkedHashSetString() set.Add("0", "1", "2", "99", "93", "32", "00", "01", "2") So(set.Length(), ShouldEqual, 8) }) @@ -35,7 +34,7 @@ func TestLinkedHashSetStringAdd(t *testing.T) { func TestLinkedHashSetStringRemove(t *testing.T) { Convey("Given LinkedHashSetString.Remove", t, func() { Convey("It should remove elements from a Set", func() { - set := set.NewLinkedHashSetString() + set := NewLinkedHashSetString() set.Add(giantStringSlice...) // first element @@ -55,7 +54,7 @@ func TestLinkedHashSetStringRemove(t *testing.T) { func TestLinkedHashSetStringIter(t *testing.T) { Convey("Given LinkedHashSetString.Iter", t, func() { Convey("It should iterate over all elements of the set respecting the insertion order", func() { - set := set.NewLinkedHashSetString() + set := NewLinkedHashSetString() set.Add(giantStringSlice...) var ( @@ -70,7 +69,7 @@ func TestLinkedHashSetStringIter(t *testing.T) { i++ } So(somethingWentWrong, ShouldBeFalse) - So(i, ShouldEqual, giangSliceLength) + So(i, ShouldEqual, giantSliceLength) }) }) } @@ -78,7 +77,7 @@ func TestLinkedHashSetStringIter(t *testing.T) { func TestLinkedHashSetStringLength(t *testing.T) { Convey("Given LinkedHashSetString.Length", t, func() { Convey("It should return the correct length of the Set", func() { - set := set.NewLinkedHashSetString() + set := NewLinkedHashSetString() set.Add("0", "1", "2", "99", "93", "32", "00", "01", "2") So(set.Length(), ShouldEqual, 8) set.Remove("1") @@ -90,7 +89,7 @@ func TestLinkedHashSetStringLength(t *testing.T) { }) Convey("It should return the correct length of the Set no matter the length of the Set", func() { - set := set.NewLinkedHashSetString() + set := NewLinkedHashSetString() set.Add(giantStringSlice...) So(set.Length(), ShouldEqual, len(giantStringSlice)) }) @@ -100,21 +99,21 @@ func TestLinkedHashSetStringLength(t *testing.T) { func TestLinkedHashSetStringInArray(t *testing.T) { Convey("Given LinkedHashSetString.InArray", t, func() { Convey("When the element is in the list", func() { - set := set.NewLinkedHashSetString("02", "04", "06", "08") + set := NewLinkedHashSetString("02", "04", "06", "08") So(set.InArray("02"), ShouldBeTrue) So(set.InArray("04"), ShouldBeTrue) So(set.InArray("06"), ShouldBeTrue) So(set.InArray("08"), ShouldBeTrue) }) Convey("When the element is not in the list", func() { - set := set.NewLinkedHashSetString("02", "04", "06", "08") + set := NewLinkedHashSetString("02", "04", "06", "08") So(set.InArray("01"), ShouldBeFalse) So(set.InArray("03"), ShouldBeFalse) So(set.InArray("05"), ShouldBeFalse) So(set.InArray("07"), ShouldBeFalse) }) Convey("When the list is empty", func() { - set := set.NewLinkedHashSetString() + set := NewLinkedHashSetString() So(set.InArray("01"), ShouldBeFalse) So(set.InArray("03"), ShouldBeFalse) So(set.InArray("05"), ShouldBeFalse) @@ -127,7 +126,7 @@ func TestLinkedHashSetStringAsSlice(t *testing.T) { Convey("Given LinkedHashSetString.AsSlice", t, func() { Convey("It should return the correct slice", func() { expectedArray := []string{"0", "1", "2", "99", "93", "32", "00", "01"} - set := set.NewLinkedHashSetString(expectedArray...) + set := NewLinkedHashSetString(expectedArray...) array := set.AsSlice() So(array, ShouldHaveLength, len(expectedArray)) @@ -143,7 +142,7 @@ func TestLinkedHashSetStringAsInterface(t *testing.T) { Convey("Given LinkedHashSetString.AsInterface", t, func() { Convey("It should return the correct slice", func() { expectedArray := []string{"0", "1", "2", "99", "93", "32", "00", "01"} - set := set.NewLinkedHashSetString(expectedArray...) + set := NewLinkedHashSetString(expectedArray...) array := set.AsInterface() So(array, ShouldHaveLength, len(expectedArray))