Skip to content

Commit

Permalink
Functionality complete. Tests Complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
galamdring committed Jan 17, 2020
1 parent 98c2ee3 commit a994923
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 0 deletions.
8 changes: 8 additions & 0 deletions exercise-001-corpus/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gofmt -w .
golint ./...
go build word_count.go
pushd corpus
go test
go test -bench=.
popd
./word_count -file 7oldsamr.txt
79 changes: 79 additions & 0 deletions exercise-001-corpus/corpus/corpus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package corpus

import (
"errors"
"io/ioutil"
"log"
"regexp"
"sort"
"strings"
)

func WordCountFromFileSorted(file string, reverse bool) (PairedList, error) {
contents, err := StringFromFile(file)
if err != nil {
return nil, err
}
words := WordCountMapFromString(contents)
plist := SortedMapPairedList(words, reverse)
return plist, nil
}

func SortedMapPairedList(unsorted map[string]int, reverse bool) PairedList {
if len(unsorted) == 0 {
return nil
}
sortable := mapToPairedList(unsorted)
if reverse {
sort.Sort(sort.Reverse(sortable))
} else {
sort.Sort(sortable)
}
return sortable
}

func mapToPairedList(unsorted map[string]int) PairedList {
sortable := make(PairedList, len(unsorted))
i := 0
for k, v := range unsorted {
sortable[i] = Pair{k, v}
i++
}
return sortable
}

func WordCountFromFile(file string) (PairedList, error) {
contents, err := StringFromFile(file)
if err != nil {
return nil, err
}
if len(contents) > 0 {
words := WordCountMapFromString(contents)
wordsPL := mapToPairedList(words)
return wordsPL, nil
} else {
return nil, errors.New("Empty File")
}
}

func StringFromFile(file string) (string, error) {
contents, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
return string(contents), nil
}

func WordCountMapFromString(str string) map[string]int {
words := strings.Fields(str)
reg, err := regexp.Compile("[^a-zA-Z]+")
if err != nil {
log.Fatal(err)
}
wordMap := make(map[string]int)
for _, word := range words {
prettyWord := strings.ToLower(reg.ReplaceAllString(word, ""))
wordMap[prettyWord] += 1
}
return wordMap
}
274 changes: 274 additions & 0 deletions exercise-001-corpus/corpus/corpus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
package corpus

import (
"testing"
)

type testpair struct {
value string
result PairedList
}

var wordCountFromFileTests = []testpair{
{"test1.txt", PairedList{Pair{Key: "test", Value: 3}}},
}

func TestWordCountFromFile(t *testing.T) {
for _, pair := range wordCountFromFileTests {
v, _ := WordCountFromFile(pair.value)
if !v.Equals(pair.result) {
t.Error(
"For", pair.value,
"expected", pair.result,
"got", v)
}
}
}

type stringTestPair struct {
value string
result string
}

var stringFromFileTests = []stringTestPair{
{"test1.txt", "test test test"},
}

func TestStringFromFile(t *testing.T) {
for _, pair := range stringFromFileTests {
v, _ := StringFromFile(pair.value)
if v != pair.result {
t.Error("For", pair.value,
"expected", pair.result,
"got", v)
}
}
}

type mapStringTestPair struct {
result map[string]int
value string
}

var wordCountMapFromStringTests = []mapStringTestPair{
{map[string]int{"test": 3}, "test test test"},
}

func mapContainsAll(a, b map[string]int) bool {
for k, v := range a {
if val, ok := b[k]; ok {
if val != v {
return false
}
} else {
return false
}
}
return true
}

func TestWordCountMapFromString(t *testing.T) {
for _, pair := range wordCountMapFromStringTests {
v := WordCountMapFromString((pair.value))
if !mapContainsAll(pair.result, v) {
t.Error("For", pair.value,
"expected", pair.result,
"got", v)
}
}
}

type mapToPairedListPair struct {
value map[string]int
result PairedList
}

var sortedMapPairedListTests = []mapToPairedListPair{
{map[string]int{"test": 3, "sample": 2},
PairedList{Pair{"test", 3}, Pair{"sample", 2}}},
}

func TestSortedMapPairedList(t *testing.T) {
for _, pair := range sortedMapPairedListTests {
v := SortedMapPairedList(pair.value, true)
if !v.Equals(pair.result) {
t.Error("For", pair.value,
"expected", pair.result,
"got", v)
}
}
}

var wordCountFromFileSortedTests = []testpair{
{"test1.txt",
PairedList{
Pair{
Key: "test",
Value: 3,
}}},
}

func TestWordCountFromFileSorted(t *testing.T) {
for _, pair := range wordCountFromFileSortedTests {
v, _ := WordCountFromFileSorted(pair.value, true)
if !v.Equals(pair.result) {
t.Error("For", pair.value,
"expected", pair.result,
"got", v)
}
}
}

func BenchmarkWordCountFromFileSorted20(b *testing.B) {
for n := 0; n < b.N; n++ {
for _, pair := range wordCountFromFileSortedTests {
WordCountFromFileSorted(pair.value, true)
}
}
}

type pairedListContainsPair struct {
value PairedList
contents Pair
result bool
}

var pairedListContainsTests = []pairedListContainsPair{
{PairedList{Pair{"test", 3}},
Pair{"test", 3},
true},
}

func TestPairedList_Contains(t *testing.T) {
for _, pair := range pairedListContainsTests {
v := pair.value.Contains(pair.contents)
if v != pair.result {
t.Error("For", pair.value,
"for contents", pair.contents,
"expected", pair.result,
"got", v)
}
}
}

type pairedListCountPairs struct {
value PairedList
counted Pair
result int
}

var pairedListCountTests = []pairedListCountPairs{
{PairedList{Pair{
Key: "test",
Value: 3,
},
Pair{
Key: "test",
Value: 3,
}},
Pair{
Key: "test",
Value: 3,
},
2},
}

func TestPairedList_Count(t *testing.T) {
for _, pair := range pairedListCountTests {
v := pair.value.Count(pair.counted)
if v != pair.result {
t.Error("For", pair.value,
"counted", pair.counted,
"expected", pair.result,
"got", v)
}
}
}

type pairedListEqualsPair struct {
lista PairedList
listb PairedList
result bool
}

var pairedListEqualsPairs = []pairedListEqualsPair{
{PairedList{Pair{
Key: "test",
Value: 25,
}},
PairedList{Pair{
Key: "test",
Value: 25,
}},
true},
}

func TestPairedList_Equals(t *testing.T) {
for _, pair := range pairedListEqualsPairs {
v := pair.lista.Equals(pair.listb)
if v != pair.result {
t.Error("For", pair.lista,
"and", pair.listb,
"expected", pair.result,
"got", v)
}
}
}

type pairedListLenPair struct {
list PairedList
length int
}

var pairedListLenTests = []pairedListLenPair{
{PairedList{Pair{
Key: "test",
Value: 3,
}, Pair{
Key: "sample",
Value: 1,
}, Pair{
Key: "example",
Value: 45,
}}, 3},
}

func TestPairedList_Len(t *testing.T) {
for _, pair := range pairedListLenTests {
v := pair.list.Len()
if v != pair.length {
t.Error("For", pair.list,
"expected", pair.length,
"got", v)
}
}
}

type pairedListLessPair struct {
list PairedList
indexa int
indexb int
result bool
}

var pairedListLessTests = []pairedListLessPair{
{PairedList{Pair{
Key: "test",
Value: 3,
}, Pair{Key: "sample", Value: 2}},
0,
1,
false},
}

func TestPairedList_Less(t *testing.T) {
for _, pair := range pairedListLessTests {
v := pair.list.Less(pair.indexa, pair.indexb)
if v != pair.result {
t.Error("For", pair.list,
"index", pair.indexa, "and", pair.indexb,
"expected", pair.result,
"got", v)
}
}
}
Loading

0 comments on commit a994923

Please sign in to comment.