Skip to content

Commit

Permalink
Stable sort
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Aug 3, 2020
1 parent 13d93f3 commit 677d37c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ go:
before_install:
- go get github.com/mattn/goveralls
- go get github.com/schrej/godacov
- go get golang.org/x/lint/golint
- go get github.com/golangci/golangci-lint/cmd/golangci-lint
- go get github.com/mfridman/tparse

script:
- ./run-tests.sh -ic ./...
- $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci; true
- $GOPATH/bin/godacov -t $CODACY_TOKEN -r ./coverage.out -c $TRAVIS_COMMIT; true
# - $GOPATH/bin/goveralls -coverprofile=coverage.out -service=travis-ci; true
# - $GOPATH/bin/godacov -t $CODACY_TOKEN -r ./coverage.out -c $TRAVIS_COMMIT; true

after_success:
- bash <(curl -s https://codecov.io/bash); true
# after_success:
# - bash <(curl -s https://codecov.io/bash); true
14 changes: 11 additions & 3 deletions feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,10 @@ func (fb *Feedback) Sort(query string, opts ...fuzzy.Option) []*fuzzy.Result {
// It returns a slice of Result structs, which contain the results of the
// fuzzy sorting.
func (fb *Feedback) Filter(query string, opts ...fuzzy.Option) []*fuzzy.Result {
var items []*Item
var res []*fuzzy.Result
var (
items []*Item
res []*fuzzy.Result
)

r := fb.Sort(query, opts...)
for i, it := range fb.Items {
Expand Down Expand Up @@ -524,7 +526,13 @@ func (fb *Feedback) Keywords(i int) string {
func (fb *Feedback) Len() int { return len(fb.Items) }

// Less implements sort.Interface.
func (fb *Feedback) Less(i, j int) bool { return fb.Keywords(i) < fb.Keywords(j) }
func (fb *Feedback) Less(i, j int) bool {
a, b := fb.Keywords(i), fb.Keywords(j)
if a == b {
return i < j
}
return a < b
}

// Swap implements sort.Interface.
func (fb *Feedback) Swap(i, j int) { fb.Items[i], fb.Items[j] = fb.Items[j], fb.Items[i] }
Expand Down
9 changes: 8 additions & 1 deletion feedback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func TestFeedback_Sort(t *testing.T) {
r := fb.Sort(td.q)
for i, it := range fb.Items {
assert.Equal(t, td.out[i], it.title, "unexpected title")
assert.Equal(t, td.m[i], r[i].Match, "unexpected title")
assert.Equal(t, td.m[i], r[i].Match, "unexpected match")
}
}
}
Expand Down Expand Up @@ -558,6 +558,13 @@ var feedbackTitles = []struct {
out: []string{"Safari", "see all fellows' armpits", "spanish harlem", "french canada"},
m: []bool{true, true, false, false},
},
// sorting is stable
{
q: "test",
in: []string{"test #1", "test #10", "test #2", "test #3"},
out: []string{"test #1", "test #2", "test #3", "test #10"},
m: []bool{true, true, true, true},
},
}

var filterTitles = []struct {
Expand Down
8 changes: 3 additions & 5 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ install() {
local name=${p:t}
installed "$name" || {
log "installing $name ..."
GO111MODULE=off go get -u $gopts $p
GO111MODULE=off go get -u $vopt $p
[[ $? -eq 0 ]] || fail "install $name failed"
success "installed $name"
}
Expand Down Expand Up @@ -94,6 +94,7 @@ while getopts ":CcghilrtvV" opt; do
case $opt in
c)
cover=true
gopts+=(-coverprofile="$covfile")
;;
g)
usegocov=true
Expand All @@ -118,7 +119,6 @@ while getopts ":CcghilrtvV" opt; do
colour=true
;;
V)
gopts+=(-v)
verbose=true
vopt='-v'
;;
Expand Down Expand Up @@ -155,8 +155,6 @@ $runlint && {
exit 0
}

$cover && gopts+=(-coverprofile="$covfile")

command mkdir $vopt -p "${testdir}"/{data,cache}
$mkip touch $vopt "$iplist"
trap "test -f \"$iplist\" && rm -f \"$iplist\"; test -d \"$testdir\" && rm -rf \"$testdir\";" EXIT INT TERM
Expand All @@ -173,7 +171,7 @@ pkgs=(./...)
st=0
$runtests && {
install github.com/mfridman/tparse
go test -cover -json $gopts $pkgs | tparse
go test -cover -json $vopt $gopts $pkgs | tparse
# gotestsum -- $gopts $pkgs
st=$?
[[ $st -eq 0 ]] && success "unit tests passed"
Expand Down

0 comments on commit 677d37c

Please sign in to comment.