forked from EndlessCheng/codeforces-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
110 lines (88 loc) · 2.3 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
. "fmt"
"github.com/EndlessCheng/codeforces-go/main/testutil"
"github.com/stretchr/testify/assert"
"testing"
)
//func init() { rand.Seed(time.Now().UnixNano()) }
const debugCaseNum = 0
const failedCountLimit = 10
var rg *testutil.RG
var failedCount int
type mockIO struct {
initData
answer
hiddenData []int //
_t *testing.T
caseNum int
queryLimit int
queryCnt int
}
func (io *mockIO) String() string {
hStr := Sprintf("%v", io.hiddenData)
//hStr := strings.Join(io.hiddenData, "\n")
return Sprintf("%+v\n%s", io.initData, hStr)
}
// Mock initData
func (io *mockIO) readInitData() (d initData) {
return io.initData
}
// Check answer
func (io *mockIO) printAnswer(actualAns answer) {
expectedAns := io.answer
if !assert.EqualValues(io._t, expectedAns, actualAns, "Wrong Answer %d\nCase Data:\n%v", io.caseNum, io) {
if failedCount++; failedCount > failedCountLimit {
io._t.Fatal("too many failed cases, terminated")
}
}
// for special judge
ansChecker := func() bool {
return true
}
if !assert.Truef(io._t, ansChecker(), "Wrong Answer %d\nMy Answer:\n%v\nCase Data:\n%v", io.caseNum, actualAns, io) {
if failedCount++; failedCount > failedCountLimit {
io._t.Fatal("too many failed cases, terminated")
}
}
}
// Mock query
func (io *mockIO) query(q request) (resp response) {
if io.caseNum == debugCaseNum {
Print("Query ", q, " => ")
defer func() { Println(resp) }()
}
io.queryCnt++
//if io.queryCnt > io.queryLimit { io._t.Fatalf("Query Limit Exceeded %d\nCase Data:\n%v", io.caseNum, io) }
// calc q ...
return
}
func Test_doInteraction(_t *testing.T) {
for tc, checkTC := 1, 1; ; tc++ {
if tc == debugCaseNum {
print()
//debug = true
}
io := &mockIO{_t: _t, caseNum: tc}
// gen random data ...
rg = testutil.NewRandGenerator()
n := rg.Int(2, 4)
a := rg.IntSlice(n, 1, n)
io.n = n
io.ans = a
io.hiddenData = a
// set limit ...
io.queryLimit = n + 30
doInteraction(io)
if io.queryCnt > io.queryLimit {
io._t.Errorf("Query Limit Exceeded %d\n%d > %d\nCase Data:\n%v", io.caseNum, io.queryCnt, io.queryLimit, io)
if failedCount++; failedCount > failedCountLimit {
io._t.Fatal("too many failed cases, terminated")
}
}
if tc == checkTC {
_t.Logf("%d cases checked.", tc)
checkTC <<= 1
}
}
}