Skip to content

Commit 42d8f86

Browse files
committed
test cases
1 parent 4e16aeb commit 42d8f86

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

tests/basic.phpt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
--TEST--
2+
PHP Bencode - Path Test
3+
--FILE--
4+
<?php
5+
error_reporting(E_ALL);
6+
7+
function println($info) { echo $info . "\n"; }
8+
9+
$broot = bitem::load(__DIR__ . '/test.in');
10+
println(sha1($broot));
11+
print_r($broot->to_array());
12+
print_r($broot->to_meta_array());
13+
$broot->save(__DIR__ . '/test.out');
14+
println(sha1_file(__DIR__ . '/test.in') === sha1_file(__DIR__ . '/test.out'));
15+
unlink(__DIR__ . '/test.out');
16+
17+
exit(0);
18+
?>
19+
--EXPECTF--
20+
bef3e94aa2c1bf31a4991c86bb3774c527a553a2
21+
Array
22+
(
23+
[key1] => Array
24+
(
25+
[0] => hello world
26+
[1] => foo bar
27+
[2] => bye world
28+
[3] => Array
29+
(
30+
[subkey] => I can eat glass
31+
)
32+
33+
)
34+
35+
[key2/slash] => 65536
36+
)
37+
Array
38+
(
39+
[_type] => bdict
40+
[_length] => 92
41+
[_data] => Array
42+
(
43+
[key1] => Array
44+
(
45+
[_type] => blist
46+
[_length] => 64
47+
[_data] => Array
48+
(
49+
[0] => Array
50+
(
51+
[_type] => bstr
52+
[_length] => 14
53+
[_data] => hello world
54+
)
55+
56+
[1] => Array
57+
(
58+
[_type] => bstr
59+
[_length] => 9
60+
[_data] => foo bar
61+
)
62+
63+
[2] => Array
64+
(
65+
[_type] => bstr
66+
[_length] => 11
67+
[_data] => bye world
68+
)
69+
70+
[3] => Array
71+
(
72+
[_type] => bdict
73+
[_length] => 28
74+
[_data] => Array
75+
(
76+
[subkey] => Array
77+
(
78+
[_type] => bstr
79+
[_length] => 18
80+
[_data] => I can eat glass
81+
)
82+
83+
)
84+
85+
)
86+
87+
)
88+
89+
)
90+
91+
[key2/slash] => Array
92+
(
93+
[_type] => bint
94+
[_length] => 7
95+
[_data] => 65536
96+
)
97+
98+
)
99+
100+
)
101+
1
102+

tests/path.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
PHP Bencode - Path Test
3+
--FILE--
4+
<?php
5+
error_reporting(E_ALL);
6+
7+
function println($info) { echo $info . "\n"; }
8+
9+
$broot = bitem::load(__DIR__ . '/test.in');
10+
println($broot->get('key1'));
11+
println($broot->get_path('key1/2'));
12+
println($broot->get_path('key2\/slash'));
13+
$broot->set_path('key3/0/new_bdict/new_str', new bstr('it does not hurt me'));
14+
println($broot);
15+
16+
exit(0);
17+
?>
18+
--EXPECTF--
19+
l11:hello world7:foo bar9:bye worldd6:subkey15:I can eat glassee
20+
9:bye world
21+
i65536e
22+
d4:key1l11:hello world7:foo bar9:bye worldd6:subkey15:I can eat glassee10:key2/slashi65536e4:key3ld9:new_bdictd7:new_str19:it does not hurt meeeee
23+

tests/ref_n_cpy.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
PHP Bencode - Reference and Copy Test
3+
--FILE--
4+
<?php
5+
error_reporting(E_ALL);
6+
7+
function println($info) { echo $info . "\n"; }
8+
9+
$broot = bitem::load(__DIR__ . '/test.in');
10+
$ref1 = $broot->get_path('key1/2');
11+
$cpy1 = $broot->get_path_copy('key1/2');
12+
$ref1->set('ref world');
13+
$cpy1->set('cpy world');
14+
unset($ref1);
15+
unset($cpy1);
16+
println($broot);
17+
$ref2 = $broot->get('key2/slash');
18+
$cpy2 = $broot->get_copy('key2/slash');
19+
$ref2->set(-1024);
20+
$cpy2->set(2048);
21+
unset($ref2);
22+
unset($cpy2);
23+
println($broot);
24+
25+
exit(0);
26+
?>
27+
--EXPECTF--
28+
d4:key1l11:hello world7:foo bar9:ref worldd6:subkey15:I can eat glassee10:key2/slashi65536ee
29+
d4:key1l11:hello world7:foo bar9:ref worldd6:subkey15:I can eat glassee10:key2/slashi-1024ee
30+

tests/search.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
PHP Bencode - Search Test
3+
--FILE--
4+
<?php
5+
error_reporting(E_ALL);
6+
7+
function println($info) { echo $info . "\n"; }
8+
9+
$broot = bitem::load(__DIR__ . '/test.in');
10+
foreach ($broot->search('world', 1) as $path) {
11+
println($broot->get_path($path));
12+
}
13+
foreach ($broot->search('key', 0) as $path) {
14+
println($broot->get_path($path));
15+
}
16+
17+
exit(0);
18+
?>
19+
--EXPECTF--
20+
11:hello world
21+
9:bye world
22+
l11:hello world7:foo bar9:bye worldd6:subkey15:I can eat glassee
23+
15:I can eat glass
24+
i65536e

tests/test.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
d4:key1l11:hello world7:foo bar9:bye worldd6:subkey15:I can eat glassee10:key2/slashi65536ee

0 commit comments

Comments
 (0)