From b5be9fd2acd33a7bdf20d18ad296d1c00ce5fd31 Mon Sep 17 00:00:00 2001 From: Jeffrey Duroyon Date: Thu, 1 Aug 2019 01:52:00 +0200 Subject: [PATCH] add unit test --- hamster_tycoon/Cage.go | 6 ++ hamster_tycoon/Game.go | 8 +++ hamster_tycoon/hamster.go | 127 +++++++++++++++++++++++++++++++++ hamster_tycoon/hamster_test.go | 91 +++++++++++++++++++++++ 4 files changed, 232 insertions(+) create mode 100644 hamster_tycoon/Cage.go create mode 100644 hamster_tycoon/Game.go create mode 100644 hamster_tycoon/hamster.go create mode 100644 hamster_tycoon/hamster_test.go diff --git a/hamster_tycoon/Cage.go b/hamster_tycoon/Cage.go new file mode 100644 index 0000000..8fdff3a --- /dev/null +++ b/hamster_tycoon/Cage.go @@ -0,0 +1,6 @@ +package hamster_tycoon + + +type Cage struct { + Hamsters []*Hamster +} diff --git a/hamster_tycoon/Game.go b/hamster_tycoon/Game.go new file mode 100644 index 0000000..15b9d92 --- /dev/null +++ b/hamster_tycoon/Game.go @@ -0,0 +1,8 @@ +package hamster_tycoon + + +type Game struct { + Cages []*Cage + SelledHamster []*Hamster + +} diff --git a/hamster_tycoon/hamster.go b/hamster_tycoon/hamster.go new file mode 100644 index 0000000..7992524 --- /dev/null +++ b/hamster_tycoon/hamster.go @@ -0,0 +1,127 @@ +package hamster_tycoon + +import ( + "errors" + "fmt" + "math/rand" +) + +const ( + Full = iota + AlmostFull + Middle + AlmostEmpty + Empty +) + +const ( + TotalGestationPeriod = 15 + MinChild = 5 + MaxCild = 9 + GestationCooldown = (3*7) + 2 + GestationMinAge = 10*6 + MaxAge = 365*2 + GestationLuck = 70 + FEMALE = "F" + MALE = "M" + +) + +var GlobalHamsterNumber = 1 + +type Hamster struct { + Name string + Number int + Sexe string + Age int + Father *Hamster + Mother *Hamster + HungerLevel int8 + ThirstLevel int8 + Weight float64 + Height float64 + Alive bool + Selled bool + Gestation bool + GestationPeriod int8 + GestationCooldown int8 + Child []*Hamster +} + +func(h *Hamster) Die(){ + h.Alive = false +} + +func (h *Hamster) Fuck(another *Hamster) (bool,error){ + if h.Sexe == another.Sexe { + return false, errors.New("can't fuck together") + } + + if h.Age <= GestationMinAge || another.Age <= GestationMinAge { + return false, errors.New("one of the hamster is too young") + } + + rand := randNumber(1,100) + + if rand <= GestationLuck { + female := func() *Hamster{if h.Sexe == FEMALE {return h } else {return another}}() + female.Gestation = true + female.GestationPeriod = 0 + female.GestationCooldown = TotalGestationPeriod + GestationCooldown + } + return rand <= GestationLuck, nil +} + +func Born(father *Hamster, mother *Hamster)([]*Hamster,error){ + if !mother.Alive || mother.Selled { + return nil, errors.New("the mother is not here") + } + if mother.GestationPeriod != TotalGestationPeriod { + return nil, errors.New("not the time to make child") + } + + mother.GestationCooldown = 0 + mother.Gestation = false + mother.GestationPeriod = 0 + + numberOfChild := randNumberChild() + child := make([]*Hamster,numberOfChild) + for i := 1; i <= numberOfChild; i++{ + child[i] = &Hamster{ + Name: fmt.Sprintf("Hamster %d",GlobalHamsterNumber), + Number: GlobalHamsterNumber, + Age : 1, + Sexe: randSexe(), + Father: father, + Mother: mother, + HungerLevel: Full, + ThirstLevel: Full, + Alive: true, + Selled: false, + Gestation: false, + GestationPeriod: 0, + GestationCooldown: 30, + } + } + mother.Child = append(mother.Child, child...) + father.Child = append(father.Child, child...) + return child,nil +} + +func randNumberChild() int { + return randNumber(MinChild,MaxCild) +} + +func randSexe() string { + return func () string{ + if randNumber(1,2) == 2 { + return MALE + } else { + return FEMALE + } + }() +} + +func randNumber(min int ,max int) int { + return min + rand.Intn(max-min) +} diff --git a/hamster_tycoon/hamster_test.go b/hamster_tycoon/hamster_test.go new file mode 100644 index 0000000..bd0e9fe --- /dev/null +++ b/hamster_tycoon/hamster_test.go @@ -0,0 +1,91 @@ +package hamster_tycoon + +import ( + "errors" + "testing" +) + +func TestDie(t *testing.T) { + testCases := []struct { + caseName string + hamster *Hamster + expectedAlive bool + }{ + { + caseName: "Should die", + hamster: &Hamster{ + Alive: true, + }, + expectedAlive: false, + }, + { + caseName: "Already die", + hamster: &Hamster{ + Alive: false, + }, + expectedAlive: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.caseName, func(t *testing.T) { + tc.hamster.Die() + if tc.hamster.Alive != tc.expectedAlive { + t.Errorf("Die result does not match expectation. \n Got : %t \n Get : %t", tc.expectedAlive, tc.hamster.Alive) + } + }) + } +} + +func TestFuck(t *testing.T) { + testCases := []struct { + caseName string + hamster1 *Hamster + hamster2 *Hamster + expectedResult bool + expectedError error + }{ + { + caseName: "Hamster 1 too young", + hamster1: &Hamster{ + Alive: true, + Sexe: MALE, + Age: GestationMinAge - 1, + }, + hamster2: &Hamster{ + Alive: true, + Sexe: FEMALE, + Age: GestationMinAge + 1, + }, + expectedResult: false, + expectedError: errors.New("one of the hamster is too young"), + }, + { + caseName: "Hamster 2 too young", + hamster1: &Hamster{ + Alive: false, + Sexe: MALE, + Age: GestationMinAge + 1, + }, + hamster2: &Hamster{ + Alive: true, + Sexe: FEMALE, + Age: GestationMinAge - 1, + }, + expectedResult: false, + expectedError: errors.New("one of the hamster is too young"), + }, + } + + for _, tc := range testCases { + t.Run(tc.caseName, func(t *testing.T) { + bool, err := tc.hamster1.Fuck(tc.hamster2) + if tc.expectedResult != bool { + t.Errorf("Fuck result does not match expectation. \n Got : %t \n Get : %t", tc.expectedResult, bool) + } + if tc.expectedError.Error() != err.Error() { + t.Errorf("Fuck result does not match expectation. \n Got : %s \n Get : %s", tc.expectedError, err) + } + }) + } +}