-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add generic copySlice function for pure package along with reve…
…rseSort helper function; add tests that ensure the incoming slice is not mutated
- Loading branch information
1 parent
d029503
commit 07016d1
Showing
3 changed files
with
69 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,55 @@ | ||
package sort_test | ||
package pure_test | ||
|
||
import ( | ||
csgo "github.com/cpustejovsky/customsortgo/sort" | ||
"github.com/cpustejovsky/customsortgo/pure" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
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) | ||
} |
This file was deleted.
Oops, something went wrong.