Skip to content

Commit

Permalink
Initial browser support
Browse files Browse the repository at this point in the history
Note that this removes benchmarking, which wasn't working well anyways
Closes #7
May improve #34, #36
  • Loading branch information
LivInTheLookingGlass committed Jul 30, 2024
1 parent 6e9ac5d commit 5bfd19a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 57 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ LivInTheLookingGlass’s Project Euler solutions
| | | | |CodeQL| |
+------------+-------------------------+--------+-------------------+
| JavaScript | Node 12+ |br| | 9 | |JavaScript| |br| |
| | Bun 0.6.10+ | | |Js-Cov| |br| |
| | | | |CodeQL| |br| |
| | | | |ESLint| |
| | Bun 0.6.10+ |br| | | |Js-Cov| |br| |
| | Firefox* |br| | | |CodeQL| |br| |
| | Chrome* | | |ESLint| |
+------------+-------------------------+--------+-------------------+
| Python | CPython 3.6+ |br| | 79 | |Python| |br| |
| | Pypy 3.6+ |br| | | |Py-Cov| |br| |
Expand Down
8 changes: 6 additions & 2 deletions javascript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ help:
@echo " $(BLUE)lint$(NC) runs eslint"
@echo " $(BLUE)bun_test$(NC) run through all tests in sequence. Utilizes the bun runtime and test runner"
@echo " $(BLUE)bun_dependencies$(NC) installs bun and runs bun install"
@echo " $(BLUE)webpack$(NC) Packages these tests for use in browsers"
@echo " $(BLUE)clean$(NC) clean up any stray files"

test: dependencies
Expand Down Expand Up @@ -66,10 +67,13 @@ _dependencies:
ifeq ($(COV),true)
npm install -g istanbul
endif
npm install
npm install --include=dev

lint:
npx eslint *.js

clean:
rm -r node_modules || echo
rm -r node_modules dist || echo

webpack: dependencies
npx webpack
27 changes: 26 additions & 1 deletion javascript/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ utilities for other problems to use.
Makefile
--------

There are four main recipes in this Makefile
There are eight recipes in this Makefile

dependencies
~~~~~~~~~~~~
Expand All @@ -43,11 +43,36 @@ though it currently does not export benchmark info except to fail tests
for time limit reasons. If you specify the number of threads as `auto`, it
will default to using 1 fewer threads than you have CPUs.

dependencies
~~~~~~~~~~~~

Install all dependencies for the NodeJS platform.

lint
~~~~

This recipe runs es-lint on the JavaScript files.

bun_test
~~~~~~~~

Run through all tests in sequence. Utilizes the bun runtime and test runner.

bun_dependencies
~~~~~~~~~~~~~~~~

Installs bun and runs bun install.

webpack
~~~~~~~

Packages these tests for use in browsers.

clean
~~~~~

Remove any installed modules or webpack files.

Tests
-----

Expand Down
42 changes: 8 additions & 34 deletions javascript/euler.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const chai = require('chai');
const expect = chai.expect;
const Benchmark = require('benchmark');

const answers = {
1: [require('./src/p0001.js'), 233168],
Expand All @@ -14,49 +13,24 @@ const answers = {
836: [require('./src/p0836.js'), 'aprilfoolsjoke'],
};
const knownSlow = [];
let benchmarkReport = '';

for (question in answers) {
if (answers.hasOwnProperty(question)) {
const formattedQuestion = `${question}`.padStart(4, '0');
const [module, answer] = answers[question];
const problem = module[`p${formattedQuestion}`];
describe(`Problem ${formattedQuestion}`, ()=>{
it(`Should equal ${answer}`, function() {
if (typeof this.timeout !== 'undefined')
if (knownSlow.includes(question)) {
this.timeout(-1);
}
else {
this.timeout(60 * 1000);
}
expect(answer).to.equal(module[`p${formattedQuestion}`]());
});
it('should return take less than 1 minute', function(done) {
if (typeof this.timeout !== 'undefined') {
this.timeout(-1);
this.slow(300000); // five minutes
if (knownSlow.includes(question)) {
this.timeout(Infinity);
}
else {
this.timeout(60 * 1000);
}
}
const b = new Benchmark(formattedQuestion, module.main, {'minSamples': 10});
const [results] = Benchmark.invoke([b], 'run');
const max = Math.max(...(results.stats.sample));
expect(60).to.be.greaterThan(max);
benchmarkReport += `================= p${formattedQuestion} =================\n`;
benchmarkReport += `Maximum time: ${max.toFixed(6)}s\n`;
benchmarkReport += `Minimum time: ${Math.min(...(results.stats.sample)).toFixed(6)}s\n`;
benchmarkReport += `Average time: ${results.stats.mean.toFixed(6)}s\n`;
benchmarkReport += `OPS: ${results.hz.toFixed(6)}/s\n`;
benchmarkReport += `Standard Deviation: ${results.stats.deviation.toFixed(6)}s\n`;
benchmarkReport += `Margin of Error: ${results.stats.moe.toFixed(6)}s\n`;
benchmarkReport += `Iterations: ${results.stats.sample.length}\n\n\n`;
done();
process.stdout.write(` Max time: ${max.toFixed(6)}s\n`);
expect(answer).to.equal(problem());
});
});
}
}

if (typeof after !== 'undefined') {
after(()=>{
process.stdout.write(benchmarkReport);
});
}
53 changes: 53 additions & 0 deletions javascript/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!-- test/support/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>

<link href="https://npmcdn.com/[email protected]/mocha.css" rel="stylesheet" />
<script src="https://npmcdn.com/[email protected]/mocha.js"></script>
</head>
<body>

<!-- A container element for the visual Mocha results -->
<div id="mocha"></div>

<!-- Mocha setup and initiation code -->
<script>
mocha.setup('bdd');
window.onload = function() {
var runner = mocha.run();
var failedTests = [];

runner.on('end', function() {
window.mochaResults = runner.stats;
window.mochaResults.reports = failedTests;
});

runner.on('fail', logFailure);

function logFailure(test, err){
var flattenTitles = function(test){
var titles = [];
while (test.parent.title){
titles.push(test.parent.title);
test = test.parent;
}
return titles.reverse();
};

failedTests.push({
name: test.title,
result: false,
message: err.message,
stack: err.stack,
titles: flattenTitles(test)
});
};
};
</script>
<script src="./dist/bundle.js"></script>

</body>
</html>
18 changes: 2 additions & 16 deletions javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"author": "Olivia Appleton",
"license": "ISC",
"dependencies": {
"benchmark": "^2.1.4",
"chai": "^4.3.6",
"istanbul": "^0.4.5",
"mocha": "^10.6.0"
Expand Down

0 comments on commit 5bfd19a

Please sign in to comment.