Add reduction code
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package hmm
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCountNSort(t *testing.T) {
|
||||
in := []string{"b", "c", "c", "a", "b", "c"}
|
||||
|
||||
ch := make(chan string)
|
||||
go func() {
|
||||
for _, i := range in {
|
||||
ch <- i
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
got := CountNSort(ch)
|
||||
want := Results{
|
||||
{"a", 1},
|
||||
{"b", 2},
|
||||
{"c", 3},
|
||||
}
|
||||
|
||||
if !EqualResults(got, want) {
|
||||
t.Fatalf("unequal Results: got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func EqualResults(a, b Results) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 0; i < len(a); i++ {
|
||||
if a[i].Key != b[i].Key {
|
||||
return false
|
||||
}
|
||||
if a[i].Count != b[i].Count {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user