Skip to content

Commit

Permalink
add tests and fix bugs accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
shraddhaag committed Jan 23, 2024
1 parent 3f269eb commit 9def7bb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
measurements.txt
measurements.out
trace*
.DS_store
.DS_store
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/shraddhaag/1brc-go

go 1.20
go 1.21

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38 changes: 27 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"cmp"

Check failure on line 4 in main.go

View workflow job for this annotation

GitHub Actions / Go Tests

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/cmp)

Check failure on line 4 in main.go

View workflow job for this annotation

GitHub Actions / Go Tests

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/cmp)

Check failure on line 4 in main.go

View workflow job for this annotation

GitHub Actions / Go Vet

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/cmp)
"errors"
"flag"
"fmt"
Expand All @@ -11,15 +12,17 @@ import (
"runtime"
"runtime/pprof"
"runtime/trace"
"sort"
"strconv"
"strings"
"sync"

"slices"

Check failure on line 19 in main.go

View workflow job for this annotation

GitHub Actions / Go Tests

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/slices)

Check failure on line 19 in main.go

View workflow job for this annotation

GitHub Actions / Go Tests

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/slices)

Check failure on line 19 in main.go

View workflow job for this annotation

GitHub Actions / Go Vet

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/slices)
)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
var executionprofile = flag.String("execprofile", "", "write tarce execution to `file`")
var input = flag.String("input", "", "path to the input file to evaluate")

func main() {

Expand Down Expand Up @@ -47,7 +50,7 @@ func main() {
defer pprof.StopCPUProfile()
}

fmt.Println(evaluate())
fmt.Println(evaluate(*input))

if *memprofile != "" {
f, err := os.Create("./profiles/" + *memprofile)
Expand All @@ -62,30 +65,36 @@ func main() {
}
}

func evaluate() string {
type result struct {
city string
temp string
}

func evaluate(input string) string {
// mapOfTemp, err := readFileLineByLineIntoAMap("./test_cases/measurements-rounding.txt")
mapOfTemp, err := readFileLineByLineIntoAMap("measurements.txt")
// mapOfTemp, err := readFileLineByLineIntoAMap("measurements.txt")
mapOfTemp, err := readFileLineByLineIntoAMap(input)
if err != nil {
panic(err)
}

var result []string
var resultArr []result
var wg sync.WaitGroup
var mx sync.Mutex

updateResult := func(input string) {
updateResult := func(city, temp string) {
mx.Lock()
defer mx.Unlock()

result = append(result, input)
resultArr = append(resultArr, result{city, temp})
}

for city, temps := range mapOfTemp {
wg.Add(1)
go func(city string, temps []float64) {
defer wg.Done()
var min, max, avg float64
min, max = math.MaxFloat64, 0
min, max = math.MaxFloat64, math.MinInt64

for _, temp := range temps {
if temp < min {
Expand All @@ -101,14 +110,21 @@ func evaluate() string {
avg = avg / float64(len(temps))
avg = math.Ceil(avg*10) / 10

updateResult(fmt.Sprintf("%s=%.1f/%.1f/%.1f", city, min, avg, max))
updateResult(city, fmt.Sprintf("%.1f/%.1f/%.1f", min, avg, max))

}(city, temps)
}

wg.Wait()
sort.Strings(result)
return strings.Join(result, ", ")
slices.SortFunc(resultArr, func(i, j result) int {
return cmp.Compare(i.city, j.city)
})

var stringsBuilder strings.Builder
for _, i := range resultArr {
stringsBuilder.WriteString(fmt.Sprintf("%s=%s, ", i.city, i.temp))
}
return stringsBuilder.String()[:stringsBuilder.Len()-2]
}

func readFileLineByLineIntoAMap(filepath string) (map[string][]float64, error) {
Expand Down
43 changes: 43 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"io/fs"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMain(t *testing.T) {

inputFiles := find("./test_cases", ".txt")
for _, test := range inputFiles {
t.Run(test, func(t *testing.T) {
output := evaluate(test + ".txt")
assert.Equal(t, readFile(test+".out"), "{"+output+"}\n")
})
}
}

func readFile(input string) string {
fileContent, err := os.ReadFile(input)
if err != nil {
panic(err)
}
return string(fileContent)
}

func find(root, ext string) []string {
var a []string
filepath.WalkDir(root, func(s string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if filepath.Ext(d.Name()) == ext {
a = append(a, s[:len(s)-len(ext)])
}
return nil
})
return a
}

0 comments on commit 9def7bb

Please sign in to comment.