1+ from __future__ import absolute_import
2+ import unittest
3+ import sc2reader
4+
5+
6+ class ColorTests (unittest .TestCase ):
7+ def test_color (self ):
8+ color = sc2reader .utils .Color (r = 0x00 , g = 0x42 , b = 0xFF , a = 75 )
9+ self .assertEqual (color .rgba , (0x00 ,0x42 ,0xFF ,75 ))
10+ self .assertEqual (color .hex , "0042FF" )
11+
12+ class PersonDictTests (unittest .TestCase ):
13+
14+ def test_brackets (self ):
15+ lookup = sc2reader .utils .PersonDict ()
16+ lookup [1 ] = sc2reader .objects .Player (1 , "player1" )
17+ lookup [2 ] = sc2reader .objects .Player (2 , "player2" )
18+ self .assertEquals (lookup [1 ].name , "player1" )
19+ self .assertEquals (lookup ["player2" ].pid , 2 )
20+
21+ def test_list_constructor (self ):
22+ lookup = sc2reader .utils .PersonDict ([
23+ sc2reader .objects .Player (1 , "player1" ),
24+ sc2reader .objects .Player (2 , "player2" )
25+ ])
26+
27+ self .assertEquals (lookup [1 ].name , "player1" )
28+ self .assertEquals (lookup ["player2" ].pid , 2 )
29+
30+ def test_deletes (self ):
31+ lookup = sc2reader .utils .PersonDict ()
32+ lookup [1 ] = sc2reader .objects .Player (1 , "player1" )
33+ lookup [2 ] = sc2reader .objects .Player (2 , "player2" )
34+ with self .assertRaises (KeyError ) as cm :
35+ del lookup [2 ]
36+ lookup [2 ].name
37+ self .assertEquals (cm .exception .message , 2 )
38+
39+ class ReplayBufferTests (unittest .TestCase ):
40+ """All methods should throw EOFError when they hit the end of the
41+ wrapped buffer."""
42+
43+ def test_seek (self ):
44+ """Should preserve the bitshift unless seeking back to beginning of
45+ the file. Seeking to position 0 leaves no bits behind to shift.
46+
47+ The bits we are testing on
48+ 01010100
49+ 00110011
50+ 00110101
51+ 01010100
52+ """
53+ buffer = sc2reader .utils .ReplayBuffer ("T35T" )
54+
55+ # Initial offset
56+ buffer .shift (8 )
57+ buffer .shift (6 )
58+
59+ # Assertions
60+ self .assertEquals (buffer .bit_shift , 6 )
61+ buffer .seek (1 )
62+ self .assertEquals (buffer .bit_shift , 6 )
63+ buffer .seek (0 )
64+ self .assertEquals (buffer .bit_shift , 0 )
65+
66+ def test_shift (self ):
67+ """ The bits we are testing on
68+ 01010100
69+ 00110011
70+ 00110101
71+ 01010100
72+ """
73+ buffer = sc2reader .utils .ReplayBuffer ("T35T" )
74+
75+ self .assertEquals (buffer .shift (8 ), 0x54 ) # Whole byte reads are valid
76+ self .assertEquals (buffer .shift (3 ), 0x03 ) # Start reading from the right
77+ self .assertEquals (buffer .shift (4 ), 0x06 ) # Account for previous shift
78+
79+ # But don't let them overshift a byte.
80+ with self .assertRaises (ValueError ) as cm :
81+ buffer .shift (3 )
82+ self .assertEquals (cm .exception .message , "Cannot shift off 3 bits. Only 1 bits remaining." )
83+
84+ buffer .shift (1 )
85+ buffer .shift (8 )
86+ buffer .shift (8 )
87+
88+ # Also don't let them shift off the end of the buffer
89+ with self .assertRaises (EOFError ) as cm :
90+ buffer .shift (8 )
91+ self .assertEquals (cm .exception .message , "Cannot shift requested bits. End of buffer reached" )
92+
93+ def test_read (self ):
94+ """The bits we are testing on
95+ 010 10100
96+ 00110 011
97+ 001 10101
98+ 01010 100
99+ 01 000 010
100+ 010101 01
101+ 010001 10
102+ 0100 0110
103+ 00110011
104+ 01010010
105+ """
106+ buffer = sc2reader .utils .ReplayBuffer ("T35TBUFF3R" )
107+
108+ # Simple shift
109+ self .assertEquals (buffer .read (bits = 5 ), [0x14 ])
110+ # Byte overflow shift
111+ self .assertEquals (buffer .read (bits = 6 ), [0x13 ])
112+ # Multi-byte unchanged bit_shift
113+ self .assertEquals (buffer .read (bytes = 2 ), [0x31 ,0xAC ])
114+ # Multi-byte increased bit_shift
115+ self .assertEquals (buffer .read (bytes = 1 , bits = 3 ), [0x50 , 0x02 ])
116+ # Mutli-byte decreased bit_shift
117+ self .assertEquals (buffer .read (bits = 22 ), [0x55 , 0x51 , 0x26 ])
118+
119+ # Run out of bytes, but don't leave the buffer dirty!
120+ initial_tell , initial_shift = buffer .tell (), buffer .bit_shift
121+ with self .assertRaises (EOFError ) as cm :
122+ buffer .read (bytes = 4 )
123+ self .assertEquals (cm .exception .message , "Cannot read requested bits/bytes. only 2 bytes left in buffer." )
124+ self .assertEquals (initial_tell , buffer .tell ())
125+ self .assertEquals (initial_shift , buffer .bit_shift )
126+
127+ def test_read_byte (self ):
128+ """The bits we are testing on
129+ 01010100
130+ 00110011
131+ 00110101
132+ 01010100
133+ """
134+ buffer = sc2reader .utils .ReplayBuffer ("T35T" )
135+ self .assertEquals (buffer .read_byte (), 0x54 )
136+ self .assertEquals (buffer .bit_shift , 0 )
137+ buffer .shift (3 ) # Test Shifted
138+ self .assertEquals (buffer .read_byte (), 0x35 )
139+ self .assertEquals (buffer .bit_shift , 3 )
140+ with self .assertRaises (EOFError ) as cm :
141+ buffer .read_byte ()
142+ buffer .read_byte ()
143+ self .assertEquals (cm .exception .message , "Cannot read byte; no bytes remaining" )
144+
145+ def test_read_short (self ):
146+ """The bits we are testing on
147+ 01010100
148+ 00110011
149+ 00110 101
150+ 010 10100
151+ 01000 010
152+ 01010101
153+ 01000110
154+ """
155+ buffer = sc2reader .utils .ReplayBuffer ("T35TBUF" )
156+ self .assertEquals (buffer .read_short (), 0x3354 )
157+ self .assertEquals (buffer .bit_shift , 0 )
158+ buffer .shift (3 ) # Test Shifted
159+ self .assertEquals (buffer .read_short (), 0xA232 )
160+ self .assertEquals (buffer .bit_shift , 3 )
161+ with self .assertRaises (EOFError ) as cm :
162+ buffer .read_short ()
163+ buffer .read_short ()
164+ self .assertEquals (cm .exception .message , "Cannot read short; only 0 bytes left in buffer" )
165+
166+ def test_read_int (self ):
167+ """The bits we are testing on
168+ 01010100
169+ 00110011
170+ 00110101
171+ 01010100
172+ 01000 010
173+ 010 10101
174+ 010 00110
175+ 010 00110
176+ 00110 011
177+ 01010010
178+ """
179+ buffer = sc2reader .utils .ReplayBuffer ("T35TBUFF3R" )
180+ self .assertEquals (buffer .read_int (), 0x54353354 )
181+ self .assertEquals (buffer .bit_shift , 0 )
182+ buffer .shift (3 ) # Test shfited
183+ self .assertEquals (buffer .read_int (), 0x3332AA42 )
184+ self .assertEquals (buffer .bit_shift , 3 )
185+ with self .assertRaises (EOFError ) as cm :
186+ buffer .read_int ()
187+ self .assertEquals (cm .exception .message , "Cannot read int; only 1 bytes left in buffer" )
188+
189+ def test_read_range (self ):
190+ buffer = sc2reader .utils .ReplayBuffer ("T35TBUFF3R" )
191+ self .assertEquals (buffer .read_range (2 ,6 ), "5TBU" )
192+ self .assertEquals (buffer .tell (), 0 )
193+
194+ def test_read_bitmask (self ):
195+ """The bits we are testing on
196+ 00001 010
197+ 01010 111
198+ 001 10011
199+ 001101 01
200+ """
201+ buffer = sc2reader .utils .ReplayBuffer ("\n W35T" )
202+ buffer .shift (3 ) #Shift to give us a managable size
203+
204+ # Length = 00010101 = 15
205+ # Bytes Reversed = 1001101 01010001
206+ # Mask = 0x4E51
207+ # Bits Reversed = 100010101011001
208+ self .assertEquals (buffer .read_bitmask (), [
209+ True , False , False , False , True , False , True ,
210+ False , True , False , True , True , False , False , True ])
211+ self .assertEquals (buffer .bit_shift , 2 )
212+
213+ def test_read_variable_int (self ):
214+ """The bits we are testing on
215+ 10101001
216+ 00010111
217+ 01111110
218+ """
219+ buffer = sc2reader .utils .ReplayBuffer (chr (0xA9 )+ chr (0x17 )+ chr (0x7E ))
220+ self .assertEquals (buffer .read_variable_int (), - 0x5D4 )
221+ self .assertEquals (buffer .read_variable_int (), 0x3F )
222+ self .assertEquals (buffer .bit_shift , 0 )
223+
224+ def test_read_coordinate (self ):
225+ pass
226+
227+ def test_read_timestamp (self ):
228+ pass
229+
230+ def test_read_data_struct (self ):
231+ pass
232+
233+
234+
235+
236+
237+
238+
239+ if __name__ == '__main__' :
240+ unittest .main ()
0 commit comments