-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting.lisp
More file actions
57 lines (52 loc) · 2.78 KB
/
Copy pathtesting.lisp
File metadata and controls
57 lines (52 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
;;;; The following is code that will generate test cases based on a
;;;; log file. It plays through the game and sees if it gets the same
;;;; result as the actual game.
(defpackage :game-server-tests
(:use :clamp :clamp-experimental :iter :ppcre :usocket)
(:shadowing-import-from :experimental
:repeat :def :fn :defmemo :mac :while
:until :in :coerce :with :summing :defmethod)
(:shadowing-import-from :ppcre :split))
(in-package :game-server-tests)
(syntax:use-syntax :clamp)
(def test (log game-id port)
"Given a log file and a game identifier, plays through all of the
games in the log file."
(w/infile *standard-input* log
(with (game-scanner (create-scanner (mkstr "^" game-id " (\\d*)$"))
move-scanner (create-scanner "^(\\d*): (.*)$")
line-num 0)
(whilet line (read-line :eof nil)
(++ line-num)
(mvb (match strings) (scan-to-strings game-scanner line)
(when match
(withs (num (parse-integer (last (coerce strings 'list)))
ps (n-of num (socket-connect "127.0.0.1" port)))
;; We are sorting them based on the player number they are assigned.
(unwind-protect (do (= ps (sort #'< ps [parse-integer+read-line :from (socket-stream _)]))
(while t
(let line (read-line)
(++ line-num)
(mvb (match strings) (scan-to-strings move-scanner line)
(if (no match)
;; Then this is the last line of the game.
(do (assert (iso (last-line ps!car!socket-stream)
line)
()
"The results do not match up on line #~A."
line-num)
;; Exit the game loop
(return))
(let (player move) (coerce strings 'list)
(zap dec+parse-integer player)
(let stream ps.player!socket-stream
(format stream "~A~%" move)
(force-output stream))))))))
(mapc #'socket-close ps))))))
t)))
(def last-line (stream)
"Returns the last line of output from a stream."
(sleep .5)
(ret prev nil
(whilet line (and (listen stream) (read-line :from stream :eof nil))
(= prev line))))