summaryrefslogtreecommitdiff
path: root/tests/testscanf.php
blob: ad530978c52989712082549d0137782b1b640226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php


function print_value($val,$postfix="<br>") {
	if (is_array($val)) {
		for ($i = 0;$i< count($val);$i++) {
			echo $val[$i] . $postfix;
		}
	} else {
		echo $val . $postfix;
	}
}

function do_sscanf($string, $format) {
	$s = "sscanf(\"" . $string . ",\"" . $format ."\").";
	echo "$s<br>";
	$s = str_repeat("-", strlen($s));
	echo "$s<br>";
	$output = sscanf($string,$format);
	echo "Result : ";
	print_value( $output );
	echo "$s<br><br>";
}


function run_sscanf_test_cases($filename="scan_cases")
{

	echo "<h3><em><br>Running Test Cases from $filename<br></em></h3>"; 
	$arr = file($filename);
	for ($i=0;$i < count($arr);$i++) {
		$line_arr = explode("|",$arr[$i]);
		
		$format = $line_arr[0];
		$string = $line_arr[1];
		if (count($arr) > 2) {
			$comment = $line_arr[2];
		} else {
			$comment = "";
		}
		if ( empty($format) || empty($string) ) {
			continue;
		}
		print("<h4>** Case : $comment ******************************</h4>");
		do_sscanf($string,$format);
	}
}

function simple_tests() {
	echo "Testing sscanf with standard ANSI syntax (values returned by
reference)-<br>";
	$decimal = -1;
	$string  = "";
	$hex	 = 0;
	$float	 = 0.0;	
	$octal	 = 0.0;
	$int	 = -1;
				
	echo "<h3><em><br>Simple Test<br></em></h3>"; 
	echo "sscanf('10','%d',&\$decimal) <br>";
	echo "<br>BEFORE : <br> decimal = $decimal.";
	$foo = sscanf("10","%d",&$decimal);
	echo "<br>AFTER  : <br> decimal = $decimal <br>";


	echo "<h3><em><br>Simple Test 2<br></em></h3>"; 
	echo "sscanf(\"ghost 0xface\",\"%s %x\",&\$string, &\$int)<br>";
	echo "<br>BEFORE : <br> string = $string, int = $int<br>";
	$foo = sscanf("ghost 0xface","%s %x",&$string, &$int);
	echo "<br>AFTER  : <br> string = $string, int = $int<br>";
	echo " sscan reports : ";
	print_value( $foo,"");
	echo " conversions <br>";

	echo "<h3><em><br>Multiple specifiers<br></em></h3>"; 
	echo "sscanf(\"jabberwocky 1024 0xFF 1.024 644 10\",
			\"%s %d  %x %f %o %i\",
			&\$string,&\$decimal,&\$hex,&\$float,&\$octal,&\$int);<br>";
	echo "<br>BEFORE : <br>";
	echo "Decimal = $decimal, String = $string, Hex = $hex<br>";
	echo "Octal = $octal , Float = $float, Int = $int<br>"; 
	$foo = sscanf(	"jabberwocky 1024 0xFF 1.024 644 10",
			"%s %d  %x %f %o %i",
			&$string,&$decimal,&$hex,&$float,&$octal,&$int);
	echo "<br>AFTER :<br>";
	echo "decimal = $decimal, string = $string, hex = $hex<br>";
	echo "octal = $octal , float = $float, int = $int<br>"; 
				
	echo " sscan reports : ";
	print_value( $foo,"");
	echo " conversions <br>";
	echo "----------------------------------------<br>";
}



?>
<html>
	<head>
		<title>Test of sscanf()</title>
	</head>
	<body>
		<strong><h1>Testing sscanf() support in PHP</h1></strong><br>	
		<?php
			if (!function_exists('sscanf')) {
				echo "<strong>I'm sorry but sscanf() does not exist !i</strong><br>";
			} else {
				simple_tests();
				run_sscanf_test_cases(); 
			}
		?>
	</body>	
</html>