Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oicompare like comparing #273

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/sinol_make/helpers/oicompare.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import requests
import subprocess

Expand Down Expand Up @@ -57,3 +58,60 @@ def check_and_download():
download_oicomapare()
if not check_installed():
util.exit_with_error("Couldn't download oicompare. Please try again later or download it manually.")


def _strip(s: str) -> str:
"""
Replace all longest sequences of spaces with a single space.
"""
return re.sub(r'[\s\0]+', ' ', s).strip()


def compare(file1_path: str, file2_path: str) -> bool:
"""
Compare two files in the same way as oicompare does. Returns True if the files are the same, False otherwise.
"""
print("start")
with open(file1_path, "r") as file1, open(file2_path, "r") as file2:
eof1 = False
eof2 = False
while True:
try:
line1 = _strip(next(file1))
except StopIteration:
eof1 = True
try:
line2 = _strip(next(file2))
except StopIteration:
eof2 = True

print(eof1, eof2)
if eof1 and eof2:
return True
if eof1:
while line2 == "":
try:
line2 = _strip(next(file2))
except StopIteration:
eof2 = True
print("xd")
break
elif eof2:
while line1 == "":
try:
line1 = _strip(next(file1))
except StopIteration:
eof1 = True
break
if line1 != "":
break

if eof1 and eof2:
return True
# print(f'"{line1}" "{line2}" {eof1=} {eof2=}')
if (eof1 and line2 == "") or (eof2 and line1 == ""):
continue
if (eof1 and line2 != "") or (eof2 and line1 != ""):
return False
if line1 != line2:
return False
2 changes: 1 addition & 1 deletion src/sinol_make/task_type/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def _run_checker(self, input_file_path, output_file_path, answer_file_path) -> T
return self._parse_checker_output(output.decode('utf-8').split('\n'))

def _run_diff(self, output_file_path, answer_file_path) -> Tuple[bool, Fraction, str]:
same = util.file_diff(output_file_path, answer_file_path)
same = oicompare.compare(output_file_path, answer_file_path)
if same:
return True, Fraction(100, 1), ""
else:
Expand Down
9 changes: 0 additions & 9 deletions src/sinol_make/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,6 @@ def lines_diff(lines1, lines2):
return True


def file_diff(file1_path, file2_path):
"""
Function to compare two files.
Returns True if they are the same, False otherwise.
"""
with open(file1_path) as file1, open(file2_path) as file2:
return lines_diff(file1.readlines(), file2.readlines())


def get_terminal_size():
"""
Function to get the size of the terminal.
Expand Down
31 changes: 31 additions & 0 deletions tests/helpers/test_oicompare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import tempfile

from sinol_make.helpers import oicompare


def test_oicompare():
with tempfile.TemporaryDirectory() as tmpdir:
tests = [
("", "", True),
("ABC", "ABC", True),
("ABC\nDEF\n", "ABC\nDEF\n", True),
("A B C D E", "A B\tC\0D\tE", True),
("A B", "\0\tA \0 \0B\t\t", True),
("A\nB\nC", "A\nB\nC\n\n\n\n\n\n\n", True),
("A\n\n\n\n", "A\n\n\n\n\n\n\n\n\n\n", True),
("", "ABC", False),
("YES", "NO", False),
("A B", "A\nB", False),
]

for i, (file1, file2, expected) in enumerate(tests):
with open(tmpdir + f"/file1_{i}.txt", "w") as f1, open(tmpdir + f"/file2_{i}.txt", "w") as f2:
f1.write(file1)
f2.write(file2)
assert oicompare.compare(tmpdir + f"/file1_{i}.txt", tmpdir + f"/file2_{i}.txt") == expected, f"Test {i} failed"

for i, (file2, file1, expected) in enumerate(tests):
with open(tmpdir + f"/file1_{i}.txt", "w") as f1, open(tmpdir + f"/file2_{i}.txt", "w") as f2:
f1.write(file1)
f2.write(file2)
assert oicompare.compare(tmpdir + f"/file1_{i}.txt", tmpdir + f"/file2_{i}.txt") == expected, f"Swapped test {i} failed"
28 changes: 0 additions & 28 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,6 @@
from sinol_make.helpers import paths
from tests import util as test_util
from tests.fixtures import create_package
from tests.commands.run import util as run_util


def test_file_diff():
with tempfile.TemporaryDirectory() as tmpdir:
a_file = os.path.join(tmpdir, 'a')
b_file = os.path.join(tmpdir, 'b')

open(a_file, 'w').write("1"
"2"
"3")

open(b_file, 'w').write("1"
"2"
"3"
"4")

assert util.file_diff(a_file, b_file) is False

open(a_file, 'w').write("1\n")
open(b_file, 'w').write("1 ")

assert util.file_diff(a_file, b_file) is True

open(a_file, 'w').write("1\n")
open(b_file, 'w').write("1\n\n")

assert util.file_diff(a_file, b_file) is False


@requests_mock.Mocker(kw="mocker")
Expand Down
Loading