Skip to content

Instantly share code, notes, and snippets.

@juusechec
Created March 6, 2021 19:26
Show Gist options
  • Save juusechec/e9aee02d87b617c0d6d7b1b53cd9dc30 to your computer and use it in GitHub Desktop.
Save juusechec/e9aee02d87b617c0d6d7b1b53cd9dc30 to your computer and use it in GitHub Desktop.
Go Testing Example
// filename sort_test.go
package main
import (
"reflect"
"sort"
"testing"
)
func SortAscending(items []int) []int {
sort.Slice(items, func(i, j int) bool {
return items[i] < items[j]
})
return items
}
func TestLastIndex(t *testing.T) {
tests := []struct {
list []int
want []int
}{
{list: []int{1}, want: []int{1}},
{list: []int{1, 1}, want: []int{1, 1}},
{list: []int{2, 1}, want: []int{1, 2}},
{list: []int{1, 2, 1, 1}, want: []int{1, 1, 1, 2}},
{list: []int{1, 1, 1, 2, 2, 1}, want: []int{1, 1, 1, 1, 2, 2}},
{list: []int{3, 1, 2, 2, 1, 1}, want: []int{1, 1, 1, 2, 2, 3}},
{list: []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, want: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
{list: []int{9999, 999, 9}, want: []int{9, 999, 9999}},
}
for _, tt := range tests {
if got := SortAscending(tt.list); !reflect.DeepEqual(got, tt.want) {
t.Errorf("LastIndex(%v) = %v, want %v", tt.list, got, tt.want)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment