Skip to content

Commit

Permalink
feat: add generic copySlice function for pure package along with reve…
Browse files Browse the repository at this point in the history
…rseSort helper function;

add tests that ensure the incoming slice is not mutated
  • Loading branch information
cpustejovsky committed Nov 27, 2022
1 parent d029503 commit 07016d1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 55 deletions.
55 changes: 55 additions & 0 deletions pure/pure.go
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
}
22 changes: 14 additions & 8 deletions sort/sort_test.go → pure/pure_test.go
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)
}
47 changes: 0 additions & 47 deletions sort/sort.go

This file was deleted.

0 comments on commit 07016d1

Please sign in to comment.