summaryrefslogtreecommitdiff
path: root/test/testtable.js
blob: 4fc664ff2a7b1f85297b5e99199f52cbf59cb9e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* configuration */
/* TODO: UNTESTED count can't be shown because it's not tracked explicitly */
headerResults = [ "PASS", "NEW", "FAIL", "XFAIL", "CRASHED" ];
logResults    = [ "PASS", "NEW", "FAIL", "XFAIL", "CRASH!" ];
resultToImgs = {
    "PASS"     : [],
    "NEW"      : [ "output" ],
    "FAIL"     : [ "output", "difference", "reference" ],
    "XFAIL"    : [],
    "UNTESTED" : [],
    "CRASHED"  : []
};

resultToString = {
    "PASS"     : "",
    "NEW"      : "",
    "FAIL"     : "",
    "XFAIL"    : "",
    "UNTESTED" : "",
    "CRASHED"  : "CRASHED!"
};

resultField = "result";
rowFields = [ "test", "offset", "scale", "similar" ];
colFields = [ "target", "format" ];
allFields = [ resultField ].concat (rowFields, colFields);


/* globals: */
function resetGlobals () {
    dragElement = undefined;
    table = document.getElementById ("testTable");
    while (table.rows.length)
	table.deleteRow (0);
    colsArray = [ "HrowHeader" ];
    colsMap = undefined;
    headerId = "HcolHeader";

    fTrue = function (x) { return true; };

    empty = new Row ();
    header = new Row ();
    header[colsArray[0]].toString = function () { return ""; };

    untested = new Test ();
    untested[resultField] = "UNTESTED";
}


/* utility functions */
function isKey (key) { return key[key.length-1] == ':'; }
function normalizeKey (key) { return key.toLowerCase ().replace (/[^a-z0-9]/, ""); }
function isVisible (x) { return x.style.display != "none"; }

function link (html, url) { return "<a href='" + url + "'>" + html + "</a>"; }
function image (url) { return "<img src='" + url + "'>"; }
function span (html, id, cls) { return "<span id='" + id + "' class='" + cls + "' onmousedown='startDrag (event)' onmouseup='mouseUp (event)'>" + html + "</span>"; }

function fieldsToHTML (bColumns, values) {
    var fields = bColumns ? colFields : rowFields;
    var prefix = bColumns ? "c" : "r";
    var tmpRE = arrayApply (function (x) { return "[^/]*"; }, fields);
    var r = Array ();
    for (var i = 0; i < fields.length; i++)
	if (fields[i] == "test") {
	    r.push (link (values[fields[i]], "output/" + values[fields[i]] + ".log"));
	} else {
	    tmpRE[i] = values[fields[i]];
	    r.push (span (values[fields[i]], prefix + "/" + tmpRE.join ("/") + "/", fields[i]));
	    tmpRE[i] = "[^/]*";
	}
    return r.join ("/");
}

function inArray (value, array) {
    for (var i = 0; i < array.length; i++)
	if (value == array[i])
	    return true;
    return false;
}

function arrayApply (fun, array) {
    var r = new Array ();
    for (var i = 0; i < array.length; i++)
	r.push (fun(array[i]));
    return r;
}

function arrayPred (pred, array) {
    var r = new Array ();
    for (var i = 0; i < array.length; i++)
	if (pred (array[i]))
	    r.push (array[i]);
    return r;
}

function arrayMap (map, array) { return arrayApply (function (x) { return map[x]; }, array); }

function binSearch (rows, newId){
    var min = 0;
    var max = rows.length;

    while (max - min > 1) {
	var mid = (max + min) >> 1;
        if (rows[mid].id > newId)
	    max = mid;
        else
	    min = mid;
    }

    if (max == min)
	return max;
    else
	return rows[min].id > newId ? min : max;
}

/* dynamic table utils */
function updateCurrent () {
    for (var i = 0; i < table.rows.length; i++) {
	var row = table.rows[i];
	if (isVisible (row)) {
	    /* j starts from 1 because we want to ignore _rowHeader */
	    for (var j = 1; j < row.cells.length; j++)
		if (row.id[0] == "H")
		    for (var k = 0; k < headerResults.length; k++)
			header[row.cells[j].id].current[headerResults[k]] = 0;
		else if (isVisible (row.cells[j]))
		    header[row.cells[j].id].current[row.cells[j].className]++;
	}
    }

    updateHeader ();
}

function setVisible (array, subsetPred, visibilityPred, visibleFlag) {
    var modified = false, somethingVisible = false;
    for (var i = 0; i < array.length; i++)
	if (array[i].id[0] != "H") {
	    if (subsetPred (array[i])) {
		var wanted = visibilityPred (array[i]);
		if (isVisible (array[i]) != wanted) {
		    modified = true;
		    array[i].style.display = wanted ? visibleFlag : "none";
		}
	    }
	    somethingVisible = somethingVisible || isVisible (array[i]);
	}
    return modified && somethingVisible;
}

function setVisibleOnly (array, pred, visibleFlag) {
    return setVisible (array, fTrue, pred, visibleFlag);
}

function flipVisible (array, subsetPred, visibleFlag) {
    return setVisible (array, subsetPred, function (x) { return !isVisible (x); }, visibleFlag);
}


/* event handling */
function ignoreEvent (event) {
    if (event.preventDefault)
        event.preventDefault();
    else
        event.returnValue= false;
    return false;
}

function mouseUp (event) {
    var visFun;
    if (event.button == 0)
	visFun = setVisibleOnly;
    else if (event.button == 2)
	visFun = flipVisible;
    else
	return false;

    var structureFun;
    if (event.target.id[0] == "r") /* rows */
	structureFun = function (f, p) { return f (table.rows, p, "table-row"); };
    else if (event.target.id[0] == "c") /* cols */
	structureFun = function (f, p) { return inArray (true, arrayApply (function (row) { return f (row.cells, p, "table-cell") }, table.rows)) };
    else
	return false;

    var pred;
    if (event.target.id[1] == "/") { /* regexp */
	var re = new RegExp (event.target.id);
	pred = function (x) { return re.test (x.id); };
    } else if (event.target.id[1] == "#") { /* counters */
	var s = event.target.id.substr (2).split ("/");
	pred = function (row) { return row.cells[s[0]].className == s[1]; }
    } else
	return false;

    if (!structureFun (visFun, pred))
	if (!structureFun (flipVisible, fTrue))
	    structureFun (flipVisible, fTrue);

    updateCurrent ();

    return false;
}

function noDrag (event) {
    dragElement = undefined;
    return false;
}

function startDrag (event) {
    if (event.button == 0)
	dragElement = event.target;
    else
	dragElement = undefined;
    return false;
}

function endDrag (event) {
    if (!dragElement)
	return false;

    if (event.currentTarget.id == colsArray[0] &&
	inArray (dragElement.className, colFields)) {
	rowFields.push (dragElement.className);
	colFields = arrayPred (function (x) { return x != dragElement.className; }, colFields);
    } else if (event.currentTarget.id == headerId &&
	       inArray (dragElement.className, rowFields)) {
	colFields.push (dragElement.className);
	rowFields = arrayPred (function (x) { return x != dragElement.className; }, rowFields);
    } else
	return true;

    reloadAll ();
    return false;
}


/* table content */
function Row (id, t) {
    this[colsArray[0]] = new RowHeader (id, t);

    this.get = function (c) { return this[c] != undefined ? this[c] : untested; }
    this.getHTML = function (c) { return this.get(c).toString (); };
    this.setStyle = function (c, element) { return this.get(c).setStyle (element); };
}

function ColumnHeader (id, values) {
    this.id = id;
    this.values = values;
    this.total = new Object ();
    this.current = new Object ();

    for (var i = 0; i < headerResults.length; i++) {
	this.total[headerResults[i]] = 0;
	this.current[headerResults[i]] = 0;
    }

    this.toString = function () {
	var counts = new Array ();
	for (var i = 0; i < headerResults.length; i++) {
	    var hr = headerResults[i];
	    var s = span (this.current[hr], "r#" + colsMap[this.id] + "/" + hr, hr);
	    if (this.current[hr] != this.total[hr])
		s += span ("[" + this.total[hr] + "]", "r#" + colsMap[this.id] + "/" + hr, hr);
	    counts.push (s);
	}

	return fieldsToHTML (true, this.values) + "<br>" + counts.join ("/");
    }

    this.setStyle = function (element) { };
}

function RowHeader (id, values) {
    this.id = id;
    this.values = values;
    this.toString = function () { return fieldsToHTML (false, this.values); }
    this.setStyle = function (element) { element.onmouseup = endDrag; };
}

function Test () {
    this.rowId = function () { return "r/" + arrayMap (this, rowFields).join("/") + "/"; };
    this.colId = function () { return "c/" + arrayMap (this, colFields).join("/") + "/"; };
    this.isComplete = function () { return !inArray (undefined, arrayMap (this, allFields)); }
    this.toString = function () {
	var images = arrayMap (this, resultToImgs[this[resultField]]);
	images = arrayPred (function (x) { return x != undefined; }, images);
	images = arrayApply (function (x) { return link (image (x), x); }, images);
	images.push (resultToString[this[resultField]]);
	return images.join (" ");
    };

    this.setStyle = function (element) { element.className = this[resultField]; };

    this.addData = function (array) {
	for (var i = 0; i < array.length - 1; i += 2)
	    if (isKey (array[i]))
		this[normalizeKey (array[i])] = array[i+1];
    };
}


/* table creation */
function insertCell (domRow, nid, tests) {
    var domCell = domRow.insertCell (nid);
    domCell.id = colsArray[nid];
    domCell.innerHTML = tests.getHTML (colsArray[nid]);
    tests.setStyle (colsArray[nid], domCell);
}

function updateRow (row, tests) {
    var domRow = document.getElementById (row);
    if (!domRow) {
	domRow = table.insertRow (binSearch (table.rows, row));
	domRow.id = row;
    }

    for (var i = 0; i < colsArray.length; i++)
	if (i >= domRow.cells.length || domRow.cells[i].id != colsArray[i])
	    insertCell (domRow, i, tests);
}

function updateHeader () {
    var visibility;
    var domRow = document.getElementById (headerId);
    if (domRow) {
	visibility = new Object ();
	for (var i = 0; i < domRow.cells.length; i++)
	    visibility[domRow.cells[i].id] = domRow.cells[i].style.display;
	table.deleteRow (domRow.rowIndex);
    }

    updateRow (headerId, header);
    table.rows[0].onmouseup = endDrag;

    if (visibility)
	for (var i = 0; i < colsArray.length; i++)
	    if (visibility[colsArray[i]])
		table.rows[0].cells[colsMap[colsArray[i]]].style.display = visibility[colsArray[i]];
}

function updateTable () {
    colsArray.sort ();

    colsMap = new Object ();
    for (var i = 0; i < colsArray.length; i++)
	colsMap[colsArray[i]] = i;

    updateHeader ();
    for (var i = 0; i < table.rows.length; i++)
	updateRow (table.rows[i].id, empty);
}


/* log file parsing */
function parseTest (testData) {
    var colsChanged = false;
    var rows = new Array ();
    var data = new Object ();
    var t = new Test ();
    var lines = testData.replace (/\r/g, "").split ("\n");
    for (var i = 0; i < lines.length; i++) {
	t.addData (lines[i].split (" "));
	if (t.isComplete ()) {
	    var c = t.colId ();
	    if (header[c] == undefined) {
		colsArray.push (c);
		header[c] = new ColumnHeader (c, t);
		colsChanged = true;
	    }

	    var r = t.rowId ();
	    if (!data[r]) {
		rows.push (r);
		data[r] = new Row (r, t);
	    }

	    data[r][c] = t;
	    header[c].total[t[resultField]]++;
	    header[c].current[t[resultField]]++;
	    t = new Test ();
	}
    }

    if (colsChanged)
	updateTable ();
    else
	updateHeader ();

    for (var i = 0; i < rows.length; i++)
	updateRow (rows[i], data[rows[i]]);
}

function parseFile (fileName, parser) {
    var req = new XMLHttpRequest ();
    req.onreadystatechange = function () {
	if (req.readyState == 4)
	    parser (req.responseText);
    }

    try {
	req.open ("GET", fileName);
	req.send (null);
    } catch (e) {}
} 

function parseTestList (listData) {
    var summaryRE = /\d+ Passed, \d+ Failed \x5b\d+ crashed, \d+ expected\x5d, \d+ Skipped/;
    var lines = listData.replace (/\r/g, "").split ("\n");
    for (var i = 0; i < lines.length; i++) {
	if (summaryRE.test (lines[i]))
	    return;
	
	var words = lines[i].split (" ");
	if (words.length >= 2 &&
	    words[0][words[0].length-1] == ":" &&
	    inArray (words[1], logResults))
	    parseFile ("output/" + words[0].substr (0, words[0].length-1) + ".log", parseTest);
    }
}

function reloadAll() {
    resetGlobals ();

    parseFile ("cairo-test-suite.log", parseTestList);
} 

window.onload = reloadAll;