-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBytesTest.class.php
More file actions
executable file
·370 lines (320 loc) · 8.79 KB
/
BytesTest.class.php
File metadata and controls
executable file
·370 lines (320 loc) · 8.79 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php namespace util\unittest;
use lang\{FormatException, IllegalArgumentException, IndexOutOfBoundsException};
use test\{Assert, Expect, Test, Values};
use util\Bytes;
class BytesTest {
/** @return iterable */
private function slices() {
yield ['0:4', 'This']; // start:stop
yield ['5:7', 'is']; // start:stop - with non-zero start
yield ['5:-5', 'is a']; // start:stop - with negative offset
yield ['-4:-2', 'test']; // start:stop - with negative offsets
yield [':4', 'This']; // :stop
yield [':-5', 'This is a']; // :stop - with negative offset
yield ['5:', 'is a test']; // start:
yield ['-4:', 'test']; // start: - at negative offset
yield [':', 'This is a test']; // : - copy
yield [[0, 4], 'This']; // start:stop
yield [[5, 7], 'is']; // start:stop - with non-zero start
yield [[5, -5], 'is a']; // start:stop - with negative offset
yield [[-4, -2], 'test']; // start:stop - with negative offsets
}
/** @return iterable */
private function comparing() {
yield [new Bytes('Test'), 0];
yield [new Bytes('T'), 1];
yield [new Bytes('Testing'), -1];
yield [null, 1];
}
#[Test]
public function creating_an_empty_bytes_without_supplying_parameters() {
Assert::equals(0, (new Bytes())->size());
}
#[Test]
public function creating_an_empty_bytes_from_an_empty_string() {
Assert::equals(0, (new Bytes(''))->size());
}
#[Test]
public function creating_an_empty_bytes_from_an_empty_array() {
Assert::equals(0, (new Bytes([]))->size());
}
#[Test]
public function from_string() {
$b= new Bytes('abcd');
Assert::equals(4, $b->size());
Assert::equals(97, $b[0]);
Assert::equals(98, $b[1]);
Assert::equals(99, $b[2]);
Assert::equals(100, $b[3]);
}
#[Test]
public function from_integer_array() {
$b= new Bytes([97, 98, 99, 100]);
Assert::equals(4, $b->size());
Assert::equals(97, $b[0]);
Assert::equals(98, $b[1]);
Assert::equals(99, $b[2]);
Assert::equals(100, $b[3]);
}
#[Test]
public function from_char_array() {
$b= new Bytes(['a', 'b', 'c', 'd']);
Assert::equals(4, $b->size());
Assert::equals(97, $b[0]);
Assert::equals(98, $b[1]);
Assert::equals(99, $b[2]);
Assert::equals(100, $b[3]);
}
#[Test]
public function from_byte_array() {
$b= new Bytes([97, 98, 99, 100]);
Assert::equals(4, $b->size());
Assert::equals(97, $b[0]);
Assert::equals(98, $b[1]);
Assert::equals(99, $b[2]);
Assert::equals(100, $b[3]);
}
#[Test]
public function from_self() {
$b= new Bytes(new Bytes('abcd'));
Assert::equals(4, $b->size());
Assert::equals(97, $b[0]);
Assert::equals(98, $b[1]);
Assert::equals(99, $b[2]);
Assert::equals(100, $b[3]);
}
#[Test]
public function from_var_args() {
$b= new Bytes('ab', [99, 100]);
Assert::equals(4, $b->size());
Assert::equals(97, $b[0]);
Assert::equals(98, $b[1]);
Assert::equals(99, $b[2]);
Assert::equals(100, $b[3]);
}
#[Test, Expect(IllegalArgumentException::class)]
public function illegalConstructorArgument() {
new Bytes(1);
}
#[Test]
public function sizeChangesAfterAppending() {
$b= new Bytes();
Assert::equals(0, $b->size());
$b[]= 1;
Assert::equals(1, $b->size());
}
#[Test]
public function sizeChangesAfterRemoving() {
$b= new Bytes("\0");
Assert::equals(1, $b->size());
unset($b[0]);
Assert::equals(0, $b->size());
}
#[Test]
public function sizeDoesNotChangeWhenSetting() {
$b= new Bytes("\0");
Assert::equals(1, $b->size());
$b[0]= "\1";
Assert::equals(1, $b->size());
}
#[Test]
public function appendInteger() {
$b= new Bytes();
$b[]= 1;
Assert::equals(1, $b[0]);
}
#[Test]
public function appendChar() {
$b= new Bytes();
$b[]= "\1";
Assert::equals(1, $b[0]);
}
#[Test]
public function appendByte() {
$b= new Bytes();
$b[]= 1;
Assert::equals(1, $b[0]);
}
#[Test]
public function setInteger() {
$b= new Bytes("\1\2");
$b[0]= 3;
Assert::equals(3, $b[0]);
}
#[Test]
public function setChar() {
$b= new Bytes("\1\2");
$b[0]= "\3";
Assert::equals(3, $b[0]);
}
#[Test]
public function setByte() {
$b= new Bytes("\1\2");
$b[0]= 3;
Assert::equals(3, $b[0]);
}
#[Test, Expect(IndexOutOfBoundsException::class)]
public function setNegative() {
$b= new Bytes('negative');
$b[-1]= 3;
}
#[Test, Expect(IndexOutOfBoundsException::class)]
public function setPastEnd() {
$b= new Bytes('ends');
$b[5]= 3;
}
#[Test, Expect(IndexOutOfBoundsException::class)]
public function getNegative() {
$b= new Bytes('negative');
$read= $b[-1];
}
#[Test, Expect(IndexOutOfBoundsException::class)]
public function getPastEnd() {
$b= new Bytes('ends');
$read= $b[5];
}
#[Test]
public function testingOffsets() {
$b= new Bytes('GIF89a');
Assert::false(isset($b[-1]), 'offset -1');
Assert::true(isset($b[0]), 'offset 0');
Assert::true(isset($b[5]), 'offset 5');
Assert::false(isset($b[6]), 'offset 6');
}
#[Test]
public function removingFromBeginning() {
$b= new Bytes('GIF89a');
unset($b[0]);
Assert::equals(new Bytes('IF89a'), $b);
}
#[Test]
public function removingFromEnd() {
$b= new Bytes('GIF89a');
unset($b[5]);
Assert::equals(new Bytes('GIF89'), $b);
}
#[Test]
public function removingInBetween() {
$b= new Bytes('GIF89a');
unset($b[3]);
Assert::equals(new Bytes('GIF9a'), $b);
}
#[Test, Expect(IndexOutOfBoundsException::class)]
public function removingNegative() {
$b= new Bytes('negative');
unset($b[-1]);
}
#[Test, Expect(IndexOutOfBoundsException::class)]
public function removingPastEnd() {
$b= new Bytes('ends');
unset($b[5]);
}
#[Test]
public function binarySafeBeginning() {
$b= new Bytes(["\0", 'A', 'B']);
Assert::equals(0, $b[0]);
Assert::equals(65, $b[1]);
Assert::equals(66, $b[2]);
}
#[Test]
public function binarySafeInBetween() {
$b= new Bytes(['A', "\0", 'B']);
Assert::equals(65, $b[0]);
Assert::equals(0, $b[1]);
Assert::equals(66, $b[2]);
}
#[Test]
public function binarySafeInEnd() {
$b= new Bytes(['A', 'B', "\0"]);
Assert::equals(65, $b[0]);
Assert::equals(66, $b[1]);
Assert::equals(0, $b[2]);
}
#[Test]
public function abcBytesToString() {
Assert::equals(
'util.Bytes(6)@{@ ABC!}',
(new Bytes('@ ABC!'))->toString()
);
}
#[Test]
public function controlCharsToString() {
Assert::equals(
'util.Bytes(32)@{'.
'\000\001\002\003\004\005\006\a'. // 0 - 7
'\b\t\n\v\f\r\016\017'. // 8 - 15
'\020\021\022\023\024\025\026\027'. // 16 - 23
'\030\031\032\033\034\035\036\037'. // 24 - 31
'}',
(new Bytes(range(0, 31)))->toString()
);
}
#[Test]
public function umlautsToString() {
Assert::equals(
'util.Bytes(9)@{A\303\244O\303\266U\303\274}',
(new Bytes('AäOöUü'))->toString()
);
}
#[Test]
public function stringCasting() {
Assert::equals('Hello', (string)new Bytes('Hello'));
}
/**
* Test creating an integer from bytes using "N" as format
* (unsigned long (always 32 bit, big endian byte order))
*
* @see php://unpack
*/
#[Test]
public function unpackUnsignedLong() {
$r= unpack('Nnumber', new Bytes("\000\000\003\350"));
Assert::equals(1000, $r['number']);
}
/**
* Test creating bytes from an integer using "N" as format
* (unsigned long (always 32 bit, big endian byte order))
*
* @see php://pack
*/
#[Test]
public function packUnsignedLong() {
Assert::equals(new Bytes("\000\000\003\350"), new Bytes(pack('N', 1000)));
}
#[Test]
public function worksWithEchoStatement() {
ob_start();
echo new Bytes('ü');
Assert::equals('ü', ob_get_clean());
}
#[Test]
public function integerArrayToBytes() {
$b= new Bytes([228, 246, 252]);
Assert::equals(-28, $b[0]);
Assert::equals(-10, $b[1]);
Assert::equals(-4, $b[2]);
}
#[Test]
public function byteArrayToBytes() {
$b= new Bytes([-28]);
Assert::equals(-28, $b[0]);
}
#[Test]
public function iteration() {
$c= ['H', "\303", "\244", 'l', 'l', 'o'];
$b= new Bytes($c);
foreach ($b as $i => $byte) {
Assert::equals($c[$i], chr($byte));
}
Assert::equals($i, sizeof($c)- 1);
}
#[Test, Values(from: 'slices')]
public function slice($slice, $expected) {
$b= new Bytes('This is a test');
Assert::equals(new Bytes($expected), $b($slice));
}
#[Test, Values(from: 'comparing')]
public function compare($value, $expected) {
Assert::equals($expected, (new Bytes('Test'))->compareTo($value));
}
}