ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Utf8Test.php
Go to the documentation of this file.
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
19 
29 require_once 'include/Unicode/UtfNormal.php';
30 mb_internal_encoding( "utf-8" );
31 
32 #$verbose = true;
33 if( php_sapi_name() != 'cli' ) {
34  die( "Run me from the command line please.\n" );
35 }
36 
37 $in = fopen( "UTF-8-test.txt", "rt" );
38 if( !$in ) {
39  print "Couldn't open UTF-8-test.txt -- can't run tests.\n";
40  print "If necessary, manually download this file. It can be obtained at\n";
41  print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
42  exit(-1);
43 }
44 
46 while( false !== ( $line = fgets( $in ) ) ) {
47  $matches = array();
48  if( preg_match( '/^(Here come the tests:\s*)\|$/', $line, $matches ) ) {
49  $columns = strpos( $line, '|' );
50  break;
51  }
52 }
53 
54 if( !$columns ) {
55  print "Something seems to be wrong; couldn't extract line length.\n";
56  print "Check that UTF-8-test.txt was downloaded correctly from\n";
57  print "http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt";
58  exit(-1);
59 }
60 
61 # print "$columns\n";
62 
64  # These two lines actually seem to be corrupt
65  '2.1.1', '2.2.1' );
66 
68  # Tests that should mark invalid characters due to using long
69  # sequences beyond what is now considered legal.
70  '2.1.5', '2.1.6', '2.2.4', '2.2.5', '2.2.6', '2.3.5',
71 
72  # Literal 0xffff, which is illegal
73  '2.2.3' );
74 
76  # These tests span multiple lines
77  '3.1.9', '3.2.1', '3.2.2', '3.2.3', '3.2.4', '3.2.5',
78  '3.4' );
79 
80 # These tests are not in proper subsections
81 $sectionTests = array( '3.4' );
82 
83 $section = NULL;
84 $test = '';
85 $failed = 0;
87 $total = 0;
88 while( false !== ( $line = fgets( $in ) ) ) {
89  $matches = array();
90  if( preg_match( '/^(\d+)\s+(.*?)\s*\|/', $line, $matches ) ) {
91  $section = $matches[1];
92  print $line;
93  continue;
94  }
95  if( preg_match( '/^(\d+\.\d+\.\d+)\s*/', $line, $matches ) ) {
96  $test = $matches[1];
97 
98  if( in_array( $test, $ignore ) ) {
99  continue;
100  }
101  if( in_array( $test, $longTests ) ) {
102  $line = fgets( $in );
103  for( $line = fgets( $in ); !preg_match( '/^\s+\|/', $line ); $line = fgets( $in ) ) {
104  testLine( $test, $line, $total, $success, $failed );
105  }
106  } else {
107  testLine( $test, $line, $total, $success, $failed );
108  }
109  }
110 }
111 
112 if( $failed ) {
113  echo "\nFailed $failed tests.\n";
114  echo "UTF-8 DECODER TEST FAILED\n";
115  exit (-1);
116 }
117 
118 echo "UTF-8 DECODER TEST SUCCESS!\n";
119 exit (0);
120 
121 
122 function testLine( $test, $line, &$total, &$success, &$failed ) {
123  $stripped = $line;
124  UtfNormal::quickisNFCVerify( $stripped );
125 
126  $same = ( $line == $stripped );
127  $len = mb_strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
128  if( $len == 0 ) {
129  $len = strlen( substr( $stripped, 0, strpos( $stripped, '|' ) ) );
130  }
131 
132  global $columns;
133  $ok = $same ^ ($test >= 3 );
134 
135  global $exceptions;
136  $ok ^= in_array( $test, $exceptions );
137 
138  $ok &= ($columns == $len);
139 
140  $total++;
141  if( $ok ) {
142  $success++;
143  } else {
144  $failed++;
145  }
146  global $verbose;
147  if( $verbose || !$ok ) {
148  print str_replace( "\n", "$len\n", $stripped );
149  }
150 }
151 
152 ?>
File written to
while(false !==( $line=fgets( $in))) if(! $columns) $ignore
Definition: Utf8Test.php:63
Hide grid lines
Definition: 21pdf.php:55
$sectionTests
Definition: Utf8Test.php:81
$verbose
$total
Definition: Utf8Test.php:87
$section
Definition: Utf8Test.php:83
$success
Definition: Utf8Test.php:86
testLine( $test, $line, &$total, &$success, &$failed)
Definition: Utf8Test.php:122
$failed
Definition: Utf8Test.php:85
Create styles array
The data for the language used.
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
if(! $in) $columns
Definition: Utf8Test.php:45
$exceptions
Definition: Utf8Test.php:67
$longTests
Definition: Utf8Test.php:75
$test
Definition: Utf8Test.php:84