Skip to content

Commit

Permalink
Merge pull request #31 from tamada/supports_input_types
Browse files Browse the repository at this point in the history
update unit tests
  • Loading branch information
tamada committed Jun 14, 2021
2 parents 51308b0 + ac578db commit 07d6704
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
10 changes: 4 additions & 6 deletions vector/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ func NewTermVectorFromReader(reader io.Reader, source Source) (*Vector, error) {
line, err := bufReader.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
}
if err != nil {
return nil, err
}
putTerms(line, values)
Expand All @@ -215,18 +216,15 @@ func NewTermVectorFromReader(reader io.Reader, source Source) (*Vector, error) {
func putData(values map[string]int, data []byte, length int) {
for i := 0; i < length; i++ {
key := string(data[i])
value, ok := values[key]
if !ok {
value = 0
}
value := values[key]
values[key] = (value + 1)
}
}

func NewByteVectorFromReader(reader io.Reader, source Source) (*Vector, error) {
values := map[string]int{}
for {
data := []byte{}
data := make([]byte, 1024)
n, err := reader.Read(data)
if err == io.EOF {
break
Expand Down
33 changes: 33 additions & 0 deletions vector/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,39 @@ func TestConstructVector(t *testing.T) {
}
}

func TestVectorFromByteFile(t *testing.T) {
vector, _ := NewByteVectorFromFile("../testdata/humpty_dumpty.txt")
hd := map[string]int{" ": 22, "\n": 4, "H": 3, "u": 7, "m": 6, "t": 13, "y": 5, "D": 2, "s": 5, "a": 11, "o": 4, "n": 7, "w": 1, "l": 9, ",": 1, "h": 5, "d": 3, "g": 5, "r": 3, "e": 7, "f": 1, "A": 1, "k": 2, "'": 3, "i": 3, "C": 1}
for term, count := range hd {
gotCount := vector.values[term]
if gotCount != count {
t.Errorf("count of character \"%s\" did not match, wont %d, got %d", term, count, gotCount)
}
}
}

func TestVectorFromTermFile(t *testing.T) {
vector, _ := NewTermVectorFromFile("../testdata/humpty_dumpty.txt")
hd := map[string]int{"humpty": 3, "dumpty": 2, "sat": 1, "on": 1, "a": 2, "wall": 1, "had": 1, "great": 1, "fall": 1, "all": 2, "the": 2, "king's": 2, "horses": 1, "and": 1, "men": 1, "couldn't": 1, "put": 1, "together": 1, "again": 1}
for term, count := range hd {
gotCount := vector.values[term]
if gotCount != count {
t.Errorf("count of term \"%s\" did not match, wont %d, got %d", term, count, gotCount)
}
}
}

func TestVectorFromJsonFile(t *testing.T) {
vector, _ := NewVectorFromJsonFile("../testdata/phenomenon.json")
hd := map[string]int{"p": 1, "h": 1, "e": 2, "n": 3, "o": 2, "m": 1}
for term, count := range hd {
gotCount := vector.values[term]
if gotCount != count {
t.Errorf("count of item \"%s\" did not match, wont %d, got %d", term, count, gotCount)
}
}
}

type TestData struct {
giveString1 string
giveString2 string
Expand Down

0 comments on commit 07d6704

Please sign in to comment.