forked from habari/tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_bitmask.php
322 lines (270 loc) · 7.92 KB
/
test_bitmask.php
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
include 'bootstrap.php';
class BitmaskTest extends UnitTestCase
{
public function setup()
{
$this->access_names = array( 'read', 'edit', 'delete', 'create' );
$this->bitmask = new Bitmask($this->access_names);
}
public function test_constructor()
{
$mask = new Bitmask( array( 'dog', 'cat' ), 3 );
$this->assert_true( $mask->dog );
$this->assert_true( $mask->cat );
$mask = new Bitmask( array( 'dog', 'cat' ), '3' );
$this->assert_true( $mask->dog );
$this->assert_true( $mask->cat );
$mask = new Bitmask( array( 'dog', 'cat' ), 'dog' );
$this->assert_true( $mask->dog );
$this->assert_false( $mask->cat );
$mask = new Bitmask( array( 'dog', 'cat' ), 'full' );
$this->assert_true( $mask->dog );
$this->assert_true( $mask->cat );
$mask = new Bitmask( array( 'flags' ) );
$this->assert_false( $mask->flags );
$mask = new Bitmask( array( 'flags' ), 'flags' );
$this->assert_true( $mask->flags );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_first_argument()
{
$this->assert_exception('InvalidArgumentException');
$mask = new Bitmask( 'brute' );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_flag_name_full()
{
$mask = new Bitmask( array( 'full' ) );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_flag_name_value()
{
$mask = new Bitmask( array( 'value' ) );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_duplicate_flag_name()
{
$mask = new Bitmask( array( 'foo', 'foo' ) );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_non_string_flag_name()
{
$mask = new Bitmask( array( 1 ) );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_second_argument_nonexistent_flag_name()
{
$this->assert_exception('InvalidArgumentException');
$mask = new Bitmask( array( 'dog', 'cat' ), 'giraffe' );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_second_argument_flags()
{
$this->assert_exception('InvalidArgumentException');
$mask = new Bitmask( array( 'dog', 'cat' ), 'flags' );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_second_argument_array()
{
$this->assert_exception('InvalidArgumentException');
$mask = new Bitmask( array( 'dog', 'cat' ), array() );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_second_argument_int_too_small()
{
$this->assert_exception('InvalidArgumentException');
$mask = new Bitmask( array( 'dog', 'cat' ), -1 );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_constructor_invalid_second_argument_int_too_large()
{
$this->assert_exception('InvalidArgumentException');
$mask = new Bitmask( array( 'dog', 'cat' ), 4 );
}
public function test_write_by_name()
{
$this->bitmask->read = true;
$this->assert_equal(1, $this->bitmask->value);
$this->bitmask->edit = true;
$this->assert_equal(3, $this->bitmask->value);
$this->bitmask->delete = true;
$this->assert_equal(7, $this->bitmask->value);
$this->bitmask->create = true;
$this->assert_equal(15, $this->bitmask->value);
$this->bitmask->read = false;
$this->assert_equal(14, $this->bitmask->value);
$this->bitmask->edit = false;
$this->assert_equal(12, $this->bitmask->value);
$this->bitmask->delete = false;
$this->assert_equal(8, $this->bitmask->value);
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_name_non_bool()
{
$this->assert_exception('InvalidArgumentException');
$this->bitmask->read = 1;
}
public function test_write_by_value()
{
$this->bitmask->value = 1;
$this->assert_true($this->bitmask->read, 'Read bit should be true and is not.');
$this->assert_false($this->bitmask->edit, 'Edit bit should be false and is not.');
$this->bitmask->value = 2;
$this->assert_true($this->bitmask->edit);
$this->assert_false($this->bitmask->delete);
$this->bitmask->value = 4;
$this->assert_true($this->bitmask->delete);
$this->assert_false($this->bitmask->create);
$this->bitmask->value = 8;
$this->assert_true($this->bitmask->create);
$this->assert_false($this->bitmask->read);
$this->bitmask->value = 14;
$this->assert_true($this->bitmask->create);
$this->assert_false($this->bitmask->read);
$this->assert_true($this->bitmask->delete);
$this->assert_true($this->bitmask->edit);
$this->bitmask->value = 0;
$this->assert_false($this->bitmask->create);
$this->assert_false($this->bitmask->read);
$this->assert_false($this->bitmask->delete);
$this->assert_false($this->bitmask->edit);
$this->bitmask->value = '8';
$this->assert_true($this->bitmask->create);
$this->assert_false($this->bitmask->read);
$this->assert_false($this->bitmask->delete);
$this->assert_false($this->bitmask->edit);
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_value_array()
{
$this->assert_exception('InvalidArgumentException');
$this->bitmask->value = array();
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_value_int_too_small()
{
$this->assert_exception('InvalidArgumentException');
$this->bitmask->value = -1;
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_value_int_too_big()
{
$this->assert_exception('InvalidArgumentException');
$this->bitmask->value = 16;
}
public function test_write_by_full()
{
$this->bitmask->full = true;
$this->assert_equal( 15, $this->bitmask->value );
$this->bitmask->full = false;
$this->assert_equal( 0, $this->bitmask->value );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_full_non_boolean()
{
$this->assert_exception('InvalidArgumentException');
$this->bitmask->full = 1;
}
public function test_write_by_array()
{
$mask = array(true, false, false, true);
$this->bitmask->value = $mask;
$this->assert_equal(9, $this->bitmask->value);
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_array_non_bool()
{
$this->assert_exception('InvalidArgumentException');
$mask = array( 1, 0, 0, 1 );
$this->bitmask->value = $mask;
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_array_too_short()
{
$this->assert_exception('InvalidArgumentException');
$mask = array( 1 );
$this->bitmask->value = $mask;
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_by_array_too_long()
{
$this->assert_exception('InvalidArgumentException');
$mask = array( 1, 0, 0, 1, 1 );
$this->bitmask->value = $mask;
}
/**
* @expectedException InvalidArgumentException
*/
public function test_write_nonexistent()
{
$this->assert_exception('InvalidArgumentException');
$this->bitmask->bogus = true;
}
public function test_get()
{
$this->assert_equal( $this->bitmask->full, 15 );
}
/**
* @expectedException InvalidArgumentException
*/
public function test_get_nonexistent()
{
$this->assert_exception('InvalidArgumentException');
$foo = $this->bitmask->bogus;
}
public function test_isset()
{
$this->assert_true( isset( $this->bitmask->full ) );
$this->assert_true( isset( $this->bitmask->value ) );
$this->assert_true( isset( $this->bitmask->read ) );
$this->assert_false( isset( $this->bitmask->bogus ) );
}
public function test__tostring()
{
$this->bitmask->value = 1;
$this->assert_equal('read', (string)$this->bitmask);
$this->bitmask->value = 0;
$this->assert_equal( 'none', (string)$this->bitmask );
$this->bitmask->value = 15;
$this->assert_equal( 'full', (string)$this->bitmask );
}
}
BitmaskTest::run_one( 'BitmaskTest' );
?>