Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* |
michael@0 | 2 | Unit testing library for the OpenGL ES 2.0 HTML Canvas context |
michael@0 | 3 | |
michael@0 | 4 | Copyright (C) 2011 Ilmari Heikkinen <ilmari.heikkinen@gmail.com> |
michael@0 | 5 | |
michael@0 | 6 | Permission is hereby granted, free of charge, to any person |
michael@0 | 7 | obtaining a copy of this software and associated documentation |
michael@0 | 8 | files (the "Software"), to deal in the Software without |
michael@0 | 9 | restriction, including without limitation the rights to use, |
michael@0 | 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell |
michael@0 | 11 | copies of the Software, and to permit persons to whom the |
michael@0 | 12 | Software is furnished to do so, subject to the following |
michael@0 | 13 | conditions: |
michael@0 | 14 | |
michael@0 | 15 | The above copyright notice and this permission notice shall be |
michael@0 | 16 | included in all copies or substantial portions of the Software. |
michael@0 | 17 | |
michael@0 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
michael@0 | 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
michael@0 | 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
michael@0 | 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
michael@0 | 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
michael@0 | 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
michael@0 | 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
michael@0 | 25 | OTHER DEALINGS IN THE SOFTWARE. |
michael@0 | 26 | */ |
michael@0 | 27 | Tests = { |
michael@0 | 28 | autorun : true, |
michael@0 | 29 | message : null, |
michael@0 | 30 | delay : 0, |
michael@0 | 31 | |
michael@0 | 32 | startUnit : function(){ return []; }, |
michael@0 | 33 | setup : function() { return arguments; }, |
michael@0 | 34 | teardown : function() {}, |
michael@0 | 35 | endUnit : function() {} |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | var __testSuccess__ = true; |
michael@0 | 39 | var __testFailCount__ = 0; |
michael@0 | 40 | var __testLog__; |
michael@0 | 41 | var __backlog__ = []; |
michael@0 | 42 | |
michael@0 | 43 | Object.toSource = function(a, seen){ |
michael@0 | 44 | if (a == null) return "null"; |
michael@0 | 45 | if (typeof a == 'boolean') return a ? "true" : "false"; |
michael@0 | 46 | if (typeof a == 'string') return '"' + a.replace(/"/g, '\\"') + '"'; |
michael@0 | 47 | if (a instanceof HTMLElement) return a.toString(); |
michael@0 | 48 | if (a.width && a.height && a.data) return "[ImageData]"; |
michael@0 | 49 | if (a instanceof Array) { |
michael@0 | 50 | if (!seen) seen = []; |
michael@0 | 51 | var idx = seen.indexOf(a); |
michael@0 | 52 | if (idx != -1) return '#'+(idx+1)+'#'; |
michael@0 | 53 | seen.unshift(a); |
michael@0 | 54 | var srcs = a.map(function(o){ return Object.toSource(o,seen) }); |
michael@0 | 55 | var prefix = ''; |
michael@0 | 56 | idx = seen.indexOf(a); |
michael@0 | 57 | if (idx != -1) prefix = '#'+(idx+1)+'='; |
michael@0 | 58 | return prefix + '[' + srcs.join(", ") + ']'; |
michael@0 | 59 | } |
michael@0 | 60 | if (typeof a == 'object') { |
michael@0 | 61 | if (!seen) seen = []; |
michael@0 | 62 | var idx = seen.indexOf(a); |
michael@0 | 63 | if (idx != -1) return '#'+(idx+1)+'#'; |
michael@0 | 64 | seen.unshift(a); |
michael@0 | 65 | var members = []; |
michael@0 | 66 | var name; |
michael@0 | 67 | try { |
michael@0 | 68 | for (var i in a) { |
michael@0 | 69 | if (i.search(/^[a-zA-Z0-9]+$/) != -1) |
michael@0 | 70 | name = i; |
michael@0 | 71 | else |
michael@0 | 72 | name = '"' + i.replace(/"/g, '\\"') + '"'; |
michael@0 | 73 | var ai; |
michael@0 | 74 | try { ai = a[i]; } |
michael@0 | 75 | catch(e) { ai = 'null /*ERROR_ACCESSING*/'; } |
michael@0 | 76 | var s = name + ':' + Object.toSource(ai, seen); |
michael@0 | 77 | members.push(s); |
michael@0 | 78 | } |
michael@0 | 79 | } catch (e) {} |
michael@0 | 80 | var prefix = ''; |
michael@0 | 81 | idx = seen.indexOf(a); |
michael@0 | 82 | if (idx != -1) prefix = '#'+(idx+1)+'='; |
michael@0 | 83 | return prefix + '{' + members.join(", ") + '}' |
michael@0 | 84 | } |
michael@0 | 85 | if (typeof a == 'function') |
michael@0 | 86 | return '('+a.toString().replace(/\n/g, " ").replace(/\s+/g, " ")+')'; |
michael@0 | 87 | return a.toString(); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | function formatError(e) { |
michael@0 | 91 | if (window.console) console.log(e); |
michael@0 | 92 | var pathSegs = location.href.toString().split("/"); |
michael@0 | 93 | var currentDoc = e.lineNumber != null ? pathSegs[pathSegs.length - 1] : null; |
michael@0 | 94 | var trace = (e.filename || currentDoc) + ":" + e.lineNumber + (e.trace ? "\n"+e.trace : ""); |
michael@0 | 95 | return e.message + "\n" + trace; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function runTests() { |
michael@0 | 99 | var h = document.getElementById('test-status'); |
michael@0 | 100 | if (h == null) { |
michael@0 | 101 | h = document.createElement('h1'); |
michael@0 | 102 | h.id = 'test-status'; |
michael@0 | 103 | document.body.appendChild(h); |
michael@0 | 104 | } |
michael@0 | 105 | h.textContent = ""; |
michael@0 | 106 | var log = document.getElementById('test-log'); |
michael@0 | 107 | if (log == null) { |
michael@0 | 108 | log = document.createElement('div'); |
michael@0 | 109 | log.id = 'test-log'; |
michael@0 | 110 | document.body.appendChild(log); |
michael@0 | 111 | } |
michael@0 | 112 | while (log.childNodes.length > 0) |
michael@0 | 113 | log.removeChild(log.firstChild); |
michael@0 | 114 | |
michael@0 | 115 | var setup_args = []; |
michael@0 | 116 | |
michael@0 | 117 | if (Tests.startUnit != null) { |
michael@0 | 118 | __testLog__ = document.createElement('div'); |
michael@0 | 119 | try { |
michael@0 | 120 | setup_args = Tests.startUnit(); |
michael@0 | 121 | if (__testLog__.childNodes.length > 0) |
michael@0 | 122 | log.appendChild(__testLog__); |
michael@0 | 123 | } catch(e) { |
michael@0 | 124 | testFailed("startUnit", formatError(e)); |
michael@0 | 125 | log.appendChild(__testLog__); |
michael@0 | 126 | printTestStatus(); |
michael@0 | 127 | return; |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | var testsRun = false; |
michael@0 | 132 | var allTestsSuccessful = true; |
michael@0 | 133 | |
michael@0 | 134 | for (var i in Tests) { |
michael@0 | 135 | if (i.substring(0,4) != "test") continue; |
michael@0 | 136 | __testLog__ = document.createElement('div'); |
michael@0 | 137 | __testSuccess__ = true; |
michael@0 | 138 | try { |
michael@0 | 139 | doTestNotify (i); |
michael@0 | 140 | var args = setup_args; |
michael@0 | 141 | if (Tests.setup != null) |
michael@0 | 142 | args = Tests.setup.apply(Tests, setup_args); |
michael@0 | 143 | Tests[i].apply(Tests, args); |
michael@0 | 144 | if (Tests.teardown != null) |
michael@0 | 145 | Tests.teardown.apply(Tests, args); |
michael@0 | 146 | } |
michael@0 | 147 | catch (e) { |
michael@0 | 148 | testFailed(i, e.name, formatError(e)); |
michael@0 | 149 | } |
michael@0 | 150 | if (__testSuccess__ == false) { |
michael@0 | 151 | ++__testFailCount__; |
michael@0 | 152 | } |
michael@0 | 153 | var h = document.createElement('h2'); |
michael@0 | 154 | h.textContent = i; |
michael@0 | 155 | __testLog__.insertBefore(h, __testLog__.firstChild); |
michael@0 | 156 | log.appendChild(__testLog__); |
michael@0 | 157 | allTestsSuccessful = allTestsSuccessful && __testSuccess__ == true; |
michael@0 | 158 | reportTestResultsToHarness(__testSuccess__, i); |
michael@0 | 159 | doTestNotify (i+"--"+(__testSuccess__?"OK":"FAIL")); |
michael@0 | 160 | testsRun = true; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | printTestStatus(testsRun); |
michael@0 | 164 | if (Tests.endUnit != null) { |
michael@0 | 165 | __testLog__ = document.createElement('div'); |
michael@0 | 166 | try { |
michael@0 | 167 | Tests.endUnit.apply(Tests, setup_args); |
michael@0 | 168 | if (__testLog__.childNodes.length > 0) |
michael@0 | 169 | log.appendChild(__testLog__); |
michael@0 | 170 | } catch(e) { |
michael@0 | 171 | testFailed("endUnit", e.name, formatError(e)); |
michael@0 | 172 | log.appendChild(__testLog__); |
michael@0 | 173 | } |
michael@0 | 174 | } |
michael@0 | 175 | notifyFinishedToHarness(allTestsSuccessful, "finished tests"); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | function doTestNotify(name) { |
michael@0 | 179 | //try { |
michael@0 | 180 | // var xhr = new XMLHttpRequest(); |
michael@0 | 181 | // xhr.open("GET", "http://localhost:8888/"+name, true); |
michael@0 | 182 | // xhr.send(null); |
michael@0 | 183 | //} catch(e) {} |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | function testFailed(assertName, name) { |
michael@0 | 187 | var d = document.createElement('div'); |
michael@0 | 188 | var h = document.createElement('h3'); |
michael@0 | 189 | var d1 = document.createElement("span"); |
michael@0 | 190 | h.appendChild(d1); |
michael@0 | 191 | d1.appendChild(document.createTextNode("FAIL: ")); |
michael@0 | 192 | d1.style.color = "red"; |
michael@0 | 193 | h.appendChild(document.createTextNode( |
michael@0 | 194 | name==null ? assertName : name + " (in " + assertName + ")")); |
michael@0 | 195 | d.appendChild(h); |
michael@0 | 196 | var args = [] |
michael@0 | 197 | for (var i=2; i<arguments.length; i++) { |
michael@0 | 198 | var a = arguments[i]; |
michael@0 | 199 | var p = document.createElement('p'); |
michael@0 | 200 | p.style.whiteSpace = 'pre'; |
michael@0 | 201 | p.textContent = (a == null) ? "null" : |
michael@0 | 202 | (typeof a == 'boolean' || typeof a == 'string') ? a : Object.toSource(a); |
michael@0 | 203 | args.push(p.textContent); |
michael@0 | 204 | d.appendChild(p); |
michael@0 | 205 | } |
michael@0 | 206 | __testLog__.appendChild(d); |
michael@0 | 207 | __testSuccess__ = false; |
michael@0 | 208 | doTestNotify([assertName, name].concat(args).join("--")); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | function testPassed(assertName, name) { |
michael@0 | 212 | var d = document.createElement('div'); |
michael@0 | 213 | var h = document.createElement('h3'); |
michael@0 | 214 | var d1 = document.createElement("span"); |
michael@0 | 215 | h.appendChild(d1); |
michael@0 | 216 | d1.appendChild(document.createTextNode("PASS: ")); |
michael@0 | 217 | d1.style.color = "green"; |
michael@0 | 218 | h.appendChild(document.createTextNode( |
michael@0 | 219 | name==null ? assertName : name + " (in " + assertName + ")")); |
michael@0 | 220 | d.appendChild(h); |
michael@0 | 221 | var args = [] |
michael@0 | 222 | for (var i=2; i<arguments.length; i++) { |
michael@0 | 223 | var a = arguments[i]; |
michael@0 | 224 | var p = document.createElement('p'); |
michael@0 | 225 | p.style.whiteSpace = 'pre'; |
michael@0 | 226 | p.textContent = (a == null) ? "null" : |
michael@0 | 227 | (typeof a == 'boolean' || typeof a == 'string') ? a : Object.toSource(a); |
michael@0 | 228 | args.push(p.textContent); |
michael@0 | 229 | d.appendChild(p); |
michael@0 | 230 | } |
michael@0 | 231 | __testLog__.appendChild(d); |
michael@0 | 232 | doTestNotify([assertName, name].concat(args).join("--")); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | function checkTestSuccess() { |
michael@0 | 236 | return __testFailCount__ == 0; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | window.addEventListener('load', function(){ |
michael@0 | 240 | for (var i=0; i<__backlog__.length; i++) |
michael@0 | 241 | log(__backlog__[i]); |
michael@0 | 242 | }, false); |
michael@0 | 243 | |
michael@0 | 244 | function log(msg) { |
michael@0 | 245 | var p = document.createElement('p'); |
michael@0 | 246 | var a = []; |
michael@0 | 247 | for (var i=0; i<arguments.length; i++) |
michael@0 | 248 | a.push(arguments[i]); |
michael@0 | 249 | p.textContent = a.join(", "); |
michael@0 | 250 | if (!__testLog__) { |
michael@0 | 251 | if (document.body) |
michael@0 | 252 | document.body.appendChild(p); |
michael@0 | 253 | else |
michael@0 | 254 | __backlog__.push(msg); |
michael@0 | 255 | } else { |
michael@0 | 256 | __testLog__.appendChild(p); |
michael@0 | 257 | } |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | function printTestStatus(testsRun) { |
michael@0 | 261 | var status = document.getElementById('test-status'); |
michael@0 | 262 | if (testsRun) { |
michael@0 | 263 | status.className = checkTestSuccess() ? 'ok' : 'fail'; |
michael@0 | 264 | status.textContent = checkTestSuccess() ? "PASS" : "FAIL"; |
michael@0 | 265 | } else { |
michael@0 | 266 | status.className = 'fail'; |
michael@0 | 267 | status.textContent = "NO TESTS FOUND"; |
michael@0 | 268 | } |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | function assertFail(name, f) { |
michael@0 | 272 | if (f == null) { f = name; name = null; } |
michael@0 | 273 | var r = false; |
michael@0 | 274 | try { f(); } catch(e) { r=true; } |
michael@0 | 275 | if (!r) { |
michael@0 | 276 | testFailed("assertFail", name, f); |
michael@0 | 277 | return false; |
michael@0 | 278 | } else { |
michael@0 | 279 | testPassed("assertFail", name, f); |
michael@0 | 280 | return true; |
michael@0 | 281 | } |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | function assertOk(name, f) { |
michael@0 | 285 | if (f == null) { f = name; name = null; } |
michael@0 | 286 | var r = false; |
michael@0 | 287 | var err; |
michael@0 | 288 | try { f(); r=true; } catch(e) { err = e; } |
michael@0 | 289 | if (!r) { |
michael@0 | 290 | testFailed("assertOk", name, f, err.toString()); |
michael@0 | 291 | return false; |
michael@0 | 292 | } else { |
michael@0 | 293 | testPassed("assertOk", name, f); |
michael@0 | 294 | return true; |
michael@0 | 295 | } |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | function assert(name, v) { |
michael@0 | 299 | if (v == null) { v = name; name = null; } |
michael@0 | 300 | if (!v) { |
michael@0 | 301 | testFailed("assert", name, v); |
michael@0 | 302 | return false; |
michael@0 | 303 | } else { |
michael@0 | 304 | testPassed("assert", name, v); |
michael@0 | 305 | return true; |
michael@0 | 306 | } |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | function assertProperty(name, v, p) { |
michael@0 | 310 | if (p == null) { p = v; v = name; name = p; } |
michael@0 | 311 | if (v[p] == null) { |
michael@0 | 312 | testFailed("assertProperty", name); |
michael@0 | 313 | return false; |
michael@0 | 314 | } else { |
michael@0 | 315 | testPassed("assertProperty", name); |
michael@0 | 316 | return true; |
michael@0 | 317 | } |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | function compare(a,b) { |
michael@0 | 321 | if (typeof a == 'number' && typeof b == 'number') { |
michael@0 | 322 | return a == b; |
michael@0 | 323 | } else { |
michael@0 | 324 | return Object.toSource(a) == Object.toSource(b); |
michael@0 | 325 | } |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | function assertEquals(name, v, p) { |
michael@0 | 329 | if (p == null) { p = v; v = name; name = null; } |
michael@0 | 330 | if (!compare(v, p)) { |
michael@0 | 331 | testFailed("assertEquals", name, v, p); |
michael@0 | 332 | return false; |
michael@0 | 333 | } else { |
michael@0 | 334 | testPassed("assertEquals", name, v, p); |
michael@0 | 335 | return true; |
michael@0 | 336 | } |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | function assertArrayEquals(name, v, p) { |
michael@0 | 340 | if (p == null) { p = v; v = name; name = null; } |
michael@0 | 341 | if (!v) { |
michael@0 | 342 | testFailed("assertArrayEquals: first array undefined", name, v, p); |
michael@0 | 343 | return false; |
michael@0 | 344 | } |
michael@0 | 345 | if (!p) { |
michael@0 | 346 | testFailed("assertArrayEquals: second array undefined", name, v, p); |
michael@0 | 347 | return false; |
michael@0 | 348 | } |
michael@0 | 349 | if (v.length != p.length) { |
michael@0 | 350 | testFailed("assertArrayEquals", name, v, p); |
michael@0 | 351 | return false; |
michael@0 | 352 | } |
michael@0 | 353 | for (var ii = 0; ii < v.length; ++ii) { |
michael@0 | 354 | if (v[ii] != p[ii]) { |
michael@0 | 355 | testFailed("assertArrayEquals", name, v, p); |
michael@0 | 356 | return false; |
michael@0 | 357 | } |
michael@0 | 358 | } |
michael@0 | 359 | testPassed("assertArrayEquals", name, v, p); |
michael@0 | 360 | return true; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | function assertNotEquals(name, v, p) { |
michael@0 | 364 | if (p == null) { p = v; v = name; name = null; } |
michael@0 | 365 | if (compare(v, p)) { |
michael@0 | 366 | testFailed("assertNotEquals", name, v, p) |
michael@0 | 367 | return false; |
michael@0 | 368 | } else { |
michael@0 | 369 | testPassed("assertNotEquals", name, v, p) |
michael@0 | 370 | return true; |
michael@0 | 371 | } |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | function time(elementId, f) { |
michael@0 | 375 | var s = document.getElementById(elementId); |
michael@0 | 376 | var t0 = new Date().getTime(); |
michael@0 | 377 | f(); |
michael@0 | 378 | var t1 = new Date().getTime(); |
michael@0 | 379 | s.textContent = 'Elapsed: '+(t1-t0)+' ms'; |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | function randomFloat () { |
michael@0 | 383 | // note that in fuzz-testing, this can used as the size of a buffer to allocate. |
michael@0 | 384 | // so it shouldn't return astronomic values. The maximum value 10000000 is already quite big. |
michael@0 | 385 | var fac = 1.0; |
michael@0 | 386 | var r = Math.random(); |
michael@0 | 387 | if (r < 0.25) |
michael@0 | 388 | fac = 10; |
michael@0 | 389 | else if (r < 0.4) |
michael@0 | 390 | fac = 100; |
michael@0 | 391 | else if (r < 0.5) |
michael@0 | 392 | fac = 1000; |
michael@0 | 393 | else if (r < 0.6) |
michael@0 | 394 | fac = 100000; |
michael@0 | 395 | else if (r < 0.7) |
michael@0 | 396 | fac = 10000000; |
michael@0 | 397 | else if (r < 0.8) |
michael@0 | 398 | fac = NaN; |
michael@0 | 399 | return -0.5*fac + Math.random() * fac; |
michael@0 | 400 | } |
michael@0 | 401 | function randomFloatFromRange(lo, hi) { |
michael@0 | 402 | var r = Math.random(); |
michael@0 | 403 | if (r < 0.05) |
michael@0 | 404 | return lo; |
michael@0 | 405 | else if (r > 0.95) |
michael@0 | 406 | return hi; |
michael@0 | 407 | else |
michael@0 | 408 | return lo + Math.random()*(hi-lo); |
michael@0 | 409 | } |
michael@0 | 410 | function randomInt (sz) { |
michael@0 | 411 | if (sz != null) |
michael@0 | 412 | return Math.floor(Math.random()*sz); |
michael@0 | 413 | else |
michael@0 | 414 | return Math.floor(randomFloat()); |
michael@0 | 415 | } |
michael@0 | 416 | function randomIntFromRange(lo, hi) { |
michael@0 | 417 | return Math.floor(randomFloatFromRange(lo, hi)); |
michael@0 | 418 | } |
michael@0 | 419 | function randomLength () { |
michael@0 | 420 | var l = Math.floor(Math.random() * 256); |
michael@0 | 421 | if (Math.random < 0.5) l = l / 10; |
michael@0 | 422 | if (Math.random < 0.3) l = l / 10; |
michael@0 | 423 | return l; |
michael@0 | 424 | } |
michael@0 | 425 | function randomSmallIntArray () { |
michael@0 | 426 | var l = randomLength(); |
michael@0 | 427 | var s = new Array(l); |
michael@0 | 428 | for (var i=0; i<l; i++) |
michael@0 | 429 | s[i] = Math.floor(Math.random() * 256)-1; |
michael@0 | 430 | return s; |
michael@0 | 431 | } |
michael@0 | 432 | function randomFloatArray () { |
michael@0 | 433 | var l = randomLength(); |
michael@0 | 434 | var s = new Array(l); |
michael@0 | 435 | for (var i=0; i<l; i++) |
michael@0 | 436 | s[i] = randomFloat(); |
michael@0 | 437 | return s; |
michael@0 | 438 | } |
michael@0 | 439 | function randomIntArray () { |
michael@0 | 440 | var l = randomLength(); |
michael@0 | 441 | var s = new Array(l); |
michael@0 | 442 | for (var i=0; i<l; i++) |
michael@0 | 443 | s[i] = randomFloat(); |
michael@0 | 444 | return s; |
michael@0 | 445 | } |
michael@0 | 446 | function randomMixedArray () { |
michael@0 | 447 | var l = randomLength(); |
michael@0 | 448 | var s = new Array(l); |
michael@0 | 449 | for (var i=0; i<l; i++) |
michael@0 | 450 | s[i] = randomNonArray(); |
michael@0 | 451 | return s; |
michael@0 | 452 | } |
michael@0 | 453 | function randomArray () { |
michael@0 | 454 | var r = Math.random(); |
michael@0 | 455 | if (r < 0.3) |
michael@0 | 456 | return randomFloatArray(); |
michael@0 | 457 | else if (r < 0.6) |
michael@0 | 458 | return randomIntArray(); |
michael@0 | 459 | else if (r < 0.8) |
michael@0 | 460 | return randomSmallIntArray(); |
michael@0 | 461 | else |
michael@0 | 462 | return randomMixedArray(); |
michael@0 | 463 | } |
michael@0 | 464 | function randomString () { |
michael@0 | 465 | return String.fromCharCode.apply(String, randomSmallIntArray()); |
michael@0 | 466 | } |
michael@0 | 467 | function randomGLConstant () { |
michael@0 | 468 | return GLConstants[Math.floor(Math.random() * GLConstants.length)]; |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | function randomNonArray() { |
michael@0 | 472 | var r = Math.random(); |
michael@0 | 473 | if (r < 0.25) { |
michael@0 | 474 | return randomFloat(); |
michael@0 | 475 | } else if (r < 0.6) { |
michael@0 | 476 | return randomInt(); |
michael@0 | 477 | } else if (r < 0.7) { |
michael@0 | 478 | return (r < 0.65); |
michael@0 | 479 | } else if (r < 0.87) { |
michael@0 | 480 | return randomString(); |
michael@0 | 481 | } else if (r < 0.98) { |
michael@0 | 482 | return randomGLConstant(); |
michael@0 | 483 | } else { |
michael@0 | 484 | return null; |
michael@0 | 485 | } |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | function generateRandomArg(pos, count) { |
michael@0 | 489 | if (pos == 0 && Math.random() < 0.5) |
michael@0 | 490 | return randomGLConstant(); |
michael@0 | 491 | if (pos == count-1 && Math.random() < 0.25) |
michael@0 | 492 | if (Math.random() < 0.5) |
michael@0 | 493 | return randomString(); |
michael@0 | 494 | else |
michael@0 | 495 | return randomArray(); |
michael@0 | 496 | var r = Math.random(); |
michael@0 | 497 | if (r < 0.25) { |
michael@0 | 498 | return randomFloat(); |
michael@0 | 499 | } else if (r < 0.6) { |
michael@0 | 500 | return randomInt(); |
michael@0 | 501 | } else if (r < 0.7) { |
michael@0 | 502 | return (r < 0.65); |
michael@0 | 503 | } else if (r < 0.77) { |
michael@0 | 504 | return randomString(); |
michael@0 | 505 | } else if (r < 0.84) { |
michael@0 | 506 | return randomArray(); |
michael@0 | 507 | } else if (r < 0.98) { |
michael@0 | 508 | return randomGLConstant(); |
michael@0 | 509 | } else { |
michael@0 | 510 | return null; |
michael@0 | 511 | } |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | |
michael@0 | 515 | function generateRandomArgs(count) { |
michael@0 | 516 | var arr = new Array(count); |
michael@0 | 517 | for (var i=0; i<count; i++) |
michael@0 | 518 | arr[i] = generateRandomArg(i, count); |
michael@0 | 519 | return arr; |
michael@0 | 520 | } |
michael@0 | 521 | |
michael@0 | 522 | // qc (arg1gen, arg2gen, ..., predicate) |
michael@0 | 523 | // qc (randomString, randomInt, randomInt, function(s,i,j){ s.substring(i,j) }) |
michael@0 | 524 | function qc() { |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | GLConstants = [ |
michael@0 | 528 | 1, |
michael@0 | 529 | 0x00000100, |
michael@0 | 530 | 0x00000400, |
michael@0 | 531 | 0x00004000, |
michael@0 | 532 | 0x0000, |
michael@0 | 533 | 0x0001, |
michael@0 | 534 | 0x0002, |
michael@0 | 535 | 0x0003, |
michael@0 | 536 | 0x0004, |
michael@0 | 537 | 0x0005, |
michael@0 | 538 | 0x0006, |
michael@0 | 539 | 0, |
michael@0 | 540 | 1, |
michael@0 | 541 | 0x0300, |
michael@0 | 542 | 0x0301, |
michael@0 | 543 | 0x0302, |
michael@0 | 544 | 0x0303, |
michael@0 | 545 | 0x0304, |
michael@0 | 546 | 0x0305, |
michael@0 | 547 | 0x0306, |
michael@0 | 548 | 0x0307, |
michael@0 | 549 | 0x0308, |
michael@0 | 550 | 0x8006, |
michael@0 | 551 | 0x8009, |
michael@0 | 552 | 0x8009, |
michael@0 | 553 | 0x883D, |
michael@0 | 554 | 0x800A, |
michael@0 | 555 | 0x800B, |
michael@0 | 556 | 0x80C8, |
michael@0 | 557 | 0x80C9, |
michael@0 | 558 | 0x80CA, |
michael@0 | 559 | 0x80CB, |
michael@0 | 560 | 0x8001, |
michael@0 | 561 | 0x8002, |
michael@0 | 562 | 0x8003, |
michael@0 | 563 | 0x8004, |
michael@0 | 564 | 0x8005, |
michael@0 | 565 | 0x8892, |
michael@0 | 566 | 0x8893, |
michael@0 | 567 | 0x8894, |
michael@0 | 568 | 0x8895, |
michael@0 | 569 | 0x88E0, |
michael@0 | 570 | 0x88E4, |
michael@0 | 571 | 0x88E8, |
michael@0 | 572 | 0x8764, |
michael@0 | 573 | 0x8765, |
michael@0 | 574 | 0x8626, |
michael@0 | 575 | 0x0404, |
michael@0 | 576 | 0x0405, |
michael@0 | 577 | 0x0408, |
michael@0 | 578 | 0x0DE1, |
michael@0 | 579 | 0x0B44, |
michael@0 | 580 | 0x0BE2, |
michael@0 | 581 | 0x0BD0, |
michael@0 | 582 | 0x0B90, |
michael@0 | 583 | 0x0B71, |
michael@0 | 584 | 0x0C11, |
michael@0 | 585 | 0x8037, |
michael@0 | 586 | 0x809E, |
michael@0 | 587 | 0x80A0, |
michael@0 | 588 | 0, |
michael@0 | 589 | 0x0500, |
michael@0 | 590 | 0x0501, |
michael@0 | 591 | 0x0502, |
michael@0 | 592 | 0x0505, |
michael@0 | 593 | 0x0900, |
michael@0 | 594 | 0x0901, |
michael@0 | 595 | 0x0B21, |
michael@0 | 596 | 0x846D, |
michael@0 | 597 | 0x846E, |
michael@0 | 598 | 0x0B45, |
michael@0 | 599 | 0x0B46, |
michael@0 | 600 | 0x0B70, |
michael@0 | 601 | 0x0B72, |
michael@0 | 602 | 0x0B73, |
michael@0 | 603 | 0x0B74, |
michael@0 | 604 | 0x0B91, |
michael@0 | 605 | 0x0B92, |
michael@0 | 606 | 0x0B94, |
michael@0 | 607 | 0x0B95, |
michael@0 | 608 | 0x0B96, |
michael@0 | 609 | 0x0B97, |
michael@0 | 610 | 0x0B93, |
michael@0 | 611 | 0x0B98, |
michael@0 | 612 | 0x8800, |
michael@0 | 613 | 0x8801, |
michael@0 | 614 | 0x8802, |
michael@0 | 615 | 0x8803, |
michael@0 | 616 | 0x8CA3, |
michael@0 | 617 | 0x8CA4, |
michael@0 | 618 | 0x8CA5, |
michael@0 | 619 | 0x0BA2, |
michael@0 | 620 | 0x0C10, |
michael@0 | 621 | 0x0C22, |
michael@0 | 622 | 0x0C23, |
michael@0 | 623 | 0x0CF5, |
michael@0 | 624 | 0x0D05, |
michael@0 | 625 | 0x0D33, |
michael@0 | 626 | 0x0D3A, |
michael@0 | 627 | 0x0D50, |
michael@0 | 628 | 0x0D52, |
michael@0 | 629 | 0x0D53, |
michael@0 | 630 | 0x0D54, |
michael@0 | 631 | 0x0D55, |
michael@0 | 632 | 0x0D56, |
michael@0 | 633 | 0x0D57, |
michael@0 | 634 | 0x2A00, |
michael@0 | 635 | 0x8038, |
michael@0 | 636 | 0x8069, |
michael@0 | 637 | 0x80A8, |
michael@0 | 638 | 0x80A9, |
michael@0 | 639 | 0x80AA, |
michael@0 | 640 | 0x80AB, |
michael@0 | 641 | 0x86A2, |
michael@0 | 642 | 0x86A3, |
michael@0 | 643 | 0x1100, |
michael@0 | 644 | 0x1101, |
michael@0 | 645 | 0x1102, |
michael@0 | 646 | 0x8192, |
michael@0 | 647 | 0x1400, |
michael@0 | 648 | 0x1401, |
michael@0 | 649 | 0x1402, |
michael@0 | 650 | 0x1403, |
michael@0 | 651 | 0x1404, |
michael@0 | 652 | 0x1405, |
michael@0 | 653 | 0x1406, |
michael@0 | 654 | 0x140C, |
michael@0 | 655 | 0x1902, |
michael@0 | 656 | 0x1906, |
michael@0 | 657 | 0x1907, |
michael@0 | 658 | 0x1908, |
michael@0 | 659 | 0x1909, |
michael@0 | 660 | 0x190A, |
michael@0 | 661 | 0x8033, |
michael@0 | 662 | 0x8034, |
michael@0 | 663 | 0x8363, |
michael@0 | 664 | 0x8B30, |
michael@0 | 665 | 0x8B31, |
michael@0 | 666 | 0x8869, |
michael@0 | 667 | 0x8DFB, |
michael@0 | 668 | 0x8DFC, |
michael@0 | 669 | 0x8B4D, |
michael@0 | 670 | 0x8B4C, |
michael@0 | 671 | 0x8872, |
michael@0 | 672 | 0x8DFD, |
michael@0 | 673 | 0x8B4F, |
michael@0 | 674 | 0x8B80, |
michael@0 | 675 | 0x8B82, |
michael@0 | 676 | 0x8B83, |
michael@0 | 677 | 0x8B85, |
michael@0 | 678 | 0x8B86, |
michael@0 | 679 | 0x8B87, |
michael@0 | 680 | 0x8B89, |
michael@0 | 681 | 0x8B8A, |
michael@0 | 682 | 0x8B8C, |
michael@0 | 683 | 0x8B8D, |
michael@0 | 684 | 0x0200, |
michael@0 | 685 | 0x0201, |
michael@0 | 686 | 0x0202, |
michael@0 | 687 | 0x0203, |
michael@0 | 688 | 0x0204, |
michael@0 | 689 | 0x0205, |
michael@0 | 690 | 0x0206, |
michael@0 | 691 | 0x0207, |
michael@0 | 692 | 0x1E00, |
michael@0 | 693 | 0x1E01, |
michael@0 | 694 | 0x1E02, |
michael@0 | 695 | 0x1E03, |
michael@0 | 696 | 0x150A, |
michael@0 | 697 | 0x8507, |
michael@0 | 698 | 0x8508, |
michael@0 | 699 | 0x1F00, |
michael@0 | 700 | 0x1F01, |
michael@0 | 701 | 0x1F02, |
michael@0 | 702 | 0x1F03, |
michael@0 | 703 | 0x2600, |
michael@0 | 704 | 0x2601, |
michael@0 | 705 | 0x2700, |
michael@0 | 706 | 0x2701, |
michael@0 | 707 | 0x2702, |
michael@0 | 708 | 0x2703, |
michael@0 | 709 | 0x2800, |
michael@0 | 710 | 0x2801, |
michael@0 | 711 | 0x2802, |
michael@0 | 712 | 0x2803, |
michael@0 | 713 | 0x1702, |
michael@0 | 714 | 0x8513, |
michael@0 | 715 | 0x8514, |
michael@0 | 716 | 0x8515, |
michael@0 | 717 | 0x8516, |
michael@0 | 718 | 0x8517, |
michael@0 | 719 | 0x8518, |
michael@0 | 720 | 0x8519, |
michael@0 | 721 | 0x851A, |
michael@0 | 722 | 0x851C, |
michael@0 | 723 | 0x84C0, |
michael@0 | 724 | 0x84C1, |
michael@0 | 725 | 0x84C2, |
michael@0 | 726 | 0x84C3, |
michael@0 | 727 | 0x84C4, |
michael@0 | 728 | 0x84C5, |
michael@0 | 729 | 0x84C6, |
michael@0 | 730 | 0x84C7, |
michael@0 | 731 | 0x84C8, |
michael@0 | 732 | 0x84C9, |
michael@0 | 733 | 0x84CA, |
michael@0 | 734 | 0x84CB, |
michael@0 | 735 | 0x84CC, |
michael@0 | 736 | 0x84CD, |
michael@0 | 737 | 0x84CE, |
michael@0 | 738 | 0x84CF, |
michael@0 | 739 | 0x84D0, |
michael@0 | 740 | 0x84D1, |
michael@0 | 741 | 0x84D2, |
michael@0 | 742 | 0x84D3, |
michael@0 | 743 | 0x84D4, |
michael@0 | 744 | 0x84D5, |
michael@0 | 745 | 0x84D6, |
michael@0 | 746 | 0x84D7, |
michael@0 | 747 | 0x84D8, |
michael@0 | 748 | 0x84D9, |
michael@0 | 749 | 0x84DA, |
michael@0 | 750 | 0x84DB, |
michael@0 | 751 | 0x84DC, |
michael@0 | 752 | 0x84DD, |
michael@0 | 753 | 0x84DE, |
michael@0 | 754 | 0x84DF, |
michael@0 | 755 | 0x84E0, |
michael@0 | 756 | 0x2901, |
michael@0 | 757 | 0x812F, |
michael@0 | 758 | 0x8370, |
michael@0 | 759 | 0x8B50, |
michael@0 | 760 | 0x8B51, |
michael@0 | 761 | 0x8B52, |
michael@0 | 762 | 0x8B53, |
michael@0 | 763 | 0x8B54, |
michael@0 | 764 | 0x8B55, |
michael@0 | 765 | 0x8B56, |
michael@0 | 766 | 0x8B57, |
michael@0 | 767 | 0x8B58, |
michael@0 | 768 | 0x8B59, |
michael@0 | 769 | 0x8B5A, |
michael@0 | 770 | 0x8B5B, |
michael@0 | 771 | 0x8B5C, |
michael@0 | 772 | 0x8B5E, |
michael@0 | 773 | 0x8B60, |
michael@0 | 774 | 0x8622, |
michael@0 | 775 | 0x8623, |
michael@0 | 776 | 0x8624, |
michael@0 | 777 | 0x8625, |
michael@0 | 778 | 0x886A, |
michael@0 | 779 | 0x8645, |
michael@0 | 780 | 0x889F, |
michael@0 | 781 | 0x8B9A, |
michael@0 | 782 | 0x8B9B, |
michael@0 | 783 | 0x8B81, |
michael@0 | 784 | 0x8B84, |
michael@0 | 785 | 0x8B88, |
michael@0 | 786 | 0x8DFA, |
michael@0 | 787 | 0x8DF8, |
michael@0 | 788 | 0x8DF9, |
michael@0 | 789 | 0x8DF0, |
michael@0 | 790 | 0x8DF1, |
michael@0 | 791 | 0x8DF2, |
michael@0 | 792 | 0x8DF3, |
michael@0 | 793 | 0x8DF4, |
michael@0 | 794 | 0x8DF5, |
michael@0 | 795 | 0x8D40, |
michael@0 | 796 | 0x8D41, |
michael@0 | 797 | 0x8056, |
michael@0 | 798 | 0x8057, |
michael@0 | 799 | 0x8D62, |
michael@0 | 800 | 0x81A5, |
michael@0 | 801 | 0x1901, |
michael@0 | 802 | 0x8D48, |
michael@0 | 803 | 0x8D42, |
michael@0 | 804 | 0x8D43, |
michael@0 | 805 | 0x8D44, |
michael@0 | 806 | 0x8D50, |
michael@0 | 807 | 0x8D51, |
michael@0 | 808 | 0x8D52, |
michael@0 | 809 | 0x8D53, |
michael@0 | 810 | 0x8D54, |
michael@0 | 811 | 0x8D55, |
michael@0 | 812 | 0x8CD0, |
michael@0 | 813 | 0x8CD1, |
michael@0 | 814 | 0x8CD2, |
michael@0 | 815 | 0x8CD3, |
michael@0 | 816 | 0x8CE0, |
michael@0 | 817 | 0x8D00, |
michael@0 | 818 | 0x8D20, |
michael@0 | 819 | 0, |
michael@0 | 820 | 0x8CD5, |
michael@0 | 821 | 0x8CD6, |
michael@0 | 822 | 0x8CD7, |
michael@0 | 823 | 0x8CD9, |
michael@0 | 824 | 0x8CDD, |
michael@0 | 825 | 0x8CA6, |
michael@0 | 826 | 0x8CA7, |
michael@0 | 827 | 0x84E8, |
michael@0 | 828 | 0x0506, |
michael@0 | 829 | 0x809D |
michael@0 | 830 | ]; |
michael@0 | 831 | |
michael@0 | 832 | function reportTestResultsToHarness(success, msg) { |
michael@0 | 833 | if (window.parent.webglTestHarness) { |
michael@0 | 834 | window.parent.webglTestHarness.reportResults(success, msg); |
michael@0 | 835 | } |
michael@0 | 836 | } |
michael@0 | 837 | |
michael@0 | 838 | function notifyFinishedToHarness() { |
michael@0 | 839 | if (window.parent.webglTestHarness) { |
michael@0 | 840 | window.parent.webglTestHarness.notifyFinished(); |
michael@0 | 841 | } |
michael@0 | 842 | } |
michael@0 | 843 | |
michael@0 | 844 | function initTests() { |
michael@0 | 845 | if (Tests.message != null) { |
michael@0 | 846 | var h = document.getElementById('test-message'); |
michael@0 | 847 | if (h == null) { |
michael@0 | 848 | h = document.createElement('p'); |
michael@0 | 849 | h.id = 'test-message'; |
michael@0 | 850 | document.body.insertBefore(h, document.body.firstChild); |
michael@0 | 851 | } |
michael@0 | 852 | h.textContent = Tests.message; |
michael@0 | 853 | } |
michael@0 | 854 | if (Tests.autorun) { |
michael@0 | 855 | runTests(); |
michael@0 | 856 | } else { |
michael@0 | 857 | var h = document.getElementById('test-run'); |
michael@0 | 858 | if (h == null) { |
michael@0 | 859 | h = document.createElement('input'); |
michael@0 | 860 | h.type = 'submit'; |
michael@0 | 861 | h.value = "Run tests"; |
michael@0 | 862 | h.addEventListener('click', function(ev){ |
michael@0 | 863 | runTests(); |
michael@0 | 864 | ev.preventDefault(); |
michael@0 | 865 | }, false); |
michael@0 | 866 | h.id = 'test-run'; |
michael@0 | 867 | document.body.insertBefore(h, document.body.firstChild); |
michael@0 | 868 | } |
michael@0 | 869 | h.textContent = Tests.message; |
michael@0 | 870 | } |
michael@0 | 871 | |
michael@0 | 872 | } |
michael@0 | 873 | |
michael@0 | 874 | window.addEventListener('load', function(){ |
michael@0 | 875 | // let the browser hopefully finish updating the gl canvas surfaces if we are given a delay |
michael@0 | 876 | if (Tests.delay) |
michael@0 | 877 | setTimeout(initTests, Tests.delay); |
michael@0 | 878 | else |
michael@0 | 879 | initTests() |
michael@0 | 880 | }, false); |
michael@0 | 881 |