Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • safroleris/comptech-more-tests
  • finnr/comptech-more-tests
  • brunorothschild/comptech-more-tests
  • friedricht/comptech-more-tests
  • luckylee/compiling-techniques-student-tests
  • lhuebner/comptech-more-tests
6 results
Show changes
Commits on Source (48)
Showing
with 86 additions and 13 deletions
...@@ -3,4 +3,5 @@ ...@@ -3,4 +3,5 @@
*.log *.log
tmp/ tmp/
tests/.lit-test-times.txt tests/.lit_test_times.txt
**/Output/
# Compiling Techniques - More Tests
This project adds additional `.choc` tests that are not covered by the provided tests.
[- Disclaimer: There is no guarantee that the tests provided here are correct. This is a collaborative student effort. -]
## Running tests
Just point `lit` at the tests directory, e.g.:
```sh
lit ../comptech-more-tests/tests/parser
```
(Your virtual environment which has `lit` and `choco-opt` in `PATH` needs to be activated.)
## Generating new tests
If you want to add a new test, the easiest way is to add a new `.choc` file in `gen-input/**/*.choc`. This is only a regular `.choc` file, demonstrating the syntax that should be tested. For example a file containing `i: [[int]] = 0` to test nested list types.
(Note: This workflow will probably need to be adapted for further assignments, but for the first one - Parser - it should be fine.)
Then run the `gen.py` script. This will write a test case that `lit` understands into `tests/**/*.choc`. It uses _your_ `choco-opt` implementation to generate the expected output. Tests that already exist will not be overwritten.
(Your virtual environment which has `choco-opt` in `PATH` needs to be activated.)
```sh
./gen.py
```
If the existing tests really need to be overwritten for some reason, pass the `--force`/`-f` flag. This is usually not necessary.
```sh
./gen.py -f
```
#!/usr/bin/env python3 #!/usr/bin/env python3
import subprocess import subprocess
import sys
import os
from pathlib import Path from pathlib import Path
def gen_test(input_file, output_file): def gen_test(input_file: Path, output_file: Path):
command = ["choco-opt", input_file] output_file_existed_before = output_file.exists()
try: try:
with open(output_file, "w") as f:
f.write("# RUN: choco-opt %s | filecheck %s\n\n")
for line in input_file.open("r"):
f.write(line)
result = subprocess.run( result = subprocess.run(
command, ["choco-opt", output_file],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, text=True,
check=True, check=True,
) )
with open(output_file, "w") as f: output_lines = result.stdout.split("\n")
f.write("# RUN: choco-opt %s | filecheck %s\n\n")
for line in input_file.open("r"):
f.write(line)
output_lines = result.stdout.split("\n") with open(output_file, "a") as f:
f.write(f"\n# CHECK: {output_lines[0]}\n") f.write(f"\n# CHECK: {output_lines[0]}\n")
for line in output_lines[1:]: for line in output_lines[1:]:
if line.strip(): if line.strip():
f.write(f"# CHECK-NEXT: {line}\n") f.write(f"# CHECK-NEXT: {line}\n")
else:
break
except subprocess.CalledProcessError as e: except Exception as e:
print(f"Error running choco-opt on {input_file}: {e.stderr}") if not output_file_existed_before:
os.remove(output_file)
raise e
def main(): def main():
...@@ -34,11 +43,19 @@ def main(): ...@@ -34,11 +43,19 @@ def main():
output_dir.mkdir(exist_ok=True) output_dir.mkdir(exist_ok=True)
force_regenerate = len(sys.argv) > 1 and sys.argv[1] in ["--force", "-f"]
for input_file in input_dir.rglob("*.choc"): for input_file in input_dir.rglob("*.choc"):
relative_path = input_file.relative_to(input_dir) relative_path = input_file.relative_to(input_dir)
output_file = output_dir / relative_path output_file = output_dir / relative_path
output_file.parent.mkdir(parents=True, exist_ok=True) output_file.parent.mkdir(parents=True, exist_ok=True)
gen_test(input_file, output_file) if force_regenerate or not output_file.exists():
print(f"Generating {output_file}...")
gen_test(input_file, output_file)
else:
print(f"Skipping re-generation of {output_file}...")
print("All done!")
if __name__ == "__main__": if __name__ == "__main__":
......
a and b and c
0 // 1 // 2
1 if 2 else 3 if 4 else 5
[[a,b],[c,d]][e][f]
0 - 1 - 2
0 % 1 % 2
0 * 1 * 2
a or b or c
a + b + c
def foo():
if True:
"then"
elif 1:
"than"
else:
"thon"
-1 = 2
----1
[ a[3][4][ [5, 6] ], b[ [1] ] ]
i: [[str]] = 0
a ----- b
a + b and not not c
3 + 4 * 5 - 6