Skip to content

Commit

Permalink
Fix some web changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 15, 2024
1 parent e45bdbd commit 0ca9bcf
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Olivia's Project Euler Solutions
and these tests not yet automated as in Nodejs and Bun. To run these tests yourself, |test-js-link|
.. [3] While these solutions do run in most browsers, they need to be bundled with Pyodide 0.26.2+ first, and these
tests are not yet automated as in CPython and pypy. To run these tests yourself, |test-py-link|
.. [4] This feature is in progress. While these solutions will run in most browsers, they need to be bundled with wasm-pack 0.2+ first, and these
.. [4] While these solutions will run in most browsers, they need to be bundled with wasm-pack 0.2+ first, and these
tests are not yet automated as on non-web platforms. To run these tests yourself, |test-rs-link|
Coverage
Expand Down
73 changes: 37 additions & 36 deletions docs/_static/test-rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,50 @@
<div id="mocha"></div>
<!-- Mocha setup and initiation code -->
<script type="module">
import initSync, { get_problems, run_problem, get_js_answer } from './dist/rust.js';
import init, { get_problems, run_problem, get_js_answer } from './dist/rust.js';
mocha.setup('bdd');
window.onload = function() {
initSync();
var runner = mocha.run();
var failedTests = [];

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

runner.on('fail', function(test, err){
var flattenTitles = function(test){
var titles = [];
while (test.parent.title){
titles.push(test.parent.title);
test = test.parent;
}
return titles.reverse();
};
init().then(() => {
for (const p of get_problems(false)) {
const expected = get_js_answer(p);
describe(`run test ${p}`, function() {
this.timeout(Infinity);
it(`should return ${expected}`, async () => {
const answer = run_problem(p);
console.log(p, answer, expected);
if (answer !== expected) {
throw new Error();
}
});
});
}
const runner = mocha.run();
let failedTests = [];

failedTests.push({
name: test.title,
result: false,
message: err.message,
stack: err.stack,
titles: flattenTitles(test)
runner.on('end', function() {
window.mochaResults = runner.stats;
window.mochaResults.reports = failedTests;
});
});
for (const p of get_problems()) {
const expected = get_js_answer(p);
describe(`run test ${p}`, function() {
this.timeout(Infinity);
it(`should return ${expected}`, async () => {
const answer = run_problem(p);
console.log(p, answer, expected);
if (answer !== expected) {
throw new Error();

runner.on('fail', function(test, err){
const flattenTitles = function(test){
let 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>
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`74` | | | | | |:py-d:`0074`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`76` |:c-d:`0076` |:cp-d:`0076`|:cs-s:`0076`|:ja-d:`0076`|:js-s:`0076`|:py-d:`0076`|:rs-d:`0076`|
|:prob:`76` |:c-d:`0076` |:cp-d:`0076`|:cs-s:`0076`|:ja-d:`0076`|:js-s:`0076`|:py-d:`0076`|:rs-s:`0076`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`77` | | | | | |:py-d:`0077`| |
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
5 changes: 5 additions & 0 deletions docs/rust.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ build or test processes.
Alias for ``cargo clean``.

Live Tests
----------

To run these problems in your browser, :live-test:`rs`

Library Code
------------

Expand Down
10 changes: 6 additions & 4 deletions rust/src/include/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn get_problem<'b>(n: usize) -> Option<ProblemRef<'b>> {
20 => Some(( &20, p0020, false)),
22 => Some(( &22, p0022, false)),
24 => Some(( &24, p0024, false)),
27 => Some(( &27, p0027, false)),
27 => Some(( &27, p0027, true)),
34 => Some(( &34, p0034, false)),
69 => Some(( &69, p0069, false)),
76 => Some(( &76, p0076, false)),
Expand All @@ -55,11 +55,13 @@ pub fn get_problem<'b>(n: usize) -> Option<ProblemRef<'b>> {
}


pub fn generate_supported_problems() -> Vec<usize> {
pub fn generate_supported_problems(include_slow: bool) -> Vec<usize> {
let mut supported_problems = Vec::new();
for n in 1..10000 {
if get_problem(n).is_some() {
supported_problems.push(n);
if let Some((_, _, slow)) = get_problem(n) {
if !slow || include_slow {
supported_problems.push(n);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub fn run_problem(n: usize) -> JsValue {

#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
#[wasm_bindgen]
pub fn get_problems() -> JsValue {
let problems = generate_supported_problems();
pub fn get_problems(include_slow: bool) -> JsValue {
let problems = generate_supported_problems(include_slow);
let js_array = Array::new_with_length(problems.len() as u32);

for (i, &item) in problems.iter().enumerate() {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
for i in sieve {
println!("{}", i);
}
let supported = generate_supported_problems();
let supported = generate_supported_problems(false);

for id in supported {
match get_problem(id) {
Expand Down

0 comments on commit 0ca9bcf

Please sign in to comment.