From: Laurent Mazet Date: Wed, 24 Jan 2024 15:22:30 +0000 (+0100) Subject: add tests X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=b568387a5425bffecabbcdb2dfb8eb5a504e168d;p=battleships.git add tests --- diff --git a/battleship.py b/battleship.py index 374ec23..2d0ae2d 100755 --- a/battleship.py +++ b/battleship.py @@ -124,18 +124,18 @@ def generate_coordonate(size, tab): if error: print("error!") error = True - x = randint(N) - y = randint(N) + x = randint(0, N - 1) + y = randint(0, N - 1) if (x <= 0) or (x > N) or (y <= 0) or (y > N): continue dir_x = dir_y = 0 - o = randint(2) + o = randint(0, 1) if o == 0: dir_x = 1 if x + size - 1 > N: continue elif o == 1: - dir_x = 0 + dir_y = 1 if y + size - 1 > N: continue pos = [] @@ -156,7 +156,7 @@ def put_in_table(element, pos, tab): else: tab[pos.get("x")][pos.get("y")] = 'O' else: - tab[pos.get("x")][pos.get("y")] = 'S' + tab[pos.get("x")][pos.get("y")] = element # check table status def check_status(tab): @@ -176,7 +176,6 @@ def main(): print("Where do you want to place a ship of", ship, "?") pos = read_coordonate(ship, tab_human) for p in pos: - print(p) put_in_table('S', p, tab_human) # init table for machine diff --git a/makefile b/makefile new file mode 100644 index 0000000..de7c761 --- /dev/null +++ b/makefile @@ -0,0 +1,47 @@ +# makefile + +MAKEFLAGS = -s +PYTHON = python3 +SHELL = bash + +# Functions + +TITLE = echo -e "\033[0;1m$(strip $(1))\033[0;0m" +PASS = echo -e "\033[1;32m$(strip $(1))\033[0;0m" +WARN = echo -e "\033[1;33m$(strip $(1))\033[0;0m" +FAIL = echo -e "\033[1;31m$(strip $(1))\033[0;0m" + +## Generic rules + +all: + echo "rules: purge tests" + +purge: + -rm -rf __pycache__ + +tests: $(subst .py,,$(subst -,_,$(wildcard test-*.py))) + +## Specific rules + +test_%: test-%.py + IFS=$$'\n'; RC=0; \ + for test in `cat $< | grep '^# test:' | sed 's/^[^:]*: //'`; do \ + $(call TITLE, "=== $${test//\\/\\\\} ==="); \ + eval $${test//%%TEST%%/$(PYTHON) $<}; \ + [ $$? -eq 0 ] && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); RC=1; }; \ + done; \ + if [ -z "$$test" ]; then \ + $(call TITLE, "=== $< ==="); \ + $(PYTHON) $<; \ + [ $$? -eq 0 ] && $(call PASS, SUCCESS) || { $(call FAIL, FAILED); RC=1; }; \ + fi; \ + test "$$RC" -ne 1 + +## Phony + +.PHONY: all clean count depends gcovs purge tests + +## Precious + +.PRECIOUS: + diff --git a/test-02.py b/test-02.py new file mode 100755 index 0000000..9aded65 --- /dev/null +++ b/test-02.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +# -*- coding: utf8 -*- + +from battleship import * + +init_table(tab_human) + +pos = read_coordonate(1, tab_human) +put_in_table('S', pos[0], tab_human) + +pos = read_coordonate(4, tab_human) +for p in pos: + put_in_table('L', p, tab_human) + +display(tab_human) + +# test: echo -e "1\n1\n4\n4\ne" | %%TEST%% +# test: echo -e "1\n10\n2\n2\n4\n4\ne" | %%TEST%% +# test: echo -e "1\n1\n8\n4\ns\n5\n5\ns" | %%TEST%% + +# vim: set ts=4 sw=4 et: diff --git a/test-03.py b/test-03.py new file mode 100755 index 0000000..5dccc62 --- /dev/null +++ b/test-03.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf8 -*- + +from battleship import * + +init_table(tab_human) + +pos = generate_coordonate(1, tab_human) +put_in_table('S', pos[0], tab_human) + +pos = generate_coordonate(4, tab_human) +for p in pos: + print(p) + put_in_table('L', p, tab_human) + +display(tab_human) + +# vim: set ts=4 sw=4 et: diff --git a/test-04.py b/test-04.py new file mode 100755 index 0000000..7675294 --- /dev/null +++ b/test-04.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 +# -*- coding: utf8 -*- + +from battleship import * + +init_table(tab_human) + +pos = {'x': 3, 'y': 6} +put_in_table('S', pos, tab_human) + +put_in_table('B', {'x': 2, 'y': 8}, tab_human) +if not(check_status(tab_human)): + print("still some ships are not skunk") +display(tab_human) + +put_in_table('B', {'x': 3, 'y': 6}, tab_human) +if check_status(tab_human): + print("you won") +display(tab_human) + +# vim: set ts=4 sw=4 et: