view src/may/test/test.yeti @ 436:563d52c00f06

Print timings
author Chris Cannam
date Tue, 08 Oct 2013 15:37:47 +0100
parents 3820e2a696f8
children 3420e5f61a1b
line wrap: on
line source
module may.test.test;

mat = load may.matrix;

import yeti.lang: FailureException;

var goodCompares = 0;

assert fact description = 
    if fact then
        goodCompares := goodCompares + 1;
        true;
    else
        println "** assertion failed: \(description)";
        false;
    fi;

compareUsing comparator obtained expected =
    if comparator obtained expected then
        goodCompares := goodCompares + 1;
        true;
    else
        println "** expected: \(expected)\n   obtained: \(obtained)";
        false;
    fi;

compare obtained expected = compareUsing (==) obtained expected;

compareMatrices tolerance obtained expected =
   (d = mat.abs (mat.difference obtained expected);
    if mat.all (< tolerance) d then
        goodCompares := goodCompares + 1;
        true;
    else
        println "** value(s) outside tolerance \(tolerance) from expected:";
        count = 40;
        faulty = mat.enumerate (mat.filter (>= tolerance) d);
        for (take count faulty) do f:
            println " * at (\(f.i),\(f.j)) diff of \(f.v) (expected: \(mat.at expected f.i f.j); obtained: \(mat.at obtained f.i f.j))";
        done;
        if length faulty > count then
            println "** (only first \(count) of \(length faulty) shown)";
        fi;
        false;
    fi);

time msg f =
   (start = System#currentTimeMillis();
    result = f ();
    finish = System#currentTimeMillis();
    println "\(msg): \(finish-start)ms";
    result);

select f = fold do r x: if f x then x::r else r fi done [];

failedTests testHash =
    select (!= "")
       (mapHash do name f:
            try
                if f () then "" else
                    println "Test \(name) failed";
                    name;
                fi 
            catch FailureException e:
                println "Test \(name) threw exception: \(e)";
                name;
            yrt;
        done testHash);
        
runTests group testHash =
   (start = System#currentTimeMillis();
    failed = failedTests testHash;
    finish = System#currentTimeMillis();
    good = (length testHash - length failed);
    bad = length failed;
    println "\(group): \(good)/\(good+bad) tests passed in \(finish-start)ms";
    if not empty? failed then
        println "\(group): Failed tests [\(bad)]: \(strJoin ' ' failed)";
    fi;
    bad);

{
    compare, compareUsing, compareMatrices, assert,
    time,
    runTests, 
}