--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/delinquent_files/test_regex.php Mon Jan 23 21:37:02 2023 +0100 @@ -0,0 +1,177 @@ +#!/usr/bin/php +<?php +## By Peter 'grin' Gervai, 2022 +## GPLv3+ and CC-By-Sa-4.0 +## +## $Id$ +## +## This file is the testing "environment" for the regex matching. +## Tries to test unlink (removal) and replacement rules. +## +## The input files are in _t_*.in, and the unlink and replace +## expected results are in _t_*.unlink and _t_*.replace. +## The tests are listed below in the array, with the original +## image and its expected good matching pattern, which is also +## verified. The replacement image also set there. +## + +$DEBUG=0; + +require_once( './matcher.inc' ); +require_once( './debug.inc' ); + +$d = new Debug; +#$d->set_level(9); # trace + +$matcher = new Matcher($d); + + +# files are in +# input: _t_{id}.in +# test data: _t_{id}.data +# good replacement: _t_{id}.replace +# good unlink: _t_{id}.unlink + +$dir_tests = './test'; +$testnames = collect_test_names($dir_tests); + +$data_keys = array( 'img', 'pattern', 'repl_img' ); + +foreach ($testnames as $t) { + $tests[$t] = get_test_data( $dir_tests, $t, $data_keys ); +} + +$test_res = array( 'ok' => 0, 'bad' => 0 ); + +# run +foreach ($tests as $key => $t) { + #print( "Running test '$key'..." ); + #print("IN=" . $t["in"] . "\n"); + #print("Im=" . $t["img"] . "\n"); + #print("RE=" . $t["unlink"] . "\n"); + + $img = $t["img"]; + $pattern = test_prepare_pattern( $img ); + if( $DEBUG ) {print(" Pat=$pattern\n");} + $res = test_result( $pattern, $t["pattern"], "pattern", $img, $key ); + if( $res == 1 ) { + $test_res['bad']++; + } else { + $test_res['ok']++; + } + + $text = read_text_from_file( "${dir_tests}/_t_${key}.in" ); + $t_unlink = read_text_from_file( "${dir_tests}/_t_${key}.unlink" ); + $t_replace = read_text_from_file( "${dir_tests}/_t_${key}.replace" ); + + $res = test_regex_unlink( $text, $pattern ); + $res = test_result( $res, $t_unlink, "unlink", $img, $key ); + if( $res == 1 ) { + $test_res['bad']++; + } else { + $test_res['ok']++; + } + + $res = test_regex_replace( $text, $pattern, $t["repl_img"] ); + $res = test_result( $res, $t_replace, "replace", $img, $key ); + if( $res == 1 ) { + $test_res['bad']++; + } else { + $test_res['ok']++; + } +} + +print( "\nResults:\n" ); +print( "OK : " . $test_res['ok'] . "\nBAD: " . $test_res['bad'] ."\n" ); + + +## collect the name of the tests (verbose code :)) +function collect_test_names($dir) { + $dlist = scandir($dir); + + foreach ($dlist as $key => $val) { + if( !is_dir( $dir . '/' . $val ) && preg_match( '/^_t_(.+)\.data$/', $val, $matches ) ) { + $tests[] = $matches[1]; + } + } + return $tests; +} + + +function get_test_data($dir, $t, $keys) { + $f = fopen( "${dir}/_t_${t}.data", 'r'); + if( $f ) { + while( ($line = fgets($f)) !== false ) { + // process a line + if( preg_match( '/^(\S+)\s*:\s*(.+)$/', $line, $matches ) ) { + # print( "Test $t ${matches[1]} => ${matches[2]}\n" ); + if( !in_array( $matches[1], $keys ) ) { + trigger_error( "Unknown key '$matches[1]' in ${t}.data", E_USER_ERROR ); + } + $test[$matches[1]] = $matches[2]; + } + } + } else { + trigger_error( "test $t data file is missing", E_USER_ERROR ); + } + + foreach ($keys as $k) { + if( !array_key_exists( $k, $test ) ) { + trigger_error( "test $t data is missing $k key", E_USER_ERROR ); + } + } + + return $test; +} + + +function test_result( $result, $expected, $name, $img, $id ) { + global $dir_tests; + if( $result <> $expected ) { + #print( "${test} BAD $name:$id ($img)! result=\n$result\n\nexpect=\n$expected\n\n" ); + if( $name == 'pattern' ) { + print( "${id} BAD $name ($img)! expected '$expected', result '$result', fix in ${dir_tests}/_t_${id}.data, expected is in _bad_${id}.${name}\n" ); + } else { + print( "${id} BAD $name ($img)! diff -u ${dir_tests}/_t_${id}.${name} _bad_${id}.${name}\n" ); + } + write_text_to_file( "_bad_${id}.${name}", $result ); + return 1; + } else { + print( "${id} OK $name ($img)!\n" ); + } + return 0; +} + + +function read_text_from_file( $fname ) { + $f = fopen( $fname, 'r') or die( "Cannot read file $fname" ); + $text = fread( $f, 1e8 ); + fclose($f); + return $text; +} + + +function write_text_to_file($fname, $text) { + #print("Recording into $fname text '$text'"); + $f = fopen( $fname, 'w') or die( "Cannot create file $fname" ); + $res = fwrite( $f, $text ); + fclose($f); +} + + +function test_prepare_pattern( $file ) { + global $matcher; + return $matcher->matcher_prepare_pattern( $file ); +} + + +function test_regex_unlink( $text, $pattern ) { + global $matcher; + return $matcher->matcher_do_unlink( $text, $pattern ); +} + + +function test_regex_replace( $text, $pattern, $newimg ) { + global $matcher; + return $matcher->matcher_do_replacement( $text, $pattern, $newimg ); +}