Added last three homeworks
This commit is contained in:
parent
f7b2367bc4
commit
0e3d948c9f
363 changed files with 18214 additions and 0 deletions
19
2025.02.28/1Ex/Makefile
Normal file
19
2025.02.28/1Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a01.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a01.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
5
2025.02.28/1Ex/a.txt
Normal file
5
2025.02.28/1Ex/a.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
61
2025.02.28/1Ex/array.c
Normal file
61
2025.02.28/1Ex/array.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
14
2025.02.28/1Ex/array.h
Normal file
14
2025.02.28/1Ex/array.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
|
||||
#endif
|
14
2025.02.28/1Ex/io_status.h
Normal file
14
2025.02.28/1Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
66
2025.02.28/1Ex/main.c
Normal file
66
2025.02.28/1Ex/main.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./aout x c n p filename */
|
||||
int c, p, n, res, task = 1;
|
||||
io_status ret;
|
||||
char *name, *x, **a;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 6 && sscanf(argv[2], "%d", &c) && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s x c n p name\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
x = argv[1];
|
||||
name = argv[5];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, p);
|
||||
t = clock();
|
||||
res = t1_solve(a, n, x, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("%s : Task = %d Res = %d Elapsed = %.2f\n", argv[0], task, res, t);
|
||||
|
||||
free_array(a, n);
|
||||
free(a);
|
||||
|
||||
return 0;
|
||||
}
|
14
2025.02.28/1Ex/solve.c
Normal file
14
2025.02.28/1Ex/solve.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t1_solve(char **a, int n, char *x, int (*cmp)(const char *, const char *)) {
|
||||
int avg = (n + (-1)*(n%2)) / 2;
|
||||
int comp;
|
||||
|
||||
if (n == 0) return 0;
|
||||
|
||||
comp = cmp(x, a[avg]);
|
||||
if (comp < 0) return t1_solve(a, avg, x, cmp);
|
||||
if (comp > 0) return avg+1 + t1_solve(a+avg+1, n-(avg+1), x, cmp);
|
||||
else return avg;
|
||||
}
|
||||
|
8
2025.02.28/1Ex/solve.h
Normal file
8
2025.02.28/1Ex/solve.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
int t1_solve(char **a, int n, char *x, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
23
2025.02.28/1Ex/sort.c
Normal file
23
2025.02.28/1Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/1Ex/sort.h
Normal file
11
2025.02.28/1Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
19
2025.02.28/2Ex/Makefile
Normal file
19
2025.02.28/2Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a02.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a02.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
4
2025.02.28/2Ex/a.txt
Normal file
4
2025.02.28/2Ex/a.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
alpha
|
||||
beta
|
||||
gamma
|
||||
delta
|
73
2025.02.28/2Ex/array.c
Normal file
73
2025.02.28/2Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/2Ex/array.h
Normal file
15
2025.02.28/2Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
5
2025.02.28/2Ex/b.txt
Normal file
5
2025.02.28/2Ex/b.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
alpha
|
||||
beta
|
||||
gamma
|
||||
delta
|
||||
epsilon
|
14
2025.02.28/2Ex/io_status.h
Normal file
14
2025.02.28/2Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
120
2025.02.28/2Ex/main.c
Normal file
120
2025.02.28/2Ex/main.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./a02.out c n p_a filename_a m p_b filename_b */
|
||||
int c, p_a, n, p_b, m, diff, task = 2;
|
||||
io_status ret;
|
||||
char *name_a, *name_b, **arr_a, **arr_b, **arr_c;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 8 && sscanf(argv[1], "%d", &c) && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p_a) == 1 && sscanf(argv[5], "%d", &m) && sscanf(argv[6], "%d",&p_b) && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s c n p_a filename_a m p_b filename_b\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name_a = argv[4];
|
||||
name_b = argv[7];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(arr_a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
if (!(arr_b = (char **)malloc(m * sizeof(char *))))
|
||||
{
|
||||
free(arr_a);
|
||||
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
if (!(arr_c = (char **)malloc((n+m) * sizeof(char *))))
|
||||
{
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
ret = read_array(arr_a, n, name_a);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_a);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_a);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
free(arr_c);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
ret = read_array(arr_b, m, name_b);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_b);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_b);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free_array(arr_a, n);
|
||||
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
free(arr_c);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(arr_a, n, p_a);
|
||||
printf("\n");
|
||||
print_array(arr_b, m, p_b);
|
||||
|
||||
t = clock();
|
||||
t2_solve(arr_a, arr_b, arr_c, n, m, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
diff = check(arr_c, n+m, cmp);
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(arr_c, n+m, p_a+p_b);
|
||||
printf("%s : Task = %d Diff = %d Elapsed = %.2f\n", argv[0], task, diff, t);
|
||||
|
||||
free_array(arr_a, n);
|
||||
free_array(arr_b, m);
|
||||
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
free(arr_c);
|
||||
|
||||
return 0;
|
||||
}
|
24
2025.02.28/2Ex/solve.c
Normal file
24
2025.02.28/2Ex/solve.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include "solve.h"
|
||||
|
||||
#include "array.h"
|
||||
|
||||
void t2_solve(char **arr_a, char **arr_b, char **arr_c, int n, int m, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i, j, k, cntr, len = n + m; // cntr - contrast
|
||||
for (i = 0, j = 0, k = 0; k < len; ++k)
|
||||
{
|
||||
if (i >= n)
|
||||
{
|
||||
arr_c[k] = arr_b[j++];
|
||||
} else if (j >= m)
|
||||
{
|
||||
arr_c[k] = arr_a[i++];
|
||||
} else
|
||||
{
|
||||
cntr = cmp(arr_a[i], arr_b[j]);
|
||||
if (cntr <= 0) arr_c[k] = arr_a[i++];
|
||||
else arr_c[k] = arr_b[j++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
9
2025.02.28/2Ex/solve.h
Normal file
9
2025.02.28/2Ex/solve.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void t2_solve(char **arr_a, char **arr_b, char **arr_c, int n, int m, int (*cmp)(const char *, const char *));
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
23
2025.02.28/2Ex/sort.c
Normal file
23
2025.02.28/2Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/2Ex/sort.h
Normal file
11
2025.02.28/2Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
19
2025.02.28/2Ex/test_cases.json
Normal file
19
2025.02.28/2Ex/test_cases.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"exe": "a02.exe",
|
||||
"filename_a": "a.txt",
|
||||
"filename_b": "b.txt",
|
||||
"tests": [
|
||||
{
|
||||
"c": 1,
|
||||
"a": "1 2 3 4\n5 6 7 8",
|
||||
"b": "9 10 11\n12 13 14",
|
||||
"expected": "1 2 3 4 5 6 7 8 9 10 11 12 13 14"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"a": "10 20\n30 40 50",
|
||||
"b": "15 25 35\n45 55",
|
||||
"expected": "10 20 30 40 50 15 25 35 45 55"
|
||||
}
|
||||
]
|
||||
}
|
130
2025.02.28/2Ex/test_runner.py
Normal file
130
2025.02.28/2Ex/test_runner.py
Normal file
|
@ -0,0 +1,130 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, c, a, b, expected):
|
||||
self.c = c
|
||||
self.a = a
|
||||
self.b = b
|
||||
self.expected = expected
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename_a = self.config["filename_a"]
|
||||
self.filename_b = self.config["filename_b"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd):
|
||||
"""Runs a shell command and handles errors"""
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
|
||||
return result
|
||||
|
||||
def count_lines(filename):
|
||||
"""Counts the number of non-empty lines in a file"""
|
||||
try:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return sum(1 for line in f if line.strip())
|
||||
except FileNotFoundError:
|
||||
print(color_text(f"[ERROR] File {filename} not found.", Fore.RED))
|
||||
return 0
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe = test_suite.exe
|
||||
filename_a = test_suite.filename_a
|
||||
filename_b = test_suite.filename_b
|
||||
|
||||
# Write input arrays to files
|
||||
with open(filename_a, "w", encoding="utf-8") as fa:
|
||||
fa.write(test.a)
|
||||
with open(filename_b, "w", encoding="utf-8") as fb:
|
||||
fb.write(test.b)
|
||||
|
||||
# Calculate n and m correctly
|
||||
n = count_lines(filename_a)
|
||||
m = count_lines(filename_b)
|
||||
pa = n
|
||||
pb = m
|
||||
|
||||
# Debug output to check file contents
|
||||
print(color_text(f"[DEBUG] a.txt content:\n{open(filename_a).read()}", Fore.CYAN))
|
||||
print(color_text(f"[DEBUG] b.txt content:\n{open(filename_b).read()}", Fore.CYAN))
|
||||
print(color_text(f"[DEBUG] Calculated n = {n}, m = {m}", Fore.CYAN))
|
||||
|
||||
# Ensure correct execution command on Windows
|
||||
exe_cmd = exe if platform.system() == "Windows" else f"./{exe}"
|
||||
|
||||
# Form command-line arguments as a list
|
||||
cmd = [exe_cmd, str(test.c), str(n), str(pa), filename_a, str(m), str(pb), filename_b]
|
||||
print(color_text(f"[DEBUG] Running command: {' '.join(cmd)}", Fore.CYAN))
|
||||
|
||||
# Run program
|
||||
result = run_command(cmd)
|
||||
if result.returncode != 0:
|
||||
print(color_text(f"[ERROR] Test failed to execute: {' '.join(cmd)}", Fore.RED))
|
||||
print(color_text(f"[EXIT CODE]: {result.returncode}", Fore.YELLOW))
|
||||
print(color_text(f"[STDOUT]: {result.stdout}", Fore.YELLOW))
|
||||
print(color_text(f"[STDERR]: {result.stderr}", Fore.YELLOW))
|
||||
return
|
||||
|
||||
# Extract relevant output
|
||||
output_lines = result.stdout.split("\n")
|
||||
try:
|
||||
start_index = output_lines.index("New array:") + 1
|
||||
end_index = next(i for i, line in enumerate(output_lines) if "Task" in line)
|
||||
output = "\n".join(output_lines[start_index:end_index])
|
||||
except (ValueError, IndexError):
|
||||
print(color_text(f"[FAIL] Could not parse output correctly.", Fore.RED))
|
||||
return
|
||||
|
||||
# Validate output
|
||||
if output == test.expected:
|
||||
print(color_text(f"[PASS] Test passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test failed.", Fore.RED))
|
||||
print(color_text(f"Expected:\n{test.expected}", Fore.YELLOW))
|
||||
print(color_text(f"Got:\n{output}", Fore.YELLOW))
|
||||
|
||||
# Cleanup files
|
||||
for file in [filename_a, filename_b]:
|
||||
try:
|
||||
os.remove(file)
|
||||
except FileNotFoundError:
|
||||
print(color_text(f"[WARNING] Could not delete {file}, may be locked.", Fore.MAGENTA))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.MAGENTA))
|
||||
run_command(["make", "clean"])
|
||||
print(color_text("[BUILD] Compiling project...", Fore.MAGENTA))
|
||||
run_command(["make"])
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.MAGENTA))
|
||||
run_command(["make", "clean"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
19
2025.02.28/3Ex/Makefile
Normal file
19
2025.02.28/3Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a03.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a03.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
4
2025.02.28/3Ex/a.txt
Normal file
4
2025.02.28/3Ex/a.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
same
|
||||
one
|
||||
one
|
||||
same
|
61
2025.02.28/3Ex/array.c
Normal file
61
2025.02.28/3Ex/array.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
14
2025.02.28/3Ex/array.h
Normal file
14
2025.02.28/3Ex/array.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
|
||||
#endif
|
14
2025.02.28/3Ex/io_status.h
Normal file
14
2025.02.28/3Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
68
2025.02.28/3Ex/main.c
Normal file
68
2025.02.28/3Ex/main.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./aout x c n p filename */
|
||||
int c, p, n, res, task = 3;
|
||||
io_status ret;
|
||||
char *name, *x, **a;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 6 && sscanf(argv[2], "%d", &c) && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s x c n p name\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
x = argv[1];
|
||||
name = argv[5];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, p);
|
||||
t = clock();
|
||||
res = t3_solve(a, n, x, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(a, n, p);
|
||||
printf("%s : Task = %d Res = %d Elapsed = %.2f\n", argv[0], task, res, t);
|
||||
|
||||
free_array(a, n);
|
||||
free(a);
|
||||
|
||||
return 0;
|
||||
}
|
21
2025.02.28/3Ex/solve.c
Normal file
21
2025.02.28/3Ex/solve.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t3_solve(char **a, int n, char *x, int (*cmp)(const char *, const char *)) {
|
||||
int i = 0, j = n-1;
|
||||
char *temp;
|
||||
|
||||
while (1)
|
||||
{
|
||||
for (; i < n; i++) if (cmp(a[i], x) >= 0) break;
|
||||
for (; j >= 0; j--) if (cmp(a[j], x) < 0) break;
|
||||
|
||||
if (i >= j) break;
|
||||
temp = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = temp;
|
||||
|
||||
i++, j--;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
8
2025.02.28/3Ex/solve.h
Normal file
8
2025.02.28/3Ex/solve.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
int t3_solve(char **a, int n, char *x, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
23
2025.02.28/3Ex/sort.c
Normal file
23
2025.02.28/3Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/3Ex/sort.h
Normal file
11
2025.02.28/3Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
110
2025.02.28/3Ex/test_cases.json
Normal file
110
2025.02.28/3Ex/test_cases.json
Normal file
|
@ -0,0 +1,110 @@
|
|||
{
|
||||
"exe": "a03.exe",
|
||||
"filename": "input.txt",
|
||||
"tests": [
|
||||
{
|
||||
"name": "Basic Sorting Test",
|
||||
"x": "5",
|
||||
"c": 1,
|
||||
"text": "9\n8\n7\n6\n5\n4\n3\n2\n1\n0",
|
||||
"expected": "0\n1\n2\n3\n4\n5\n6\n7\n8\n9",
|
||||
"res": 5
|
||||
},
|
||||
{
|
||||
"name": "Insert at Start (Should not change order)",
|
||||
"x": "aaa",
|
||||
"c": 1,
|
||||
"text": "bbb\nccc\nddd\neee",
|
||||
"expected": "bbb\nccc\nddd\neee",
|
||||
"res": 0
|
||||
},
|
||||
{
|
||||
"name": "Insert at End (Should not change order)",
|
||||
"x": "zzz",
|
||||
"c": 1,
|
||||
"text": "aaa\nbbb\nccc\nddd",
|
||||
"expected": "aaa\nbbb\nccc\nddd",
|
||||
"res": 4
|
||||
},
|
||||
{
|
||||
"name": "Insert in Middle",
|
||||
"x": "mmm",
|
||||
"c": 1,
|
||||
"text": "aaa\njjj\nnnn\nzzz",
|
||||
"expected": "aaa\njjj\nnnn\nzzz",
|
||||
"res": 2
|
||||
},
|
||||
{
|
||||
"name": "Already Present",
|
||||
"x": "hello",
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\nhello\norange\ntiger",
|
||||
"expected": "apple\nbanana\nhello\norange\ntiger",
|
||||
"res": 2
|
||||
},
|
||||
{
|
||||
"name": "Reverse Order",
|
||||
"x": "h",
|
||||
"c": 2,
|
||||
"text": "z\ny\nx\nw\nv\nu\nt\ns\nr\nq\np",
|
||||
"expected": "z\ny\nx\nw\nv\nu\nt\ns\nr\nq\np",
|
||||
"res": 11
|
||||
},
|
||||
{
|
||||
"name": "Equal Length Sorting",
|
||||
"x": "abc",
|
||||
"c": 3,
|
||||
"text": "x\ny\nz\napple\nbanana\ncoconut",
|
||||
"expected": "x\ny\nz\napple\nbanana\ncoconut",
|
||||
"res": 3
|
||||
},
|
||||
{
|
||||
"name": "All Same Elements",
|
||||
"x": "same",
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame",
|
||||
"res": 0
|
||||
},
|
||||
{
|
||||
"name": "Similar Elements",
|
||||
"x": "one",
|
||||
"c": 1,
|
||||
"text": "same\none\none\nsame",
|
||||
"expected": "same\none\none\nsame",
|
||||
"res": 0
|
||||
},
|
||||
{
|
||||
"name": "Insert in Empty List",
|
||||
"x": "new",
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": "",
|
||||
"res": 1
|
||||
},
|
||||
{
|
||||
"name": "Insert Before Larger Values",
|
||||
"x": "mid",
|
||||
"c": 1,
|
||||
"text": "zeta\nzulu",
|
||||
"expected": "zeta\nzulu",
|
||||
"res": 0
|
||||
},
|
||||
{
|
||||
"name": "Insert After Smaller Values",
|
||||
"x": "mid",
|
||||
"c": 1,
|
||||
"text": "alpha\nbeta",
|
||||
"expected": "alpha\nbeta",
|
||||
"res": 2
|
||||
},
|
||||
{
|
||||
"name": "Sorting Numbers as Strings",
|
||||
"x": "5",
|
||||
"c": 1,
|
||||
"text": "10\n20\n30\n40",
|
||||
"expected": "10\n20\n30\n40",
|
||||
"res": 4
|
||||
}
|
||||
]
|
||||
}
|
156
2025.02.28/3Ex/test_runner.py
Normal file
156
2025.02.28/3Ex/test_runner.py
Normal file
|
@ -0,0 +1,156 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
import re
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, x, c, text, expected, res, p=None, debug=False, name=None):
|
||||
self.x = x
|
||||
self.c = c
|
||||
self.text = text
|
||||
self.expected = expected
|
||||
self.res = res
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
self.name = name if name else str(c) # Используем c, если name отсутствует
|
||||
|
||||
def get_num_lines(self):
|
||||
"""Returns the number of lines in the input text (n)"""
|
||||
return self.text.count("\n") + 1 # +1, чтобы учесть последнюю строку
|
||||
|
||||
def should_fail(self):
|
||||
"""Checks if the test expects a failure"""
|
||||
return self.expected.lower() == "fall"
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def parse_sorted_output(output):
|
||||
"""Extracts the second print_array output from program output"""
|
||||
parts = output.split("New array:\n")
|
||||
if len(parts) > 1:
|
||||
sorted_array = parts[1].strip().split("\n")
|
||||
sorted_array = sorted_array[:-1] # Убираем последнюю строку (Task = ... Res = ... Elapsed = ...)
|
||||
return "\n".join(sorted_array)
|
||||
return ""
|
||||
|
||||
def check_res(output, expected_res):
|
||||
"""Checks if Res matches expected value"""
|
||||
match = re.search(r"Res\s*=\s*(-?\d+)", output)
|
||||
if match:
|
||||
res_value = int(match.group(1))
|
||||
if res_value != expected_res:
|
||||
print(color_text(f"[FAIL] Test failed: Res = {res_value} (expected {expected_res})", Fore.RED))
|
||||
return False
|
||||
return True
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
n = test.get_num_lines() # Auto-count lines in input
|
||||
p = test.p if test.p is not None else n # Default p = n
|
||||
|
||||
# Write input data to a file (Ensure last line has \n)
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
text = test.text.rstrip() + "\n" # Если нет \n в конце, добавляем
|
||||
f.write(text)
|
||||
|
||||
# Windows fix: remove './' for executables
|
||||
cmd = [exe, test.x, str(test.c), str(n), str(p), filename]
|
||||
if test.debug:
|
||||
cmd.append("DEBUG")
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# Check if test expected failure
|
||||
if test.should_fail():
|
||||
if result and result.returncode != 0:
|
||||
print(color_text(f"[PASS] Test '{test.name}' correctly failed (expected crash).", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.name}' should have failed but did not.", Fore.RED))
|
||||
return
|
||||
|
||||
# Extract sorted array output
|
||||
sorted_output = parse_sorted_output(result.stdout) if result else None
|
||||
|
||||
# Check Res value
|
||||
if not check_res(result.stdout, test.res):
|
||||
return
|
||||
|
||||
# Check result
|
||||
if sorted_output == test.expected:
|
||||
print(color_text(f"[PASS] Test '{test.name}' passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.name}' failed.", Fore.RED))
|
||||
print(f"Expected:\n{test.expected}")
|
||||
print(f"Got:\n{sorted_output}")
|
||||
if test.debug:
|
||||
print(color_text("[DEBUG] Full Program Output:", Fore.YELLOW))
|
||||
print(result.stdout)
|
||||
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
19
2025.02.28/4Ex/Makefile
Normal file
19
2025.02.28/4Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a04.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a04.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
4
2025.02.28/4Ex/a.txt
Normal file
4
2025.02.28/4Ex/a.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
apple
|
||||
banana
|
||||
cherry
|
||||
date
|
73
2025.02.28/4Ex/array.c
Normal file
73
2025.02.28/4Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/4Ex/array.h
Normal file
15
2025.02.28/4Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
14
2025.02.28/4Ex/io_status.h
Normal file
14
2025.02.28/4Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
68
2025.02.28/4Ex/main.c
Normal file
68
2025.02.28/4Ex/main.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./a04.out c n p filename */
|
||||
int c, n, p, diff, task = 4;
|
||||
io_status ret;
|
||||
char *name, **a;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &c) && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p) == 1 && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s c n p name\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[4];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, p);
|
||||
t = clock();
|
||||
t4_solve(a, n, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
diff = check(a, n, cmp);
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(a, n, p);
|
||||
printf("%s : Task = %d Diff = %d Elapsed = %.2f\n", argv[0], task, diff, t);
|
||||
|
||||
free_array(a, n);
|
||||
free(a);
|
||||
|
||||
return 0;
|
||||
}
|
20
2025.02.28/4Ex/solve.c
Normal file
20
2025.02.28/4Ex/solve.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "solve.h"
|
||||
|
||||
void t4_solve(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
char *temp;
|
||||
int i, j;
|
||||
for (i=n; i > 0; --i)
|
||||
{
|
||||
for (j=0; j < i-1; ++j)
|
||||
{
|
||||
if (cmp(a[j], a[j+1]) >= 0)
|
||||
{
|
||||
temp = a[j];
|
||||
a[j] = a[j+1];
|
||||
a[j+1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
8
2025.02.28/4Ex/solve.h
Normal file
8
2025.02.28/4Ex/solve.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void t4_solve(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
23
2025.02.28/4Ex/sort.c
Normal file
23
2025.02.28/4Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/4Ex/sort.h
Normal file
11
2025.02.28/4Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
76
2025.02.28/4Ex/test_cases.json
Normal file
76
2025.02.28/4Ex/test_cases.json
Normal file
|
@ -0,0 +1,76 @@
|
|||
{
|
||||
"exe": "a04.exe",
|
||||
"filename": "input.txt",
|
||||
"tests": [
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
}
|
||||
]
|
||||
}
|
153
2025.02.28/4Ex/test_runner.py
Normal file
153
2025.02.28/4Ex/test_runner.py
Normal file
|
@ -0,0 +1,153 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
import re
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, c, text, expected, p=None, debug=False):
|
||||
self.c = c
|
||||
self.text = text
|
||||
self.expected = expected
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
|
||||
def get_num_lines(self):
|
||||
"""Returns the number of lines in the input text (n)"""
|
||||
return self.text.count("\n") + 1 # +1, чтобы учесть последнюю строку
|
||||
|
||||
def should_fail(self):
|
||||
"""Checks if the test expects a failure"""
|
||||
return self.expected.lower() == "fall"
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def parse_sorted_output(output):
|
||||
"""Extracts the second print_array output from program output"""
|
||||
parts = output.split("New array:\n")
|
||||
if len(parts) > 1:
|
||||
sorted_array = parts[1].rstrip().split("\n")
|
||||
sorted_array = sorted_array[:-1] # Убираем последнюю строку (Task = ... Diff = ... Elapsed = ...)
|
||||
return "\n".join(sorted_array)
|
||||
return ""
|
||||
|
||||
def check_diff(output):
|
||||
"""Checks if Diff != 0 in the program output"""
|
||||
match = re.search(r"Diff\s*=\s*(\d+)", output)
|
||||
if match:
|
||||
diff_value = int(match.group(1))
|
||||
if diff_value != 0:
|
||||
print(color_text(f"[FAIL] Test failed: Diff = {diff_value} (expected 0)", Fore.RED))
|
||||
return False
|
||||
return True
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
n = test.get_num_lines() # Auto-count lines in input
|
||||
p = test.p if test.p is not None else n # Default p = n
|
||||
|
||||
# Write input data to a file (Ensure last line has \n)
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
text = test.text.rstrip() + "\n" # Если нет \n в конце, добавляем
|
||||
f.write(text)
|
||||
|
||||
# Windows fix: remove './' for executables
|
||||
cmd = [exe, str(test.c), str(n), str(p), filename]
|
||||
if test.debug:
|
||||
cmd.append("DEBUG")
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# Check if test expected failure
|
||||
if test.should_fail():
|
||||
if result and result.returncode != 0:
|
||||
print(color_text(f"[PASS] Test '{test.c}' correctly failed (expected crash).", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' should have failed but did not.", Fore.RED))
|
||||
return
|
||||
|
||||
# Extract sorted array output
|
||||
sorted_output = parse_sorted_output(result.stdout) if result else None
|
||||
|
||||
# Check Diff value
|
||||
if not check_diff(result.stdout):
|
||||
return
|
||||
|
||||
# Check result
|
||||
if sorted_output == test.expected:
|
||||
print(color_text(f"[PASS] Test '{test.c}' passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' failed.", Fore.RED))
|
||||
print(f"Expected:\n{test.expected}")
|
||||
print(f"Got:\n{sorted_output}")
|
||||
if test.debug:
|
||||
print(color_text("[DEBUG] Full Program Output:", Fore.YELLOW))
|
||||
print(result.stdout)
|
||||
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
19
2025.02.28/5Ex/Makefile
Normal file
19
2025.02.28/5Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a05.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a05.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
4
2025.02.28/5Ex/a.txt
Normal file
4
2025.02.28/5Ex/a.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
apple
|
||||
banana
|
||||
cherry
|
||||
date
|
73
2025.02.28/5Ex/array.c
Normal file
73
2025.02.28/5Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/5Ex/array.h
Normal file
15
2025.02.28/5Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
14
2025.02.28/5Ex/io_status.h
Normal file
14
2025.02.28/5Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
68
2025.02.28/5Ex/main.c
Normal file
68
2025.02.28/5Ex/main.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./a05.out c n p filename */
|
||||
int c, n, p, diff, task = 5;
|
||||
io_status ret;
|
||||
char *name, **a;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &c) && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p) == 1 && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s c n p name\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[4];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, p);
|
||||
t = clock();
|
||||
t5_solve(a, n, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
diff = check(a, n, cmp);
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(a, n, p);
|
||||
printf("%s : Task = %d Diff = %d Elapsed = %.2f\n", argv[0], task, diff, t);
|
||||
|
||||
free_array(a, n);
|
||||
free(a);
|
||||
|
||||
return 0;
|
||||
}
|
28
2025.02.28/5Ex/solve.c
Normal file
28
2025.02.28/5Ex/solve.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
#include "solve.h"
|
||||
|
||||
void t5_solve(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
char *temp;
|
||||
size_t size_c = sizeof(char);
|
||||
int i, min;
|
||||
for (i=0; i < n-1; ++i)
|
||||
{
|
||||
min = minimum(a+i*size_c, n-i, cmp)+i;
|
||||
temp = a[min];
|
||||
a[min] = a[i];
|
||||
a[i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int minimum(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i, min=0;
|
||||
if (n <= 0) return 0;
|
||||
for (i = 1; i < n; ++i)
|
||||
{
|
||||
if (cmp(a[min], a[i]) > 0) min = i;
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
|
9
2025.02.28/5Ex/solve.h
Normal file
9
2025.02.28/5Ex/solve.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void t5_solve(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
int minimum(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
23
2025.02.28/5Ex/sort.c
Normal file
23
2025.02.28/5Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/5Ex/sort.h
Normal file
11
2025.02.28/5Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
231
2025.02.28/5Ex/test_cases.json
Normal file
231
2025.02.28/5Ex/test_cases.json
Normal file
|
@ -0,0 +1,231 @@
|
|||
{
|
||||
"exe": "a05.exe",
|
||||
"filename": "input.txt",
|
||||
"tests": [
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "zoo\napple\nbanana\ncherry",
|
||||
"expected": "zoo\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "a\nabc\nabcd\nabcdef"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "abcdef\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "1\n12\n123\n1234\n12345"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "12345\n1234\n123\n12\n1"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "hello\nHELLO\nhElLo\nHeLLo",
|
||||
"expected": "HELLO\nHeLLo\nhElLo\nhello"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "A\nB\nC\nD\nE"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "E\nD\nC\nB\nA"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "c\nbb\naaa"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "aaa\nbb\nc"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "3\n1\n4\n1\n5\n9\n2\n6\n5\n3",
|
||||
"expected": "1\n1\n2\n3\n3\n4\n5\n5\n6\n9"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "hi\ncode\nhello\nworld"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "world\nhello\ncode\nhi"
|
||||
}
|
||||
]
|
||||
}
|
138
2025.02.28/5Ex/test_runner.py
Normal file
138
2025.02.28/5Ex/test_runner.py
Normal file
|
@ -0,0 +1,138 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, c, text, expected, p=None, debug=False):
|
||||
self.c = c
|
||||
self.text = text
|
||||
self.expected = expected
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
|
||||
def get_num_lines(self):
|
||||
"""Returns the number of lines in the input text (n)"""
|
||||
return self.text.count("\n") + 1 # +1, чтобы учесть последнюю строку
|
||||
|
||||
def should_fail(self):
|
||||
"""Checks if the test expects a failure"""
|
||||
return self.expected.lower() == "fall"
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def parse_sorted_output(output):
|
||||
"""Extracts the second print_array output from program output"""
|
||||
parts = output.split("New array:\n")
|
||||
if len(parts) > 1:
|
||||
sorted_array = parts[1].rstrip().split("\n")
|
||||
sorted_array = sorted_array[:-1] # Убираем последнюю строку (Task = ... Diff = ... Elapsed = ...)
|
||||
return "\n".join(sorted_array)
|
||||
return ""
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
n = test.get_num_lines() # Auto-count lines in input
|
||||
p = test.p if test.p is not None else n # Default p = n
|
||||
|
||||
# Write input data to a file (Ensure last line has \n)
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
text = test.text.rstrip() + "\n" # Если нет \n в конце, добавляем
|
||||
f.write(text)
|
||||
|
||||
# Windows fix: remove './' for executables
|
||||
cmd = [exe, str(test.c), str(n), str(p), filename]
|
||||
if test.debug:
|
||||
cmd.append("DEBUG")
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# Check if test expected failure
|
||||
if test.should_fail():
|
||||
if result and result.returncode != 0:
|
||||
print(color_text(f"[PASS] Test '{test.c}' correctly failed (expected crash).", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' should have failed but did not.", Fore.RED))
|
||||
return
|
||||
|
||||
# Extract sorted array output
|
||||
sorted_output = parse_sorted_output(result.stdout) if result else None
|
||||
|
||||
# Check result
|
||||
if sorted_output == test.expected:
|
||||
print(color_text(f"[PASS] Test '{test.c}' passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' failed.", Fore.RED))
|
||||
print(f"Expected:\n{test.expected}")
|
||||
print(f"Got:\n{sorted_output}")
|
||||
if test.debug:
|
||||
print(color_text("[DEBUG] Full Program Output:", Fore.YELLOW))
|
||||
print(result.stdout)
|
||||
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
19
2025.02.28/6Ex/Makefile
Normal file
19
2025.02.28/6Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a06.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a06.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
4
2025.02.28/6Ex/a.txt
Normal file
4
2025.02.28/6Ex/a.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
apple
|
||||
banana
|
||||
cherry
|
||||
date
|
73
2025.02.28/6Ex/array.c
Normal file
73
2025.02.28/6Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/6Ex/array.h
Normal file
15
2025.02.28/6Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
14
2025.02.28/6Ex/io_status.h
Normal file
14
2025.02.28/6Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
68
2025.02.28/6Ex/main.c
Normal file
68
2025.02.28/6Ex/main.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./a06.out c n p filename */
|
||||
int c, n, p, diff, task = 6;
|
||||
io_status ret;
|
||||
char *name, **a;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &c) && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p) == 1 && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s c n p name\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[4];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, p);
|
||||
t = clock();
|
||||
t6_solve(a, n, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
diff = check(a, n, cmp);
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(a, n, p);
|
||||
printf("%s : Task = %d Diff = %d Elapsed = %.2f\n", argv[0], task, diff, t);
|
||||
|
||||
free_array(a, n);
|
||||
free(a);
|
||||
|
||||
return 0;
|
||||
}
|
21
2025.02.28/6Ex/solve.c
Normal file
21
2025.02.28/6Ex/solve.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include "solve.h"
|
||||
|
||||
void t6_solve(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < n; ++i) append(a, i+1, a[i], find(a, i, a[i], cmp));
|
||||
}
|
||||
|
||||
int find(char **arr, int n, char *a, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<n; ++i) if (cmp(a, arr[i]) < 0) break;
|
||||
return i;
|
||||
}
|
||||
|
||||
void append(char **arr, int n, char *a, int index)
|
||||
{
|
||||
for (int i = n-1; i > index; --i) arr[i] = arr[i-1];
|
||||
arr[index] = a;
|
||||
}
|
||||
|
10
2025.02.28/6Ex/solve.h
Normal file
10
2025.02.28/6Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void t6_solve(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
int find(char **arr, int n, char *a, int (*cmp)(const char *, const char *));
|
||||
void append(char **arr, int n, char *a, int index);
|
||||
|
||||
#endif
|
23
2025.02.28/6Ex/sort.c
Normal file
23
2025.02.28/6Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/6Ex/sort.h
Normal file
11
2025.02.28/6Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
231
2025.02.28/6Ex/test_cases.json
Normal file
231
2025.02.28/6Ex/test_cases.json
Normal file
|
@ -0,0 +1,231 @@
|
|||
{
|
||||
"exe": "a06.exe",
|
||||
"filename": "input.txt",
|
||||
"tests": [
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "zoo\napple\nbanana\ncherry",
|
||||
"expected": "zoo\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "a\nabc\nabcd\nabcdef"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "abcdef\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "1\n12\n123\n1234\n12345"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "12345\n1234\n123\n12\n1"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "hello\nHELLO\nhElLo\nHeLLo",
|
||||
"expected": "HELLO\nHeLLo\nhElLo\nhello"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "A\nB\nC\nD\nE"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "E\nD\nC\nB\nA"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "c\nbb\naaa"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "aaa\nbb\nc"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "3\n1\n4\n1\n5\n9\n2\n6\n5\n3",
|
||||
"expected": "1\n1\n2\n3\n3\n4\n5\n5\n6\n9"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "hi\ncode\nhello\nworld"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "world\nhello\ncode\nhi"
|
||||
}
|
||||
]
|
||||
}
|
138
2025.02.28/6Ex/test_runner.py
Normal file
138
2025.02.28/6Ex/test_runner.py
Normal file
|
@ -0,0 +1,138 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, c, text, expected, p=None, debug=False):
|
||||
self.c = c
|
||||
self.text = text
|
||||
self.expected = expected
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
|
||||
def get_num_lines(self):
|
||||
"""Returns the number of lines in the input text (n)"""
|
||||
return self.text.count("\n") + 1 # +1, чтобы учесть последнюю строку
|
||||
|
||||
def should_fail(self):
|
||||
"""Checks if the test expects a failure"""
|
||||
return self.expected.lower() == "fall"
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def parse_sorted_output(output):
|
||||
"""Extracts the second print_array output from program output"""
|
||||
parts = output.split("New array:\n")
|
||||
if len(parts) > 1:
|
||||
sorted_array = parts[1].rstrip().split("\n")
|
||||
sorted_array = sorted_array[:-1] # Убираем последнюю строку (Task = ... Diff = ... Elapsed = ...)
|
||||
return "\n".join(sorted_array)
|
||||
return ""
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
n = test.get_num_lines() # Auto-count lines in input
|
||||
p = test.p if test.p is not None else n # Default p = n
|
||||
|
||||
# Write input data to a file (Ensure last line has \n)
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
text = test.text.rstrip() + "\n" # Если нет \n в конце, добавляем
|
||||
f.write(text)
|
||||
|
||||
# Windows fix: remove './' for executables
|
||||
cmd = [exe, str(test.c), str(n), str(p), filename]
|
||||
if test.debug:
|
||||
cmd.append("DEBUG")
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# Check if test expected failure
|
||||
if test.should_fail():
|
||||
if result and result.returncode != 0:
|
||||
print(color_text(f"[PASS] Test '{test.c}' correctly failed (expected crash).", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' should have failed but did not.", Fore.RED))
|
||||
return
|
||||
|
||||
# Extract sorted array output
|
||||
sorted_output = parse_sorted_output(result.stdout) if result else None
|
||||
|
||||
# Check result
|
||||
if sorted_output == test.expected:
|
||||
print(color_text(f"[PASS] Test '{test.c}' passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' failed.", Fore.RED))
|
||||
print(f"Expected:\n{test.expected}")
|
||||
print(f"Got:\n{sorted_output}")
|
||||
if test.debug:
|
||||
print(color_text("[DEBUG] Full Program Output:", Fore.YELLOW))
|
||||
print(result.stdout)
|
||||
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
19
2025.02.28/7Ex/Makefile
Normal file
19
2025.02.28/7Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a07.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a07.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
8
2025.02.28/7Ex/a.txt
Normal file
8
2025.02.28/7Ex/a.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
zzzzzzzz
|
||||
zzzzzzz
|
||||
zzzzzz
|
||||
zzzzz
|
||||
zzzz
|
||||
zzz
|
||||
zz
|
||||
z
|
73
2025.02.28/7Ex/array.c
Normal file
73
2025.02.28/7Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/7Ex/array.h
Normal file
15
2025.02.28/7Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
14
2025.02.28/7Ex/io_status.h
Normal file
14
2025.02.28/7Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
68
2025.02.28/7Ex/main.c
Normal file
68
2025.02.28/7Ex/main.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./a07.out c n p filename */
|
||||
int c, n, p, diff, task = 7;
|
||||
io_status ret;
|
||||
char *name, **a;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &c) && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p) == 1 && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s c n p name\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[4];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, p);
|
||||
t = clock();
|
||||
t7_solve(a, n, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
diff = check(a, n, cmp);
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(a, n, p);
|
||||
printf("%s : Task = %d Diff = %d Elapsed = %.2f\n", argv[0], task, diff, t);
|
||||
|
||||
free_array(a, n);
|
||||
free(a);
|
||||
|
||||
return 0;
|
||||
}
|
25
2025.02.28/7Ex/solve.c
Normal file
25
2025.02.28/7Ex/solve.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include "solve.h"
|
||||
|
||||
void t7_solve(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < n; ++i) append(a, i+1, a[i], find(a, i, a[i], cmp));
|
||||
}
|
||||
|
||||
int find(char **a, int n, char *x, int (*cmp)(const char *, const char *)) {
|
||||
int avg = (n + (-1)*(n%2)) / 2;
|
||||
int comp;
|
||||
|
||||
if (n == 0) return 0;
|
||||
|
||||
comp = cmp(x, a[avg]);
|
||||
if (comp < 0) return find(a, avg, x, cmp);
|
||||
if (comp > 0) return avg+1 + find(a+(avg+1), n-(avg+1), x, cmp);
|
||||
else return avg;
|
||||
}
|
||||
|
||||
void append(char **arr, int n, char *a, int index)
|
||||
{
|
||||
for (int i = n-1; i > index; --i) arr[i] = arr[i-1];
|
||||
arr[index] = a;
|
||||
}
|
10
2025.02.28/7Ex/solve.h
Normal file
10
2025.02.28/7Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void t7_solve(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
int find(char **arr, int n, char *a, int (*cmp)(const char *, const char *));
|
||||
void append(char **arr, int n, char *a, int index);
|
||||
|
||||
#endif
|
23
2025.02.28/7Ex/sort.c
Normal file
23
2025.02.28/7Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/7Ex/sort.h
Normal file
11
2025.02.28/7Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
231
2025.02.28/7Ex/test_cases.json
Normal file
231
2025.02.28/7Ex/test_cases.json
Normal file
|
@ -0,0 +1,231 @@
|
|||
{
|
||||
"exe": "a07.exe",
|
||||
"filename": "input.txt",
|
||||
"tests": [
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "zoo\napple\nbanana\ncherry",
|
||||
"expected": "zoo\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "a\nabc\nabcd\nabcdef"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "abcdef\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "1\n12\n123\n1234\n12345"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "12345\n1234\n123\n12\n1"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "hello\nHELLO\nhElLo\nHeLLo",
|
||||
"expected": "HELLO\nHeLLo\nhElLo\nhello"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "A\nB\nC\nD\nE"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "E\nD\nC\nB\nA"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "c\nbb\naaa"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "aaa\nbb\nc"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "3\n1\n4\n1\n5\n9\n2\n6\n5\n3",
|
||||
"expected": "1\n1\n2\n3\n3\n4\n5\n5\n6\n9"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "hi\ncode\nhello\nworld"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "world\nhello\ncode\nhi"
|
||||
}
|
||||
]
|
||||
}
|
138
2025.02.28/7Ex/test_runner.py
Normal file
138
2025.02.28/7Ex/test_runner.py
Normal file
|
@ -0,0 +1,138 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, c, text, expected, p=None, debug=False):
|
||||
self.c = c
|
||||
self.text = text
|
||||
self.expected = expected
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
|
||||
def get_num_lines(self):
|
||||
"""Returns the number of lines in the input text (n)"""
|
||||
return self.text.count("\n") + 1 # +1, чтобы учесть последнюю строку
|
||||
|
||||
def should_fail(self):
|
||||
"""Checks if the test expects a failure"""
|
||||
return self.expected.lower() == "fall"
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def parse_sorted_output(output):
|
||||
"""Extracts the second print_array output from program output"""
|
||||
parts = output.split("New array:\n")
|
||||
if len(parts) > 1:
|
||||
sorted_array = parts[1].rstrip().split("\n")
|
||||
sorted_array = sorted_array[:-1] # Убираем последнюю строку (Task = ... Diff = ... Elapsed = ...)
|
||||
return "\n".join(sorted_array)
|
||||
return ""
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
n = test.get_num_lines() # Auto-count lines in input
|
||||
p = test.p if test.p is not None else n # Default p = n
|
||||
|
||||
# Write input data to a file (Ensure last line has \n)
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
text = test.text.rstrip() + "\n" # Если нет \n в конце, добавляем
|
||||
f.write(text)
|
||||
|
||||
# Windows fix: remove './' for executables
|
||||
cmd = [exe, str(test.c), str(n), str(p), filename]
|
||||
if test.debug:
|
||||
cmd.append("DEBUG")
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# Check if test expected failure
|
||||
if test.should_fail():
|
||||
if result and result.returncode != 0:
|
||||
print(color_text(f"[PASS] Test '{test.c}' correctly failed (expected crash).", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' should have failed but did not.", Fore.RED))
|
||||
return
|
||||
|
||||
# Extract sorted array output
|
||||
sorted_output = parse_sorted_output(result.stdout) if result else None
|
||||
|
||||
# Check result
|
||||
if sorted_output == test.expected:
|
||||
print(color_text(f"[PASS] Test '{test.c}' passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' failed.", Fore.RED))
|
||||
print(f"Expected:\n{test.expected}")
|
||||
print(f"Got:\n{sorted_output}")
|
||||
if test.debug:
|
||||
print(color_text("[DEBUG] Full Program Output:", Fore.YELLOW))
|
||||
print(result.stdout)
|
||||
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
47
2025.02.28/8Ex/'
Normal file
47
2025.02.28/8Ex/'
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "solve.h"
|
||||
|
||||
#include "array.h"
|
||||
|
||||
void t8_solve(char **a, char **b, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i = 2, j, margin;
|
||||
do {
|
||||
int hf_i = i/2;
|
||||
for (j = 0; j < n; j += i)
|
||||
{
|
||||
if (n < j+i)
|
||||
{
|
||||
margin = n-(j+hf_i);
|
||||
if (margin <= 0) break;
|
||||
} else margin = hf_i;
|
||||
|
||||
unite(a+j, a+(j+hf_i), b, hf_i, margin, cmp);
|
||||
for (int k = 0; k < hf_i+margin; k++) a[i+j] = b[i];
|
||||
}
|
||||
|
||||
printf("\ni: %d:\n", i);
|
||||
print_array(a, n, n);
|
||||
i *= 2;
|
||||
} while (i < n);
|
||||
}
|
||||
|
||||
void unite(char **arr_a, char **arr_b, char **arr_c, int n, int m, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i, j, k, cntr, len = n + m; // cntr - contrast
|
||||
for (i = 0, j = 0, k = 0; k < len; ++k)
|
||||
{
|
||||
if (i >= n)
|
||||
{
|
||||
arr_c[k] = arr_b[j++];
|
||||
} else if (j >= m)
|
||||
{
|
||||
arr_c[k] = arr_a[i++];
|
||||
} else
|
||||
{
|
||||
cntr = cmp(arr_a[i], arr_b[j]);
|
||||
if (cntr <= 0) arr_c[k] = arr_a[i++];
|
||||
else arr_c[k] = arr_b[j++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
19
2025.02.28/8Ex/Makefile
Normal file
19
2025.02.28/8Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a08.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a08.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
9
2025.02.28/8Ex/a.txt
Normal file
9
2025.02.28/8Ex/a.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
9
|
||||
8
|
||||
7
|
||||
6
|
||||
5
|
||||
4
|
||||
3
|
||||
2
|
||||
1
|
73
2025.02.28/8Ex/array.c
Normal file
73
2025.02.28/8Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/8Ex/array.h
Normal file
15
2025.02.28/8Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
14
2025.02.28/8Ex/io_status.h
Normal file
14
2025.02.28/8Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
76
2025.02.28/8Ex/main.c
Normal file
76
2025.02.28/8Ex/main.c
Normal file
|
@ -0,0 +1,76 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./a08.out c n p filename */
|
||||
int c, n, p, diff, task = 8;
|
||||
io_status ret;
|
||||
char *name, **a, **b;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &c) && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p) == 1 && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s c n p name\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[4];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
} if (!(b = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
|
||||
free(a);
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
memset(b, 0, (n * sizeof(char *)));
|
||||
ret = read_array(a, n, name);
|
||||
|
||||
do {
|
||||
switch(ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, p);
|
||||
t = clock();
|
||||
t8_solve(a, b, n, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
diff = check(a, n, cmp);
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(a, n, p);
|
||||
printf("%s : Task = %d Diff = %d Elapsed = %.2f\n", argv[0], task, diff, t);
|
||||
|
||||
free_array(a, n);
|
||||
free(a);
|
||||
free(b);
|
||||
|
||||
return 0;
|
||||
}
|
43
2025.02.28/8Ex/solve.c
Normal file
43
2025.02.28/8Ex/solve.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include "solve.h"
|
||||
|
||||
void t8_solve(char **a, char **b, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i = 2, j, margin;
|
||||
do {
|
||||
int hf_i = i/2;
|
||||
for (j = 0; j < n; j += i)
|
||||
{
|
||||
if (n < j+i)
|
||||
{
|
||||
margin = n-(j+hf_i);
|
||||
if (margin <= 0) break;
|
||||
} else margin = hf_i;
|
||||
|
||||
unite(a+j, a+(j+hf_i), b, hf_i, margin, cmp);
|
||||
for (int k = 0; k < hf_i+margin; k++) a[j+k] = b[k];
|
||||
}
|
||||
|
||||
i *= 2;
|
||||
} while (i < n*2);
|
||||
}
|
||||
|
||||
void unite(char **arr_a, char **arr_b, char **arr_c, int n, int m, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i, j, k, cntr, len = n + m; // cntr - contrast
|
||||
for (i = 0, j = 0, k = 0; k < len; ++k)
|
||||
{
|
||||
if (i >= n)
|
||||
{
|
||||
arr_c[k] = arr_b[j++];
|
||||
} else if (j >= m)
|
||||
{
|
||||
arr_c[k] = arr_a[i++];
|
||||
} else
|
||||
{
|
||||
cntr = cmp(arr_a[i], arr_b[j]);
|
||||
if (cntr <= 0) arr_c[k] = arr_a[i++];
|
||||
else arr_c[k] = arr_b[j++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
9
2025.02.28/8Ex/solve.h
Normal file
9
2025.02.28/8Ex/solve.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void t8_solve(char **a, char **b, int n, int (*cmp)(const char *, const char *));
|
||||
void unite(char **arr_a, char **arr_b, char **arr_c, int n, int m, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
23
2025.02.28/8Ex/sort.c
Normal file
23
2025.02.28/8Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/8Ex/sort.h
Normal file
11
2025.02.28/8Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
231
2025.02.28/8Ex/test_cases.json
Normal file
231
2025.02.28/8Ex/test_cases.json
Normal file
|
@ -0,0 +1,231 @@
|
|||
{
|
||||
"exe": "a08.exe",
|
||||
"filename": "input.txt",
|
||||
"tests": [
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "1\n3\n2\n4\n5",
|
||||
"expected": "1\n2\n3\n4\n5"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "date\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "a\nabc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "a\nabcde\nabc\nabcd",
|
||||
"expected": "abcde\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "",
|
||||
"expected": ""
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "onlyone",
|
||||
"expected": "onlyone"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "same\nsame\nsame\nsame",
|
||||
"expected": "same\nsame\nsame\nsame"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "apple\nbanana\ncherry\ndate",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "date\ncherry\nbanana\napple",
|
||||
"expected": "apple\nbanana\ncherry\ndate"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "a\nbbbbbbbbbbbbbbbbbbbbbbbb\nccc",
|
||||
"expected": "a\nccc\nbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "Apple\nbanana\nCherry\ndate",
|
||||
"expected": "Apple\nCherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": " apple\nbanana\n cherry\ndate",
|
||||
"expected": " apple\n cherry\nbanana\ndate"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "10\n2\n1",
|
||||
"expected": "1\n10\n2"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "abc\nabcd\nabcde",
|
||||
"expected": "abc\nabcd\nabcde"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "zoo\napple\nbanana\ncherry",
|
||||
"expected": "zoo\ncherry\nbanana\napple"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "a\nabc\nabcd\nabcdef"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "abcd\na\nabcdef\nabc",
|
||||
"expected": "abcdef\nabcd\nabc\na"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "1\n12\n123\n1234\n12345"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "12345\n1234\n123\n12\n1",
|
||||
"expected": "12345\n1234\n123\n12\n1"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "hello\nHELLO\nhElLo\nHeLLo",
|
||||
"expected": "HELLO\nHeLLo\nhElLo\nhello"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "A\nB\nC\nD\nE"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "A\nB\nC\nD\nE",
|
||||
"expected": "E\nD\nC\nB\nA"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "c\nbb\naaa"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "aaa\nbb\nc",
|
||||
"expected": "aaa\nbb\nc"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"text": "z\nzz\nzzz\nzzzz\nzzzzz\nzzzzzz\nzzzzzzz\nzzzzzzzz",
|
||||
"expected": "zzzzzzzz\nzzzzzzz\nzzzzzz\nzzzzz\nzzzz\nzzz\nzz\nz"
|
||||
},
|
||||
{
|
||||
"c": 1,
|
||||
"text": "3\n1\n4\n1\n5\n9\n2\n6\n5\n3",
|
||||
"expected": "1\n1\n2\n3\n3\n4\n5\n5\n6\n9"
|
||||
},
|
||||
{
|
||||
"c": 3,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "hi\ncode\nhello\nworld"
|
||||
},
|
||||
{
|
||||
"c": 4,
|
||||
"text": "hello\nworld\nhi\ncode",
|
||||
"expected": "world\nhello\ncode\nhi"
|
||||
}
|
||||
]
|
||||
}
|
138
2025.02.28/8Ex/test_runner.py
Normal file
138
2025.02.28/8Ex/test_runner.py
Normal file
|
@ -0,0 +1,138 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, c, text, expected, p=None, debug=False):
|
||||
self.c = c
|
||||
self.text = text
|
||||
self.expected = expected
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
|
||||
def get_num_lines(self):
|
||||
"""Returns the number of lines in the input text (n)"""
|
||||
return self.text.count("\n") + 1 # +1, чтобы учесть последнюю строку
|
||||
|
||||
def should_fail(self):
|
||||
"""Checks if the test expects a failure"""
|
||||
return self.expected.lower() == "fall"
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def parse_sorted_output(output):
|
||||
"""Extracts the second print_array output from program output"""
|
||||
parts = output.split("New array:\n")
|
||||
if len(parts) > 1:
|
||||
sorted_array = parts[1].rstrip().split("\n")
|
||||
sorted_array = sorted_array[:-1] # Убираем последнюю строку (Task = ... Diff = ... Elapsed = ...)
|
||||
return "\n".join(sorted_array)
|
||||
return ""
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
n = test.get_num_lines() # Auto-count lines in input
|
||||
p = test.p if test.p is not None else n # Default p = n
|
||||
|
||||
# Write input data to a file (Ensure last line has \n)
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
text = test.text.rstrip() + "\n" # Если нет \n в конце, добавляем
|
||||
f.write(text)
|
||||
|
||||
# Windows fix: remove './' for executables
|
||||
cmd = [exe, str(test.c), str(n), str(p), filename]
|
||||
if test.debug:
|
||||
cmd.append("DEBUG")
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# Check if test expected failure
|
||||
if test.should_fail():
|
||||
if result and result.returncode != 0:
|
||||
print(color_text(f"[PASS] Test '{test.c}' correctly failed (expected crash).", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' should have failed but did not.", Fore.RED))
|
||||
return
|
||||
|
||||
# Extract sorted array output
|
||||
sorted_output = parse_sorted_output(result.stdout) if result else None
|
||||
|
||||
# Check result
|
||||
if sorted_output == test.expected:
|
||||
print(color_text(f"[PASS] Test '{test.c}' passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test '{test.c}' failed.", Fore.RED))
|
||||
print(f"Expected:\n{test.expected}")
|
||||
print(f"Got:\n{sorted_output}")
|
||||
if test.debug:
|
||||
print(color_text("[DEBUG] Full Program Output:", Fore.YELLOW))
|
||||
print(result.stdout)
|
||||
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
19
2025.02.28/9Ex/Makefile
Normal file
19
2025.02.28/9Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a09.exe: main.o array.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a09.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
10
2025.02.28/9Ex/a.txt
Normal file
10
2025.02.28/9Ex/a.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
3
|
||||
1
|
||||
4
|
||||
1
|
||||
5
|
||||
9
|
||||
2
|
||||
6
|
||||
5
|
||||
3
|
73
2025.02.28/9Ex/array.c
Normal file
73
2025.02.28/9Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/9Ex/array.h
Normal file
15
2025.02.28/9Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue