xref: /php-src/ext/mbstring/tests/mb_str_pad.phpt (revision 5853cdb7)
1--TEST--
2mb_str_pad()
3--EXTENSIONS--
4mbstring
5--FILE--
6<?php
7
8echo "--- Error conditions ---\n";
9try {
10    var_dump(mb_str_pad('▶▶', 6, '', STR_PAD_RIGHT));
11} catch (ValueError $e) {
12    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
13}
14try {
15    var_dump(mb_str_pad('▶▶', 6, '', STR_PAD_LEFT));
16} catch (ValueError $e) {
17    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
18}
19try {
20    var_dump(mb_str_pad('▶▶', 6, '', STR_PAD_BOTH));
21} catch (ValueError $e) {
22    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
23}
24try {
25    var_dump(mb_str_pad('▶▶', 6, ' ', 123456));
26} catch (ValueError $e) {
27    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
28}
29try {
30    var_dump(mb_str_pad('▶▶', 6, ' ', STR_PAD_BOTH, 'non-existing'));
31} catch (ValueError $e) {
32    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
33}
34
35echo "--- Simple ASCII strings ---\n";
36var_dump(mb_str_pad('Hello', 7, '+-', STR_PAD_BOTH));
37var_dump(mb_str_pad('World', 10, '+-', STR_PAD_BOTH));
38var_dump(mb_str_pad('Hello', 7, '+-', STR_PAD_LEFT));
39var_dump(mb_str_pad('World', 10, '+-', STR_PAD_LEFT));
40var_dump(mb_str_pad('Hello', 7, '+-', STR_PAD_RIGHT));
41var_dump(mb_str_pad('World', 10, '+-', STR_PAD_RIGHT));
42
43echo "--- Edge cases pad length ---\n";
44var_dump(mb_str_pad('▶▶', 2, ' ', STR_PAD_BOTH));
45var_dump(mb_str_pad('▶▶', 1, ' ', STR_PAD_BOTH));
46var_dump(mb_str_pad('▶▶', 0, ' ', STR_PAD_BOTH));
47var_dump(mb_str_pad('▶▶', -1, ' ', STR_PAD_BOTH));
48
49echo "--- No pad string ---\n";
50var_dump(mb_str_pad('▶▶', 4));
51var_dump(mb_str_pad('▶▶', 3));
52var_dump(mb_str_pad('▶▶', 2));
53var_dump(mb_str_pad('▶▶', 1));
54
55echo "--- Empty input string ---\n";
56var_dump(mb_str_pad('', 2, ' ', STR_PAD_BOTH));
57var_dump(mb_str_pad('', 1, ' ', STR_PAD_BOTH));
58var_dump(mb_str_pad('', 0, ' ', STR_PAD_BOTH));
59var_dump(mb_str_pad('', -1, ' ', STR_PAD_BOTH));
60
61echo "--- No default argument ---\n";
62var_dump(mb_str_pad('▶▶', 6, pad_type: STR_PAD_RIGHT));
63var_dump(mb_str_pad('▶▶', 6, pad_type: STR_PAD_LEFT));
64var_dump(mb_str_pad('▶▶', 6, pad_type: STR_PAD_BOTH));
65
66echo "--- UTF-8 emojis ---\n";
67for ($i = 6; $i > 0; $i--) {
68    var_dump(mb_str_pad('▶▶', $i, '❤❓❇', STR_PAD_RIGHT));
69    var_dump(mb_str_pad('▶▶', $i, '❤❓❇', STR_PAD_LEFT));
70    var_dump(mb_str_pad('▶▶', $i, '❤❓❇', STR_PAD_BOTH));
71}
72
73echo "--- UTF-8, 32, 7 test ---\n";
74
75// Taken from mb_substr.phpt
76$utf8 = "Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь";
77$utf32 = mb_convert_encoding($utf8, 'UTF-32', 'UTF-8');
78$utf7 = mb_convert_encoding($utf8, 'UTF-7', 'UTF-8');
79$tests = ["UTF-8" => $utf8, "UTF-32" => $utf32, "UTF-7" => $utf7];
80
81foreach ($tests as $encoding => $test) {
82    $pad_str = mb_convert_encoding('▶▶', $encoding, 'UTF-8');
83    var_dump(mb_convert_encoding(mb_str_pad($test, 44, $pad_str, STR_PAD_RIGHT, $encoding), 'UTF-8', $encoding));
84    var_dump(mb_convert_encoding(mb_str_pad($test, 44, $pad_str, STR_PAD_LEFT, $encoding), 'UTF-8', $encoding));
85    var_dump(mb_convert_encoding(mb_str_pad($test, 44, $pad_str, STR_PAD_BOTH, $encoding), 'UTF-8', $encoding));
86}
87?>
88--EXPECT--
89--- Error conditions ---
90ValueError: mb_str_pad(): Argument #3 ($pad_string) must not be empty
91ValueError: mb_str_pad(): Argument #3 ($pad_string) must not be empty
92ValueError: mb_str_pad(): Argument #3 ($pad_string) must not be empty
93ValueError: mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH
94ValueError: mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "non-existing" given
95--- Simple ASCII strings ---
96string(7) "+Hello+"
97string(10) "+-World+-+"
98string(7) "+-Hello"
99string(10) "+-+-+World"
100string(7) "Hello+-"
101string(10) "World+-+-+"
102--- Edge cases pad length ---
103string(6) "▶▶"
104string(6) "▶▶"
105string(6) "▶▶"
106string(6) "▶▶"
107--- No pad string ---
108string(8) "▶▶  "
109string(7) "▶▶ "
110string(6) "▶▶"
111string(6) "▶▶"
112--- Empty input string ---
113string(2) "  "
114string(1) " "
115string(0) ""
116string(0) ""
117--- No default argument ---
118string(10) "▶▶    "
119string(10) "    ▶▶"
120string(10) "  ▶▶  "
121--- UTF-8 emojis ---
122string(18) "▶▶❤❓❇❤"
123string(18) "❤❓❇❤▶▶"
124string(18) "❤❓▶▶❤❓"
125string(15) "▶▶❤❓❇"
126string(15) "❤❓❇▶▶"
127string(15) "❤▶▶❤❓"
128string(12) "▶▶❤❓"
129string(12) "❤❓▶▶"
130string(12) "❤▶▶❤"
131string(9) "▶▶❤"
132string(9) "❤▶▶"
133string(9) "▶▶❤"
134string(6) "▶▶"
135string(6) "▶▶"
136string(6) "▶▶"
137string(6) "▶▶"
138string(6) "▶▶"
139string(6) "▶▶"
140--- UTF-8, 32, 7 test ---
141string(92) "Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь▶▶▶"
142string(92) "▶▶▶Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь"
143string(92) "▶Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь▶▶"
144string(92) "Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь▶▶▶"
145string(92) "▶▶▶Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь"
146string(92) "▶Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь▶▶"
147string(92) "Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь▶▶▶"
148string(92) "▶▶▶Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь"
149string(92) "▶Σὲ γνωρίζω ἀπὸ τὴν κόψη Зарегистрируйтесь▶▶"
150