def test_527(self): url = 'https://yukicoder.me/problems/527' filename = 'main.cpp' code = textwrap.dedent(r""" #include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; string s; cin >> s; cout << a + b << ' ' << s << endl; return 0; } """) with tempfile.TemporaryDirectory() as tempdir: path = pathlib.Path(tempdir) / filename with open(path, 'w') as fh: fh.write(code) language_id = main(['guess-language-id', '--file', str(path), url], debug=True)['result']['id'] data = main([ 'submit-code', '--file', str(path), '--language', language_id, url ], debug=True) self.assertEqual(data['status'], 'ok')
def test_contest_1080_a(self): url = 'https://codeforces.com/contest/1080/problem/A' expected = { "status": "ok", "messages": [], "result": { "url": "https://codeforces.com/contest/1080/problem/A", "tests": [{ "input": "3 5\n", "output": "10\n" }, { "input": "15 6\n", "output": "38\n" }], "name": "Petya and Origami", "context": { "contest": { "name": "Codeforces Round #524 (Div. 2)", "url": "https://codeforces.com/contest/1080" }, "alphabet": "A" } } } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_simple_language(self): url = 'http://golf.shinh.org/p.rb?simple+language' expected = { "status": "ok", "messages": [], "result": { "url": "http://golf.shinh.org/p.rb?simple+language", "tests": [ { "input": "2+2", "output": "4" }, { "input": "a:{x.2+x}\na2", "output": "4" }, { "input": "ack:{m n.if (m=0){n+1}{if (n=0){ack (m-1) 1}{ack (m-1) (ack m (n-1))}}}\nfact:{n.if (n=0){1}{n*(fact (n-1))}}\nack 3 4\nfact 4", "output": "125\n24" }, ], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_new_year_couple(self) -> None: url = 'https://toph.co/p/new-year-couple' expected = { "status": "ok", "messages": [], "result": { "url": "https://toph.co/p/new-year-couple", "tests": [ { "input": "8\r\nmrtarek 10 20\r\noptarkk 15 30\r\nmmtarqq 12 25\r\naatarcc 8 18\r\nheloptr 22 50\r\nmemopto 10 40\r\nkokoptz 15 45\r\nlmkoopa 5 90\r\n5\r\n3 5 20\r\n3 5 11\r\n3 5 16\r\n5 6 25\r\n5 6 16", "output": "3\r\n1\r\n6\r\n3\r\n1" }, { "input": "5\r\noookhzal 11 20\r\napkoptay 10 45\r\nappokyat 30 32\r\npopaozal 10 15\r\ntotkozal 15 25\r\n4\r\n6 8 15\r\n7 7 10\r\n3 5 25\r\n1 2 30", "output": "3\r\n1\r\n0\r\n1\r\n" }, ], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_100(self): """This tests about sample cases. """ url = 'http://yukicoder.me/problems/100' expected = { "status": "ok", "messages": [], "result": { "url": "https://yukicoder.me/problems/100", "tests": [{ "input": "30\n5\n", "output": "19\n" }, { "input": "100\n2\n", "output": "75\n" }, { "input": "100000\n1\n", "output": "100000\n" }], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_contest_1344_d(self): """The sample output of this problem has superfluous whitespaces. We should check that they are removed. .. seealso:: see https://github.com/online-judge-tools/api-client/issues/24 """ url = 'https://codeforces.com/contest/1334/problem/D' expected = { "status": "ok", "messages": [], "result": { "url": "https://codeforces.com/contest/1334/problem/D", "tests": [{ "input": "3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n", "output": "1 2 1\n1 3 2 3\n1\n" }], "name": "Minimum Euler Cycle", "context": { "contest": { "name": "Educational Codeforces Round 85 (Rated for Div. 2)", "url": "https://codeforces.com/contest/1334" }, "alphabet": "D" } }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_gym_101020_a(self): """This tests a problem in the gym. """ url = 'http://codeforces.com/gym/101020/problem/A' expected = { "status": "ok", "messages": [], "result": { "url": "https://codeforces.com/gym/101020/problem/A", "tests": [{ "input": "3\n5 4\n1 2\n33 33\n", "output": "20\n2\n1089\n" }], "name": "Jerry's Window", "context": { "contest": { "name": "2015 Syrian Private Universities Collegiate Programming Contest", "url": "https://codeforces.com/gym/101020" }, "alphabet": "A" } }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_contest_538_h(self): """This tests a old problem. """ url = 'http://codeforces.com/contest/538/problem/H' expected = { "status": "ok", "messages": [], "result": { "url": "https://codeforces.com/contest/538/problem/H", "tests": [{ "input": "10 20\n3 0\n3 6\n4 9\n16 25\n", "output": "POSSIBLE\n4 16\n112\n" }, { "input": "1 10\n3 3\n0 10\n0 10\n0 10\n1 2\n1 3\n2 3\n", "output": "IMPOSSIBLE\n" }], "name": "Summer Dichotomy", "context": { "contest": { "name": "Codeforces Round #300", "url": "https://codeforces.com/contest/538" }, "alphabet": "H" } }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_1371(self): url = 'http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1371' expected = { "status": "ok", "messages": [], "result": { "url": "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1371", "tests": [{ "input": "ACM\n", "output": "0\n" }, { "input": "icpc\n", "output": "1\n" }, { "input": "BAYL0R\n", "output": "3\n" }, { "input": "-AB+AC-A\n", "output": "1\n" }, { "input": "abcdefghi\n", "output": "0\n" }, { "input": "111-10=1+10*10\n", "output": "1\n" }, { "input": "0=10-1\n", "output": "0\n" }], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_contest_1080_a(self): """Recent (Nov 2018) problems has leading spaces for sample cases. We should check that they are removed. .. seealso:: https://github.com/online-judge-tools/oj/issues/198 """ url = 'https://codeforces.com/contest/1080/problem/A' expected = { "status": "ok", "messages": [], "result": { "url": "https://codeforces.com/contest/1080/problem/A", "tests": [{ "input": "3 5\n", "output": "10\n" }, { "input": "15 6\n", "output": "38\n" }], "name": "Petya and Origami", "context": { "contest": { "name": "Codeforces Round #524 (Div. 2)", "url": "https://codeforces.com/contest/1080" }, "alphabet": "A" } }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_ITP1_1_B(self): url = 'http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_B' expected = { "status": "ok", "messages": [], "result": { "url": "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_B", "tests": [{ "input": "1\n", "output": "1\n", "name": "test1" }, { "input": "3\n", "output": "27\n", "name": "test2" }, { "input": "64\n", "output": "262144\n", "name": "test3" }, { "input": "100\n", "output": "1000000\n", "name": "test4" }], "context": {} }, } actual = main(['get-problem', '--system', url], debug=True) self.assertEqual(expected, actual)
def test_10760(self): # `double` is used and one of values is a scientific form `1.0E50`. url = 'https://community.topcoder.com/stat?c=problem_statement&pm=10760' expected = { "status": "ok", "messages": [], "result": { "url": "https://community.topcoder.com/stat?c=problem_statement&pm=10760", "tests": [{ "input": "2 5 8\n", "output": "40.0\n" }, { "input": "2 1.5 1.8\n", "output": "3.3\n" }, { "input": "3 8.26 7.54 3.2567\n", "output": "202.82857868\n" }, { "input": "4 1.5 1.7 1.6 1.5\n", "output": "9.920000000000002\n" }, { "input": "50 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10\n", "output": "1.0E50\n" }], "name": "Nisoku" }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_2310(self): url = 'http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2310&lang=jp' expected = { "status": "ok", "messages": [], "result": { "url": "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2310", "tests": [{ "input": "3 5\n##..#\n#..##\n####.\n", "output": "4\n" }, { "input": "3 3\n#.#\n###\n#.#\n", "output": "3\n" }, { "input": "10 9\n.........\n.........\n####.....\n#..#.##..\n#..#..#..\n#..##.###\n#..##...#\n##..##.##\n###.#..#.\n###.####.\n", "output": "6\n" }, { "input": "10 11\n###########\n#.#.#.#.#.#\n#.#.#.#.#.#\n#.#.#.#.#.#\n#.#.#.#.#.#\n#.#.#.#.#.#\n#.#.#.#.#.#\n#.#.#.#.#.#\n#.#.#.#.#.#\n#.#.#.#.#.#\n", "output": "7\n" }, { "input": "25 38\n...........#...............#..........\n...........###..........####..........\n...........#####.......####...........\n............###############...........\n............###############...........\n............##############............\n.............#############............\n............###############...........\n...........#################..........\n.......#...#################...#......\n.......##.##################..##......\n........####################.##.......\n..........####################........\n.........#####..########..#####.......\n.......######....#####....######......\n......########...#####..########.#....\n....#######..#...#####..#..########...\n..#########.....#######......#######..\n...#######......#######........###....\n..####.........#########........###...\n...............#########..............\n..............##########..............\n..............##########..............\n...............########...............\n...............########...............\n", "output": "8\n" }], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_k_swap(self) -> None: url = 'https://csacademy.com/contest/round-39/task/k-swap/' expected = { "status": "ok", "messages": [], "result": { "url": "https://csacademy.com/contest/round-39/task/k-swap/", "tests": [ { "input": "4 2\n4 3 2 1\n", "output": "2 3 4 1 " }, { "input": "7 2\n4 3 2 1 2 3 4\n", "output": "2 2 3 3 4 1 4 " }, { "input": "10 2\n4 3 2 1 2 3 4 3 2 1\n", "output": "2 2 2 3 3 3 4 1 4 1 " }, ], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_abc114_c(self): """This tests a problem which uses a new-style format HTML. """ url = 'https://atcoder.jp/contests/abc114/tasks/abc114_c' expected = { "status": "ok", "messages": [], "result": { "url": "https://atcoder.jp/contests/abc114/tasks/abc114_c", "tests": [{ "input": "575\n", "output": "4\n" }, { "input": "3600\n", "output": "13\n" }, { "input": "999999999\n", "output": "26484\n" }], "name": "755", "context": { "contest": { "name": "AtCoder Beginner Contest 114", "url": "https://atcoder.jp/contests/abc114" }, "alphabet": "C" }, "memoryLimit": 1024, "timeLimit": 2000 }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_login_failure(self) -> None: url = 'https://codeforces.com/' expected = {'status': 'error', 'messages': ['onlinejudge.type.LoginError: Invalid handle or password.'], 'result': None} with update_environ(USERNAME='******', PASSWORD='******'): with temporary_cookie() as cookie_path: actual = main(['--cookie', str(cookie_path), 'login-service', url], debug=True) self.assertEqual(expected, actual)
def test_login_success(self) -> None: url = 'https://codeforces.com/' expected = {"status": "ok", "messages": [], "result": {"loggedIn": True}} with update_environ(USERNAME=os.environ[CODEFORCES_USERNAME], PASSWORD=os.environ[CODEFORCES_PASSWORD]): with temporary_cookie() as cookie_path: actual = main(['--cookie', str(cookie_path), 'login-service', url], debug=True) self.assertEqual(expected, actual)
def test_login_failure(self) -> None: url = 'https://atcoder.jp/' expected = {"status": "error", "messages": ["onlinejudge.type.LoginError: failed to login"], "result": None} with update_environ(USERNAME='******', PASSWORD='******'): with temporary_cookie() as cookie_path: actual = main(['--cookie', str(cookie_path), 'login-service', url], debug=True) self.assertEqual(expected, actual)
def test_edu_2_2_1_a(self): """This tests an educational problem. Unlike other problems educational problems are only accessible if the user is logged in. The user for test is: https://codeforces.com/profile/online-judge-tools """ login_url = 'https://codeforces.com/' with update_environ(USERNAME=os.environ[CODEFORCES_USERNAME], PASSWORD=os.environ[CODEFORCES_PASSWORD]): with temporary_cookie() as cookie_path: main( ['--cookie', str(cookie_path), 'login-service', login_url], debug=True) url = 'https://codeforces.com/edu/course/2/lesson/2/1/practice/contest/269100/problem/A' expected = { "status": "ok", "messages": [], "result": { "url": "https://codeforces.com/edu/course/2/lesson/2/1/practice/contest/269100/problem/A", "tests": [{ "input": "ababba\n", "output": "6 5 0 2 4 1 3\n" }, { "input": "aaaa\n", "output": "4 3 2 1 0\n" }, { "input": "ppppplppp\n", "output": "9 5 8 4 7 3 6 2 1 0\n" }, { "input": "nn\n", "output": "2 1 0\n" }], "context": {} }, } actual = main( ['--cookie', str(cookie_path), 'get-problem', url], debug=True) self.assertEqual(expected, actual)
def is_logged_in(url: str, *, memo={}) -> bool: # functools.lru_cache is unusable since Service are unhashable, so we need to use `memo={}`. service = dispatch.service_from_url(url) assert service is not None url = service.get_url() # normalize url if url not in memo: # We need to use main instead of `service.is_logged_in()` to use cookies. result = main(['login-service', '--check', url], debug=True) memo[url] = bool((result.get('result') or {}).get('loggedIn')) return memo[url]
def test_9000(self): url = 'https://yukicoder.me/problems/no/9000' filename = 'main.py' code = textwrap.dedent(r""" #!/usr/bin/env python3 print "Hello World!" """) with tempfile.TemporaryDirectory() as tempdir: path = pathlib.Path(tempdir) / filename with open(path, 'w') as fh: fh.write(code) language_id = main(['guess-language-id', '--file', str(path), url], debug=True)['result']['id'] data = main([ 'submit-code', '--file', str(path), '--language', language_id, url ], debug=True) self.assertEqual(data['status'], 'ok')
def test_non_existing_problem(self): """This tests an non-existing problem. """ url = 'http://abc001.contest.atcoder.jp/tasks/abc001_100' expected = { "status": "error", "messages": ["requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://atcoder.jp/contests/abc001/tasks/abc001_100"], "result": None, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_impossible_problem(self): """This tests a problem impossible to parse sample cases. """ url = 'https://chokudai001.contest.atcoder.jp/tasks/chokudai_001_a' expected = { "status": "error", "messages": ["onlinejudge.type.SampleParseError: failed to parse samples"], "result": None, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_hourrank_30_video_conference(self): url = 'https://www.hackerrank.com/contests/101hack54/challenges/weather-forecast-quality' expected = { "status": "ok", "messages": [], "result": { "url": "https://www.hackerrank.com/contests/101hack54/challenges/weather-forecast-quality", "tests": [ { "input": "14 13 12 13 16 18 21\n15 11 12 11 16 19 24\n", "output": "9\n" }, { "input": "-73 48 -86 72 -16 96 29\n60 -96 67 -16 97 10 -60\n", "output": "806\n" }, { "input": "48 69 -34 66 -43 49 31\n-45 22 -77 -81 -54 -75 28\n", "output": "468\n" }, { "input": "-36 -50 -4 -38 -99 -100 -21\n30 -4 17 46 -38 -35 45\n", "output": "409\n" }, { "input": "-73 77 79 -54 -67 24 21\n-61 66 -53 56 -67 -21 72\n", "output": "361\n" }, { "input": "100 -50 -91 92 -65 -73 -38\n100 -50 -91 92 -65 -73 -38\n", "output": "0\n" }, { "input": "-100 -100 100 100 100 100 -100\n100 100 -100 -100 -100 -100 100\n", "output": "1400\n" }, ], "context": {} }, } actual = main(['--user-agent', 'dummy', 'get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_1000(self): url = 'http://poj.org/problem?id=1000' expected = { "status": "ok", "messages": [], "result": { "url": "http://poj.org/problem?id=1000", "tests": [{ "input": "1 2\r\n", "output": "3\r\n" }], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_2020_qual_d1(self): url = 'https://www.facebook.com/codingcompetitions/hacker-cup/2020/qualification-round/problems/D1' expected = { "status": "ok", "messages": [], "result": { "url": "https://www.facebook.com/codingcompetitions/hacker-cup/2020/qualification-round/problems/D1", "tests": [{ "input": "7\n5 3\n0\n20\n30\n0\n10\n5 2\n0\n20\n30\n0\n10\n5 1\n0\n20\n30\n0\n10\n4 1\n99\n88\n77\n66\n4 4\n99\n88\n77\n66\n6 2\n0\n0\n20\n30\n0\n10\n12 3\n0\n1\n4\n7\n0\n5\n9\n8\n0\n3\n0\n6\n", "output": "Case #1: 20\nCase #2: 30\nCase #3: -1\nCase #4: 165\nCase #5: 0\nCase #6: 50\nCase #7: 19\n" }], "context": {}, }, } actual = main(['--user-agent', 'dummy', 'get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_yupro_d(self): url = 'https://onlinejudge.u-aizu.ac.jp/services/room.html#yupro/problems/D' expected = { "status": "ok", "messages": [], "result": { "url": "https://onlinejudge.u-aizu.ac.jp/services/room.html#yupro/problems/D", "tests": [{ "input": "6/2*(1+2)\n1-1-1\n(1-1-1)/2\n#\n", "output": "2\n2\n1\n" }], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_icpc2013spring_a(self): """This problem contains both words `Input` and `Output` for the headings for sample outputs. """ url = 'http://jag2013spring.contest.atcoder.jp/tasks/icpc2013spring_a' expected = { "status": "ok", "messages": [], "result": { "url": "https://atcoder.jp/contests/jag2013spring/tasks/icpc2013spring_a", "tests": [{ "input": "2 2\n2 \n1 >= 3\n2 <= 5\n2\n1 >= 4\n2 >= 3\n", "output": "Yes\n" }, { "input": "2 2\n2 \n1 >= 5\n2 >= 5\n2\n1 <= 4\n2 <= 3\n", "output": "Yes\n" }, { "input": "2 2\n2 \n1 >= 3\n2 <= 3\n2\n1 <= 2\n2 >= 5\n", "output": "No\n" }, { "input": "1 2\n2\n1 <= 10\n1 >= 15\n", "output": "No\n" }, { "input": "5 5\n3\n2 <= 1\n3 <= 1\n4 <= 1\n4\n2 >= 2\n3 <= 1\n4 <= 1\n5 <= 1\n3\n3 >= 2\n4 <= 1\n5 <= 1\n2\n4 >= 2\n5 <= 1\n1\n5 >= 2 \n", "output": "Yes\n" }], "name": "Everlasting Zero", "context": { "contest": { "name": "Japan Alumni Group Spring Contest 2013", "url": "https://atcoder.jp/contests/jag2013spring" }, "alphabet": "A" }, "memoryLimit": 128, "timeLimit": 5000 }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_power_and_mod(self) -> None: url = 'https://toph.co/p/power-and-mod' expected = { "status": "ok", "messages": [], "result": { "url": "https://toph.co/p/power-and-mod", "tests": [ { "input": "2\r\n2 3 4\r\n3 4 5", "output": "0\r\n1" }, ], "context": {} }, } actual = main(['get-problem', url], debug=True) self.assertEqual(expected, actual)
def test_hourrank_1_beautiful_array(self): """the "Download all test cases" feature is not supported for this problem. """ url = 'https://www.hackerrank.com/contests/hourrank-1/challenges/beautiful-array' expected = { "status": "error", "messages": [ "requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://www.hackerrank.com/rest/contests/hourrank-1/challenges/beautiful-array/download_testcases" ], "result": None, } actual = main(['--user-agent', 'dummy', 'get-problem', url], debug=True) self.assertEqual(expected, actual)