chess_test.go (3748B)
1 package interceptors 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "math" 6 "testing" 7 ) 8 9 func isCloseTo(a, b, delta float64) bool { 10 return math.Abs(a-b) <= delta 11 } 12 13 func fill(n, v int) []int { 14 out := make([]int, n) 15 for i := 0; i < n; i++ { 16 out[i] = v 17 } 18 return out 19 } 20 21 func fill1(n int, v []int) []int { 22 out := make([]int, n*len(v)) 23 for i := 0; i < n*len(v); i += len(v) { 24 for j := range v { 25 out[i+j] = v[j] 26 } 27 } 28 return out 29 } 30 31 func Test_gameAccuracy(t *testing.T) { 32 // two good moves 33 w, b := gameAccuracy([]int{15, 15}) 34 assert.True(t, isCloseTo(w, 100, 1)) 35 assert.True(t, isCloseTo(b, 100, 1)) 36 // white blunders on first move 37 w, b = gameAccuracy([]int{-900, -900}) 38 assert.True(t, isCloseTo(w, 10, 5)) 39 assert.True(t, isCloseTo(b, 100, 1)) 40 // black blunders on first move 41 w, b = gameAccuracy([]int{15, 900}) 42 assert.True(t, isCloseTo(w, 100, 1)) 43 assert.True(t, isCloseTo(b, 10, 5)) 44 // both blunder on first move 45 w, b = gameAccuracy([]int{-900, 0}) 46 assert.True(t, isCloseTo(w, 10, 5)) 47 assert.True(t, isCloseTo(b, 10, 5)) 48 // 20 perfect moves 49 w, b = gameAccuracy(fill(20, 15)) 50 assert.True(t, isCloseTo(w, 100, 1)) 51 assert.True(t, isCloseTo(b, 100, 1)) 52 // 20 perfect moves and a white blunder 53 cps := fill(20, 15) 54 cps = append(cps, -900) 55 w, b = gameAccuracy(cps) 56 assert.True(t, isCloseTo(w, 50, 5)) 57 assert.True(t, isCloseTo(b, 100, 1)) 58 // 21 perfect moves and a black blunder 59 cps = fill(21, 15) 60 cps = append(cps, 900) 61 w, b = gameAccuracy(cps) 62 assert.True(t, isCloseTo(w, 100, 1)) 63 assert.True(t, isCloseTo(b, 50, 5)) 64 // 5 average moves (65 cpl) on each side 65 cps = []int{-50, 15, -50, 15, -50, 15, -50, 15, -50, 15} 66 w, b = gameAccuracy(cps) 67 assert.True(t, isCloseTo(w, 76, 8)) 68 assert.True(t, isCloseTo(b, 76, 8)) 69 // 50 average moves (65 cpl) on each side 70 cps = fill1(50, []int{-50, 15}) 71 w, b = gameAccuracy(cps) 72 assert.True(t, isCloseTo(w, 76, 8)) 73 assert.True(t, isCloseTo(b, 76, 8)) 74 // 50 mediocre moves (150 cpl) on each side 75 cps = fill1(50, []int{-135, 15}) 76 w, b = gameAccuracy(cps) 77 assert.True(t, isCloseTo(w, 54, 8)) 78 assert.True(t, isCloseTo(b, 54, 8)) 79 // 50 terrible moves (500 cpl) on each side 80 cps = fill1(50, []int{-435, 15}) 81 w, b = gameAccuracy(cps) 82 assert.True(t, isCloseTo(w, 20, 8)) 83 assert.True(t, isCloseTo(b, 20, 8)) 84 } 85 86 func Test_gameAccuracy1(t *testing.T) { 87 type args struct { 88 cps []int 89 } 90 tests := []struct { 91 name string 92 args args 93 want float64 94 want1 float64 95 }{ 96 { 97 name: "test1", 98 args: args{ 99 cps: []int{48, 38, 36, 36, 25, 61, 42, 131, 82, 135, 109, 186, 152, 145, 134, 280, 287, 264, 278, 271, 271, 306, 246, 335, 311, 500, 87, 276, 284, 398, 196, 514, 520, 641, -144, -74, -156, -107, -94, 60, 47, 146, -33, 98, -483, 31, -174, 385, -152, 467, 515, 846, 874, 884, 874, 938, 885, 889, 935, 946, 831, 998, 1018, 1026, 1031, 1142, 935, 1062, 1096, 1123, 1112, 1123}, 100 }, 101 want: 61.61609601278025, 102 want1: 59.024965435590794, 103 }, 104 } 105 for _, tt := range tests { 106 t.Run(tt.name, func(t *testing.T) { 107 got, got1 := gameAccuracy(tt.args.cps) 108 assert.Equalf(t, tt.want, got, "gameAccuracy(%v)", tt.args.cps) 109 assert.Equalf(t, tt.want1, got1, "gameAccuracy(%v)", tt.args.cps) 110 }) 111 } 112 } 113 114 func Test_standardDeviation(t *testing.T) { 115 type args struct { 116 num []float64 117 } 118 tests := []struct { 119 name string 120 args args 121 want float64 122 }{ 123 { 124 name: "", 125 args: args{ 126 num: []float64{91.37426170872673, 37.04656899332343, 43.22998523826144, 36.02211364871788, 40.27589492090241, 41.43247132264411, 55.50076484124536}, 127 }, 128 want: 18.186779319663053, 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 assert.Equalf(t, tt.want, standardDeviation(tt.args.num), "standardDeviation(%v)", tt.args.num) 134 }) 135 } 136 }