Annotation of embedaddon/php/ext/standard/tests/strings/strlen.phpt, revision 1.1.1.2

1.1       misho       1: --TEST--
                      2: strlen() function
                      3: --INI--
                      4: precision = 12
                      5: --FILE--
                      6: <?php
                      7: /* returns the length of a given string */
                      8: 
                      9: echo "#### Basic operations and  variations ####\n";
                     10: $strings = array( "Hello, World",
                     11:                  'Hello, World',
                     12:                  '!!Hello, World',
                     13:                  "??Hello, World",
                     14:                  "$@#%^&*!~,.:;?",
                     15:                  "123",
                     16:                  123,
                     17:                  "-1.2345",
                     18:                  -1.2344,
                     19:                  NULL,
                     20:                  "",
                     21:                  " ",
                     22:                  "\0",
                     23:                  "\x000",                                      // len = 2
                     24:                  "\xABC",                                      // len = 2
                     25:                  "\0000",                                      // len = 2
                     26:                  "0",
                     27:                  0,
                     28:                  "\t",                                         // len = 1
                     29:                  '\t',                                         // len = 2
                     30:                  TRUE,
                     31:                  FALSE,
                     32:                  "Hello, World\0", 
                     33:                  "Hello\0World", 
                     34:                  'Hello, World\0', 
                     35:                  "Hello, World\n", 
                     36:                  "Hello, World\r", 
                     37:                  "Hello, World\t", 
                     38:                  "Hello, World\\", 
                     39:                  "              ", 
                     40:                  chr(128).chr(234).chr(65).chr(255).chr(256),          
                     41: 
                     42:                  "abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
                     43:                    []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
                     44:                    abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
                     45:                    []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
                     46:                    abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
                     47:                    []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
                     48:                    abcdefghijklmnopqrstuvwxyz0123456789"
                     49:                );
                     50: 
                     51: /* loop through to find the length of each string */             
                     52: for($i=0; $i<count($strings); $i++) {
                     53:   echo "String length of '$strings[$i]' is => ";
                     54:   var_dump( strlen($strings[$i]) );
                     55: }
                     56: 
                     57: 
                     58: 
                     59: echo "\n#### Testing Miscelleneous inputs ####\n";
                     60: 
                     61: echo "--- Testing objects ---\n";
                     62: /* we get "Catchable fatal error: saying Object of class could not be converted
                     63:        to string" by default when an object is passed instead of string:
1.1.1.2 ! misho      64: The error can be  avoided by choosing the __toString magix method as follows: */
1.1       misho      65: 
                     66: class string {
                     67:   function __toString() {
                     68:     return "Hello, world";
                     69:   }
                     70: }
                     71: $obj_string = new string;
                     72: 
                     73: var_dump(strlen("$obj_string"));
                     74: 
                     75: 
                     76: echo "\n--- Testing arrays ---\n";
                     77: $str_arr = array("hello", "?world", "!$%**()%**[][[[&@#~!", array());
                     78: var_dump(strlen($str_arr));
                     79: var_dump(strlen("$str_arr[1]"));
                     80: var_dump(strlen("$str_arr[2]"));
                     81: 
                     82: echo "\n--- Testing Resources ---\n";
                     83: $filename1 = "dummy.txt";
                     84: $file1 = fopen($filename1, "w");                // creating new file
                     85: 
                     86: /* getting resource type for file handle */
                     87: $string1 = get_resource_type($file1);
                     88: $string2 = (int)get_resource_type($file1);     // converting stream type to int
                     89: 
                     90: /* $string1 is of "stream" type */
                     91: var_dump(strlen($string1));                            // int(6)
                     92: 
                     93: /* $string2 holds a value of "int(0)" */
                     94: var_dump(strlen($string2));                    // int(1)
                     95: 
                     96: fclose($file1);                                 // closing the file "dummy.txt"
                     97: unlink("$filename1");                           // deletes "dummy.txt"
                     98: 
                     99: 
                    100: echo "\n--- Testing a longer and heredoc string ---\n";
                    101: $string = <<<EOD
                    102: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    103: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    104: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    105: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    106: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    107: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    108: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    109: @#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
                    110: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
                    111: EOD;
                    112: var_dump(strlen($string));
                    113: 
                    114: echo "\n--- Testing a heredoc null string ---\n";
                    115: $str = <<<EOD
                    116: EOD;
                    117: var_dump(strlen($str));
                    118: 
                    119: 
                    120: echo "\n--- Testing simple and complex syntax strings ---\n";
                    121: $str = 'world';
                    122: 
                    123: /* Simple syntax */
                    124: var_dump(strlen("$str"));
                    125: var_dump(strlen("$str'S"));
                    126: var_dump(strlen("$strS"));
                    127: 
                    128: /* String with curly braces, complex syntax */
                    129: var_dump(strlen("${str}S"));
                    130: var_dump(strlen("{$str}S"));
                    131: 
                    132: echo "\n--- strlen for long float values ---\n";
                    133: /* Here two different outputs, which depends on the rounding value
                    134:    before converting to string. Here Precision = 12  */
                    135: var_dump(strlen(10.55555555555555555555555555));               // len = 13
                    136: var_dump(strlen(10.55555555595555555555555555));               // len = 12 
                    137: 
                    138: echo "\n--- Nested strlen() ---\n";
                    139: var_dump(strlen(strlen("Hello")));                             // len=1
                    140: 
                    141: 
                    142: echo "\n#### error conditions ####";
                    143: /* Zero arguments */
                    144: strlen();
                    145: /* Greater number of args than expected */
                    146: strlen("string1", "string2");
                    147: strlen("", "");
                    148: 
                    149: echo "Done\n";
                    150: ?>
                    151: --EXPECTF--
                    152: #### Basic operations and  variations ####
                    153: String length of 'Hello, World' is => int(12)
                    154: String length of 'Hello, World' is => int(12)
                    155: String length of '!!Hello, World' is => int(14)
                    156: String length of '??Hello, World' is => int(14)
                    157: String length of '$@#%^&*!~,.:;?' is => int(14)
                    158: String length of '123' is => int(3)
                    159: String length of '123' is => int(3)
                    160: String length of '-1.2345' is => int(7)
                    161: String length of '-1.2344' is => int(7)
                    162: String length of '' is => int(0)
                    163: String length of '' is => int(0)
                    164: String length of ' ' is => int(1)
                    165: String length of '' is => int(1)
                    166: String length of '0' is => int(2)
                    167: String length of '«C' is => int(2)
                    168: String length of '0' is => int(2)
                    169: String length of '0' is => int(1)
                    170: String length of '0' is => int(1)
                    171: String length of '     ' is => int(1)
                    172: String length of '\t' is => int(2)
                    173: String length of '1' is => int(1)
                    174: String length of '' is => int(0)
                    175: String length of 'Hello, World' is => int(13)
                    176: String length of 'HelloWorld' is => int(11)
                    177: String length of 'Hello, World\0' is => int(14)
                    178: String length of 'Hello, World
                    179: ' is => int(13)
                    180: String length of 'Hello, World
' is => int(13)
                    181: String length of 'Hello, World ' is => int(13)
                    182: String length of 'Hello, World\' is => int(13)
                    183: String length of '              ' is => int(14)
                    184: String length of '€êAÿ' is => int(5)
                    185: String length of 'abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
                    186:                    []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
                    187:                    abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
                    188:                    []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
                    189:                    abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
                    190:                    []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
                    191:                    abcdefghijklmnopqrstuvwxyz0123456789' is => int(495)
                    192: 
                    193: #### Testing Miscelleneous inputs ####
                    194: --- Testing objects ---
                    195: int(12)
                    196: 
                    197: --- Testing arrays ---
                    198: 
                    199: Warning: strlen() expects parameter 1 to be string, array given in %s on line %d
                    200: NULL
                    201: int(6)
                    202: int(20)
                    203: 
                    204: --- Testing Resources ---
                    205: int(6)
                    206: int(1)
                    207: 
                    208: --- Testing a longer and heredoc string ---
                    209: int(639)
                    210: 
                    211: --- Testing a heredoc null string ---
                    212: int(0)
                    213: 
                    214: --- Testing simple and complex syntax strings ---
                    215: int(5)
                    216: int(7)
                    217: 
                    218: Notice: Undefined variable: strS in %s on line %d
                    219: int(0)
                    220: int(6)
                    221: int(6)
                    222: 
                    223: --- strlen for long float values ---
                    224: int(13)
                    225: int(12)
                    226: 
                    227: --- Nested strlen() ---
                    228: int(1)
                    229: 
                    230: #### error conditions ####
                    231: Warning: strlen() expects exactly 1 parameter, 0 given in %s on line %d
                    232: 
                    233: Warning: strlen() expects exactly 1 parameter, 2 given in %s on line %d
                    234: 
                    235: Warning: strlen() expects exactly 1 parameter, 2 given in %s on line %d
                    236: Done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>