Skip to content

Commit 897e94d

Browse files
committed
test: add dictionary in functions
1 parent 0e1efc7 commit 897e94d

File tree

6 files changed

+455
-0
lines changed

6 files changed

+455
-0
lines changed

tests/data.dict

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
To be or not to be, that is the question;
2+
Whether 'tis nobler in the mind to suffer
3+
The Slings and Arrows of outrageous Fortune
4+
Or to take arms against a sea of troubles,
5+
And by opposing, end them. To die, to sleep;
6+
No more; and by a sleep to say we end
7+
The heart-ache and the thousand natural shocks
8+
That flesh is heir to 'tis a consummation
9+
Devoutly to be wish'd. To die, to sleep;
10+
To sleep, perchance to dream. Ay, there's the rub,
11+
For in that sleep of death what dreams may come,
12+
When we have shuffled off this mortal coil,
13+
Must give us pause. There's the respect
14+
That makes calamity of so long life,
15+
For who would bear the whips and scorns of time,
16+
Th'oppressor's wrong, the proud man's contumely,
17+
The pangs of dispriz'd love, the law's delay,
18+
The insolence of office, and the spurns
19+
That patient merit of th'unworthy takes,
20+
When he himself might his quietus make
21+
With a bare bodkin? who would fardels bear,
22+
To grunt and sweat under a weary life,
23+
But that the dread of something after death,
24+
The undiscovered country from whose bourn
25+
No traveller returns, puzzles the will,
26+
And makes us rather bear those ills we have
27+
Than fly to others that we know not of?
28+
Thus conscience does make cowards of us all,
29+
And thus the native hue of resolution
30+
Is sicklied o'er with the pale cast of thought,
31+
And enterprises of great pitch and moment
32+
With this regard their currents turn away,
33+
And lose the name of action.
34+
35+
36+
Hath not a Jew eyes? Hath not a Jew hands, organs, dimensions,
37+
senses, affections, passions; fed with the same food, hurt with
38+
the same weapons, subject to the same diseases, heal'd by
39+
the same means, warm'd and cool'd by the same winter and summer
40+
as a Christian is? If you prick us, do we not bleed? If you
41+
tickle us, do we not laugh? If you poison us, do we not die?
42+
And if you wrong us, shall we not revenge? If we are like you
43+
in the rest, we will resemble you in that. If a Jew wrong a
44+
Christian, what is his humility? Revenge. If a Christian wrong
45+
a Jew, what should his sufferance be by Christian example? Why,
46+
revenge. The villainy you teach me, I will execute, and it
47+
shall go hard but I will better the instruction.
48+
49+
Is this a dagger which I see before me,
50+
The handle toward my hand? Come, let me clutch thee.
51+
I have thee not, and yet I see thee still.
52+
Art thou not, fatal vision, sensible
53+
To feeling as to sight? or art thou but
54+
A dagger of the mind, a false creation,
55+
Proceeding from the heat-oppress'd brain?
56+
I see thee yet, in form as palpable
57+
As this which now I draw.
58+
Thou marshall'st me the way that I was going;
59+
And such an instrument I was to use.
60+
Mine eyes are made the fools o' the other senses,
61+
Or else worth all the rest; I see thee still,
62+
And on thy blade and dudgeon gouts of blood,
63+
Which was not so before.
64+
There's no such thing:
65+
It is the bloody business which informs Thus to mine eyes.
66+
Now o'er the one halfworld Nature seems dead,
67+
and wicked dreams abuse The curtain'd sleep; witchcraft celebrates
68+
Pale Hecate's offerings, and wither'd murder,
69+
Alarum'd by his sentinel, the wolf,
70+
Whose howl's his watch, thus with his stealthy pace.
71+
With Tarquin's ravishing strides, towards his design
72+
Moves like a ghost. Thou sure and firm-set earth,
73+
Hear not my steps, which way they walk,
74+
for fear Thy very stones prate of my whereabout,
75+
And take the present horror from the time,
76+
Which now suits with it.
77+
Whiles I threat, he lives:
78+
Words to the heat of deeds too cold breath gives.
79+
I go, and it is done; the bell invites me.
80+
Hear it not, Duncan; for it is a knell
81+
That summons thee to heaven or to hell.

tests/dictionary_args.phpt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--TEST--
2+
use dictionary: arguments
3+
--SKIPIF--
4+
<?php
5+
if (BROTLI_DICTIONARY_SUPPORT === false) die('skip dictionary not supported');
6+
?>
7+
--FILE--
8+
<?php
9+
include(dirname(__FILE__) . '/data.inc');
10+
$dictionary = file_get_contents(dirname(__FILE__) . '/data.dict');
11+
12+
function check($data, $dictionary, $level, $mode)
13+
{
14+
switch ($mode) {
15+
case BROTLI_GENERIC:
16+
$modeName = 'generic';
17+
break;
18+
case BROTLI_TEXT:
19+
$modeName = 'text';
20+
break;
21+
case BROTLI_FONT:
22+
$modeName = 'font';
23+
break;
24+
default:
25+
$modeName = '';
26+
}
27+
28+
$output = brotli_compress($data, $level, $mode, $dictionary);
29+
echo $level, ' -- ', $modeName, ' -- ',
30+
($dictionary === null ? 'null' : 'dict'), ' -- ',
31+
var_export(
32+
(($output === false) ? false
33+
: brotli_uncompress($output, 0, $dictionary) === $data),
34+
true
35+
), PHP_EOL;
36+
}
37+
38+
echo "*** Compression Level ***", PHP_EOL;
39+
$mode = BROTLI_GENERIC;
40+
for (
41+
$level = BROTLI_COMPRESS_LEVEL_MIN;
42+
$level <= BROTLI_COMPRESS_LEVEL_MAX;
43+
$level++
44+
) {
45+
check($data, $dictionary, $level, $mode);
46+
}
47+
48+
echo "*** Compression Mode ***", PHP_EOL;
49+
$level = BROTLI_COMPRESS_LEVEL_DEFAULT;
50+
check($data, $dictionary, $level, BROTLI_GENERIC);
51+
check($data, $dictionary, $level, BROTLI_TEXT);
52+
check($data, $dictionary, $level, BROTLI_FONT);
53+
54+
$mode = BROTLI_GENERIC;
55+
56+
echo "*** Compression Dictionary ***", PHP_EOL;
57+
check($data, null, $level, $mode);
58+
?>
59+
===DONE===
60+
--EXPECTF--
61+
*** Compression Level ***
62+
0 -- generic -- dict -- true
63+
1 -- generic -- dict -- true
64+
2 -- generic -- dict -- true
65+
3 -- generic -- dict -- true
66+
4 -- generic -- dict -- true
67+
5 -- generic -- dict -- true
68+
6 -- generic -- dict -- true
69+
7 -- generic -- dict -- true
70+
8 -- generic -- dict -- true
71+
9 -- generic -- dict -- true
72+
10 -- generic -- dict -- true
73+
11 -- generic -- dict -- true
74+
*** Compression Mode ***
75+
11 -- generic -- dict -- true
76+
11 -- text -- dict -- true
77+
11 -- font -- dict -- true
78+
*** Compression Dictionary ***
79+
11 -- generic -- null -- true
80+
===DONE===

tests/dictionary_basic.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
use dictionary: basic
3+
--SKIPIF--
4+
<?php
5+
if (BROTLI_DICTIONARY_SUPPORT === false) die('skip dictionary not supported');
6+
?>
7+
--FILE--
8+
<?php
9+
include(dirname(__FILE__) . '/data.inc');
10+
$dictionary = file_get_contents(dirname(__FILE__) . '/data.dict');
11+
12+
$level = BROTLI_COMPRESS_LEVEL_DEFAULT;
13+
$mode = BROTLI_GENERIC;
14+
15+
echo "*** Dictionary Compression ***", PHP_EOL;
16+
17+
$out1 = brotli_compress($data, $level, $mode, $dictionary);
18+
$out2 = \Brotli\compress($data, $level, $mode, $dictionary);
19+
20+
var_dump(brotli_uncompress($out1, 0, $dictionary) === $data);
21+
var_dump(brotli_uncompress($out2, 0, $dictionary) === $data);
22+
var_dump(\Brotli\uncompress($out1, 0, $dictionary) === $data);
23+
var_dump(\Brotli\uncompress($out2, 0, $dictionary) === $data);
24+
25+
var_dump(brotli_uncompress($out1) === $data);
26+
var_dump(brotli_uncompress($out2) === $data);
27+
var_dump(brotli_uncompress($out1, 0, 'dictionary') === $data);
28+
var_dump(brotli_uncompress($out2, 0, 'dictionary') === $data);
29+
?>
30+
===DONE===
31+
--EXPECTF--
32+
*** Dictionary Compression ***
33+
bool(true)
34+
bool(true)
35+
bool(true)
36+
bool(true)
37+
38+
Warning: brotli_uncompress(): failed to uncompress in %s on line %d
39+
bool(false)
40+
41+
Warning: brotli_uncompress(): failed to uncompress in %s on line %d
42+
bool(false)
43+
44+
Warning: brotli_uncompress(): failed to uncompress in %s on line %d
45+
bool(false)
46+
47+
Warning: brotli_uncompress(): failed to uncompress in %s on line %d
48+
bool(false)
49+
===DONE===

tests/dictionary_incremental.phpt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
use dictionary: incremental
3+
--SKIPIF--
4+
<?php
5+
if (BROTLI_DICTIONARY_SUPPORT === false) die('skip dictionary not supported');
6+
?>
7+
--FILE--
8+
<?php
9+
include(dirname(__FILE__) . '/data.inc');
10+
$dictionary = file_get_contents(dirname(__FILE__) . '/data.dict');
11+
12+
$modeTypes = [
13+
'BROTLI_PROCESS' => BROTLI_PROCESS,
14+
'BROTLI_FLUSH' => BROTLI_FLUSH,
15+
];
16+
17+
foreach ($modeTypes as $modeTypeKey => $modeType) {
18+
$compressed = '';
19+
20+
$context = brotli_compress_init(BROTLI_COMPRESS_LEVEL_DEFAULT,
21+
BROTLI_GENERIC,
22+
$dictionary);
23+
24+
foreach (str_split($data, 6) as $var) {
25+
$compressed .= brotli_compress_add($context, $var, $modeType);
26+
}
27+
$compressed .= brotli_compress_add($context, '', BROTLI_FINISH);
28+
29+
if ($data === brotli_uncompress($compressed, 0, $dictionary)) {
30+
echo "OK: {$modeTypeKey}\n";
31+
} else {
32+
echo "ERROR: {$modeTypeKey}\n";
33+
}
34+
35+
$out = '';
36+
$context = brotli_uncompress_init($dictionary);
37+
foreach (str_split($compressed, 6) as $var) {
38+
$out .= brotli_uncompress_add($context, $var, $modeType);
39+
}
40+
$out .= brotli_uncompress_add($context, '', BROTLI_FINISH);
41+
42+
if ($data === $out) {
43+
echo "Ok: {$modeTypeKey}\n";
44+
} else {
45+
echo "Error: {$modeTypeKey}\n";
46+
}
47+
}
48+
?>
49+
===DONE===
50+
--EXPECT--
51+
OK: BROTLI_PROCESS
52+
Ok: BROTLI_PROCESS
53+
OK: BROTLI_FLUSH
54+
Ok: BROTLI_FLUSH
55+
===DONE===

tests/dictionary_named_args.phpt

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
--TEST--
2+
use dictionary: named arguments
3+
--SKIPIF--
4+
<?php
5+
if (PHP_VERSION_ID < 80000) die("skip requires PHP 8.0+");
6+
if (BROTLI_DICTIONARY_SUPPORT === false) die('skip dictionary not supported');
7+
?>
8+
--FILE--
9+
<?php
10+
include(dirname(__FILE__) . '/data.inc');
11+
$dictionary = file_get_contents(dirname(__FILE__) . '/data.dict');
12+
$level = BROTLI_COMPRESS_LEVEL_MAX;
13+
$mode = BROTLI_TEXT;
14+
15+
echo "** brotli_compress(dict:) **\n";
16+
try {
17+
var_dump(gettype(brotli_compress(dict: $dictionary)));
18+
} catch (Error $e) {
19+
echo $e->getMessage(), PHP_EOL;
20+
}
21+
22+
echo "** brotli_compress(data:, dict:) **\n";
23+
try {
24+
var_dump(gettype(brotli_compress(data: $data, dict: $dictionary)));
25+
var_dump(brotli_uncompress(brotli_compress(data: $data, dict: $dictionary), dict: $dictionary) === $data);
26+
} catch (Error $e) {
27+
echo $e->getMessage(), PHP_EOL;
28+
}
29+
30+
echo "** brotli_compress(level:, dict:) **\n";
31+
try {
32+
var_dump(gettype(brotli_compress(level: $level, dict: $dictionary)));
33+
} catch (Error $e) {
34+
echo $e->getMessage(), PHP_EOL;
35+
}
36+
37+
echo "** brotli_compress(mode:, dict:) **\n";
38+
try {
39+
var_dump(gettype(brotli_compress(mode: $mode, dict: $dictionary)));
40+
} catch (Error $e) {
41+
echo $e->getMessage(), PHP_EOL;
42+
}
43+
44+
echo "** brotli_compress(data:, level:, dict:) **\n";
45+
try {
46+
var_dump(gettype(brotli_compress(data: $data, level: $level, dict: $dictionary)));
47+
var_dump(brotli_uncompress(brotli_compress(data: $data, level: $level, dict: $dictionary), dict: $dictionary) === $data);
48+
} catch (Error $e) {
49+
echo $e->getMessage(), PHP_EOL;
50+
}
51+
52+
echo "** brotli_compress(data:, mode:, dict:) **\n";
53+
try {
54+
var_dump(gettype(brotli_compress(data: $data, mode: $mode, dict: $dictionary)));
55+
var_dump(brotli_uncompress(brotli_compress(data: $data, mode: $mode, dict: $dictionary), dict: $dictionary) === $data);
56+
} catch (Error $e) {
57+
echo $e->getMessage(), PHP_EOL;
58+
}
59+
60+
echo "** brotli_compress(level:, mode:, dict:) **\n";
61+
try {
62+
var_dump(gettype(brotli_compress(level: $level, mode: $mode, dict: $dictionary)));
63+
} catch (Error $e) {
64+
echo $e->getMessage(), PHP_EOL;
65+
}
66+
67+
$compressed = brotli_compress(data: $data, dict: $dictionary);
68+
69+
echo "** brotli_uncompress(dict:): false **\n";
70+
try {
71+
var_dump(gettype(brotli_uncompress(dict: $dictionary)));
72+
} catch (Error $e) {
73+
echo $e->getMessage(), PHP_EOL;
74+
}
75+
76+
echo "** brotli_uncompress(data:, dict:) **\n";
77+
try {
78+
var_dump(gettype(brotli_uncompress(data: $compressed, dict: $dictionary)));
79+
var_dump(brotli_uncompress(data: $compressed, dict: $dictionary) === $data);
80+
} catch (Error $e) {
81+
echo $e->getMessage(), PHP_EOL;
82+
}
83+
84+
echo "** brotli_uncompress(length:, dict:) **\n";
85+
try {
86+
var_dump(gettype(brotli_uncompress(length: strlen($compressed), dict: $dictionary)));
87+
} catch (Error $e) {
88+
echo $e->getMessage(), PHP_EOL;
89+
}
90+
91+
echo "** brotli_uncompress(data: length:, dict:) **\n";
92+
try {
93+
var_dump(gettype(brotli_uncompress(data: $compressed, length: strlen($compressed), dict: $dictionary)));
94+
var_dump(brotli_uncompress(data: $compressed, length: strlen($compressed), dict: $dictionary) === $data);
95+
} catch (Error $e) {
96+
echo $e->getMessage(), PHP_EOL;
97+
}
98+
?>
99+
===DONE===
100+
--EXPECTF--
101+
** brotli_compress(dict:) **
102+
brotli_compress(): Argument #1 ($data) not passed
103+
** brotli_compress(data:, dict:) **
104+
string(6) "string"
105+
bool(true)
106+
** brotli_compress(level:, dict:) **
107+
brotli_compress(): Argument #1 ($data) not passed
108+
** brotli_compress(mode:, dict:) **
109+
brotli_compress(): Argument #1 ($data) not passed
110+
** brotli_compress(data:, level:, dict:) **
111+
string(6) "string"
112+
bool(true)
113+
** brotli_compress(data:, mode:, dict:) **
114+
string(6) "string"
115+
bool(true)
116+
** brotli_compress(level:, mode:, dict:) **
117+
brotli_compress(): Argument #1 ($data) not passed
118+
** brotli_uncompress(dict:): false **
119+
brotli_uncompress(): Argument #1 ($data) not passed
120+
** brotli_uncompress(data:, dict:) **
121+
string(6) "string"
122+
bool(true)
123+
** brotli_uncompress(length:, dict:) **
124+
brotli_uncompress(): Argument #1 ($data) not passed
125+
** brotli_uncompress(data: length:, dict:) **
126+
string(6) "string"
127+
bool(true)
128+
===DONE===

0 commit comments

Comments
 (0)