Skip to content

Instantly share code, notes, and snippets.

@juusechec
Created March 6, 2021 19:26
Show Gist options
  • Select an option

  • Save juusechec/e9aee02d87b617c0d6d7b1b53cd9dc30 to your computer and use it in GitHub Desktop.

Select an option

Save juusechec/e9aee02d87b617c0d6d7b1b53cd9dc30 to your computer and use it in GitHub Desktop.

Revisions

  1. juusechec created this gist Mar 6, 2021.
    36 changes: 36 additions & 0 deletions sort_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    // 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)
    }
    }
    }