-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestCase.js
269 lines (239 loc) · 6.68 KB
/
testCase.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import React, { useEffect, useState } from "react";
import { Input, Button, Select, message, Checkbox, InputNumber } from "antd";
import {
buildRandomTree,
serialise_bfs,
copyToClipboard,
getRandom,
getRandomUnique,
} from "./utils";
const { Option } = Select;
/*global chrome*/
// TODO referrence 支持 , 比如 k 是在数组大小范围内动态的
function isNull(c) {
return c === "null";
}
function isTree(A) {
return A.length > 0 && A.some(isNull);
}
function isBinary(c) {
return c === "1" || c === "0" || c === 1 || c === 0;
}
function isBinaryArray(A) {
return A.every(isBinary);
}
const zip = (rows) =>
rows[0].map((_, c) => rows.map((row) => row[c]).join("\r\n"));
// 支持 数组,链表,树。
// 值只支持数字,不支持字符串
// 支持自动识别二值和普通
function handleGenerate({
input,
amount = 10,
lower = 0,
upper = 10001,
maxLength = 10,
isUnique,
type,
}) {
// 多行测试用例,parts 就是每一行
const parts = input.split(" ");
if (parts.length > 1) {
return zip(
parts.map((part) =>
handleGenerate({
input: part,
amount,
upper,
maxLength,
type,
isUnique,
})
)
);
}
const finalType = !["multi", ""].includes(type) ? type : gussType(input);
if (finalType === "tree") {
const ans = [];
for (let i = 0; i < maxLength; i++) {
const nodes = serialise_bfs(
buildRandomTree({ lower, amount, upper, isUnique })
);
ans.push(nodes);
}
return ans;
} else if (finalType === "array" || finalType === "linked-list") {
const ans = [];
const items = input.slice(1, -1).split(",");
if (isBinaryArray(items)) {
upper = 1;
lower = 0;
}
for (let i = 0; i < maxLength; i++) {
let r = [];
if (isUnique) {
r = getRandomUnique(lower, upper, amount);
} else {
for (let i = 0; i < amount; i++) {
r.push(getRandom(lower, upper));
}
}
ans.push("[" + r.toString() + "]");
}
return ans;
} else if (finalType === "single") {
// 单一值
const ans = [];
for (let i = 0; i < maxLength; i++) {
ans.push(getRandom(lower, upper));
}
return ans;
} else {
return [];
}
}
function gussType(input) {
if (!input) return "";
const parts = input.split(" ");
if (parts.length > 1) return "multi";
if (input.length > 1 && input[0] == "[" && input[input.length - 1] == "]") {
if (input[1] == "[") return "";
const v = input.slice(1, -1);
if (isNaN(v[0])) return "";
const items = v.split(",");
// 数组,链表或者树
if (isTree(items)) return "tree";
if (Array.isArray(items)) {
if (Array.isArray(items[0])) return "";
return "array";
}
}
if (isNaN(input)) return "";
return "single";
}
function t() {
return new Promise((r) => {
chrome.tabs.query({ active: true }, function (tabs) {
var tab = tabs[0];
chrome.tabs.executeScript(
tab.id,
{
code: 'document.getElementsByTagName("pre")',
},
r
);
});
});
}
function getProviedTestCases(pres) {
const ans = [];
for (var i = 0; i < pres.length; ++i) {
if (pres[i].innerText.includes("输入:")) {
const testcase = pres[i].innerText.match(/输入:(.*)/)[1];
ans.push(testcase);
}
}
return ans;
}
// desc 是力扣的官方描述,需要转换为标准测试用例
// eg: "nums = [1,2,3,3], quantity = [2]" => "[1,2,3,3]\n[2]"
function normalize(testCase) {
const parts = testCase.split(", ");
return parts.map((q) => q.match(/=(.*)$/)[1]).join("\n");
}
export default function TestCase() {
// [1,2,null]
const [input, setInput] = useState("");
const [testCases, setTestCases] = useState([]);
const [type, setType] = useState("");
const [isUnique, setIsUnique] = useState(false);
const [lower, setLower] = useState(0);
const [upper, setUpper] = useState(10001);
const [amount, setAmount] = useState(10);
useEffect(() => {
const t = Array.isArray(testCases) && testCases.join("\r\n");
if (t) {
if (copyToClipboard(t)) message.success("复制成功");
}
}, [testCases]);
return (
<>
<div style={{ display: "none" }}>
<div>
测试用例类型:
<Select value={type} style={{ width: 120 }} onChange={setType}>
<Option value="array">数组(或链表)</Option>
<Option value="tree">树</Option>
<Option value="single">单一值</Option>
<Option value="multi" disabled>
多个参数
</Option>
</Select>
<div>选项</div>
<Checkbox
style={type === "single" ? { display: "none" } : {}}
checked={upper - lower + 1 < amount ? false : isUnique}
disabled={upper - lower + 1 < amount}
onChange={() => setIsUnique(!isUnique)}
>
是否唯一
</Checkbox>
数据值范围:
<InputNumber value={lower} onChange={setLower} /> -
<InputNumber value={upper} onChange={setUpper} />
<span style={type === "single" ? { display: "none" } : {}}>
长度限制:
<InputNumber value={amount} onChange={setAmount} />
</span>
</div>
<pre>
{Array.isArray(testCases) && testCases.length == 0
? "不支持的数据格式。目前只支持:单个值,数组,链表,树。"
: testCases.join("\r\n")}
</pre>
<Input
style={{ width: 320, marginRight: 20 }}
placeholder="粘贴题目默认的测试用例"
value={input}
onChange={(e) => {
const v = e.target.value;
setInput(v);
setType(gussType(v));
}}
/>
<Button
type="primary"
onClick={() => {
setTestCases(
handleGenerate({
input,
type,
lower,
upper,
amount,
isUnique: upper - lower + 1 < amount ? false : isUnique,
})
);
}}
>
生成十个随机测试用例
</Button>
</div>
<Button
type="primary"
onClick={() => {
t().then((elements) => {
const cases = getProviedTestCases(elements);
const ans = cases.map(normalize).join("\n");
console.log(cases, ans);
if (ans) {
if (copyToClipboard(ans)) message.success("复制成功");
}
});
}}
>
获取所有测试用例
</Button>
</>
);
}