xref: /PHP-7.4/ext/standard/tests/strings/pack.phpt (revision ded3d984)
1--TEST--
2Generic pack()/unpack() tests
3--SKIPIF--
4<?php
5if (PHP_INT_MAX > 2147483647) {
6	die("skip 32bit test only");
7}
8?>
9--FILE--
10<?php
11print_r(unpack("A", pack("A", "hello world")));
12print_r(unpack("A*", pack("A*", "hello world")));
13echo '"'.pack("A9", "hello").'"';
14echo "\n";
15
16print_r(unpack("C", pack("C", -127)));
17print_r(unpack("C", pack("C", 127)));
18print_r(unpack("C", pack("C", 255)));
19print_r(unpack("C", pack("C", -129)));
20
21print_r(unpack("H", pack("H", 0x04)));
22
23print_r(unpack("I", pack("I", 65534)));
24print_r(unpack("I", pack("I", 0)));
25print_r(unpack("I", pack("I", -1000)));
26print_r(unpack("I", pack("I", -64434)));
27print_r(unpack("I", pack("I", 4294967296)));
28print_r(unpack("I", pack("I", -4294967296)));
29
30print_r(unpack("L", pack("L", 65534)));
31print_r(unpack("L", pack("L", 0)));
32print_r(unpack("L", pack("L", 2147483650)));
33print_r(unpack("L", pack("L", 4294967295)));
34print_r(unpack("L", pack("L", -2147483648)));
35
36print_r(unpack("N", pack("N", 65534)));
37print_r(unpack("N", pack("N", 0)));
38print_r(unpack("N", pack("N", 2147483650)));
39print_r(unpack("N", pack("N", 4294967296)));
40print_r(unpack("N", pack("N", -2147483648)));
41print_r(unpack("N", pack("N", -30000)));
42
43print_r(unpack("S", pack("S", 65534)));
44print_r(unpack("S", pack("S", 65537)));
45print_r(unpack("S", pack("S", 0)));
46print_r(unpack("S", pack("S", -1000)));
47print_r(unpack("S", pack("S", -64434)));
48print_r(unpack("S", pack("S", -65535)));
49
50print_r(unpack("V", pack("V", 65534)));
51print_r(unpack("V", pack("V", 0)));
52print_r(unpack("V", pack("V", 2147483650)));
53print_r(unpack("V", pack("V", 4294967296)));
54print_r(unpack("V", pack("V", -2147483648)));
55
56print_r(unpack("a", pack("a", "hello world")));
57print_r(unpack("a*", pack("a*", "hello world")));
58
59print_r(unpack("c", pack("c", -127)));
60print_r(unpack("c", pack("c", 127)));
61print_r(unpack("c", pack("c", 255)));
62print_r(unpack("c", pack("c", -129)));
63
64print_r(unpack("h", pack("h", 3000000)));
65
66print_r(unpack("i", pack("i", 65534)));
67print_r(unpack("i", pack("i", 0)));
68print_r(unpack("i", pack("i", -1000)));
69print_r(unpack("i", pack("i", -64434)));
70print_r(unpack("i", pack("i", -65535)));
71print_r(unpack("i", pack("i", -2147483647)));
72
73print_r(unpack("l", pack("l", 65534)));
74print_r(unpack("l", pack("l", 0)));
75print_r(unpack("l", pack("l", 2147483650)));
76print_r(unpack("l", pack("l", 4294967296)));
77print_r(unpack("l", pack("l", -2147483648)));
78
79print_r(unpack("n", pack("n", 65534)));
80print_r(unpack("n", pack("n", 65537)));
81print_r(unpack("n", pack("n", 0)));
82print_r(unpack("n", pack("n", -1000)));
83print_r(unpack("n", pack("n", -64434)));
84print_r(unpack("n", pack("n", -65535)));
85
86print_r(unpack("s", pack("s", 32767)));
87print_r(unpack("s", pack("s", 65535)));
88print_r(unpack("s", pack("s", 0)));
89print_r(unpack("s", pack("s", -1000)));
90print_r(unpack("s", pack("s", -64434)));
91print_r(unpack("s", pack("s", -65535)));
92
93print_r(unpack("v", pack("v", 65534)));
94print_r(unpack("v", pack("v", 65537)));
95print_r(unpack("v", pack("v", 0)));
96print_r(unpack("v", pack("v", -1000)));
97print_r(unpack("v", pack("v", -64434)));
98print_r(unpack("v", pack("v", -65535)));
99?>
100--EXPECT--
101Array
102(
103    [1] => h
104)
105Array
106(
107    [1] => hello world
108)
109"hello    "
110Array
111(
112    [1] => 129
113)
114Array
115(
116    [1] => 127
117)
118Array
119(
120    [1] => 255
121)
122Array
123(
124    [1] => 127
125)
126Array
127(
128    [1] => 4
129)
130Array
131(
132    [1] => 65534
133)
134Array
135(
136    [1] => 0
137)
138Array
139(
140    [1] => -1000
141)
142Array
143(
144    [1] => -64434
145)
146Array
147(
148    [1] => 0
149)
150Array
151(
152    [1] => 0
153)
154Array
155(
156    [1] => 65534
157)
158Array
159(
160    [1] => 0
161)
162Array
163(
164    [1] => -2147483646
165)
166Array
167(
168    [1] => -1
169)
170Array
171(
172    [1] => -2147483648
173)
174Array
175(
176    [1] => 65534
177)
178Array
179(
180    [1] => 0
181)
182Array
183(
184    [1] => -2147483646
185)
186Array
187(
188    [1] => 0
189)
190Array
191(
192    [1] => -2147483648
193)
194Array
195(
196    [1] => -30000
197)
198Array
199(
200    [1] => 65534
201)
202Array
203(
204    [1] => 1
205)
206Array
207(
208    [1] => 0
209)
210Array
211(
212    [1] => 64536
213)
214Array
215(
216    [1] => 1102
217)
218Array
219(
220    [1] => 1
221)
222Array
223(
224    [1] => 65534
225)
226Array
227(
228    [1] => 0
229)
230Array
231(
232    [1] => -2147483646
233)
234Array
235(
236    [1] => 0
237)
238Array
239(
240    [1] => -2147483648
241)
242Array
243(
244    [1] => h
245)
246Array
247(
248    [1] => hello world
249)
250Array
251(
252    [1] => -127
253)
254Array
255(
256    [1] => 127
257)
258Array
259(
260    [1] => -1
261)
262Array
263(
264    [1] => 127
265)
266Array
267(
268    [1] => 3
269)
270Array
271(
272    [1] => 65534
273)
274Array
275(
276    [1] => 0
277)
278Array
279(
280    [1] => -1000
281)
282Array
283(
284    [1] => -64434
285)
286Array
287(
288    [1] => -65535
289)
290Array
291(
292    [1] => -2147483647
293)
294Array
295(
296    [1] => 65534
297)
298Array
299(
300    [1] => 0
301)
302Array
303(
304    [1] => -2147483646
305)
306Array
307(
308    [1] => 0
309)
310Array
311(
312    [1] => -2147483648
313)
314Array
315(
316    [1] => 65534
317)
318Array
319(
320    [1] => 1
321)
322Array
323(
324    [1] => 0
325)
326Array
327(
328    [1] => 64536
329)
330Array
331(
332    [1] => 1102
333)
334Array
335(
336    [1] => 1
337)
338Array
339(
340    [1] => 32767
341)
342Array
343(
344    [1] => -1
345)
346Array
347(
348    [1] => 0
349)
350Array
351(
352    [1] => -1000
353)
354Array
355(
356    [1] => 1102
357)
358Array
359(
360    [1] => 1
361)
362Array
363(
364    [1] => 65534
365)
366Array
367(
368    [1] => 1
369)
370Array
371(
372    [1] => 0
373)
374Array
375(
376    [1] => 64536
377)
378Array
379(
380    [1] => 1102
381)
382Array
383(
384    [1] => 1
385)
386