Skip to content

Commit

Permalink
fix missing-initialization bug
Browse files Browse the repository at this point in the history
addresses #20

Signed-off-by: Tim Bray <tbray@textuality.com>
  • Loading branch information
timbray committed Apr 23, 2024
1 parent 1ba6119 commit 0019c49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/keyfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (kf *keyFinder) getKey(record []byte) ([]byte, error) {
field++
}
} else {
kf.key = kf.key[:0]
allFields := kf.separator.Split(string(record), -1)
for i, field := range kf.fields {
if int(field) >= len(allFields) {
Expand Down
31 changes: 31 additions & 0 deletions internal/keyfinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package topfew

import (
"bytes"
"os"
"testing"
)

Expand Down Expand Up @@ -35,6 +36,36 @@ func TestFieldSeparator(t *testing.T) {
}
}

func TestCSVSeparator(t *testing.T) {
args := []string{"-p", ",", "-f", "11"}
c, err := Configure(args)
if err != nil {
t.Error("Config!")
}
input, err := os.Open("../test/data/csoc.csv")
if err != nil {
t.Error("Open: " + err.Error())
}
counts, err := Run(c, input)
if err != nil {
t.Error("Run: " + err.Error())
}
if len(counts) != 5 {
t.Errorf("Got %d results, wanted 5", len(counts))
}
wantCounts := []uint64{4, 2, 1, 1, 1}
wantKeys := []string{"50", "-1.97", "amount", "-1.75", "-1.9"}
for i, count := range counts {
if *count.Count != wantCounts[i] {
t.Errorf("Counts[%d] is %d wanted %d", i, *count.Count, wantCounts[i])
}
// because for equal values, the sort isn't stable - Counts[2,3,4] are all 1
if i < 2 && count.Key != wantKeys[i] {
t.Errorf("Keys[%d] is %s wanted %s", i, count.Key, wantKeys[i])
}
}
}

func TestKeyFinder(t *testing.T) {
var records = []string{
"a x c",
Expand Down

0 comments on commit 0019c49

Please sign in to comment.