Added Random and Failing Tests

This commit is contained in:
hkrish 2013-05-14 20:27:37 +02:00
parent 4e2680e605
commit 5bdf3bce91
2 changed files with 1129 additions and 1024 deletions

View file

@ -35,6 +35,11 @@
hari</p>
</footer>
<svg class="hide" version="1.1" id="svgrandom1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="200px" height="200px" viewBox="0 0 200 200" enable-background="new 0 0 200 200" xml:space="preserve">
<path d="M121.938,15.632c-13.717-12.536 -86.488,106.314 -97.28853,96.70168c-10.79979,-9.61253 74.75206,22.03522 62.05339,14.94206c-12.69866,-7.09316 -39.33589,-120.29063 -50.13215,-122.55235c-10.79626,-2.26173 20.61549,120.92844 6.21122,120.91582c-14.40427,-0.01263 95.8736,-97.47028 79.15607,-110.0072z" />
<path d="M112.56135,26.45534c-11.3324,-19.66938 2.46546,15.5079 -15.74899,1.46029c-18.21444,-14.04761 -66.32694,49.96681 -85.68888,45.03851c-19.36194,-4.9283 25.00762,66.13871 9.65758,51.54906c-15.35004,-14.58966 80.2128,-61.80948 76.57166,-81.27628c-3.64115,-19.4668 26.54102,2.89779 15.20863,-16.77158z" />
</svg>
<svg class="hide" version="1.1" id="glyphsys" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="200px" height="200px" viewBox="0 0 200 200" enable-background="new 0 0 200 200" xml:space="preserve">

View file

@ -18,7 +18,7 @@ if (window.performance && window.performance.now) {
}
function runTests() {
var caption, pathA, pathB, group, testdata = [], testQueued = 0, testExecuted = 0;;
var caption, pathA, pathB, group, testdata = [], randomtestdata = [], testQueued = 0, testExecuted = 0;
var container = document.getElementById( 'container' );
@ -33,7 +33,11 @@ function runTests() {
console.log('\n' + testName);
paper.setup(canvas);
var paths = handler();
testIntersections(paths[0], paths[1], caption, testName, testdata);
var success = testIntersections(paths[0], paths[1], caption, testName, testdata);
if( !success ){
window.p1 = paths[0].exportSVG();
window.p2 = paths[1].exportSVG();
}
testExecuted++;
if( testExecuted === testQueued ){
plotData();
@ -42,13 +46,28 @@ function runTests() {
return caption;
}
var caption = document.createElement('h3');
caption.appendChild(document.createTextNode("Randomised tests"));
container.appendChild(caption);
var count = 100, randomData = [];
while( count-- ){
// var caption = document.createElement('h3');
// caption.appendChild(document.createTextNode("Randomised tests (may take a while...)"));
// container.appendChild(caption);
// var canvas = document.createElement('CANVAS');
// container.appendChild( canvas );
// paper.setup( canvas );
// doRandomTests( randomtestdata );
// window.d = randomtestdata;
// container.removeChild( canvas );
}
// runTest('random', function(){
// pathA = getRandomPath(5);
// pathB = getRandomPath(5);
// return [pathA, pathB];
// });
runTest('random 2', function(){
group = paper.project.importSVG( document.getElementById( 'svgrandom1' ) );
pathA = group.children[0];
pathB = group.children[1];
return [pathA, pathB];
});
runTest('Overlapping circles', function(){
pathA = new Path.Circle(new Point(80, 110), 50);
@ -207,9 +226,6 @@ function runTests() {
});
// Do randomised tests
// Plot the run times
function plotData(){
prepareTest( 'Results', container, true );
@ -320,6 +336,7 @@ function runTests() {
txt = new PointText([630, ny+5]);
txt.fillColor = '#000';
txt.content = avgSpeedup + ' times';
view.draw();
var tool = new Tool();
tool.onMouseMove = function( e ){
@ -390,8 +407,8 @@ var pathStyleBoolean = {
// Better if path1 and path2 fit nicely inside a 200x200 pixels rect
function testIntersections( path1, path2, caption, testname, testdata, nomark) {
var i, l, maxCount = 10, count = maxCount, st, t1, t2,
ixsPaper, ixsFatline, success = false;
var i, l, maxCount = 1, count = maxCount, st, t1, t2,
ixsPaper, ixsFatline, success = false, maxdiff = -Infinity;
try{
path1.style = path2.style = pathStyleNormal;
@ -412,11 +429,22 @@ function testIntersections( path1, path2, caption, testname, testdata, nomark) {
t2 = (getTimestamp() - st) / maxCount;
if( !nomark ) console.timeEnd('fatline x ' + maxCount);
var equal = true;
for(i=0, l=ixsFatline.length; i<l && ixsPaper[i]; i++){
equal = equal && ixsPaper[i].point.equals( ixsPaper[i].point );
var found = 0, tol = 0.1;
if( ixsFatline.length === ixsPaper.length ){
for(i=0, l=ixsFatline.length; i<l; i++){
pa = ixsFatline[i].point;
for (j = 0; j < ixsPaper.length; j++) {
if( !ixsPaper[j]._found ){
pb = ixsPaper[j].point;
if( Math.abs( pa.x - pb.x ) < tol && Math.abs( pa.y - pb.y ) < tol ){
++found;
ixsPaper[j]._found = true;
}
success = ( ixsPaper.length === ixsFatline.length ) && equal;
}
}
}
}
success = ixsPaper.length === found;
if( !nomark ){
markIntersections( ixsPaper, '#00f', 'paperjs' );
@ -437,7 +465,79 @@ function testIntersections( path1, path2, caption, testname, testdata, nomark) {
fatTime: t2,
success: success
});
console.log( found );
}
return success;
}
function doRandomTests( testdata ){
var p1 = new Path(), p2 = new Path(), ixspaper, ixsfat;
var seg = 5, maxseg = 20, maxiter = 10;
var i, j, halfseg = (maxseg / 2) | 0;
var p, hi, ho, st, t1, t2, success;
while( seg <= maxseg ){
for (i = 0; i < maxiter; i++) {
p1.removeSegments();
p2.removeSegments();
for (j = 0; j < seg; j++) {
p = new Point.random().multiply( [100, 100] );
v = new Point.random().multiply( [20, 20] );
p1.add( new Segment( p, v, v.multiply(-1) ) );
p1.closed = true;
p = new Point.random().multiply( [100, 100] );
v = new Point.random().multiply( [20, 20] );
p2.add( new Segment( p, v, v.multiply(-1) ) );
p2.closed = true;
}
st = getTimestamp();
ixspaper = p1.getIntersections( p2 );
t1 = (getTimestamp() - st);
st = getTimestamp();
ixsfat = getIntersections2( p1, p2 );
t2 = (getTimestamp() - st);
// Check against paperjs output
var found = 0, tol = 1;
if( ixsfat.length === ixspaper.length ){
for(i=0, l=ixsfat.length; i<l; i++){
pa = ixsfat[i].point;
for (j = 0; j < ixspaper.length; j++) {
if( !ixspaper[j]._found ){
pb = ixspaper[j].point;
if( Math.abs( pa.x - pb.x ) < tol && Math.abs( pa.y - pb.y ) < tol ){
++found;
ixspaper[j]._found = true;
}
}
}
}
}
success = ixspaper.length === found;
testdata.push({
curves: seg,
ixsfat: ixsfat.length,
ixspaper: ixspaper.length,
ratio: ixsfat.length / (seg),
paperTime: t1,
fatTime: t2,
speedup: t1 / t2,
success: success
});
}
++seg;
if( seg === halfseg ) maxiter = (maxiter / 2) | 0;
}
}
function getRandomPath(seg){
seg = seg || 3;
var p = new Path(), pnt, hi, ho, v;
for (j = 0; j < seg; j++) {
pnt = new Point.random().multiply( [130, 130] );
v = new Point.random().multiply( [20, 20] );
p.add( new Segment( pnt, v, v.multiply(-1) ) );
p.closed = true;
}
return p;
}
function markIntersections( ixs, c, txt ){