~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to tests/testscanf.php

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
 
 
4
function print_value($val,$postfix="<br>") {
 
5
        if (is_array($val)) {
 
6
                for ($i = 0;$i< count($val);$i++) {
 
7
                        echo $val[$i] . $postfix;
 
8
                }
 
9
        } else {
 
10
                echo $val . $postfix;
 
11
        }
 
12
}
 
13
 
 
14
function do_sscanf($string, $format) {
 
15
        $s = "sscanf(\"" . $string . ",\"" . $format ."\").";
 
16
        echo "$s<br>";
 
17
        $s = str_repeat("-", strlen($s));
 
18
        echo "$s<br>";
 
19
        $output = sscanf($string,$format);
 
20
        echo "Result : ";
 
21
        print_value( $output );
 
22
        echo "$s<br><br>";
 
23
}
 
24
 
 
25
 
 
26
function run_sscanf_test_cases($filename="scan_cases")
 
27
{
 
28
 
 
29
        echo "<h3><em><br>Running Test Cases from $filename<br></em></h3>"; 
 
30
        $arr = file($filename);
 
31
        for ($i=0;$i < count($arr);$i++) {
 
32
                $line_arr = explode("|",$arr[$i]);
 
33
                
 
34
                $format = $line_arr[0];
 
35
                $string = $line_arr[1];
 
36
                if (count($arr) > 2) {
 
37
                        $comment = $line_arr[2];
 
38
                } else {
 
39
                        $comment = "";
 
40
                }
 
41
                if ( empty($format) || empty($string) ) {
 
42
                        continue;
 
43
                }
 
44
                print("<h4>** Case : $comment ******************************</h4>");
 
45
                do_sscanf($string,$format);
 
46
        }
 
47
}
 
48
 
 
49
function simple_tests() {
 
50
        echo "Testing sscanf with standard ANSI syntax (values returned by
 
51
reference)-<br>";
 
52
        $decimal = -1;
 
53
        $string  = "";
 
54
        $hex     = 0;
 
55
        $float   = 0.0; 
 
56
        $octal   = 0.0;
 
57
        $int     = -1;
 
58
                                
 
59
        echo "<h3><em><br>Simple Test<br></em></h3>"; 
 
60
        echo "sscanf('10','%d',&\$decimal) <br>";
 
61
        echo "<br>BEFORE : <br> decimal = $decimal.";
 
62
        $foo = sscanf("10","%d",&$decimal);
 
63
        echo "<br>AFTER  : <br> decimal = $decimal <br>";
 
64
 
 
65
 
 
66
        echo "<h3><em><br>Simple Test 2<br></em></h3>"; 
 
67
        echo "sscanf(\"ghost 0xface\",\"%s %x\",&\$string, &\$int)<br>";
 
68
        echo "<br>BEFORE : <br> string = $string, int = $int<br>";
 
69
        $foo = sscanf("ghost 0xface","%s %x",&$string, &$int);
 
70
        echo "<br>AFTER  : <br> string = $string, int = $int<br>";
 
71
        echo " sscan reports : ";
 
72
        print_value( $foo,"");
 
73
        echo " conversions <br>";
 
74
 
 
75
        echo "<h3><em><br>Multiple specifiers<br></em></h3>"; 
 
76
        echo "sscanf(\"jabberwocky 1024 0xFF 1.024 644 10\",
 
77
                        \"%s %d  %x %f %o %i\",
 
78
                        &\$string,&\$decimal,&\$hex,&\$float,&\$octal,&\$int);<br>";
 
79
        echo "<br>BEFORE : <br>";
 
80
        echo "Decimal = $decimal, String = $string, Hex = $hex<br>";
 
81
        echo "Octal = $octal , Float = $float, Int = $int<br>"; 
 
82
        $foo = sscanf(  "jabberwocky 1024 0xFF 1.024 644 10",
 
83
                        "%s %d  %x %f %o %i",
 
84
                        &$string,&$decimal,&$hex,&$float,&$octal,&$int);
 
85
        echo "<br>AFTER :<br>";
 
86
        echo "decimal = $decimal, string = $string, hex = $hex<br>";
 
87
        echo "octal = $octal , float = $float, int = $int<br>"; 
 
88
                                
 
89
        echo " sscan reports : ";
 
90
        print_value( $foo,"");
 
91
        echo " conversions <br>";
 
92
        echo "----------------------------------------<br>";
 
93
}
 
94
 
 
95
 
 
96
 
 
97
?>
 
98
<html>
 
99
        <head>
 
100
                <title>Test of sscanf()</title>
 
101
        </head>
 
102
        <body>
 
103
                <strong><h1>Testing sscanf() support in PHP</h1></strong><br>   
 
104
                <?php
 
105
                        if (!function_exists('sscanf')) {
 
106
                                echo "<strong>I'm sorry but sscanf() does not exist !i</strong><br>";
 
107
                        } else {
 
108
                                simple_tests();
 
109
                                run_sscanf_test_cases(); 
 
110
                        }
 
111
                ?>
 
112
        </body> 
 
113
</html>