From 07016d1ea6dd1806148f5bc32c38b0daa43438da Mon Sep 17 00:00:00 2001 From: cpustejovsky Date: Sat, 26 Nov 2022 20:53:42 -0500 Subject: [PATCH] feat: add generic copySlice function for pure package along with reverseSort helper function; add tests that ensure the incoming slice is not mutated --- pure/pure.go | 55 ++++++++++++++++++++++++++ sort/sort_test.go => pure/pure_test.go | 22 +++++++---- sort/sort.go | 47 ---------------------- 3 files changed, 69 insertions(+), 55 deletions(-) create mode 100644 pure/pure.go rename sort/sort_test.go => pure/pure_test.go (71%) delete mode 100644 sort/sort.go diff --git a/pure/pure.go b/pure/pure.go new file mode 100644 index 0000000..23b0116 --- /dev/null +++ b/pure/pure.go @@ -0,0 +1,55 @@ +package pure + +import ( + "sort" +) + +type Item interface { + int | float64 | string +} + +func reserveSort(x sort.Interface) { + sort.Sort(sort.Reverse(x)) +} + +func copySlice[I Item](original []I) []I { + c := make([]I, len(original)) + copy(c, original) + return c +} + +func NewSortedIntegers(ints []int) []int { + n := copySlice(ints) + sort.IntSlice(n).Sort() + return n +} + +func NewSortedFloats(float64s []float64) []float64 { + n := copySlice(float64s) + sort.Float64Slice(n).Sort() + return n +} + +func NewSortedStrings(strings []string) []string { + s := copySlice(strings) + sort.StringSlice(s).Sort() + return append(s) +} + +func NewReverseSortedIntegers(integers []int) []int { + n := copySlice(integers) + reserveSort(sort.IntSlice(n)) + return n +} + +func NewReverseSortedFloats(float64s []float64) []float64 { + n := copySlice(float64s) + reserveSort(sort.Float64Slice(n)) + return n +} + +func NewReverseSortedStrings(strings []string) []string { + s := copySlice(strings) + reserveSort(sort.StringSlice(s)) + return s +} diff --git a/sort/sort_test.go b/pure/pure_test.go similarity index 71% rename from sort/sort_test.go rename to pure/pure_test.go index 7df8928..d17832a 100644 --- a/sort/sort_test.go +++ b/pure/pure_test.go @@ -1,7 +1,7 @@ -package sort_test +package pure_test import ( - csgo "github.com/cpustejovsky/customsortgo/sort" + "github.com/cpustejovsky/customsortgo/pure" "github.com/stretchr/testify/assert" "testing" ) @@ -9,41 +9,47 @@ import ( func TestSortForStrings(t *testing.T) { w := []string{"cat", "albatross", "dolphin", "bee", "zebra", "aardvark"} want := []string{"aardvark", "albatross", "bee", "cat", "dolphin", "zebra"} - got := csgo.NewSortedStrings(w) + got := pure.NewSortedStrings(w) assert.Equal(t, want, got) + assert.NotEqual(t, got, w) } func TestReverseSortForStrings(t *testing.T) { w := []string{"cat", "albatross", "dolphin", "bee", "zebra", "aardvark"} want := []string{"zebra", "dolphin", "cat", "bee", "albatross", "aardvark"} - got := csgo.NewReverseSortedStrings(w) + got := pure.NewReverseSortedStrings(w) assert.Equal(t, want, got) + assert.NotEqual(t, got, w) } func TestSortInts(t *testing.T) { n := []int{1, 3, 5, 2, 7, 6, 4} want := []int{1, 2, 3, 4, 5, 6, 7} - got := csgo.NewSortedIntegers(n) + got := pure.NewSortedIntegers(n) assert.Equal(t, want, got) + assert.NotEqual(t, got, n) } func TestReverseSortInts(t *testing.T) { n := []int{1, 3, 5, 2, 7, 6, 4} want := []int{7, 6, 5, 4, 3, 2, 1} - got := csgo.NewReverseSortedIntegers(n) + got := pure.NewReverseSortedIntegers(n) assert.Equal(t, want, got) + assert.NotEqual(t, got, n) } func TestSortFloat64s(t *testing.T) { n := []float64{1.1, 3.3, 5.5, 2.2, 7.7, 6.6, 4.4} want := []float64{1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7} - got := csgo.NewSortedFloats(n) + got := pure.NewSortedFloats(n) assert.Equal(t, want, got) + assert.NotEqual(t, got, n) } func TestReverseSortFloat64s(t *testing.T) { n := []float64{1.1, 3.3, 5.5, 2.2, 7.7, 6.6, 4.4} want := []float64{7.7, 6.6, 5.5, 4.4, 3.3, 2.2, 1.1} - got := csgo.NewReverseSortedFloats(n) + got := pure.NewReverseSortedFloats(n) assert.Equal(t, want, got) + assert.NotEqual(t, got, n) } diff --git a/sort/sort.go b/sort/sort.go deleted file mode 100644 index 697366d..0000000 --- a/sort/sort.go +++ /dev/null @@ -1,47 +0,0 @@ -package sort - -import ( - "sort" -) - -func NewSortedIntegers(nums []int) []int { - n := make([]int, len(nums)) - copy(n, nums) - sort.IntSlice(n).Sort() - return n -} - -func NewSortedFloats(nums []float64) []float64 { - n := make([]float64, len(nums)) - copy(n, nums) - sort.Float64Slice(n).Sort() - return n -} - -func NewSortedStrings(strs []string) []string { - s := make([]string, len(strs)) - copy(s, strs) - sort.StringSlice(s).Sort() - return s -} - -func NewReverseSortedIntegers(nums []int) []int { - n := make([]int, len(nums)) - copy(n, nums) - sort.Sort(sort.Reverse(sort.IntSlice(n))) - return n -} - -func NewReverseSortedFloats(nums []float64) []float64 { - n := make([]float64, len(nums)) - copy(n, nums) - sort.Sort(sort.Reverse(sort.Float64Slice(n))) - return n -} - -func NewReverseSortedStrings(strs []string) []string { - s := make([]string, len(strs)) - copy(s, strs) - sort.Sort(sort.Reverse(sort.StringSlice(s))) - return s -}