Skip to content

Commit f9a11bf

Browse files
committed
labs/lab-10: Add checker infrastructure
- Added for each task a directory named tests - Added for each task a specified checker in directory tests Fixes #43 Signed-off-by: Alexandru Braslasu <[email protected]>
1 parent 7dc238e commit f9a11bf

33 files changed

+1182
-2
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CC = gcc
2+
CFLAGS = -Wall -Wextra -O2
3+
TARGET = test_overflow_for_binary
4+
5+
SRCS = test_overflow_for_binary.c graded_test.c
6+
7+
OBJS = $(SRCS:.c=.o)
8+
9+
all: $(TARGET)
10+
11+
$(TARGET): $(OBJS)
12+
$(CC) $(CFLAGS) $(OBJS) -o $@
13+
14+
%.o: %.c
15+
$(CC) $(CFLAGS) -c $< -o $@
16+
17+
clean:
18+
rm -f $(TARGET) $(OBJS)
19+
20+
check: all
21+
chmod +x run_all_tests.h
22+
./run_all_tests.h
23+
24+
.PHONY: all clean test
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <unistd.h>
6+
#include <sys/param.h>
7+
8+
#include "./graded_test.h"
9+
10+
static void my_itoa(size_t num, char *a)
11+
{
12+
unsigned char digit3;
13+
unsigned char digit2;
14+
unsigned char digit1;
15+
16+
/* Print at most 3 decimal digits. */
17+
if (num > 999)
18+
num = 999;
19+
20+
digit1 = num % 10;
21+
num /= 10;
22+
digit2 = num % 10;
23+
num /= 10;
24+
digit3 = num % 10;
25+
26+
if (digit3 == 0)
27+
a[0] = ' ';
28+
else
29+
a[0] = '0' + digit3;
30+
31+
if (digit2 == 0)
32+
a[1] = ' ';
33+
else
34+
a[1] = '0' + digit2;
35+
36+
a[2] = '0' + digit1;
37+
}
38+
39+
/*
40+
* Print test result. Printed message should fit in 72 characters.
41+
*
42+
* Print format is:
43+
*
44+
* description ...................... passed ... NNN
45+
* description ...................... failed ... NNN
46+
* 32 chars 24 chars 6 3 3
47+
*/
48+
49+
static void print_test(const char *description, int result, size_t points)
50+
{
51+
/* Make these global linkage, so it's only allocated once. */
52+
static char print_buffer[74];
53+
static const char failed[] = "failed";
54+
static const char passed[] = "passed";
55+
size_t i;
56+
size_t len;
57+
58+
/* Collect description in print_buffer. */
59+
len = MIN(strlen(description), 32);
60+
for (i = 0; i < len; i++)
61+
print_buffer[i] = description[i];
62+
63+
/* Collect dots in print_buffer. */
64+
for (i = 0; i < 40; i++)
65+
print_buffer[12+i] = '.';
66+
67+
/* Collect passed / failed. */
68+
for (i = 0; i < 6; i++) {
69+
if (result == 1)
70+
print_buffer[52+i] = passed[i];
71+
else
72+
print_buffer[52+i] = failed[i];
73+
}
74+
75+
/* Collect newline. */
76+
print_buffer[59] = '\n';
77+
78+
write(1, print_buffer, 58);
79+
}
80+
81+
void run_test(struct graded_test *test)
82+
{
83+
int res;
84+
85+
res = test->function();
86+
print_test(test->description, res, test->points);
87+
#ifdef EXIT_IF_FAIL
88+
exit(EXIT_FAILURE);
89+
#endif
90+
}
91+
92+
void run_tests(struct graded_test *tests, size_t count)
93+
{
94+
size_t i;
95+
96+
for (i = 0; i < count; i++)
97+
run_test(&tests[i]);
98+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include <stddef.h>
4+
5+
#ifndef GRADED_TEST_H_
6+
#define GRADED_TEST_H_ 1
7+
8+
/* test function prototype */
9+
typedef int (*test_f)(void);
10+
11+
struct graded_test {
12+
test_f function; /* test/evaluation function */
13+
char *description; /* test description */
14+
size_t points; /* points for each test */
15+
};
16+
17+
void run_test(struct graded_test *test);
18+
void run_tests(struct graded_test *tests, size_t count);
19+
20+
#endif /* GRADED_TEST_H_ */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
./test_overflow_for_binary | tee results.txt
5+
6+
total=$(if tail -n 1 results.txt | grep -q 'passed$'; then echo 100; else echo 0; fi)
7+
echo ""
8+
echo -n "Total: "
9+
echo -n " "
10+
LC_ALL=C printf "%3d/100\n" "$total"
11+
12+
rm results.txt
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <stdlib.h>
6+
#include <assert.h>
7+
8+
#include "graded_test.h"
9+
10+
char *correct_payload = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPR0N";
11+
12+
static int test_payload(void)
13+
{
14+
char payload[1000];
15+
16+
printf("Spune payload-ul: ");
17+
fflush(stdout);
18+
19+
fgets(payload, 1000, stdin);
20+
21+
payload[strlen(payload) - 1] = '\0';
22+
return !strcmp(payload, correct_payload);
23+
}
24+
25+
static struct graded_test all_tests[] = {
26+
{ test_payload, "test_payload", 100},
27+
};
28+
29+
int main(void)
30+
{
31+
run_tests(all_tests, sizeof(all_tests) / sizeof(all_tests[0]));
32+
return 0;
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
CC = gcc
2+
CFLAGS = -Wall -Wextra -O2
3+
4+
SRCS1 = test_overflow_in_C.c graded_test.c
5+
OBJS1 = $(SRCS1:.c=.o)
6+
TARGET1 = test_overflow_in_C
7+
8+
SRCS2 = ../support/do_overflow.c
9+
OBJS2 = $(patsubst ../support/%.c, %.o, $(SRCS2))
10+
TARGET2 = do_overflow
11+
12+
all: $(TARGET1) $(TARGET2)
13+
14+
$(TARGET1): $(OBJS1)
15+
$(CC) $(CFLAGS) $^ -o $@
16+
17+
$(TARGET2): $(OBJS2)
18+
$(CC) $(CFLAGS) $^ -o $@
19+
20+
%.o: %.c
21+
$(CC) $(CFLAGS) -c $< -o $@
22+
23+
%.o: ../support/%.c
24+
$(CC) $(CFLAGS) -c $< -o $@
25+
26+
check: $(TARGET1)
27+
chmod +x run_all_tests.h
28+
./run_all_tests.h
29+
30+
clean:
31+
rm -f $(TARGET1) $(TARGET2) $(OBJS1) $(OBJS2)
32+
33+
.PHONY: all check clean
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include <string.h>
4+
#include <stdlib.h>
5+
#include <unistd.h>
6+
#include <sys/param.h>
7+
8+
#include "./graded_test.h"
9+
10+
static void my_itoa(size_t num, char *a)
11+
{
12+
unsigned char digit3;
13+
unsigned char digit2;
14+
unsigned char digit1;
15+
16+
/* Print at most 3 decimal digits. */
17+
if (num > 999)
18+
num = 999;
19+
20+
digit1 = num % 10;
21+
num /= 10;
22+
digit2 = num % 10;
23+
num /= 10;
24+
digit3 = num % 10;
25+
26+
if (digit3 == 0)
27+
a[0] = ' ';
28+
else
29+
a[0] = '0' + digit3;
30+
31+
if (digit2 == 0)
32+
a[1] = ' ';
33+
else
34+
a[1] = '0' + digit2;
35+
36+
a[2] = '0' + digit1;
37+
}
38+
39+
/*
40+
* Print test result. Printed message should fit in 72 characters.
41+
*
42+
* Print format is:
43+
*
44+
* description ...................... passed ... NNN
45+
* description ...................... failed ... NNN
46+
* 32 chars 24 chars 6 3 3
47+
*/
48+
49+
static void print_test(const char *description, int result, size_t points)
50+
{
51+
/* Make these global linkage, so it's only allocated once. */
52+
static char print_buffer[74];
53+
static const char failed[] = "failed";
54+
static const char passed[] = "passed";
55+
size_t i;
56+
size_t len;
57+
58+
/* Collect description in print_buffer. */
59+
len = MIN(strlen(description), 32);
60+
for (i = 0; i < len; i++)
61+
print_buffer[i] = description[i];
62+
63+
/* Collect dots in print_buffer. */
64+
for (i = 0; i < 40; i++)
65+
print_buffer[12+i] = '.';
66+
67+
/* Collect passed / failed. */
68+
for (i = 0; i < 6; i++) {
69+
if (result == 1)
70+
print_buffer[52+i] = passed[i];
71+
else
72+
print_buffer[52+i] = failed[i];
73+
}
74+
75+
/* Collect newline. */
76+
print_buffer[59] = '\n';
77+
78+
write(1, print_buffer, 58);
79+
}
80+
81+
void run_test(struct graded_test *test)
82+
{
83+
int res;
84+
85+
res = test->function();
86+
print_test(test->description, res, test->points);
87+
#ifdef EXIT_IF_FAIL
88+
exit(EXIT_FAILURE);
89+
#endif
90+
}
91+
92+
void run_tests(struct graded_test *tests, size_t count)
93+
{
94+
size_t i;
95+
96+
for (i = 0; i < count; i++)
97+
run_test(&tests[i]);
98+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause */
2+
3+
#include <stddef.h>
4+
5+
#ifndef GRADED_TEST_H_
6+
#define GRADED_TEST_H_ 1
7+
8+
/* test function prototype */
9+
typedef int (*test_f)(void);
10+
11+
struct graded_test {
12+
test_f function; /* test/evaluation function */
13+
char *description; /* test description */
14+
size_t points; /* points for each test */
15+
};
16+
17+
void run_test(struct graded_test *test);
18+
void run_tests(struct graded_test *tests, size_t count);
19+
20+
#endif /* GRADED_TEST_H_ */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
./test_overflow_in_C | tee results.txt
5+
6+
total=$(if tail -n 1 results.txt | grep -q 'passed$'; then echo 100; else echo 0; fi)
7+
echo ""
8+
echo -n "Total: "
9+
echo -n " "
10+
LC_ALL=C printf "%3d/100\n" "$total"
11+
12+
rm results.txt
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <stdlib.h>
6+
#include <assert.h>
7+
8+
#include "graded_test.h"
9+
10+
char *correct_payload = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMIAU";
11+
12+
static int test_payload(void)
13+
{
14+
char payload[1000];
15+
16+
printf("Spune payload-ul: ");
17+
fflush(stdout);
18+
19+
fgets(payload, 1000, stdin);
20+
21+
payload[strlen(payload) - 1] = '\0';
22+
return !strcmp(payload, correct_payload);
23+
}
24+
25+
static struct graded_test all_tests[] = {
26+
{ test_payload, "test_payload", 100},
27+
};
28+
29+
int main(void)
30+
{
31+
run_tests(all_tests, sizeof(all_tests) / sizeof(all_tests[0]));
32+
return 0;
33+
}
Binary file not shown.

0 commit comments

Comments
 (0)