HW3: test package

This commit is contained in:
Claudio Maggioni 2019-12-10 16:49:54 +01:00
parent 13c8a836d3
commit 6c6a445831
15 changed files with 1277 additions and 1 deletions

146
chess_paths/Makefile Normal file
View file

@ -0,0 +1,146 @@
OBJECTS = chess_paths.o
PROGRAMS = paths_io
CFLAGS=-Wall -g
CXXFLAGS=-Wall -g
SHELL=/bin/bash
TIMEOUT=8
TESTS_DIR=tests
TESTS_SH:=$(wildcard $(TESTS_DIR)/*.sh)
TESTS_SH_NAMES:=$(patsubst $(TESTS_DIR)/%.sh, %, $(TESTS_SH))
TESTS_IO:=$(wildcard $(TESTS_DIR)/*.in)
TESTS_IO_NAMES:=$(patsubst $(TESTS_DIR)/%.in, %, $(TESTS_IO))
TESTS_C:=$(wildcard $(TESTS_DIR)/*.c)
TESTS_CXX:=$(wildcard $(TESTS_DIR)/*.cc)
TESTS_BIN:=$(patsubst $(TESTS_DIR)/%.c, $(TESTS_DIR)/%, $(TESTS_C)) \
$(patsubst $(TESTS_DIR)/%.cc, $(TESTS_DIR)/%, $(TESTS_CXX))
TESTS_BIN_NAMES:=$(patsubst $(TESTS_DIR)/%.c, %, $(TESTS_C)) $(patsubst $(TESTS_DIR)/%.cc, %, $(TESTS_CXX))
.PHONY: all
all: compile check
.PHONY: compile-program
compile: $(PROGRAMS) $(OBJECTS)
paths_io: paths_io.c chess_paths.o
$(CC) $(CFLAGS) $(LDFLAGS) paths_io.c chess_paths.o -o $@
.PHONY: check
check: check-bin check-io-sh
.PHONY: check-io-sh
check-io-sh: compile $(TESTS_IO) $(TESTS_SH)
@exec 2> /dev/null; \
for p in $(foreach prog,$(PROGRAMS),$(dir $(prog))$(prog)); do \
echo "Testing $${p}:" ; \
for t in $(TESTS_IO_NAMES); do \
echo -n "Running test $$t..." ; \
"$$p" < "$(TESTS_DIR)/$$t.in" > "$$t.out" 2>&1 & \
prog_pid=$$!; \
( sleep $(TIMEOUT); kill $$prog_pid > /dev/null 2>&1 ) & \
killer_pid=$$!; \
wait $$prog_pid; \
res=$$?; \
if test $$res -gt 128; \
then \
case `kill -l $$(($$res - 128))` in \
ABRT ) echo "FAIL"; ;; \
TERM ) echo "TIME OUT"; ;; \
* ) echo "UNKNOWN ERROR"; ;; \
esac ; \
echo "see $(TESTS_DIR)/$$t.in" ;\
echo "you may run $$p < $(TESTS_DIR)/$$t.in" ;\
echo "to see what went wrong";\
rm -f "$$t.out" ;\
else \
kill $$killer_pid > /dev/null 2>&1 ;\
wait $$killer_pid; \
if cmp -s "$$t.out" "$(TESTS_DIR)/$$t.expected"; \
then \
echo "PASS" ;\
rm -f "$$t.out" ;\
else \
echo "FAIL" ;\
echo "see $(TESTS_DIR)/$$t.sh" ;\
echo "run diff $$t.out $(TESTS_DIR)/$$t.expected";\
echo "to see the difference between the actual and expected output";\
fi; \
fi; \
done; \
for t in $(TESTS_SH_NAMES); do \
echo -n "Running test $$t..." ; \
$(SHELL) "$(TESTS_DIR)/$$t.sh" "$$p" > "$$t.out" 2>&1 & \
prog_pid=$$!; \
( sleep $(TIMEOUT); kill $$prog_pid > /dev/null 2>&1 ) & \
killer_pid=$$!; \
wait $$prog_pid; \
res=$$?; \
if test $$res -gt 128; \
then \
case `kill -l $$(($$res - 128))` in \
ABRT ) echo "FAIL"; ;; \
TERM ) echo "TIME OUT"; ;; \
* ) echo "UNKNOWN ERROR"; ;; \
esac ; \
echo "see $(TESTS_DIR)/$$t.sh" ;\
echo "you may run $(TESTS_DIR)/$$t.sh $$p" ;\
echo "to see what went wrong";\
rm -f "$$t.out" ;\
else \
kill $$killer_pid > /dev/null 2>&1 ;\
wait $$killer_pid; \
if cmp -s "$$t.out" "$(TESTS_DIR)/$$t.expected"; \
then \
echo "PASS" ;\
rm -f "$$t.out" ;\
else \
echo "FAIL" ;\
echo "see $(TESTS_DIR)/$$t.sh" ;\
echo "run diff $$t.out $(TESTS_DIR)/$$t.expected";\
echo "to see the difference between the actual and expected output";\
fi; \
fi; \
done; \
done
$(TESTS_DIR)/%: $(TESTS_DIR)/%.c $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) $(TESTS_DIR)/$*.c $(OBJECTS) -o $@
$(TESTS_DIR)/%: $(TESTS_DIR)/%.cc $(OBJECTS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(TESTS_DIR)/$*.cc $(OBJECTS) -o $@
.PHONY: check-bin
check-bin: $(TESTS_BIN)
@exec 2> /dev/null; \
for t in $(TESTS_BIN_NAMES); do \
echo -n "Running test $$t..." ; \
"$(TESTS_DIR)/$$t" &\
prog_pid=$$!; \
( sleep $(TIMEOUT); kill $$prog_pid > /dev/null 2>&1 ) & \
killer_pid=$$!; \
wait $$prog_pid; \
res=$$?; \
if test $$res -gt 128; \
then \
case `kill -l $$(($$res - 128))` in \
ABRT ) echo "FAIL"; ;; \
TERM ) echo "TIME OUT"; ;; \
* ) echo "UNKNOWN ERROR"; ;; \
esac ; \
echo "you may run $(TESTS_DIR)/$$t to see what went wrong" ;\
else \
kill $$killer_pid > /dev/null 2>&1 ;\
wait $$killer_pid; \
echo "PASS" ;\
fi; \
done
.PHONY: clean
clean:
rm -f $(PROGRAMS) $(OBJECTS) tests/*.o $(TESTS_BIN)

View file

@ -6,13 +6,13 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
struct position { struct position {
unsigned int column; unsigned int column;
unsigned int row; unsigned int row;
struct position* next; struct position* next;
}; };
#if DEBUG
char* NAMES[] = { "KING", "QUEEN", "ROOK", "BISHOP", "KNIGHT", "PAWN" }; char* NAMES[] = { "KING", "QUEEN", "ROOK", "BISHOP", "KNIGHT", "PAWN" };
void print_p_position(struct piece_position* p) { void print_p_position(struct piece_position* p) {
@ -36,6 +36,7 @@ void print_visited(const struct chessboard* ch, bool* v) {
puts(""); puts("");
} }
} }
#endif
void prepend(struct position** poss_p, unsigned column, unsigned row) { void prepend(struct position** poss_p, unsigned column, unsigned row) {
struct position* prev = *poss_p; struct position* prev = *poss_p;

24
chess_paths/chess_paths.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef CHESS_PATHS_H_INCLUDED
#define CHESS_PATHS_H_INCLUDED
enum chess_piece { KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN };
/* Implemented by the application: */
struct chessboard;
int value_at(const struct chessboard * c, unsigned column, unsigned row);
unsigned int columns(const struct chessboard * c);
unsigned int rows(const struct chessboard * c);
struct piece_position {
enum chess_piece piece;
unsigned int column;
unsigned int row;
};
/* Implemented by you: */
unsigned int increasing_path_len(const struct chessboard * c, struct piece_position * p);
unsigned int decreasing_path_len(const struct chessboard * c, struct piece_position * p);
unsigned int simple_path_len(const struct chessboard * c, struct piece_position * p);
#endif

84
chess_paths/paths_io.c Normal file
View file

@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
int board[];
};
int value_at(const struct chessboard * this, unsigned column, unsigned row) {
row = this->R - row - 1;
return this->board[row*this->C + column];
}
unsigned int columns(const struct chessboard * this) {
return this->C;
}
unsigned int rows(const struct chessboard * this) {
return this->R;
}
struct chessboard * new_board(unsigned int cols, unsigned int rows) {
struct chessboard * this = malloc(sizeof(struct chessboard) + sizeof(int)*cols*rows);
if (this) {
this->C = cols;
this->R = rows;
}
return this;
}
int read_board(struct chessboard * this, FILE * input) {
for (unsigned int i = 0; i < this->C * this->R; ++i)
if (fscanf(input, "%d", this->board + i) != 1)
return 0;
return 1;
}
enum chess_piece char_to_piece(char piece) {
switch (piece) {
case 'P':
case 'p': return PAWN;
case 'N':
case 'n': return KNIGHT;
case 'B':
case 'b': return BISHOP;
case 'R':
case 'r': return ROOK;
case 'Q':
case 'q': return QUEEN;
case 'K':
case 'k': return KING;
default: return PAWN;
}
}
int main() {
unsigned int r, c;
assert (scanf("%u%u", &c, &r) == 2);
struct chessboard * board = new_board (c, r);
assert (board);
assert (read_board(board, stdin));
char piece[2];
char path_type[2];
struct piece_position p;
while (scanf("%1s%1s%u%u", path_type, piece, &c, &r) == 4) {
unsigned int len;
p.column = c;
p.row = r;
p.piece = char_to_piece(*piece);
switch (*path_type) {
case 'i':
case 'I': len = increasing_path_len(board, &p); break;
case 'd':
case 'D': len = decreasing_path_len(board, &p); break;
case 's':
case 'S': len = simple_path_len(board, &p); break;
}
printf("%s (%u,%u) --> (%u,%u) in %u steps\n", piece, c, r, p.column, p.row, len);
}
}

View file

@ -0,0 +1,98 @@
#ifndef BASIC_TESTING_H_INCLUDED
#define BASIC_TESTING_H_INCLUDED
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define ck_assert_msg(expr, ...) \
if (!(expr)) { \
fprintf(stderr, "%s:%d: Assertion '"#expr"' failed\n" , __FILE__, __LINE__); \
fprintf(stderr, ## __VA_ARGS__, NULL); \
return 0; \
}
#define ck_assert_int(X, OP, Y) do { \
intmax_t _ck_x = (X); \
intmax_t _ck_y = (Y); \
if (!(_ck_x OP _ck_y)) { \
fprintf(stderr, "%s:%d: Assertion '%s' failed: %s == %jd, %s == %jd\n", \
__FILE__, __LINE__, #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
return 0; \
} \
} while (0)
#define ck_assert_uint(X, OP, Y) do { \
uintmax_t _ck_x = (X); \
uintmax_t _ck_y = (Y); \
if (!(_ck_x OP _ck_y)) { \
fprintf(stderr, "%s:%d: Assertion '%s' failed: %s == %ju, %s == %ju\n", \
__FILE__, __LINE__, #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
return 0; \
} \
} while (0)
#define assert_int_eq(X, Y) ck_assert_int(X,==,Y)
#define assert_uint_eq(X, Y) ck_assert_uint(X,==,Y)
typedef struct {
const char * name;
int (*test_function)();
const char * file;
int line;
} test_descriptor;
#define TEST(_Name) \
static int _Name ## _fn (); \
static const test_descriptor _Name ## _descr = { # _Name, _Name ## _fn, __FILE__, __LINE__}; \
static const test_descriptor * _Name = & _Name ## _descr; \
static int _Name ## _fn ()
#define TEST_PASSED do { return (1); } while(0)
#define TEST_SUITE(_Name) \
unsigned int _Name ## _failed; \
unsigned int _Name ## _passed; \
const test_descriptor * _Name ## _suite []
#define RUN_SUITE(_Name) \
do { \
_Name ## _failed = 0; \
_Name ## _passed = 0; \
for (int _i = 0; _i < sizeof( _Name ## _suite )/sizeof(test_descriptor *); ++_i) { \
if (!(_Name ## _suite [_i]->test_function())) { \
_Name ## _failed += 1; \
fprintf(stderr, "Test suite "#_Name": test %s failed\n", _Name ## _suite [_i]->name); \
} else { \
_Name ## _passed += 1; \
} \
} \
} while (0)
#define TEST_SUITE_PASS(_Name) (_Name ## _failed == 0)
#define PRINT_SUITE_RESULTS(_Name) \
do { \
printf("Test suite "#_Name": pass %u/%u ", _Name ## _passed, _Name ## _passed + _Name ## _failed); \
} while(0)
static unsigned int tests_run_fail = 0;
static unsigned int tests_run_pass = 0;
#define RUN_TEST(_Name) \
do { \
if (!(_Name ->test_function())) { \
tests_run_fail += 1; \
fprintf(stderr, "Test %s failed\n", _Name ->name); \
} else { \
tests_run_pass += 1; \
} \
} while (0)
#define PRINT_TEST_RESULTS do { \
printf(" %u/%u ", tests_run_pass, tests_run_pass + tests_run_fail); \
} while (0);
#define ALL_TESTS_PASSED (tests_run_fail == 0)
#endif /* BASIC_TESTING_H_INCLUDED */

40
chess_paths/tests/test0.c Normal file
View file

@ -0,0 +1,40 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(compilation_and_invocation) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = PAWN;
p.column = 0;
p.row = 0;
increasing_path_len(&c, &p);
decreasing_path_len(&c, &p);
simple_path_len(&c, &p);
TEST_PASSED;
}
int main() {
RUN_TEST(compilation_and_invocation);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

104
chess_paths/tests/test1.c Normal file
View file

@ -0,0 +1,104 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
}
TEST(dec_pawn) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = PAWN;
p.column = 0;
p.row = 0;
assert_uint_eq(decreasing_path_len(&c, &p), 0);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_king) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KING;
p.column = 0;
p.row = 0;
assert_uint_eq(decreasing_path_len(&c, &p), 0);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_queen) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = QUEEN;
p.column = 0;
p.row = 0;
assert_uint_eq(decreasing_path_len(&c, &p), 0);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_knight) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 0;
p.row = 0;
assert_uint_eq(decreasing_path_len(&c, &p), 0);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_bishop) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = BISHOP;
p.column = 0;
p.row = 0;
assert_uint_eq(decreasing_path_len(&c, &p), 0);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_rook) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = ROOK;
p.column = 0;
p.row = 0;
assert_uint_eq(decreasing_path_len(&c, &p), 0);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
int main() {
RUN_TEST(dec_pawn);
RUN_TEST(dec_king);
RUN_TEST(dec_queen);
RUN_TEST(dec_knight);
RUN_TEST(dec_bishop);
RUN_TEST(dec_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

105
chess_paths/tests/test2.c Normal file
View file

@ -0,0 +1,105 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(inc_pawn) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = PAWN;
p.column = 0;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 9);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_king) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KING;
p.column = 0;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 9);
assert_uint_eq(p.column, 9);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_queen) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = QUEEN;
p.column = 0;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 1);
assert_uint_eq(p.column, 9);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_knight) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 0;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 5);
assert_uint_eq(p.column, 6);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_bishop) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = BISHOP;
p.column = 0;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 1);
assert_uint_eq(p.column, 9);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_rook) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = ROOK;
p.column = 0;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 9);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
int main() {
RUN_TEST(inc_pawn);
RUN_TEST(inc_king);
RUN_TEST(inc_queen);
RUN_TEST(inc_knight);
RUN_TEST(inc_bishop);
RUN_TEST(inc_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

105
chess_paths/tests/test3.c Normal file
View file

@ -0,0 +1,105 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(simple_pawn) {
struct chessboard c = { .C = 3, .R = 3 };
struct piece_position p;
p.piece = PAWN;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 2);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 2);
TEST_PASSED;
}
TEST(simple_king) {
struct chessboard c = { .C = 3, .R = 3 };
struct piece_position p;
p.piece = KING;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 8);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(simple_queen) {
struct chessboard c = { .C = 3, .R = 3 };
struct piece_position p;
p.piece = QUEEN;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 8);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(simple_knight) {
struct chessboard c = { .C = 3, .R = 3 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 7);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 1);
TEST_PASSED;
}
TEST(simple_bishop) {
struct chessboard c = { .C = 3, .R = 3 };
struct piece_position p;
p.piece = BISHOP;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 4);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(simple_rook) {
struct chessboard c = { .C = 3, .R = 3 };
struct piece_position p;
p.piece = ROOK;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 6);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 1);
TEST_PASSED;
}
int main() {
RUN_TEST(simple_pawn);
RUN_TEST(simple_king);
RUN_TEST(simple_queen);
RUN_TEST(simple_knight);
RUN_TEST(simple_bishop);
RUN_TEST(simple_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

105
chess_paths/tests/test4.c Normal file
View file

@ -0,0 +1,105 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(dec_pawn) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = PAWN;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 0);
assert_uint_eq(p.column, 5);
assert_uint_eq(p.row, 2);
TEST_PASSED;
}
TEST(dec_king) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KING;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 5);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_queen) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = QUEEN;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_knight) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 1);
assert_uint_eq(p.column, 4);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_bishop) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = BISHOP;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 1);
assert_uint_eq(p.column, 3);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(dec_rook) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = ROOK;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
int main() {
RUN_TEST(dec_pawn);
RUN_TEST(dec_king);
RUN_TEST(dec_queen);
RUN_TEST(dec_knight);
RUN_TEST(dec_bishop);
RUN_TEST(dec_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

106
chess_paths/tests/test5.c Normal file
View file

@ -0,0 +1,106 @@
#include <stdlib.h>
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
}
TEST(inc_pawn) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = PAWN;
p.column = 5;
p.row = 2;
assert_uint_eq(increasing_path_len(&c, &p), 7);
assert_uint_eq(p.column, 5);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_king) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KING;
p.column = 5;
p.row = 2;
assert_uint_eq(increasing_path_len(&c, &p), 7);
assert_uint_eq(p.column, 9);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_queen) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = QUEEN;
p.column = 5;
p.row = 2;
assert_uint_eq(increasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 9);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_knight) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 5;
p.row = 2;
assert_uint_eq(increasing_path_len(&c, &p), 4);
assert_uint_eq(p.column, 6);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_bishop) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = BISHOP;
p.column = 5;
p.row = 2;
assert_uint_eq(increasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(inc_rook) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = ROOK;
p.column = 5;
p.row = 2;
assert_uint_eq(increasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 9);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
int main(void) {
RUN_TEST(inc_pawn);
RUN_TEST(inc_king);
RUN_TEST(inc_queen);
RUN_TEST(inc_knight);
RUN_TEST(inc_bishop);
RUN_TEST(inc_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

105
chess_paths/tests/test6.c Normal file
View file

@ -0,0 +1,105 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(simple_pawn) {
struct chessboard c = { .C = 8, .R = 8 };
struct piece_position p;
p.piece = PAWN;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 7);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 7);
TEST_PASSED;
}
TEST(simple_king) {
struct chessboard c = { .C = 8, .R = 8 };
struct piece_position p;
p.piece = KING;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 63);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(simple_queen) {
struct chessboard c = { .C = 8, .R = 8 };
struct piece_position p;
p.piece = QUEEN;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 63);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(simple_knight) {
struct chessboard c = { .C = 8, .R = 8 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 46);
assert_uint_eq(p.column, 1);
assert_uint_eq(p.row, 7);
TEST_PASSED;
}
TEST(simple_bishop) {
struct chessboard c = { .C = 8, .R = 8 };
struct piece_position p;
p.piece = BISHOP;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 28);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
TEST(simple_rook) {
struct chessboard c = { .C = 8, .R = 8 };
struct piece_position p;
p.piece = ROOK;
p.column = 0;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 63);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 0);
TEST_PASSED;
}
int main() {
RUN_TEST(simple_pawn);
RUN_TEST(simple_king);
RUN_TEST(simple_queen);
RUN_TEST(simple_knight);
RUN_TEST(simple_bishop);
RUN_TEST(simple_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

40
chess_paths/tests/test7.c Normal file
View file

@ -0,0 +1,40 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(simple_pawn) {
struct chessboard c = { .C = 10000, .R = 40000 };
struct piece_position p;
p.piece = PAWN;
p.column = 9999;
p.row = 0;
assert_uint_eq(simple_path_len(&c, &p), 39999);
assert_uint_eq(p.column, 9999);
assert_uint_eq(p.row, 39999);
TEST_PASSED;
}
int main() {
RUN_TEST(simple_pawn);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

105
chess_paths/tests/test8.c Normal file
View file

@ -0,0 +1,105 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
return column + row * c->C;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(inc_pawn) {
struct chessboard c = {.C = 100000, .R = 10000 };
struct piece_position p;
p.piece = PAWN;
p.column = 12345;
p.row = 1;
assert_uint_eq(increasing_path_len(&c, &p), 9998);
assert_uint_eq(p.column, 12345);
assert_uint_eq(p.row, 9999);
TEST_PASSED;
}
TEST(inc_king) {
struct chessboard c = {.C = 100000, .R = 10000 };
struct piece_position p;
p.piece = KING;
p.column = 1;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 99998);
assert_uint_eq(p.column, 99999);
assert_uint_eq(p.row, 9999);
TEST_PASSED;
}
TEST(inc_queen) {
struct chessboard c = {.C = 100000, .R = 10000 };
struct piece_position p;
p.piece = QUEEN;
p.column = 345;
p.row = 4123;
assert_uint_eq(increasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 99999);
assert_uint_eq(p.row, 9999);
TEST_PASSED;
}
TEST(inc_knight) {
struct chessboard c = {.C = 100000, .R = 10000 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 12345;
p.row = 1;
assert_uint_eq(increasing_path_len(&c, &p), 4999);
assert_uint_eq(p.column, 17344);
assert_uint_eq(p.row, 9999);
TEST_PASSED;
}
TEST(inc_bishop) {
struct chessboard c = {.C = 100000, .R = 10000 };
struct piece_position p;
p.piece = BISHOP;
p.column = 12345;
p.row = 0;
assert_uint_eq(increasing_path_len(&c, &p), 1);
assert_uint_eq(p.column, 22344);
assert_uint_eq(p.row, 9999);
TEST_PASSED;
}
TEST(inc_rook) {
struct chessboard c = {.C = 100000, .R = 10000 };
struct piece_position p;
p.piece = ROOK;
p.column = 12345;
p.row = 4321;
assert_uint_eq(increasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 99999);
assert_uint_eq(p.row, 9999);
TEST_PASSED;
}
int main() {
RUN_TEST(inc_pawn);
RUN_TEST(inc_king);
RUN_TEST(inc_queen);
RUN_TEST(inc_knight);
RUN_TEST(inc_bishop);
RUN_TEST(inc_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}

108
chess_paths/tests/test9.c Normal file
View file

@ -0,0 +1,108 @@
#include <assert.h>
#include "basic_testing.h"
#include "../chess_paths.h"
struct chessboard {
unsigned int C;
unsigned int R;
};
int value_at(const struct chessboard * c, unsigned int column, unsigned int row) {
row = c->R - row - 1;
column += row;
column *= column + 1;
return column/2 + row;
}
unsigned int columns(const struct chessboard * c) {
return c->C;
}
unsigned int rows(const struct chessboard * c) {
return c->R;
};
TEST(dec_pawn) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = PAWN;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 7);
assert_uint_eq(p.column, 5);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(dec_king) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KING;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 7);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(dec_queen) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = QUEEN;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(dec_knight) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = KNIGHT;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 4);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(dec_bishop) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = BISHOP;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 2);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
TEST(dec_rook) {
struct chessboard c = {.C = 10, .R = 10 };
struct piece_position p;
p.piece = ROOK;
p.column = 5;
p.row = 2;
assert_uint_eq(decreasing_path_len(&c, &p), 2);
assert_uint_eq(p.column, 0);
assert_uint_eq(p.row, 9);
TEST_PASSED;
}
int main() {
RUN_TEST(dec_pawn);
RUN_TEST(dec_king);
RUN_TEST(dec_queen);
RUN_TEST(dec_knight);
RUN_TEST(dec_bishop);
RUN_TEST(dec_rook);
PRINT_TEST_RESULTS;
assert(ALL_TESTS_PASSED);
}