Annotation of embedaddon/php/ext/standard/tests/strings/str_pad.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: str_pad() function
3: --INI--
4: precision=14
5: --FILE--
6: <?php
7: /* Pad a string to a certain length with another string */
8:
9: echo "\n#### Basic operations ####\n";
10: $input_string = "str_pad()";
11: $pad_length = 20;
12: $pad_string = "-+";
13: var_dump( str_pad($input_string, $pad_length) ); // default pad_string & pad_type
14: var_dump( str_pad($input_string, $pad_length, $pad_string) ); // default pad_type
15: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_LEFT) );
16: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_RIGHT) );
17: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_BOTH) );
18:
19: echo "\n#### variations with input string and pad-length ####\n";
20: /* different input string variation */
21: $input_strings = array(
22: "variation", // normal string
23: chr(0).chr(255).chr(128).chr(234).chr(143), // >7-bit ASCII
24: "", // empty string
25: NULL, // NULL
26: true, // boolean
27: 15, // numeric
28: 15.55, // numeric
29: "2990" // numeric string
30: );
31: /* different pad_lengths */
32: $pad_lengths = array(
33: -PHP_INT_MAX, // huge negative value
34: -1, // negative value
35: 0, // pad_length < sizeof(input_string)
36: 9, // pad_length <= sizeof(input_string)
37: 10, // pad_length > sizeof(input_string)
38: 16, // pad_length > sizeof(input_string)
39: );
40: $pad_string = "=";
41: /*loop through to use each varient of $pad_length on
42: each element of $input_strings array */
43: foreach ($input_strings as $input_string ) {
44: foreach ($pad_lengths as $pad_length ) {
45: var_dump( str_pad($input_string, $pad_length) ); // default pad_string & pad_type
46: var_dump( str_pad($input_string, $pad_length, $pad_string) ); // default pad_type
47: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_LEFT) );
48: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_RIGHT) );
49: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_BOTH) );
50: }
51: }
52:
53: echo "\n#### variation with pad string ####\n";
54: $pad_strings = array ("=", 1, true, "string_pad", 1.5, "\t", '\t');
55: $input_string="variation";
56: $pad_length = 16;
57: foreach ( $pad_strings as $pad_string ) {
58: var_dump( str_pad($input_string, $pad_length, $pad_string) ); // default pad_type
59: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_LEFT) );
60: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_RIGHT) );
61: var_dump( str_pad($input_string, $pad_length, $pad_string, STR_PAD_BOTH) );
62: }
63:
64: echo "\n#### error conditions ####";
65: /* args less than min. expected of 2 */
66: str_pad();
67: str_pad($input_string);
68:
69: /* args more than expected,expected 4 */
70: str_pad($input_tring, $pad_length, $pad_string, STR_PAD_LEFT, NULL );
71:
72: echo "\n--- padding string as null ---";
73: str_pad($input_string, 12, NULL);
74: str_pad($input_string, 12, "");
75:
76: /* bad pad_type - passing an undefined one */
77: var_dump( str_pad($input_string, $pad_length, "+", 15) );
78:
79: echo "Done\n";
80: ?>
81: --EXPECTF--
82: #### Basic operations ####
83: string(20) "str_pad() "
84: string(20) "str_pad()-+-+-+-+-+-"
85: string(20) "-+-+-+-+-+-str_pad()"
86: string(20) "str_pad()-+-+-+-+-+-"
87: string(20) "-+-+-str_pad()-+-+-+"
88:
89: #### variations with input string and pad-length ####
90: string(9) "variation"
91: string(9) "variation"
92: string(9) "variation"
93: string(9) "variation"
94: string(9) "variation"
95: string(9) "variation"
96: string(9) "variation"
97: string(9) "variation"
98: string(9) "variation"
99: string(9) "variation"
100: string(9) "variation"
101: string(9) "variation"
102: string(9) "variation"
103: string(9) "variation"
104: string(9) "variation"
105: string(9) "variation"
106: string(9) "variation"
107: string(9) "variation"
108: string(9) "variation"
109: string(9) "variation"
110: string(10) "variation "
111: string(10) "variation="
112: string(10) "=variation"
113: string(10) "variation="
114: string(10) "variation="
115: string(16) "variation "
116: string(16) "variation======="
117: string(16) "=======variation"
118: string(16) "variation======="
119: string(16) "===variation===="
120: string(5) "