summaryrefslogtreecommitdiff
path: root/chess-test.el
blob: 228fcfa49661be94942c78ce62c98951b9a45ce4 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;; Soon: Put Emacs Chess through an enormous battery of tests.

(eval-when-compile
  (require 'cl))

(require 'chess-database)
(require 'chess-game)

(defun chess-test (&optional file start count)
  (unless file
    (setq file (nth 0 command-line-args-left)))
  (unless start
    (setq start (ignore-errors
		  (string-to-number (nth 1 command-line-args-left)))))
  (unless count
    (setq count (ignore-errors
		  (string-to-number (nth 2 command-line-args-left)))))

  (message "Opening chess database '%s'" file)

  (let ((database (chess-database-open file)))
    (if database
	(progn
	  (message "Running validation suite...")
	  (condition-case err
	      (let* ((db-count (chess-database-count database))
		     (ply-count 0)
		     (index (if start
				(max start 1)
			      1))
		     (last-index (if count
				     (min db-count (+ index count))
				   db-count))
		     (begin (current-time))
		     (read-count 0))
		(message "Testing legality of games in range [%d, %d):"
			 index last-index)
		(while (< index last-index)
		  ;; Reading in the game will cause it to be converted from PGN
		  ;; (this is true currently) to a chess-game, during which time
		  ;; every move will be tested for legality.
		  ;;
		  ;; jww (2008-08-31): We should add some extra checks here, if we
		  ;; want to verify the final position and such.
		  (let ((game (chess-database-read database index)))
		    (when game
		      (setq read-count (1+ read-count)
			    ply-count
			    (+ ply-count (length (chess-game-plies game))))
		      (if (= 0 (mod index 1000))
			  (message "Read %d games (now at game %d): %d total plies (%.2f ply/sec)"
				   read-count index ply-count
				   (/ (float ply-count)
				      (float
				       (time-to-seconds
					(subtract-time (current-time)
						       begin))))))))
		  (setq index (1+ index)))
		(message "Read %d games (up to game %d): %d plies (%.2f ply/sec, %.2f seconds)"
			 read-count index ply-count
			 (/ (float ply-count)
			    (float
			     (time-to-seconds
			      (subtract-time (current-time)
					     begin))))
			 (time-to-seconds (subtract-time (current-time)
							 begin)))
		(message "Running validation suite...done"))
	    (t
	     (error "Failed to open chess database '%s'" file)))
	  (chess-database-close database)))))

;;; chess-test.el ends here