#!/usr/bin/perl
############################################################
# &outputDebug(); # uncomment this to enable debug mode output
# On the web, see http://www.bath.ac.uk/%7Epy8ieh/cgi/development/tests/tables
############################################################
# name: Tables
# author: Ian Hickson
# license: GPL
# description: serious stress tests for table layout
# This test is at:
# http://www.bath.ac.uk/%7Epy8ieh/cgi/development/tests/tables
############################################################

# Initialise System
use lib '../utils';
use strict 'vars';
$output::mediaPath = '../../../media/';
$output::appName = 'Table Stress Tests';
$output::appStyle = 'style/table-test.css';
$output::authorContact = 'http://www.bath.ac.uk/%7Epy8ieh/';
$output::authorName = 'Ian Hickson';
$output::authorEmail = 'py8ieh=website=cgi=development=tests=tables@bath.ac.uk';
require 'inputRoutines.pl'; # get cookies, parameters
require 'outputRoutines.pl'; # so that we can output stuff
############################################################

$param::tables = 1 unless defined($param::tables);
$param::heads = 0 unless defined($param::heads);
$param::foots = 0 unless defined($param::foots);
$param::bodies = 1 unless defined($param::bodies);
$param::rows = 5 unless defined($param::rows);
$param::cols = 5 unless defined($param::cols);
$param::delay = 0 unless defined($param::delay);
$param::junk = 6 unless defined($param::junk);
$param::confusion = 0 unless defined($param::confusion);
$param::tablestyle = 'table-layout: fixed; width: 80%;' unless defined($param::tablestyle);

&output::printHTMLHeader();

print '<p>Version 2.2! Now with Extra <em>Confusion</em> Parameter!</p>';

print "
  <form method=\"GET\" action=\"".$input::relativeURI."\">
     <ol>
        <li>Tables: <input size=4 name=\"tables\" type=\"text\" value=\"".$param::tables."\"> (tables per test)</li>
        <li>Head: <input size=4 name=\"heads\" type=\"checkbox\" value=\"1\" ".($param::heads == 1 ? 'checked="checked"' : '')."> (include 1 &lt;thead&gt; per table)</li>
        <li>Foot: <input size=4 name=\"foots\" type=\"checkbox\" value=\"1\" ".($param::foots == 1 ? 'checked="checked"' : '')."> (include 1 &lt;tfoot&gt; per table)</li>
        <li>Bodies: <input size=4 name=\"bodies\" type=\"text\" value=\"".$param::bodies."\"> (&lt;tbody&gt;s per table)</li>
        <li>Rows: <input size=4 name=\"rows\" type=\"text\" value=\"".$param::rows."\"> (rows per table section)</li>
        <li>Cols: <input size=4 name=\"cols\" type=\"text\" value=\"".$param::cols."\"> (cells per row)</li>
        <li>Delay: <input size=4 name=\"delay\" type=\"text\" value=\"".$param::delay."\"> (seconds delay per cell)
           <ul>
             <li> Include <input size=4 name=\"junk\" type=\"text\" value=\"".$param::junk."\"> KB of compressible junk in between each cell (to force everything on the network to flush caches between each cell)</li>
           </ul>
        </li>
        <li>Confusion Offset: <input size=4 name=\"confusion\" type=\"text\" value=\"".$param::confusion."\"> (normal cells between extra cells)</li>
        <li>Table Style: <input name=\"tablestyle\" type=\"text\" value=\"".$param::tablestyle."\"> (content of table's <code>style</code> attribute)</li>
        <li><input type=\"submit\"></li>
     </ol>
  </form>

  <hr>
";


my ($total) = $param::tables * ($param::rows * $param::cols) * ($param::heads + $param::bodies + $param::foots);

print "
  <p>$param::tables test table".($param::tables == 1 ? '' : 's')." &times; ($param::rows row".($param::rows == 1 ? '' : 's')." &times; $param::cols cell".($param::cols == 1 ? '' : 's').") &times ($param::heads header".($param::heads == 1 ? '' : 's')." + $param::bodies bod".($param::bodies == 1 ? 'y' : 'ies')." + $param::foots footer".($param::foots == 1 ? '' : 's').") = $total cell".($total == 1 ? '' : 's')." total".(($param::delay > 0) ? (" (plus ".($total * $param::delay)." second".(($total * $param::delay) == 1 ? '' : 's')." delay minimum)") : "") .($param::confusion > 0 ? ", excluding any extra confusion cell" : "").".</p>
";

my ($t, $x, $y);
my ($cells) = $param::confusion;
for ($t = 1; $t <= $param::tables; $t++) {
    print "  <table style=\"$param::tablestyle\">\n";
    
    for ($b = 1; $b <= $param::heads; $b++) {
        print "    <thead>\n";
        &docells('H');
        print "    </thead>\n";
    }
    
    for ($b = 1; $b <= $param::foots; $b++) {
        print "    <tfoot>\n";
        &docells('F');
        print "    </tfoot>\n";
    }
    
    for ($b = 1; $b <= $param::bodies; $b++) {
        print "    <tbody>\n";
        &docells('B');
        print "    </tbody>\n";
    }

    print "  </table>\n\n";
}

&output::printHTMLFooter();
############################################################

sub docells {
    my($T) = @_;
    my($JUNK);
    for ($x = 1; $x <= $param::rows; $x++) {
        print "        <tr> ";
        for ($y = 1; $y <= $param::cols; $y++) {
            print "<td> $T $x:$y ";
            if ($param::delay > 0) {
                sleep $param::delay;
                if ($param::junk) {
                    print "<!-- Random Compressible Junk ($param::junk KB): ";
                    $JUNK = 1024*$param::junk;
                    for ($JUNK = 1024*$param::junk; $JUNK > 0; $JUNK--) {
                        print "#";
                    }
                    print " End of Junk -->";
                }
            }
            print " </td>";
            if (($param::confusion > 0) and (--$cells <= 0)) {
                print "<td> extra cell! </td>";
                $cells = $param::confusion;
            }            
        }
        print "</tr>\n";
    }
}
