Add a couple of general JS tests.

The primes one prints its execution time:

  NetSurf without -v: 1300ish
             with -v: 5000ish
      Firefox 38.5.0:  300ish
This commit is contained in:
Michael Drake 2016-01-23 10:28:10 +00:00
parent 5b422a3ec9
commit 4b0ebae56a
3 changed files with 113 additions and 0 deletions

View File

@ -6,6 +6,12 @@
<body> <body>
<h1>JavaScript Tests</h1> <h1>JavaScript Tests</h1>
<h2>General</h2>
<ul>
<li><a href="js-fractal.html">Fractal</a></li>
<li><a href="js-primes.html">Primes</a></li>
</ul>
<h2>Core</h2> <h2>Core</h2>
<ul> <ul>
<li><a href="core.recursion.html">Infinite Recursion</a></li> <li><a href="core.recursion.html">Infinite Recursion</a></li>

41
test/js/js-fractal.html Normal file
View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<title>JavaScript mandelbrot set</title>
<style>pre {font-size:8pt;}</style>
</head>
<body>
<h1>JavaScript generated mandelbrot set</h1>
<script>
function show_mandelbrot()
{
var gradation = " .,:;=|i+IhHOE#$ ";
document.writeln("<pre>");
for(i = -1.2; i <= 1.2; i += 0.04) {
for(r = -2; r <= 1; r += 0.02) {
var zr = r;
var zi = i;
var n = 0;
for(; n < 16; n++) {
var zr2 = zr * zr;
var zi2 = zi * zi;
if((zr2 + zi2) > 4) {
break;
}
zi = 2 * zr * zi + i;
zr = zr2 - zi2 + r;
}
document.write(gradation.charAt(n));
}
document.write("\n");
}
document.writeln("</pre>");
document.close();
}
show_mandelbrot();
</script>
<noscript>
JavaScript not enabled.
</noscript>
</body>
</html>

66
test/js/js-primes.html Normal file
View File

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<head>
<title>How many bit-reversed primes are prime?</title>
</head>
<body>
<pre>
<script>
var start = new Date().getTime();
// Add the lpad method to Strings, for adding left padding
String.prototype.lpad = function(padString, length) {
var str = this;
while (str.length < length)
str = padString + str;
return str;
}
// Naive prime checker
function is_prime(n)
{
for (var i = 2; i <= n/2; i++) {
if (n % i === 0) {
return false;
}
}
return true;
}
// Bit reversal
function bit_reverse(num)
{
var rev = 0;
while (num > 0) {
rev <<= 1;
rev |= (num & 1);
num >>= 1;
}
return rev;
}
for (var num = 1; num < 10000; num++) {
var rev = bit_reverse(num);
var num_prime = is_prime(num);
var rev_prime = is_prime(rev);
document.write(num.toString().lpad(" ", 7) + " ");
document.write(num_prime ? "Prime" : " ");
document.write(" ");
document.write(rev.toString().lpad(" ", 7) + " ");
document.write(rev_prime ? "Prime" : " ");
document.write(" ");
document.write(num_prime && rev_prime ? "(both)" : "");
document.write("\n");
}
var end = new Date().getTime();
var time = end - start;
document.write('Execution time: ' + time + '\n');
</script>
</pre>
</body>
</html>