AtCoder Easy Test v2

Make testing sample cases easy

As of 2021-09-30. See the latest version.

  1. // ==UserScript==
  2. // @name AtCoder Easy Test v2
  3. // @namespace https://atcoder.jp/
  4. // @version 2.1.0
  5. // @description Make testing sample cases easy
  6. // @author magurofly
  7. // @license MIT
  8. // @supportURL https://github.com/magurofly/atcoder-easy-test/
  9. // @match https://atcoder.jp/contests/*/tasks/*
  10. // @match https://yukicoder.me/problems/no/*
  11. // @grant unsafeWindow
  12. // ==/UserScript==
  13. (function() {
  14. const codeSaver = {
  15. LIMIT: 10,
  16. get() {
  17. // `json` は、ソースコード文字列またはJSON文字列
  18. let json = unsafeWindow.localStorage.AtCoderEasyTest$lastCode;
  19. let data = [];
  20. try {
  21. if (typeof json == "string") {
  22. data.push(...JSON.parse(json));
  23. }
  24. else {
  25. data = [];
  26. }
  27. }
  28. catch (e) {
  29. data.push({
  30. path: unsafeWindow.localStorage.AtCoderEasyTset$lastPage,
  31. code: json,
  32. });
  33. }
  34. return data;
  35. },
  36. set(data) {
  37. unsafeWindow.localStorage.AtCoderEasyTest$lastCode = JSON.stringify(data);
  38. },
  39. save(code) {
  40. let data = codeSaver.get();
  41. const idx = data.findIndex(({ path }) => path == location.pathname);
  42. if (idx != -1)
  43. data.splice(idx, idx + 1);
  44. data.push({
  45. path: location.pathname,
  46. code,
  47. });
  48. while (data.length > codeSaver.LIMIT)
  49. data.shift();
  50. codeSaver.set(data);
  51. },
  52. restore() {
  53. const data = codeSaver.get();
  54. const idx = data.findIndex(({ path }) => path == location.pathname);
  55. if (idx == -1 || !(data[idx] instanceof Object))
  56. return Promise.reject(`No saved code found for ${location.pathname}`);
  57. return Promise.resolve(data[idx].code);
  58. }
  59. };
  60.  
  61. function similarLangs(targetLang, candidateLangs) {
  62. const [targetName, targetDetail] = targetLang.split(" ", 2);
  63. const selectedLangs = candidateLangs.filter(candidateLang => {
  64. const [name, _] = candidateLang.split(" ", 2);
  65. return name == targetName;
  66. }).map(candidateLang => {
  67. const [_, detail] = candidateLang.split(" ", 2);
  68. return [candidateLang, similarity(detail, targetDetail)];
  69. });
  70. return selectedLangs.sort((a, b) => a[1] - b[1]).map(([lang, _]) => lang);
  71. }
  72. function similarity(s, t) {
  73. const n = s.length, m = t.length;
  74. let dp = new Array(m + 1).fill(0);
  75. for (let i = 0; i < n; i++) {
  76. const dp2 = new Array(m + 1).fill(0);
  77. for (let j = 0; j < m; j++) {
  78. const cost = (s.charCodeAt(i) - t.charCodeAt(j)) ** 2;
  79. dp2[j + 1] = Math.min(dp[j] + cost, dp[j + 1] + cost * 0.25, dp2[j] + cost * 0.25);
  80. }
  81. dp = dp2;
  82. }
  83. return dp[m];
  84. }
  85.  
  86. class CodeRunner {
  87. get label() {
  88. return this._label;
  89. }
  90. constructor(label, site) {
  91. this._label = `${label} [${site}]`;
  92. }
  93. async test(sourceCode, input, expectedOutput, options) {
  94. const result = await this.run(sourceCode, input);
  95. if (expectedOutput != null)
  96. result.expectedOutput = expectedOutput;
  97. if (result.status != "OK" || typeof expectedOutput != "string")
  98. return result;
  99. let output = result.output || "";
  100. if (options.trim) {
  101. expectedOutput = expectedOutput.trim();
  102. output = output.trim();
  103. }
  104. let equals = (x, y) => x === y;
  105. if (options.allowableError) {
  106. const floatPattern = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
  107. const superEquals = equals;
  108. equals = (x, y) => {
  109. if (floatPattern.test(x) && floatPattern.test(y))
  110. return Math.abs(parseFloat(x) - parseFloat(y)) <= options.allowableError;
  111. return superEquals(x, y);
  112. };
  113. }
  114. if (options.split) {
  115. const superEquals = equals;
  116. equals = (x, y) => {
  117. const xs = x.split(/\s+/);
  118. const ys = y.split(/\s+/);
  119. if (xs.length != ys.length)
  120. return false;
  121. const len = xs.length;
  122. for (let i = 0; i < len; i++) {
  123. if (!superEquals(xs[i], ys[i]))
  124. return false;
  125. }
  126. return true;
  127. };
  128. }
  129. result.status = equals(output, expectedOutput) ? "AC" : "WA";
  130. return result;
  131. }
  132. }
  133.  
  134. class CustomRunner extends CodeRunner {
  135. run;
  136. constructor(label, run) {
  137. super(label, "Browser");
  138. this.run = run;
  139. }
  140. }
  141.  
  142. function buildParams(data) {
  143. return Object.entries(data).map(([key, value]) => encodeURIComponent(key) + "=" + encodeURIComponent(value)).join("&");
  144. }
  145. function sleep(ms) {
  146. return new Promise(done => setTimeout(done, ms));
  147. }
  148. function html2element(html) {
  149. const template = document.createElement("template");
  150. template.innerHTML = html;
  151. return template.content.firstChild;
  152. }
  153. const eventListeners = {};
  154. const events = {
  155. on(name, listener) {
  156. const listeners = (name in eventListeners ? eventListeners[name] : eventListeners[name] = []);
  157. listeners.push(listener);
  158. },
  159. trig(name) {
  160. if (name in eventListeners) {
  161. for (const listener of eventListeners[name])
  162. listener();
  163. }
  164. },
  165. };
  166. class ObservableValue {
  167. _value;
  168. _listeners;
  169. constructor(value) {
  170. this._value = value;
  171. this._listeners = new Set();
  172. }
  173. get value() {
  174. return this._value;
  175. }
  176. set value(value) {
  177. this._value = value;
  178. for (const listener of this._listeners)
  179. listener(value);
  180. }
  181. addListener(listener) {
  182. this._listeners.add(listener);
  183. listener(this._value);
  184. }
  185. removeListener(listener) {
  186. this._listeners.delete(listener);
  187. }
  188. map(f) {
  189. const y = new ObservableValue(f(this.value));
  190. this.addListener(x => {
  191. y.value = f(x);
  192. });
  193. return y;
  194. }
  195. }
  196.  
  197. let waitAtCoderCustomTest = Promise.resolve();
  198. const AtCoderCustomTestBase = location.href.replace(/\/tasks\/.+$/, "/custom_test");
  199. const AtCoderCustomTestResultAPI = AtCoderCustomTestBase + "/json?reload=true";
  200. const AtCoderCustomTestSubmitAPI = AtCoderCustomTestBase + "/submit/json";
  201. class AtCoderRunner extends CodeRunner {
  202. languageId;
  203. constructor(languageId, label) {
  204. super(label, "AtCoder");
  205. this.languageId = languageId;
  206. }
  207. async run(sourceCode, input) {
  208. const promise = this.submit(sourceCode, input);
  209. waitAtCoderCustomTest = promise;
  210. return await promise;
  211. }
  212. async submit(sourceCode, input) {
  213. try {
  214. await waitAtCoderCustomTest;
  215. }
  216. catch (error) {
  217. console.error(error);
  218. }
  219. const error = await fetch(AtCoderCustomTestSubmitAPI, {
  220. method: "POST",
  221. credentials: "include",
  222. headers: {
  223. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
  224. },
  225. body: buildParams({
  226. "data.LanguageId": String(this.languageId),
  227. sourceCode,
  228. input,
  229. csrf_token: unsafeWindow.csrfToken,
  230. }),
  231. }).then(r => r.text());
  232. if (error) {
  233. throw new Error(error);
  234. }
  235. await sleep(100);
  236. for (;;) {
  237. const data = await fetch(AtCoderCustomTestResultAPI, {
  238. method: "GET",
  239. credentials: "include",
  240. }).then(r => r.json());
  241. if (!("Result" in data))
  242. continue;
  243. const result = data.Result;
  244. if ("Interval" in data) {
  245. await sleep(data.Interval);
  246. continue;
  247. }
  248. return {
  249. status: (result.ExitCode == 0) ? "OK" : (result.TimeConsumption == -1) ? "CE" : "RE",
  250. exitCode: result.ExitCode,
  251. execTime: result.TimeConsumption,
  252. memory: result.MemoryConsumption,
  253. input,
  254. output: data.Stdout,
  255. error: data.Stderr,
  256. };
  257. }
  258. }
  259. }
  260.  
  261. class PaizaIORunner extends CodeRunner {
  262. name;
  263. constructor(name, label) {
  264. super(label, "PaizaIO");
  265. this.name = name;
  266. }
  267. async run(sourceCode, input) {
  268. let id, status, error;
  269. try {
  270. const res = await fetch("https://api.paiza.io/runners/create?" + buildParams({
  271. source_code: sourceCode,
  272. language: this.name,
  273. input,
  274. longpoll: "true",
  275. longpoll_timeout: "10",
  276. api_key: "guest",
  277. }), {
  278. method: "POST",
  279. mode: "cors",
  280. }).then(r => r.json());
  281. id = res.id;
  282. status = res.status;
  283. error = res.error;
  284. }
  285. catch (error) {
  286. return {
  287. status: "IE",
  288. input,
  289. error: String(error),
  290. };
  291. }
  292. while (status == "running") {
  293. const res = await fetch("https://api.paiza.io/runners/get_status?" + buildParams({
  294. id,
  295. api_key: "guest",
  296. }), {
  297. mode: "cors",
  298. }).then(res => res.json());
  299. status = res.status;
  300. error = res.error;
  301. }
  302. const res = await fetch("https://api.paiza.io/runners/get_details?" + buildParams({
  303. id,
  304. api_key: "guest",
  305. }), {
  306. mode: "cors",
  307. }).then(r => r.json());
  308. const result = {
  309. status: "OK",
  310. exitCode: String(res.exit_code),
  311. execTime: +res.time * 1e3,
  312. memory: +res.memory * 1e-3,
  313. input,
  314. };
  315. if (res.build_result == "failure") {
  316. result.status = "CE";
  317. result.exitCode = res.build_exit_code;
  318. result.output = res.build_stdout;
  319. result.error = res.build_stderr;
  320. }
  321. else {
  322. result.status = (res.result == "timeout") ? "TLE" : (res.result == "failure") ? "RE" : "OK";
  323. result.exitCode = res.exit_code;
  324. result.output = res.stdout;
  325. result.error = res.stderr;
  326. }
  327. return result;
  328. }
  329. }
  330.  
  331. class WandboxRunner extends CodeRunner {
  332. name;
  333. options;
  334. constructor(name, label, options = {}) {
  335. super(label, "Wandbox");
  336. this.name = name;
  337. this.options = options;
  338. }
  339. getOptions(sourceCode, input) {
  340. if (typeof this.options == "function")
  341. return this.options(sourceCode, input);
  342. return this.options;
  343. }
  344. run(sourceCode, input) {
  345. const options = this.getOptions(sourceCode, input);
  346. return this.request(Object.assign({
  347. compiler: this.name,
  348. code: sourceCode,
  349. stdin: input,
  350. }, options));
  351. }
  352. async request(body) {
  353. const startTime = Date.now();
  354. let res;
  355. try {
  356. res = await fetch("https://wandbox.org/api/compile.json", {
  357. method: "POST",
  358. mode: "cors",
  359. headers: {
  360. "Content-Type": "application/json",
  361. },
  362. body: JSON.stringify(body),
  363. }).then(r => r.json());
  364. }
  365. catch (error) {
  366. console.error(error);
  367. return {
  368. status: "IE",
  369. input: body.stdin,
  370. error: String(error),
  371. };
  372. }
  373. const endTime = Date.now();
  374. const result = {
  375. status: "OK",
  376. exitCode: String(res.status),
  377. execTime: endTime - startTime,
  378. input: body.stdin,
  379. output: String(res.program_output || ""),
  380. error: String(res.program_error || ""),
  381. };
  382. // 正常終了以外の場合
  383. if (res.status != 0) {
  384. if (res.signal) {
  385. result.exitCode += ` (${res.signal})`;
  386. }
  387. result.output = String(res.compiler_output || "") + String(result.output || "");
  388. result.error = String(res.compiler_error || "") + String(result.error || "");
  389. if (res.compiler_output || res.compiler_error) {
  390. result.status = "CE";
  391. }
  392. else {
  393. result.status = "RE";
  394. }
  395. }
  396. return result;
  397. }
  398. }
  399.  
  400. class WandboxCppRunner extends WandboxRunner {
  401. async run(sourceCode, input) {
  402. // ACL を結合する
  403. const ACLBase = "https://cdn.jsdelivr.net/gh/atcoder/ac-library/";
  404. const files = new Map();
  405. const includeHeader = async (source) => {
  406. const pattern = /^#\s*include\s*[<"]atcoder\/([^>"]+)[>"]/gm;
  407. const loaded = [];
  408. let match;
  409. while (match = pattern.exec(source)) {
  410. const file = "atcoder/" + match[1];
  411. if (files.has(file))
  412. continue;
  413. files.set(file, null);
  414. loaded.push([file, fetch(ACLBase + file, { mode: "cors", cache: "force-cache", }).then(r => r.text())]);
  415. }
  416. const included = await Promise.all(loaded.map(async ([file, r]) => {
  417. const source = await r;
  418. files.set(file, source);
  419. return source;
  420. }));
  421. for (const source of included) {
  422. await includeHeader(source);
  423. }
  424. };
  425. await includeHeader(sourceCode);
  426. const codes = [];
  427. for (const [file, code] of files) {
  428. codes.push({ file, code, });
  429. }
  430. const options = this.getOptions(sourceCode, input);
  431. return await this.request(Object.assign({
  432. compiler: this.name,
  433. code: sourceCode,
  434. stdin: input,
  435. codes,
  436. "compiler-option-raw": "-I.",
  437. }, options));
  438. }
  439. }
  440.  
  441. let brythonRunnerLoaded = false;
  442. const brythonRunner = new CustomRunner("Brython", async (sourceCode, input) => {
  443. if (!brythonRunnerLoaded) {
  444. // BrythonRunner を読み込む
  445. await new Promise((resolve) => {
  446. const script = document.createElement("script");
  447. script.src = "https://cdn.jsdelivr.net/gh/pythonpad/brython-runner/lib/brython-runner.bundle.js";
  448. script.onload = () => {
  449. brythonRunnerLoaded = true;
  450. resolve(null);
  451. };
  452. document.head.appendChild(script);
  453. });
  454. }
  455. let stdout = "";
  456. let stderr = "";
  457. let stdinOffset = 0;
  458. const BrythonRunner = unsafeWindow.BrythonRunner;
  459. const runner = new BrythonRunner({
  460. stdout: { write(content) { stdout += content; }, flush() { } },
  461. stderr: { write(content) { stderr += content; }, flush() { } },
  462. stdin: { async readline() {
  463. let index = input.indexOf("\n", stdinOffset) + 1;
  464. if (index == 0)
  465. index = input.length;
  466. const text = input.slice(stdinOffset, index);
  467. stdinOffset = index;
  468. return text;
  469. } },
  470. });
  471. const timeStart = Date.now();
  472. await runner.runCode(sourceCode);
  473. const timeEnd = Date.now();
  474. return {
  475. status: "OK",
  476. exitCode: "0",
  477. execTime: (timeEnd - timeStart),
  478. input,
  479. output: stdout,
  480. error: stderr,
  481. };
  482. });
  483.  
  484. let atcoder = null;
  485. function init$1() {
  486. const doc = unsafeWindow.document;
  487. const eLanguage = unsafeWindow.$("#select-lang>select");
  488. const langMap = {
  489. 4001: "C GCC 9.2.1",
  490. 4002: "C Clang 10.0.0",
  491. 4003: "C++ GCC 9.2.1",
  492. 4004: "C++ Clang 10.0.0",
  493. 4005: "Java OpenJDK 11.0.6",
  494. 4006: "Python3 CPython 3.8.2",
  495. 4007: "Bash 5.0.11",
  496. 4008: "bc 1.07.1",
  497. 4009: "Awk GNU Awk 4.1.4",
  498. 4010: "C# .NET Core 3.1.201",
  499. 4011: "C# Mono-mcs 6.8.0.105",
  500. 4012: "C# Mono-csc 3.5.0",
  501. 4013: "Clojure 1.10.1.536",
  502. 4014: "Crystal 0.33.0",
  503. 4015: "D DMD 2.091.0",
  504. 4016: "D GDC 9.2.1",
  505. 4017: "D LDC 1.20.1",
  506. 4018: "Dart 2.7.2",
  507. 4019: "dc 1.4.1",
  508. 4020: "Erlang 22.3",
  509. 4021: "Elixir 1.10.2",
  510. 4022: "F# .NET Core 3.1.201",
  511. 4023: "F# Mono 10.2.3",
  512. 4024: "Forth gforth 0.7.3",
  513. 4025: "Fortran GNU Fortran 9.2.1",
  514. 4026: "Go 1.14.1",
  515. 4027: "Haskell GHC 8.8.3",
  516. 4028: "Haxe 4.0.3",
  517. 4029: "Haxe 4.0.3",
  518. 4030: "JavaScript Node.js 12.16.1",
  519. 4031: "Julia 1.4.0",
  520. 4032: "Kotlin 1.3.71",
  521. 4033: "Lua Lua 5.3.5",
  522. 4034: "Lua LuaJIT 2.1.0",
  523. 4035: "Dash 0.5.8",
  524. 4036: "Nim 1.0.6",
  525. 4037: "Objective-C Clang 10.0.0",
  526. 4038: "Lisp SBCL 2.0.3",
  527. 4039: "OCaml 4.10.0",
  528. 4040: "Octave 5.2.0",
  529. 4041: "Pascal FPC 3.0.4",
  530. 4042: "Perl 5.26.1",
  531. 4043: "Raku Rakudo 2020.02.1",
  532. 4044: "PHP 7.4.4",
  533. 4045: "Prolog SWI-Prolog 8.0.3",
  534. 4046: "Python PyPy2 7.3.0",
  535. 4047: "Python3 PyPy3 7.3.0",
  536. 4048: "Racket 7.6",
  537. 4049: "Ruby 2.7.1",
  538. 4050: "Rust 1.42.0",
  539. 4051: "Scala 2.13.1",
  540. 4052: "Java OpenJDK 1.8.0",
  541. 4053: "Scheme Gauche 0.9.9",
  542. 4054: "ML MLton 20130715",
  543. 4055: "Swift 5.2.1",
  544. 4056: "Text cat 8.28",
  545. 4057: "TypeScript 3.8",
  546. 4058: "Basic .NET Core 3.1.101",
  547. 4059: "Zsh 5.4.2",
  548. 4060: "COBOL Fixed OpenCOBOL 1.1.0",
  549. 4061: "COBOL Free OpenCOBOL 1.1.0",
  550. 4062: "Brainfuck bf 20041219",
  551. 4063: "Ada Ada2012 GNAT 9.2.1",
  552. 4064: "Unlambda 2.0.0",
  553. 4065: "Cython 0.29.16",
  554. 4066: "Sed 4.4",
  555. 4067: "Vim 8.2.0460",
  556. };
  557. const languageId = new ObservableValue(eLanguage.val());
  558. eLanguage.change(() => {
  559. languageId.value = eLanguage.val();
  560. });
  561. const language = languageId.map(lang => langMap[lang]);
  562. function getTestCases() {
  563. const selectors = [
  564. ["#task-statement p+pre.literal-block", ".section"],
  565. ["#task-statement pre.source-code-for-copy", ".part"],
  566. ["#task-statement .lang>*:nth-child(1) .div-btn-copy+pre", ".part"],
  567. ["#task-statement .div-btn-copy+pre", ".part"],
  568. ["#task-statement>.part pre.linenums", ".part"],
  569. ["#task-statement>.part:not(.io-style)>h3+section>pre", ".part"],
  570. ["#task-statement pre", ".part"],
  571. ];
  572. for (const [selector, closestSelector] of selectors) {
  573. const e = [...document.querySelectorAll(selector)].filter(e => {
  574. if (e.closest(".io-style"))
  575. return false; // practice2
  576. return true;
  577. });
  578. if (e.length == 0)
  579. continue;
  580. const testcases = [];
  581. let sampleId = 1;
  582. for (let i = 0; i < e.length; i += 2) {
  583. const container = e[i].closest(closestSelector) || e[i].parentElement;
  584. testcases.push({
  585. title: `Sample ${sampleId++}`,
  586. input: (e[i] || {}).textContent,
  587. output: (e[i + 1] || {}).textContent,
  588. anchor: container.querySelector("h3"),
  589. });
  590. }
  591. return testcases;
  592. }
  593. return [];
  594. }
  595. atcoder = {
  596. name: "AtCoder",
  597. language,
  598. get sourceCode() {
  599. return unsafeWindow.getSourceCode();
  600. },
  601. set sourceCode(sourceCode) {
  602. doc.querySelector(".plain-textarea").value = sourceCode;
  603. unsafeWindow.$(".editor").data("editor").doc.setValue(sourceCode);
  604. },
  605. submit() {
  606. doc.querySelector("#submit").click();
  607. },
  608. get testButtonContainer() {
  609. return doc.querySelector("#submit").parentElement;
  610. },
  611. get sideButtonContainer() {
  612. return doc.querySelector(".editor-buttons");
  613. },
  614. get bottomMenuContainer() {
  615. return doc.getElementById("main-div");
  616. },
  617. get resultListContainer() {
  618. return doc.querySelector(".form-code-submit");
  619. },
  620. get testCases() {
  621. return getTestCases();
  622. }
  623. };
  624. }
  625. if (location.host == "atcoder.jp")
  626. init$1();
  627. var atcoder$1 = atcoder;
  628.  
  629. let yukicoder = null;
  630. function init() {
  631. const $ = unsafeWindow.$;
  632. const doc = unsafeWindow.document;
  633. const editor = unsafeWindow.ace.edit("rich_source");
  634. const eSourceObject = $("#source");
  635. const eLang = $("#lang");
  636. const eSamples = $(".sample");
  637. const langMap = {
  638. "cpp14": "C++ C++14 GCC 11.1.0 + Boost 1.77.0",
  639. "cpp17": "C++ C++17 GCC 11.1.0 + Boost 1.77.0",
  640. "cpp-clang": "C++ C++17 Clang 10.0.0 + Boost 1.76.0",
  641. "cpp23": "C++ C++11 GCC 8.4.1",
  642. "c11": "C++ C++11 GCC 11.1.0",
  643. "c": "C C90 GCC 8.4.1",
  644. "java8": "Java Java16 OpenJDK 16.0.1",
  645. "csharp": "C# CSC 3.9.0",
  646. "csharp_mono": "C# Mono 6.12.0.147",
  647. "csharp_dotnet": "C# .NET 5.0",
  648. "perl": "Perl 5.26.3",
  649. "raku": "Raku Rakudo v2021-07-2-g74d7ff771",
  650. "php": "PHP 7.2.24",
  651. "php7": "PHP 8.0.8",
  652. "python3": "Python3 3.9.6 + numpy 1.14.5 + scipy 1.1.0",
  653. "pypy2": "Python PyPy2 7.3.5",
  654. "pypy3": "Python3 PyPy3 7.3.5",
  655. "ruby": "Ruby 3.0.2p107",
  656. "d": "D DMD 2.097.1",
  657. "go": "Go 1.16.6",
  658. "haskell": "Haskell 8.10.5",
  659. "scala": "Scala 2.13.6",
  660. "nim": "Nim 1.4.8",
  661. "rust": "Rust 1.53.0",
  662. "kotlin": "Kotlin 1.5.21",
  663. "scheme": "Scheme Gauche 0.9.10",
  664. "crystal": "Crystal 1.1.1",
  665. "swift": "Swift 5.4.2",
  666. "ocaml": "OCaml 4.12.0",
  667. "clojure": "Clojure 1.10.2.790",
  668. "fsharp": "F# 5.0",
  669. "elixir": "Elixir 1.7.4",
  670. "lua": "Lua LuaJIT 2.0.5",
  671. "fortran": "Fortran gFortran 8.4.1",
  672. "node": "JavaScript Node.js 15.5.0",
  673. "typescript": "TypeScript 4.3.5",
  674. "lisp": "Lisp Common Lisp sbcl 2.1.6",
  675. "sml": "ML Standard ML MLton 20180207-6",
  676. "kuin": "Kuin KuinC++ v.2021.7.17",
  677. "vim": "Vim v8.2",
  678. "sh": "Bash 4.4.19",
  679. "nasm": "Assembler nasm 2.13.03",
  680. "clay": "cLay 20210917-1",
  681. "bf": "Brainfuck BFI 1.1",
  682. "Whitespace": "Whitespace 0.3",
  683. "text": "Text cat 8.3",
  684. };
  685. const language = new ObservableValue(langMap[eLang.val()]);
  686. eLang.on("change", () => {
  687. language.value = langMap[eLang.val()];
  688. });
  689. yukicoder = {
  690. name: "yukicoder",
  691. language,
  692. get sourceCode() {
  693. if (eSourceObject.is(":visible"))
  694. return eSourceObject.val();
  695. return editor.getSession().getValue();
  696. },
  697. set sourceCode(sourceCode) {
  698. eSourceObject.val(sourceCode);
  699. editor.getSession().setValue(sourceCode);
  700. },
  701. submit() {
  702. doc.querySelector(`#submit_form input[type="submit"]`).click();
  703. },
  704. get testButtonContainer() {
  705. return doc.querySelector("#submit_form");
  706. },
  707. get sideButtonContainer() {
  708. return doc.querySelector("#toggle_source_editor").parentElement;
  709. },
  710. get bottomMenuContainer() {
  711. return doc.body;
  712. },
  713. get resultListContainer() {
  714. return doc.querySelector("#content");
  715. },
  716. get testCases() {
  717. const testCases = [];
  718. let sampleId = 1;
  719. for (let i = 0; i < eSamples.length; i++) {
  720. const eSample = eSamples.eq(i);
  721. const [eInput, eOutput] = eSample.find("pre");
  722. testCases.push({
  723. title: `Sample ${sampleId++}`,
  724. input: eInput.textContent,
  725. output: eOutput.textContent,
  726. anchor: eSample.find("button")[0],
  727. });
  728. }
  729. return testCases;
  730. },
  731. };
  732. }
  733. if (location.host == "yukicoder.me")
  734. init();
  735. var yukicoder$1 = yukicoder;
  736.  
  737. const site = atcoder$1 || yukicoder$1;
  738.  
  739. const runners = {
  740. "C GCC 10.1.0 Wandbox": new WandboxRunner("gcc-10.1.0-c", "C (GCC 10.1.0)"),
  741. "C C17 Clang 10.0.0 paiza.io": new PaizaIORunner("c", "C (C17 / Clang 10.0.0)"),
  742. "C++ GCC 10.1.0 + Boost 1.73.0 + ACL Wandbox": new WandboxCppRunner("gcc-10.1.0", "C++ (GCC 10.1.0) + ACL", { options: "warning,boost-1.73.0-gcc-9.2.0,gnu++17" }),
  743. "C++ Clang 10.0.0 + ACL Wandbox": new WandboxCppRunner("clang-10.0.0", "C++ (Clang 10.0.0) + ACL", { options: "warning,boost-nothing-clang-10.0.0,c++17" }),
  744. "Python3 CPython 3.8.2 paiza.io": new PaizaIORunner("python3", "Python (3.8.2)"),
  745. "Python3 Brython": brythonRunner,
  746. "Bash 5.0.17 paiza.io": new PaizaIORunner("bash", "Bash (5.0.17)"),
  747. "C# .NET Core 6.0.100-alpha.1.20562.2 Wandbox": new WandboxRunner("csharp", "C# (.NET Core 6.0.100-alpha.1.20562.2)"),
  748. "C# Mono-mcs HEAD Wandbox": new WandboxRunner("mono-head", "C# (Mono-mcs HEAD)"),
  749. "Clojure 1.10.1-1 paiza.io": new PaizaIORunner("clojure", "Clojure (1.10.1-1)"),
  750. "D LDC 1.23.0 paiza.io": new PaizaIORunner("d", "D (LDC 1.23.0)"),
  751. "Erlang 10.6.4 paiza.io": new PaizaIORunner("erlang", "Erlang (10.6.4)"),
  752. "Elixir 1.10.4 paiza.io": new PaizaIORunner("elixir", "Elixir (1.10.4)"),
  753. "F# Interactive 4.0 paiza.io": new PaizaIORunner("fsharp", "F# (Interactive 4.0)"),
  754. "Go 1.14.1 Wandbox": new WandboxRunner("go-1.14.1", "Go (1.14.1)"),
  755. "Haskell GHC HEAD Wandbox": new WandboxRunner("ghc-head", "Haskell (GHC HEAD)"),
  756. "JavaScript Node.js paiza.io": new PaizaIORunner("javascript", "JavaScript (Node.js 12.18.3)"),
  757. "Kotlin 1.4.0 paiza.io": new PaizaIORunner("kotlin", "Kotlin (1.4.0)"),
  758. "Lua 5.3.4 Wandbox": new WandboxRunner("lua-5.3.4", "Lua (Lua 5.3.4)"),
  759. "Lua LuaJIT HEAD Wandbox": new WandboxRunner("luajit-head", "Lua (LuaJIT HEAD)"),
  760. "Nim 1.0.6 Wandbox": new WandboxRunner("nim-1.0.6", "Nim (1.0.6)"),
  761. "Objective-C Clang 10.0.0 paiza.io": new PaizaIORunner("objective-c", "Objective-C (Clang 10.0.0)"),
  762. "Ocaml HEAD Wandbox": new WandboxRunner("ocaml-head", "OCaml (HEAD)"),
  763. "Pascal FPC 3.0.2 Wandbox": new WandboxRunner("fpc-3.0.2", "Pascal (FPC 3.0.2)"),
  764. "Perl 5.30.0 paiza.io": new PaizaIORunner("perl", "Perl (5.30.0)"),
  765. "PHP 7.4.10 paiza.io": new PaizaIORunner("php", "PHP (7.4.10)"),
  766. "PHP 7.3.3 Wandbox": new WandboxRunner("php-7.3.3", "PHP (7.3.3)"),
  767. "Python PyPy HEAD Wandbox": new WandboxRunner("pypy-head", "PyPy2 (HEAD)"),
  768. "Python3 PyPy3 7.2.0 Wandbox": new WandboxRunner("pypy-7.2.0-3", "PyPy3 (7.2.0)"),
  769. "Ruby 2.7.1 paiza.io": new PaizaIORunner("ruby", "Ruby (2.7.1)"),
  770. "Ruby HEAD Wandbox": new WandboxRunner("ruby-head", "Ruby (HEAD)"),
  771. "Ruby 2.7.1 Wandbox": new WandboxRunner("ruby-2.7.1", "Ruby (2.7.1)"),
  772. "Rust 1.42.0 AtCoder": new AtCoderRunner("4050", "Rust (1.42.0)"),
  773. "Rust HEAD Wandbox": new WandboxRunner("rust-head", "Rust (HEAD)"),
  774. "Rust 1.43.0 paiza.io": new PaizaIORunner("rust", "Rust (1.43.0)"),
  775. "Scala 2.13.3 paiza": new PaizaIORunner("scala", "Scala (2.13.3)"),
  776. "Scheme Gauche 0.9.6 paiza.io": new PaizaIORunner("scheme", "Scheme (Gauche 0.9.6)"),
  777. "Swift 5.2.5 paiza.io": new PaizaIORunner("swift", "Swift (5.2.5)"),
  778. "Text local": new CustomRunner("Text", async (sourceCode, input) => {
  779. return {
  780. status: "OK",
  781. exitCode: "0",
  782. input,
  783. output: sourceCode,
  784. };
  785. }),
  786. "Basic Visual Basic .NET Core 4.0.1 paiza.io": new PaizaIORunner("vb", "Visual Basic (.NET Core 4.0.1)"),
  787. "COBOL Free OpenCOBOL 2.2.0 paiza.io": new PaizaIORunner("cobol", "COBOL - Free (OpenCOBOL 2.2.0)"),
  788. "COBOL Fixed OpenCOBOL 1.1.0 AtCoder": new AtCoderRunner("4060", "COBOL - Fixed (OpenCOBOL 1.1.0)"),
  789. "COBOL Free OpenCOBOL 1.1.0 AtCoder": new AtCoderRunner("4061", "COBOL - Free (OpenCOBOL 1.1.0)"),
  790. "C++ GCC 9.2.0 + ACL Wandbox": new WandboxCppRunner("gcc-9.2.0", "C++ (GCC 9.2.0) + ACL"),
  791. };
  792. if (site.name == "AtCoder") {
  793. // AtCoderRunner がない場合は、追加する
  794. for (const e of document.querySelectorAll("#select-lang option[value]")) {
  795. const m = e.textContent.match(/([^ ]+) \(([^)]+)\)/);
  796. if (m) {
  797. const name = `${m[1]} ${m[2]} AtCoder`;
  798. const languageId = e.value;
  799. runners[name] = new AtCoderRunner(languageId, e.textContent);
  800. }
  801. }
  802. }
  803. console.info("AtCoder Easy Test: codeRunner OK");
  804. var codeRunner = {
  805. // 指定した環境でコードを実行する
  806. run(languageId, sourceCode, input, expectedOutput, options = { trim: true, split: true }) {
  807. // CodeRunner が存在しない言語ID
  808. if (!(languageId in runners))
  809. return Promise.reject("Language not supported");
  810. // 最後に実行したコードを保存
  811. codeSaver.save(sourceCode);
  812. // 実行
  813. return runners[languageId].test(sourceCode, input, expectedOutput, options);
  814. },
  815. // 環境の名前の一覧を取得する
  816. async getEnvironment(languageId) {
  817. const langs = similarLangs(languageId, Object.keys(runners));
  818. if (langs.length == 0)
  819. throw `Undefined language: ${languageId}`;
  820. return langs.map(lang => [lang, runners[lang].label]);
  821. },
  822. };
  823.  
  824. var hBottomMenu = "<div id=\"bottom-menu-wrapper\" class=\"navbar navbar-default navbar-fixed-bottom\">\n <div class=\"container\">\n <div class=\"navbar-header\">\n <button id=\"bottom-menu-key\" type=\"button\" class=\"navbar-toggle collapsed glyphicon glyphicon-menu-down\" data-toggle=\"collapse\" data-target=\"#bottom-menu\"></button>\n </div>\n <div id=\"bottom-menu\" class=\"collapse navbar-collapse\">\n <ul id=\"bottom-menu-tabs\" class=\"nav nav-tabs\"></ul>\n <div id=\"bottom-menu-contents\" class=\"tab-content\"></div>\n </div>\n </div>\n</div>";
  825.  
  826. var hStyle$1 = "<style>\n#bottom-menu-wrapper {\n background: transparent;\n border: none;\n pointer-events: none;\n padding: 0;\n}\n\n#bottom-menu-wrapper>.container {\n position: absolute;\n bottom: 0;\n width: 100%;\n padding: 0;\n}\n\n#bottom-menu-wrapper>.container>.navbar-header {\n float: none;\n}\n\n#bottom-menu-key {\n display: block;\n float: none;\n margin: 0 auto;\n padding: 10px 3em;\n border-radius: 5px 5px 0 0;\n background: #000;\n opacity: 0.5;\n color: #FFF;\n cursor: pointer;\n pointer-events: auto;\n text-align: center;\n}\n\n@media screen and (max-width: 767px) {\n #bottom-menu-key {\n opacity: 0.25;\n }\n}\n\n#bottom-menu-key.collapsed:before {\n content: \"\\e260\";\n}\n\n#bottom-menu-tabs {\n padding: 3px 0 0 10px;\n cursor: n-resize;\n}\n\n#bottom-menu-tabs a {\n pointer-events: auto;\n}\n\n#bottom-menu {\n pointer-events: auto;\n background: rgba(0, 0, 0, 0.8);\n color: #fff;\n max-height: unset;\n}\n\n#bottom-menu.collapse:not(.in) {\n display: none !important;\n}\n\n#bottom-menu-tabs>li>a {\n background: rgba(150, 150, 150, 0.5);\n color: #000;\n border: solid 1px #ccc;\n filter: brightness(0.75);\n}\n\n#bottom-menu-tabs>li>a:hover {\n background: rgba(150, 150, 150, 0.5);\n border: solid 1px #ccc;\n color: #111;\n filter: brightness(0.9);\n}\n\n#bottom-menu-tabs>li.active>a {\n background: #eee;\n border: solid 1px #ccc;\n color: #333;\n filter: none;\n}\n\n.bottom-menu-btn-close {\n font-size: 8pt;\n vertical-align: baseline;\n padding: 0 0 0 6px;\n margin-right: -6px;\n}\n\n#bottom-menu-contents {\n padding: 5px 15px;\n max-height: 50vh;\n overflow-y: auto;\n}\n\n#bottom-menu-contents .panel {\n color: #333;\n}\n</style>";
  827.  
  828. const style = html2element(hStyle$1);
  829. const bottomMenu = html2element(hBottomMenu);
  830. unsafeWindow.document.head.appendChild(style);
  831. site.bottomMenuContainer.appendChild(bottomMenu);
  832. const bottomMenuKey = bottomMenu.querySelector("#bottom-menu-key");
  833. const bottomMenuTabs = bottomMenu.querySelector("#bottom-menu-tabs");
  834. const bottomMenuContents = bottomMenu.querySelector("#bottom-menu-contents");
  835. // メニューのリサイズ
  836. {
  837. let resizeStart = null;
  838. const onStart = (event) => {
  839. const target = event.target;
  840. const pageY = event.pageY;
  841. if (target.id != "bottom-menu-tabs")
  842. return;
  843. resizeStart = { y: pageY, height: bottomMenuContents.getBoundingClientRect().height };
  844. };
  845. const onMove = (event) => {
  846. if (!resizeStart)
  847. return;
  848. event.preventDefault();
  849. bottomMenuContents.style.height = `${resizeStart.height - (event.pageY - resizeStart.y)}px`;
  850. };
  851. const onEnd = () => {
  852. resizeStart = null;
  853. };
  854. bottomMenuTabs.addEventListener("mousedown", onStart);
  855. bottomMenuTabs.addEventListener("mousemove", onMove);
  856. bottomMenuTabs.addEventListener("mouseup", onEnd);
  857. bottomMenuTabs.addEventListener("mouseleave", onEnd);
  858. }
  859. let tabs = new Set();
  860. let selectedTab = null;
  861. /** 下メニューの操作 */
  862. const menuController = {
  863. /** タブを選択 */
  864. selectTab(tabId) {
  865. const tab = unsafeWindow.$(`#bottom-menu-tab-${tabId}`);
  866. if (tab && tab[0]) {
  867. tab.tab("show"); // Bootstrap 3
  868. selectedTab = tabId;
  869. }
  870. },
  871. /** 下メニューにタブを追加する */
  872. addTab(tabId, tabLabel, paneContent, options = {}) {
  873. console.log(`AtCoder Easy Test: addTab: ${tabLabel} (${tabId})`, paneContent);
  874. // タブを追加
  875. const tab = document.createElement("a");
  876. tab.textContent = tabLabel;
  877. tab.id = `bottom-menu-tab-${tabId}`;
  878. tab.href = "#";
  879. tab.dataset.target = `#bottom-menu-pane-${tabId}`;
  880. tab.dataset.toggle = "tab";
  881. tab.addEventListener("click", event => {
  882. event.preventDefault();
  883. menuController.selectTab(tabId);
  884. });
  885. const tabLi = document.createElement("li");
  886. tabLi.appendChild(tab);
  887. bottomMenuTabs.appendChild(tabLi);
  888. // 内容を追加
  889. const pane = document.createElement("div");
  890. pane.className = "tab-pane";
  891. pane.id = `bottom-menu-pane-${tabId}`;
  892. pane.appendChild(paneContent);
  893. bottomMenuContents.appendChild(pane);
  894. const controller = {
  895. get id() {
  896. return tabId;
  897. },
  898. close() {
  899. bottomMenuTabs.removeChild(tabLi);
  900. bottomMenuContents.removeChild(pane);
  901. tabs.delete(tab);
  902. if (selectedTab == tabId) {
  903. selectedTab = null;
  904. if (tabs.size > 0) {
  905. menuController.selectTab(tabs.values().next().value.id);
  906. }
  907. }
  908. },
  909. show() {
  910. menuController.show();
  911. menuController.selectTab(tabId);
  912. },
  913. set color(color) {
  914. tab.style.backgroundColor = color;
  915. },
  916. };
  917. // 選択されているタブがなければ選択
  918. if (!selectedTab)
  919. menuController.selectTab(tabId);
  920. return controller;
  921. },
  922. /** 下メニューを表示する */
  923. show() {
  924. if (bottomMenuKey.classList.contains("collapsed"))
  925. bottomMenuKey.click();
  926. },
  927. /** 下メニューの表示/非表示を切り替える */
  928. toggle() {
  929. bottomMenuKey.click();
  930. },
  931. };
  932. console.info("AtCoder Easy Test: bottomMenu OK");
  933.  
  934. var hRowTemplate = "<div class=\"atcoder-easy-test-cases-row alert alert-dismissible\">\n <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"close\">\n <span aria-hidden=\"true\">×</span>\n </button>\n <div class=\"progress\">\n <div class=\"progress-bar\" style=\"width: 0%;\">0 / 0</div>\n </div>\n <!--div class=\"label label-default label-warning\" style=\"margin: 3px; cursor: pointer;\">WA</div>\n <div class=\"label label-default label-warning\" style=\"margin: 3px; cursor: pointer;\">WA</div>\n <div class=\"label label-default label-warning\" style=\"margin: 3px; cursor: pointer;\">WA</div-->\n</div>";
  935.  
  936. class ResultRow {
  937. _tabs;
  938. _element;
  939. _promise;
  940. constructor(pairs) {
  941. this._tabs = pairs.map(([_, tab]) => tab);
  942. this._element = html2element(hRowTemplate);
  943. const numCases = pairs.length;
  944. let numFinished = 0;
  945. let numAccepted = 0;
  946. const progressBar = this._element.querySelector(".progress-bar");
  947. progressBar.textContent = `${numFinished} / ${numCases}`;
  948. this._promise = Promise.all(pairs.map(([pResult, tab]) => {
  949. const button = html2element(`<div class="label label-default" style="margin: 3px; cursor: pointer;">WJ</div>`);
  950. button.addEventListener("click", () => {
  951. tab.show();
  952. });
  953. this._element.appendChild(button);
  954. return pResult.then(result => {
  955. button.textContent = result.status;
  956. if (result.status == "AC") {
  957. button.classList.add("label-success");
  958. }
  959. else if (result.status != "OK") {
  960. button.classList.add("label-warning");
  961. }
  962. numFinished++;
  963. if (result.status == "AC")
  964. numAccepted++;
  965. progressBar.textContent = `${numFinished} / ${numCases}`;
  966. progressBar.style.width = `${100 * numFinished / numCases}%`;
  967. if (numFinished == numCases) {
  968. if (numAccepted == numCases)
  969. this._element.classList.add("alert-success");
  970. else
  971. this._element.classList.add("alert-warning");
  972. }
  973. }).catch(reason => {
  974. button.textContent = "IE";
  975. button.classList.add("label-danger");
  976. console.error(reason);
  977. });
  978. }));
  979. }
  980. get element() {
  981. return this._element;
  982. }
  983. onFinish(listener) {
  984. this._promise.then(listener);
  985. }
  986. remove() {
  987. for (const tab of this._tabs)
  988. tab.close();
  989. const parent = this._element.parentElement;
  990. if (parent)
  991. parent.removeChild(this._element);
  992. }
  993. }
  994.  
  995. var hResultList = "<div class=\"row\"></div>";
  996.  
  997. const eResultList = html2element(hResultList);
  998. site.resultListContainer.appendChild(eResultList);
  999. const resultList = {
  1000. addResult(pairs) {
  1001. const result = new ResultRow(pairs);
  1002. eResultList.insertBefore(result.element, eResultList.firstChild);
  1003. return result;
  1004. },
  1005. };
  1006.  
  1007. let data = {};
  1008. function toString() {
  1009. return JSON.stringify(data);
  1010. }
  1011. function save() {
  1012. localStorage.setItem("AtCoderEasyTest", toString());
  1013. }
  1014. function load() {
  1015. if ("AtCoderEasyTest" in localStorage) {
  1016. data = JSON.parse(localStorage.getItem("AtCoderEasyTest"));
  1017. }
  1018. }
  1019. load();
  1020. const config = {
  1021. get(key, defaultValue = "") {
  1022. if (!(key in data))
  1023. config.set(key, defaultValue);
  1024. return data[key];
  1025. },
  1026. set(key, value) {
  1027. data[key] = value;
  1028. save();
  1029. },
  1030. has(key) {
  1031. return key in data;
  1032. },
  1033. getAsJSON(key, defaultValue = null) {
  1034. if (!(key in data))
  1035. config.setAsJSON(key, defaultValue);
  1036. return JSON.parse(data[key]);
  1037. },
  1038. setAsJSON(key, value) {
  1039. config.set(key, JSON.stringify(value));
  1040. },
  1041. save,
  1042. load,
  1043. toString,
  1044. };
  1045.  
  1046. var hTabTemplate = "<div class=\"atcoder-easy-test-result container\">\n <div class=\"row\">\n <div class=\"atcoder-easy-test-result-col-input col-xs-12\" data-if-expected-output=\"col-sm-6 col-sm-push-6\">\n <div class=\"form-group\">\n <label class=\"control-label col-xs-12\">\n Standard Input\n <div class=\"col-xs-12\">\n <textarea class=\"atcoder-easy-test-result-input form-control\" rows=\"3\" readonly=\"readonly\"></textarea>\n </div>\n </label>\n </div>\n </div>\n <div class=\"atcoder-easy-test-result-col-expected-output col-xs-12 col-sm-6 hidden\" data-if-expected-output=\"!hidden col-sm-pull-6\">\n <div class=\"form-group\">\n <label class=\"control-label col-xs-12\">\n Expected Output\n <div class=\"col-xs-12\">\n <textarea class=\"atcoder-easy-test-result-expected-output form-control\" rows=\"3\" readonly=\"readonly\"></textarea>\n </div>\n </label>\n </div>\n </div>\n </div>\n <div class=\"row\"><div class=\"col-sm-6 col-sm-offset-3\">\n <div class=\"panel panel-default\">\n <table class=\"table table-condensed\">\n <tbody>\n <tr>\n <th class=\"text-center\">Exit Code</th>\n <th class=\"text-center\">Exec Time</th>\n <th class=\"text-center\">Memory</th>\n </tr>\n <tr>\n <td class=\"atcoder-easy-test-result-exit-code text-center\"></td>\n <td class=\"atcoder-easy-test-result-exec-time text-center\"></td>\n <td class=\"atcoder-easy-test-result-memory text-center\"></td>\n </tr>\n </tbody>\n </table>\n </div>\n </div></div>\n <div class=\"row\">\n <div class=\"atcoder-easy-test-result-col-output col-xs-12\" data-if-error=\"col-md-6\">\n <div class=\"form-group\">\n <label class=\"control-label col-xs-12\">\n Standard Output\n <div class=\"col-xs-12\">\n <textarea class=\"atcoder-easy-test-result-output form-control\" rows=\"5\" readonly=\"readonly\"></textarea>\n </div>\n </label>\n </div>\n </div>\n <div class=\"atcoder-easy-test-result-col-error col-xs-12 col-md-6 hidden\" data-if-error=\"!hidden\">\n <div class=\"form-group\">\n <label class=\"control-label col-xs-12\">\n Standard Error\n <div class=\"col-xs-12\">\n <textarea class=\"atcoder-easy-test-result-error form-control\" rows=\"5\" readonly=\"readonly\"></textarea>\n </div>\n </label>\n </div>\n </div>\n </div>\n</div>";
  1047.  
  1048. function setClassFromData(element, name) {
  1049. const classes = element.dataset[name].split(/\s+/);
  1050. for (let className of classes) {
  1051. let flag = true;
  1052. if (className[0] == "!") {
  1053. className = className.slice(1);
  1054. flag = false;
  1055. }
  1056. element.classList.toggle(className, flag);
  1057. }
  1058. }
  1059. class ResultTabContent {
  1060. _title;
  1061. _uid;
  1062. _element;
  1063. _result;
  1064. constructor() {
  1065. this._uid = Date.now().toString(16);
  1066. this._result = null;
  1067. this._element = html2element(hTabTemplate);
  1068. this._element.id = `atcoder-easy-test-result-${this._uid}`;
  1069. }
  1070. set result(result) {
  1071. this._result = result;
  1072. if (result.status == "AC") {
  1073. this.outputStyle.backgroundColor = "#dff0d8";
  1074. }
  1075. else if (result.status != "OK") {
  1076. this.outputStyle.backgroundColor = "#fcf8e3";
  1077. }
  1078. this.input = result.input;
  1079. if ("expectedOutput" in result)
  1080. this.expectedOutput = result.expectedOutput;
  1081. this.exitCode = result.exitCode;
  1082. if ("execTime" in result)
  1083. this.execTime = `${result.execTime} ms`;
  1084. if ("memory" in result)
  1085. this.memory = `${result.memory} KB`;
  1086. if ("output" in result)
  1087. this.output = result.output;
  1088. if (result.error)
  1089. this.error = result.error;
  1090. }
  1091. get result() {
  1092. return this._result;
  1093. }
  1094. get uid() {
  1095. return this._uid;
  1096. }
  1097. get element() {
  1098. return this._element;
  1099. }
  1100. set title(title) {
  1101. this._title = title;
  1102. }
  1103. get title() {
  1104. return this._title;
  1105. }
  1106. set input(input) {
  1107. this._get("input").value = input;
  1108. }
  1109. get inputStyle() {
  1110. return this._get("input").style;
  1111. }
  1112. set expectedOutput(output) {
  1113. this._get("expected-output").value = output;
  1114. setClassFromData(this._get("col-input"), "ifExpectedOutput");
  1115. setClassFromData(this._get("col-expected-output"), "ifExpectedOutput");
  1116. }
  1117. get expectedOutputStyle() {
  1118. return this._get("expected-output").style;
  1119. }
  1120. set output(output) {
  1121. this._get("output").value = output;
  1122. }
  1123. get outputStyle() {
  1124. return this._get("output").style;
  1125. }
  1126. set error(error) {
  1127. this._get("error").value = error;
  1128. setClassFromData(this._get("col-output"), "ifError");
  1129. setClassFromData(this._get("col-error"), "ifError");
  1130. }
  1131. set exitCode(code) {
  1132. const element = this._get("exit-code");
  1133. element.textContent = code;
  1134. const isSuccess = code == "0";
  1135. element.classList.toggle("bg-success", isSuccess);
  1136. element.classList.toggle("bg-danger", !isSuccess);
  1137. }
  1138. set execTime(time) {
  1139. this._get("exec-time").textContent = time;
  1140. }
  1141. set memory(memory) {
  1142. this._get("memory").textContent = memory;
  1143. }
  1144. _get(name) {
  1145. return this._element.querySelector(`.atcoder-easy-test-result-${name}`);
  1146. }
  1147. }
  1148.  
  1149. var hRoot = "<form id=\"atcoder-easy-test-container\" class=\"form-horizontal\">\n <small style=\"position: absolute; display: block; bottom: 0; right: 0; padding: 1% 4%; width: 95%; text-align: right;\">AtCoder Easy Test v<span id=\"atcoder-easy-test-version\"></span></small>\n <div class=\"row\">\n <div class=\"col-xs-12 col-lg-8\">\n <div class=\"form-group\">\n <label class=\"control-label col-sm-2\">Test Environment</label>\n <div class=\"col-sm-10\">\n <select class=\"form-control\" id=\"atcoder-easy-test-language\"></select>\n </div>\n </div>\n <div class=\"form-group\">\n <label class=\"control-label col-sm-2\" for=\"atcoder-easy-test-input\">Standard Input</label>\n <div class=\"col-sm-10\">\n <textarea id=\"atcoder-easy-test-input\" name=\"input\" class=\"form-control\" rows=\"3\"></textarea>\n </div>\n </div>\n </div>\n <div class=\"col-xs-12 col-lg-4\">\n <details close>\n <summary>Expected Output</summary>\n <div class=\"form-group\">\n <label class=\"control-label col-sm-2\" for=\"atcoder-easy-test-allowable-error-check\">Allowable Error</label>\n <div class=\"col-sm-10\">\n <div class=\"input-group\">\n <span class=\"input-group-addon\">\n <input id=\"atcoder-easy-test-allowable-error-check\" type=\"checkbox\" checked=\"checked\">\n </span>\n <input id=\"atcoder-easy-test-allowable-error\" type=\"text\" class=\"form-control\" value=\"1e-6\">\n </div>\n </div>\n </div>\n <div class=\"form-group\">\n <label class=\"control-label col-sm-2\" for=\"atcoder-easy-test-output\">Expected Output</label>\n <div class=\"col-sm-10\">\n <textarea id=\"atcoder-easy-test-output\" name=\"output\" class=\"form-control\" rows=\"3\"></textarea>\n </div>\n </div>\n </details>\n </div>\n <div class=\"col-xs-12\">\n <div class=\"col-xs-11 col-xs-offset=1\">\n <div class=\"form-group\">\n <a id=\"atcoder-easy-test-run\" class=\"btn btn-primary\">Run</a>\n </div>\n </div>\n </div>\n </div>\n <style>\n #atcoder-easy-test-language {\n border: none;\n background: transparent;\n font: inherit;\n color: #fff;\n }\n #atcoder-easy-test-language option {\n border: none;\n color: #333;\n font: inherit;\n }\n </style>\n</form>";
  1150.  
  1151. var hStyle = "<style>\n.atcoder-easy-test-result textarea {\n font-family: monospace;\n font-weight: normal;\n}\n</style>";
  1152.  
  1153. var hTestAndSubmit = "<a id=\"atcoder-easy-test-btn-test-and-submit\" class=\"btn btn-info btn\" style=\"margin-left: 1rem\" title=\"Ctrl+Enter\" data-toggle=\"tooltip\">Test &amp; Submit</a>";
  1154.  
  1155. var hTestAllSamples = "<a id=\"atcoder-easy-test-btn-test-all\" class=\"btn btn-default btn-sm\" style=\"margin-left: 1rem\" title=\"Alt+Enter\" data-toggle=\"tooltip\">Test All Samples</a>";
  1156.  
  1157. const doc = unsafeWindow.document;
  1158. // external interfaces
  1159. unsafeWindow.bottomMenu = menuController;
  1160. unsafeWindow.codeRunner = codeRunner;
  1161. doc.head.appendChild(html2element(hStyle));
  1162. // place "Easy Test" tab
  1163. {
  1164. // declare const hRoot: string;
  1165. const root = html2element(hRoot);
  1166. const E = (id) => root.querySelector(`#atcoder-easy-test-${id}`);
  1167. const eLanguage = E("language");
  1168. const eInput = E("input");
  1169. const eAllowableErrorCheck = E("allowable-error-check");
  1170. const eAllowableError = E("allowable-error");
  1171. const eOutput = E("output");
  1172. const eRun = E("run");
  1173. E("version").textContent = "2.1.0";
  1174. events.on("enable", () => {
  1175. eRun.classList.remove("disabled");
  1176. });
  1177. events.on("disable", () => {
  1178. eRun.classList.remove("enabled");
  1179. });
  1180. // 言語選択関係
  1181. {
  1182. eLanguage.addEventListener("change", () => {
  1183. const langSelection = config.getAsJSON("langSelection", {});
  1184. langSelection[site.language.value] = eLanguage.value;
  1185. config.setAsJSON("langSelection", langSelection);
  1186. });
  1187. async function setLanguage() {
  1188. const languageId = site.language.value;
  1189. while (eLanguage.firstChild)
  1190. eLanguage.removeChild(eLanguage.firstChild);
  1191. try {
  1192. const langs = await codeRunner.getEnvironment(languageId);
  1193. console.log(`language: ${langs[1]} (${langs[0]})`);
  1194. // add <option>
  1195. for (const [languageId, label] of langs) {
  1196. const option = document.createElement("option");
  1197. option.value = languageId;
  1198. option.textContent = label;
  1199. eLanguage.appendChild(option);
  1200. }
  1201. // load
  1202. const langSelection = config.getAsJSON("langSelection", {});
  1203. if (languageId in langSelection) {
  1204. const prev = langSelection[languageId];
  1205. const [lang, _] = langs.find(([lang, label]) => lang == prev);
  1206. if (lang)
  1207. eLanguage.value = lang;
  1208. }
  1209. events.trig("enable");
  1210. }
  1211. catch (error) {
  1212. console.log(`language: ? (${languageId})`);
  1213. console.error(error);
  1214. const option = document.createElement("option");
  1215. option.className = "fg-danger";
  1216. option.textContent = error;
  1217. eLanguage.appendChild(option);
  1218. events.trig("disable");
  1219. }
  1220. }
  1221. site.language.addListener(() => setLanguage());
  1222. eAllowableError.disabled = !eAllowableErrorCheck.checked;
  1223. eAllowableErrorCheck.addEventListener("change", event => {
  1224. eAllowableError.disabled = !eAllowableErrorCheck.checked;
  1225. });
  1226. }
  1227. let runId = 0;
  1228. // テスト実行
  1229. function runTest(title, input, output = null) {
  1230. runId++;
  1231. events.trig("disable");
  1232. const options = { trim: true, split: true, };
  1233. if (eAllowableErrorCheck.checked) {
  1234. options.allowableError = parseFloat(eAllowableError.value);
  1235. }
  1236. const content = new ResultTabContent();
  1237. const tab = menuController.addTab("easy-test-result-" + content.uid, `#${runId} ${title}`, content.element, { active: true, closeButton: true });
  1238. const pResult = codeRunner.run(eLanguage.value, site.sourceCode, input, output, options);
  1239. pResult.then(result => {
  1240. content.result = result;
  1241. if (result.status == "AC") {
  1242. tab.color = "#dff0d8";
  1243. }
  1244. else if (result.status != "OK") {
  1245. tab.color = "#fcf8e3";
  1246. }
  1247. events.trig("enable");
  1248. });
  1249. return [pResult, tab];
  1250. }
  1251. function runAllCases(testcases) {
  1252. const pairs = testcases.map(testcase => runTest(testcase.title, testcase.input, testcase.output));
  1253. resultList.addResult(pairs);
  1254. return Promise.all(pairs.map(([pResult, _]) => pResult.then(result => {
  1255. if (result.status == "AC")
  1256. return Promise.resolve(result);
  1257. else
  1258. return Promise.reject(result);
  1259. })));
  1260. }
  1261. eRun.addEventListener("click", _ => {
  1262. const title = "Run";
  1263. const input = eInput.value;
  1264. const output = eOutput.value;
  1265. runTest(title, input, output || null);
  1266. });
  1267. menuController.addTab("easy-test", "Easy Test", root);
  1268. // place "Test & Submit" button
  1269. {
  1270. const button = html2element(hTestAndSubmit);
  1271. site.testButtonContainer.appendChild(button);
  1272. button.addEventListener("click", async () => {
  1273. await runAllCases(site.testCases);
  1274. site.submit();
  1275. });
  1276. }
  1277. // place "Test All Samples" button
  1278. {
  1279. const button = html2element(hTestAllSamples);
  1280. site.testButtonContainer.appendChild(button);
  1281. button.addEventListener("click", () => runAllCases(site.testCases));
  1282. }
  1283. }
  1284. // place "Restore Last Play" button
  1285. try {
  1286. const restoreButton = doc.createElement("a");
  1287. restoreButton.className = "btn btn-danger btn-sm";
  1288. restoreButton.textContent = "Restore Last Play";
  1289. restoreButton.addEventListener("click", async () => {
  1290. try {
  1291. const lastCode = await codeSaver.restore();
  1292. if (confirm("Your current code will be replaced. Are you sure?")) {
  1293. site.sourceCode = lastCode;
  1294. }
  1295. }
  1296. catch (reason) {
  1297. alert(reason);
  1298. }
  1299. });
  1300. site.sideButtonContainer.appendChild(restoreButton);
  1301. }
  1302. catch (e) {
  1303. console.error(e);
  1304. }
  1305. })();