github.com/fluhus/gostuff@v0.4.1-0.20240331134726-be71864f2b5d/morris/morris_test.go (about) 1 package morris 2 3 import "testing" 4 5 func TestRestore(t *testing.T) { 6 tests := []struct { 7 i byte 8 m uint 9 want uint 10 }{ 11 {0, 1, 0}, 12 {1, 1, 1}, 13 {2, 1, 4}, 14 {3, 1, 9}, 15 {4, 1, 19}, 16 {0, 10, 0}, 17 {1, 10, 1}, 18 {2, 10, 2}, 19 {10, 10, 10}, 20 {15, 10, 20}, 21 {20, 10, 31}, 22 {25, 10, 51}, 23 {30, 10, 72}, 24 } 25 for _, test := range tests { 26 if got := Restore(test.i, test.m); got != test.want { 27 t.Errorf("Restore(%d,%d)=%d, want %d", 28 test.i, test.m, got, test.want) 29 } 30 } 31 } 32 33 func TestRaise_overflow(t *testing.T) { 34 if !checkOverFlow { 35 t.Skip() 36 } 37 defer func() { recover() }() 38 got := Raise(byte(255), 10) 39 t.Fatalf("Raise(byte(255)=%d, want fail", got) 40 } 41 42 func TestRaise(t *testing.T) { 43 const reps = 1000 44 want := map[byte]int{ 45 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 46 15: 20, 20: 30, 25: 50, 30: 70, 35: 110, 40: 150, 47 } 48 margins := map[byte]int{ 49 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 50 15: 2, 20: 2, 25: 2, 30: 2, 35: 2, 40: 2, 51 } 52 got := map[byte]int{} 53 54 for i := 0; i < reps; i++ { 55 a, n := byte(0), 0 56 for a < 40 { 57 n++ 58 aa := Raise(a, 10) 59 if aa == a { 60 continue 61 } 62 a = aa 63 if _, ok := want[a]; !ok { 64 continue 65 } 66 got[a] += n 67 } 68 } 69 70 for k, n := range got { 71 got := n / reps 72 if got < want[k]-margins[k] || got > want[k]+margins[k] { 73 t.Errorf("Raise() to %d took %d, want %d-%d", k, got, 74 want[k]-margins[k], want[k]+margins[k]) 75 } 76 } 77 }