// Set of utilities for manipulating fruits.packagefruits// Find a fruit based on its index.funcFindFromInteger(integeruint16)string{ifinteger<9{switch(integer){case1:return"kiwi"case2:return"pear"case3:return"kiwi"case4:return"banana"case5:return"melon"case6:return"banana"case7:return"melon"case8:return"pineapple"}}return"apple"}
Commands for the unit tests.
$touch fruits_test.go
The source-code for unit tests.
packagefruits_testimport"testing"import"fruits"import"fmt"funcTestFindFromInteger(t*testing.T){varvaluesExpectationsmap[uint16]string=map[uint16]string{0:"apple",1:"kiwi",2:"pear",3:"kiwi",4:"banana",5:"melon",6:"banana",7:"melon",8:"pineapple",9:"apple",10:"apple",100:"apple",1_000:"apple",325:"apple",10_000:"apple",}forvalue,expectation:=rangevaluesExpectations{varresultstring=fruits.FindFromInteger(value)ifresult!=expectation{t.Errorf("Expected fruits.FindFromInteger(%d) to equal %s but got %s.",value,expectation,result)}}}funcBenchmarkFindFromInteger(b*testing.B){forindex:=0;index<b.N;index++{fruits.FindFromInteger(10_000)}}funcExampleFindFromInteger(){fmt.Printf("Fruit for index %d is %q.\n",15,"apple")// Output: Fruit for index 15 is "apple".}
Commands for the tests, benchmark and coverage.
$go test-cover-bench.goos: linux
goarch: amd64
pkg: fruits
BenchmarkFindFromInteger-4 482598516 2.58 ns/op
PASS
coverage: 100.0% of statements
ok fruits 1.500s
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Go
EDIT: just realized this challenge was a trap, but leaving the (wrong) solution here for people wanting to try out Go (it's awesome, try it).
Commands for initializing the project.
The source-code.
Commands for the unit tests.
The source-code for unit tests.
Commands for the tests, benchmark and coverage.