add tests
authorLaurent Mazet <mazet@softndesign.org>
Wed, 24 Jan 2024 15:22:30 +0000 (16:22 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 24 Jan 2024 15:22:30 +0000 (16:22 +0100)
battleship.py
makefile [new file with mode: 0644]
test-02.py [new file with mode: 0755]
test-03.py [new file with mode: 0755]
test-04.py [new file with mode: 0755]

index 374ec238b48d140592d33f6d4edb791d97ff7f91..2d0ae2d37182334f1bb996ae8a092838c82ef92a 100755 (executable)
@@ -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 (file)
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 (executable)
index 0000000..9aded65
--- /dev/null
@@ -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 (executable)
index 0000000..5dccc62
--- /dev/null
@@ -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 (executable)
index 0000000..7675294
--- /dev/null
@@ -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: