Lets have a Go, shall we?
population.go
package population // Threshold returns the number of years needed for the given population // to reach a given population threshold func Threshold(p0, aug, p int, percent float64) (year int) { for p0 < p { year++ p0 += int(float64(p0)*(percent/100)) + aug } return }
population_test.go
package population import "testing" type testCase struct { description string p0 int aug int p int percent float64 expected int } func TestThreshold(t *testing.T) { testCases := []testCase{ testCase{"first example", 1500, 100, 5000, 5, 15}, testCase{"second example", 1500000, 10000, 2000000, 2.5, 10}, } for _, test := range testCases { if result := Threshold(test.p0, test.aug, test.p, test.percent); result != test.expected { t.Fatalf("FAIL: %s - Threshold(%d, %d, %d, %f): %d - expected: %d", test.description, test.p0, test.aug, test.p, test.percent, result, test.expected) } t.Logf("PASS: %s", test.description) } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
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.
Lets have a Go, shall we?
population.go
population_test.go