File: test_clock.py

package info (click to toggle)
gameclock 5.0
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 272 kB
  • ctags: 289
  • sloc: python: 1,551; makefile: 9
file content (182 lines) | stat: -rwxr-xr-x 7,636 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python

import unittest
import sys
import os
import time

sys.path.append((os.path.dirname(__file__) or '.') + '/..')
import gameclock.clock
import gameclock.game
import gameclock.i18n

class BaseClockTest(unittest.TestCase):
    """
    This is the base classe for all clock tests and shouldn't be used directly.
    """

    settings = { 'start_time': 1000 }
    decisecs = True # deciseconds, aka tenth of a second

    def setUp(self):
        self.clock = gameclock.clock.ChessClock(**self.settings)

class SimpleClockTest(BaseClockTest):
    """those are tests that don't need to be ran on all clocks"""

    def test_format(self):
        """test if the output is readable"""
        self.assertEqual(self.clock.format(), '00:01')

    def test_str(self):
        self.assertRegexpMatches(str(self.clock), 'gameclock.clock.ChessClock')

    def test_moves_format(self):
        self.assertEqual(self.clock.moves_fmt(), _('0 moves'))

class NegativeClockTest(BaseClockTest):
    settings = { 'start_time': -1000 }

    def test_format(self):
        self.assertEqual(self.clock.format(), '-00:01')

class ChessClockTest(BaseClockTest):
    """this tests the basic chess clock, but also contains tests
    which should be ran on all clocks"""

    settings = { 'start_time': 10 } # 10ms, may need to be bumped up on slow computers

    def test_start(self):
        """test if the clock starts without error and is running"""
        self.clock.start()
        self.assertTrue(self.clock.running())

    def test_stopped(self):
        """test if the clock is stopped when initialised"""
        self.assertFalse(self.clock.running())

    def test_stop(self):
        """test if the clock stops without error and is stopped"""
        self.clock.start()
        self.clock.stop()
        self.test_stopped()

    def test_dead(self):
        """test if clock is alive when started then dies after set time"""
        self.clock.start()
        self.assertFalse(self.clock.is_dead())
        time.sleep(self.settings['start_time']/1000.0)
        self.assertLess(self.clock.get_time(), 0)
        self.assertTrue(self.clock.is_dead())
        self.assertRegexpMatches(self.clock.format(), '^-')

    def test_time(self):
        """test if the current time makes sense"""
        self.assertEqual(self.clock.get_time(), self.settings['start_time'])
        self.clock.start()
        time.sleep(0.010) # 10ms
        self.assertLess(self.clock.get_time(), self.settings['start_time'])

    def test_turn_one(self):
        """test if the first turn is turn 0"""
        self.clock.start()
        self.assertEqual(self.clock.moves, 0)

class FischerChessClockTest(ChessClockTest):
    settings = { 'start_time': 10, 'delay': 1000 }

    def setUp(self):
        self.clock = gameclock.clock.FischerChessClock(**self.settings)

    def test_stop_fischer(self):
        """test that stop adds time to the clock"""
        self.clock.stop()
        self.assertEqual(self.clock.get_time(), self.settings['start_time'] + self.settings['delay'])

class BoardClockTest(ChessClockTest):
    def setUp(self):
        self.clock = gameclock.clock.BoardClock(**self.settings)

    def test_stop_board(self):
        """test that stop resets the clock"""
        self.test_time() # safety check, but also expected to start the clock
        self.clock.stop()
        self.assertEqual(self.clock.get_time(), self.settings['start_time'])

class HourglassClockTest(ChessClockTest):
    def setUp(self):
        self.clock = gameclock.clock.HourglassClock(**self.settings)

    def test_stop(self):
        """test that time goes backwards when the clock is stopped"""
        self.test_time() # safety check, but also expected to start the clock
        self.clock.stop()
        time.sleep(0.100) # 100ms, should be enough to go backwards
        self.assertGreater(self.clock.get_time(), self.settings['start_time'])

class GoStandardByoyomiClockTest(ChessClockTest):
    settings = { 'start_time': 100, 'byoyomi': 2, 'delay': 50 } # 100ms, 2 byoyomis, 10ms

    def setUp(self):
        self.clock = gameclock.clock.GoStandardByoyomiClock(**self.settings)

    def test_dead(self):
        self.clock.start()
        self.assertFalse(self.clock.is_dead(), "game should start alive")
        self.assertFalse(self.clock.is_byoyomi(), "we shouldn't be in byoyomi yet")
        time.sleep(0.110) # 110 ms should cross the above 100ms
        self.assertEqual(self.clock.get_byoyomi(), self.settings['byoyomi'], "we should be in our first byoyomi: %d" % self.clock.get_byoyomi())
        self.assertTrue(self.clock.is_byoyomi(), "we should be in a byoyomi however")
        self.assertFalse(self.clock.is_dead(), "go games have a byoyomi after regular time, so this should not die just yet")
        time.sleep(0.110) # 110 ms should cross the first byoyomi, but not die yet
        self.assertEqual(self.clock.get_byoyomi(), self.settings['byoyomi'] - 1, "we should be in our second byoyomi: %d" % self.clock.get_byoyomi())
        self.assertTrue(self.clock.is_byoyomi(), "we should be in a byoyomi however")
        self.assertFalse(self.clock.is_dead(), "go games have a byoyomi after regular time, so this should not die just yet (clock time: %d, byoyomis: %d)" % (self.clock.time, self.clock.get_byoyomi()))
        time.sleep(0.110) # 110 ms should cross the first byoyomi, but not die yet
        self.assertEqual(self.clock.get_byoyomi(), 0, "we should be in our last byoyomi")
        self.assertTrue(self.clock.is_dead(), "we have crossed the byoyomi, we should die")
        self.assertEqual(self.clock.get_byoyomi(), 0, "we should STILL be in our last byoyomi")

    def test_stop_go(self):
        """test that the clock doesn't reset for nothing"""
        self.clock.start()
        self.clock.stop()
        self.assertLess(self.clock.get_time(), self.settings['start_time'])
        self.assertNotEqual(self.clock.get_time(), self.settings['delay'])
        self.assertFalse(self.clock.is_byoyomi(), "we shouldn't be in byoyomi yet")

    def test_moves_format(self):
        self.assertEqual(self.clock.moves_fmt(), _('0 moves'))
        self.clock.start()
        time.sleep(0.110)
        self.assertEqual(self.clock.moves_fmt(), _('0 moves 2 byoyomis'))
        time.sleep(0.110)
        self.assertEqual(self.clock.moves_fmt(), _('0 moves 1 byoyomi'))
        time.sleep(0.110)
        self.assertEqual(self.clock.moves_fmt(), _('0 moves 0 byoyomis (lost)'))


class WeirdGoStandardByoyomiClockTest(BaseClockTest):
    settings = { 'start_time': 100, 'byoyomi': 1, 'delay': 50 } # 100ms, 2 byoyomis, 10ms

    def setUp(self):
        self.clock = gameclock.clock.GoStandardByoyomiClock(**self.settings)

    def test_die_clock(self):
        """test that the clock dies in the last byoyomi"""
        self.clock.start()
        self.assertFalse(self.clock.is_dead(), "game should start alive")
        self.assertFalse(self.clock.is_byoyomi(), "we shouldn't be in byoyomi yet")
        time.sleep(0.100)
        self.assertTrue(self.clock.is_byoyomi(), "we should be in a byoyomi now")
        self.assertEqual(self.clock.get_byoyomi(), 1, "we should be in our first byoyomi: %d" % self.clock.get_byoyomi())
        time.sleep(0.05)
        self.assertLess(self.clock.get_time(), 0)
        self.assertEqual(self.clock.get_byoyomi(), 0, "we should be in our second byoyomi: %d" % self.clock.get_byoyomi())
        self.assertTrue(self.clock.is_dead())
        self.assertTrue(gameclock.clock.Clock.is_dead(self.clock), "time clock should also die when byoyomi")



if __name__ == '__main__':
    unittest.main()