<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
  <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
	 <title>JS normalizer tester</title> 
	 <style type="text/css">
body { background: white; color: black; font-family: sans-serif; }
.smallprint { font-size: 80%; }
</style> 
<script type="text/javascript" src="/rishida/code/normalization/js/n11n.js" xml:space="preserve">//</script>
<script type="text/javascript" src="/rishida/code/normalization/js/n11ndata.js" xml:space="preserve">//</script>
  </head> 
  <body> 
	 <h1>JS normalizer tester</h1> 
<script type="text/javascript">
tests = new Array();

<?php

function cp2utf8 ($hexcp) {
	$outputString = '';
	$numbers = explode(' ', $hexcp); //print 'count numbers '.count($numbers);
	for ($i=0; $i<count($numbers); $i++) {
	$n = hexdec($numbers[$i]);
	if ($n <= 0x7F) {
		$outputString .= chr($n);
		}
	else if ($n <= 0x7FF) {
		$outputString .= chr(0xC0 | (($n>>6) & 0x1F)).chr(0x80 | ($n & 0x3F));
		}
	else if ($n <= 0xFFFF) {
		$outputString .= chr(0xE0 | (($n>>12) & 0x0F)).chr(0x80 | (($n>>6) & 0x3F)).chr(0x80 | ($n & 0x3F));
		}
	else if ($n <= 0x10FFFF) {
		$outputString .= chr(0xF0 | (($n>>18) & 0x07)).chr(0x80 | (($n>>12) & 0x3F)).chr(0x80 | (($n>>6) & 0x3F)).chr(0x80 | ($n & 0x3F));
		}
	else {
		$outputString .= 'Error=> ' + $n +'!';
		}
	}
	return $outputString;
	}


$testsuite = fopen('NormalizationTest.txt','r');

$counter = 0;
while(!feof($testsuite)) {
	$line = fgets($testsuite);

	if (substr($line,0,1) != '#' && substr($line,0,1) != '@' && strlen($line) > 6) {
		$items = explode(";", $line);
		//$testarray[$counter] = array(cp2utf8($items[0]), cp2utf8($items[1]), cp2utf8($items[2]), cp2utf8($items[3]), $counter );
		for ($i=0;$i<count($items);$i++) {
			$items[$i] = cp2utf8($items[$i]);
			if ($items[$i] == '\\') { $items[$i].='\\'; }
			if ($items[$i] == '\'') { $items[$i]='\\'.$items[$i]; }
			//$items[$i] = '\u'.$items[$i];
			//$items[$i] = str_replace(' ','\u',$items[$i]);
			}
		echo "tests[$counter] = [ '".$items[0]."','".$items[1]."','".$items[2]."','".$items[3]."','".$items[4]."' ];\n";
		$counter++;
		}
	}

?>

function reportnumber () {
	msg = (tests.length*4)+' tests in total.';
	textnode = document.createTextNode(msg);
	para = document.createElement('p');
	para.appendChild(textnode);
	document.getElementById('output').appendChild(para);
	}

function testme () {
	var nfdcounter = 0;
	var nfccounter = 0;
	var noDiffCounter = 0;
	var normOutput; 
	var msg, textnode, para;

	for (var i=0; i<tests.length-1; i++) {
		nfdPassed = true;
		normOutput = nfd(tests[i][0]);
		if (tests[i][2] != normOutput) {
			msg = 'NFD test '+i+' failed. ['+tests[i][0]+'>'+normOutput+'!'+tests[i][2]+'] (1st test)';
			textnode = document.createTextNode(msg);
			para = document.createElement('p');
			para.appendChild(textnode);
			document.getElementById('output').appendChild(para);
			nfdcounter++;
			nfdPassed = false;
			}
	
		normOutput = nfd(tests[i][1]);
		if (tests[i][2] != normOutput) {
			msg = 'NFD test '+i+' failed. ['+tests[i][1]+'>'+normOutput+'!'+tests[i][2]+'] (2nd test)';
			textnode = document.createTextNode(msg);
			para = document.createElement('p');
			para.appendChild(textnode);
			document.getElementById('output').appendChild(para);
			nfdcounter++;
			nfdPassed = false;
			}
	
		if (nfdPassed) {
			normOutput = nfc(tests[i][0]);
			if (tests[i][1] != normOutput) {
				msg = 'NFC test '+i+' failed. ['+tests[i][0]+'>'+normOutput+'!'+tests[i][1]+'] (1st test)';
				textnode = document.createTextNode(msg);
				para = document.createElement('p');
				para.appendChild(textnode);
				document.getElementById('output').appendChild(para);
				nfccounter++;
				}
			}
	
		if (nfdPassed) {
			normOutput = nfc(tests[i][2]);
			if (tests[i][1] != normOutput) {
				msg = 'NFC test '+i+' failed. ['+tests[i][2]+'>'+normOutput+'!'+tests[i][1]+'] (2nd test)';
				textnode = document.createTextNode(msg);
				para = document.createElement('p');
				para.appendChild(textnode);
				document.getElementById('output').appendChild(para);
				nfccounter++;
				}
			}
		}
		
	msg = nfdcounter+' NFD tests failed.';
	textnode = document.createTextNode(msg);
	para = document.createElement('h3');
	para.appendChild(textnode);
	document.getElementById('output').appendChild(para);
					   
	msg = nfccounter+' NFC tests failed.';
	textnode = document.createTextNode(msg);
	para = document.createElement('h3');
	para.appendChild(textnode);
	document.getElementById('output').appendChild(para);
					   
					   
	//document.write('<h3>'+counter+' tests failed.</h3>');
	}
</script>

<p><input type="button" onclick="reportnumber(); testme(); return false;">Start test</input></p>

<div id='output'></div>


</body>
</html>