DEV Community

Discussion on: Daily Challenge #120 - Growth of a Population

Collapse
 
dak425 profile image
Donald Feury

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)
    }
}