github.com/yaricom/goNEAT@v0.0.0-20210507221059-e2110b885482/experiments/trial_test.go (about) 1 package experiments 2 3 import ( 4 "testing" 5 "math" 6 "bytes" 7 "encoding/gob" 8 ) 9 10 func TestTrial_Encode_Decode(t *testing.T) { 11 trial := buildTestTrial(1, 3) 12 13 var buff bytes.Buffer 14 enc := gob.NewEncoder(&buff) 15 16 // encode trial 17 err := trial.Encode(enc) 18 if err != nil { 19 t.Error("failed to encode Trial", err) 20 return 21 } 22 23 // decode trial 24 data := buff.Bytes() 25 dec := gob.NewDecoder(bytes.NewBuffer(data)) 26 27 dec_trial := Trial{} 28 err = dec_trial.Decode(dec) 29 if err != nil { 30 t.Error("failed to decode trial", err) 31 return 32 } 33 34 // do deep compare of Trail fields 35 deepCompareTrials(trial, &dec_trial, t) 36 } 37 38 func deepCompareTrials(first, second *Trial, t *testing.T) { 39 if first.Id != second.Id { 40 t.Error("first.Id != second.Id") 41 } 42 if len(first.Generations) != len(second.Generations) { 43 t.Error("len(first.Generations) != len(second.Generations)") 44 return 45 } 46 47 for i := 0; i < len(first.Generations); i++ { 48 deepCompareGenerations(&first.Generations[i], &second.Generations[i], t) 49 } 50 } 51 52 func buildTestTrial(id, num_generations int) *Trial { 53 trial := Trial{Id:id, Generations:make([]Generation, num_generations)} 54 for i := 0; i < num_generations; i++ { 55 trial.Generations[i] = *buildTestGeneration(i + 1, float64(i + 1) * math.E) 56 } 57 return &trial 58 }