add tests for task1

This commit is contained in:
2026-01-29 23:15:45 +05:00
parent 0658a15911
commit 1e5dbe95ec
9 changed files with 207 additions and 55 deletions

View File

@@ -55,7 +55,7 @@ int main() {
}
```
# Решение
# Решение (C)
```c
#include <assert.h>
@@ -106,3 +106,5 @@ ull gcd_cycle(ll x, ll y) {
return x;
}
```
# Решение (C++)

View File

@@ -0,0 +1,6 @@
public class Solution {
public static void main(String[] args) {
}
}

View File

@@ -1,57 +1,3 @@
/*
Problem RL -- рекурсия в цикл
Ниже приведён исходный код программы для рекурсивного алгоритма Евклида.
Ваша задача: переписать его так, чтобы вместо рекурсии использовался цикл
Посылка должна состоять из программы, считывающей со стандартного ввода два числа x и y и выводящей на стандартный вывод одно число g
Пример 1
Ввод: 2 1
Вывод: 1
Пример 2
Ввод: 3 2
Вывод: 1
Пример 3
Ввод: 2 4
Вывод: 2
Примечания
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
unsigned long long gcd(unsigned long long x,
unsigned long long y) {
unsigned long long q;
if (y > x)
return gcd(y, x);
assert (y > 0);
q = x % y;
if (q == 0)
return y;
return gcd(y, q);
}
int main() {
unsigned long long x = 0, y = 0, g;
int res;
res = scanf("%llu %llu", &x, &y);
assert(res == 2);
g = gcd(x, y);
printf("%llu\n", g);
return 0;
}
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

View File

@@ -0,0 +1,46 @@
#include <iostream>
#include <assert.h>
#define ull unsigned long long
#define ll long long
ull gcd_cycle(ll x, ll y);
ull gcd_recursive(ull x, ull y);
int main() {
ull x = 0, y = 0, g;
int res;
res = scanf("%llu %llu", &x, &y);
assert(res == 2);
g = gcd_cycle(x, y);
printf("%llu\n", g);
return 0;
}
ull gcd_recursive(ull x, ull y) {
ull q;
if (y > x)
return gcd_recursive(y, x);
assert (y > 0);
q = x % y;
if (q == 0)
return y;
return gcd_recursive(y, q);
}
ull gcd_cycle(ll x, ll y) {
x = llabs(x);
y = llabs(y);
if (x == 0) return y;
if (y == 0) return x;
while (y != 0) {
ll r = x % y;
x = y;
y = r;
}
return x;
}

View File

63
1. problem_rl/test.py Executable file
View File

@@ -0,0 +1,63 @@
#!/usr/bin/env python
from pathlib import Path
from subprocess import run
tests_1: dict = {
"2 1" : "1",
"3 2" : "1",
"2 4" : "2",
"1769 427" : "61",
"698917 -102089" : "7853",
"917 872" : "1",
"-417143 -455947" : "9701",
"63862 -55298" : "2",
"702228 373125" : "3",
"-659323 727699" : "7"
}
def test(file_name: str, compile_cmd: str, run_cmd: list[str], tests_dict: dict):
print(f"=== TESTING {file_name.upper()} ===")
file_path = Path(file_name)
if not file_path.is_file():
print(f"file '{file_name}' was not found.")
return
print(f"{file_name} found")
run([compile_cmd, file_name], check=True)
for t, res in tests_dict.items():
out = run(
run_cmd,
input=t,
text=True,
capture_output=True
)
program_result = out.stdout.strip()
if program_result != res:
print("test failed:")
print(f"\ttest: {t}")
print(f"\tprogram result: {program_result}")
print(f"\tcorrect result: {res}")
break
else:
print("test passed:")
print(f"\ttest: {t}")
print(f"\tresult: {res}")
else:
print("all tests passed!")
def main():
test("solution.c", "gcc", ["./a.out"], tests_1)
test("solution.cpp", "g++", ["./a.out"], tests_1)
if __name__ == "__main__":
main()