Skip to content

Commit baf37c1

Browse files
committed
Add a way to write Unit Tests.
Add some Mocha[1]-based tests for Pseudo and Statistics as initial examples. They are run by loading `unit-tests/run-unit-tests.html` in the browser. [1] https://mochajs.org/
1 parent da4695d commit baf37c1

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, user-scalable=no">
6+
<title>MotionMark Unit Tests</title>
7+
8+
<script src="../resources/strings.js"></script>
9+
<script src="../resources/extensions.js"></script>
10+
<script src="../resources/statistics.js"></script>
11+
12+
<script src="../resources/runner/tests.js"></script>
13+
<script src="../resources/debug-runner/tests.js"></script>
14+
15+
<script src="../resources/runner/benchmark-runner.js"></script>
16+
17+
<script src="https://cdnjs.cloudflare.com/ajax/libs/expect.js/0.2.0/expect.min.js"></script>
18+
<script src="https://unpkg.com/mocha/mocha.js"></script>
19+
20+
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
21+
<link rel="stylesheet" href="../resources/runner/motionmark.css">
22+
<link rel="stylesheet" href="../resources/debug-runner/motionmark.css">
23+
24+
<style>
25+
body {
26+
-webkit-user-select: text;
27+
cursor: auto;
28+
margin: 8px;
29+
}
30+
31+
#mocha {
32+
margin: 20px 50px;
33+
}
34+
</style>
35+
</head>
36+
<body>
37+
<h1>MotionMark Unit Tests</h1>
38+
<div id="mocha"></div>
39+
40+
<script class="mocha-init">
41+
mocha.setup('bdd');
42+
mocha.checkLeaks();
43+
</script>
44+
45+
<script src="tests/test-random.js"></script>
46+
<script src="tests/test-statistics.js"></script>
47+
48+
<script class="mocha-exec">
49+
mocha.run();
50+
</script>
51+
</body>
52+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2024 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
* THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
describe('Random', function() {
27+
it('Check random sequence', function() {
28+
Pseudo.resetRandomSeed();
29+
expect(Pseudo.randomSeed).to.be(49734321);
30+
expect(Pseudo.random().toFixed(6)).to.be('0.987282');
31+
expect(Pseudo.random().toFixed(6)).to.be('0.348803');
32+
expect(Pseudo.random().toFixed(6)).to.be('0.563193');
33+
});
34+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2024 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
* THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
describe('Statistics', function() {
27+
describe('sampleMean()', function() {
28+
it('sampleMean should compute the mean', function() {
29+
expect(Statistics.sampleMean(10, 100)).to.be(10);
30+
});
31+
32+
it('sampleMean with zero samples should be zero', function() {
33+
expect(Statistics.sampleMean(0, 100)).to.be(0);
34+
});
35+
});
36+
37+
describe('geometricMean()', function() {
38+
it('geometricMean should compute the geometric mean', function() {
39+
const values = [6, 2, 3, 5];
40+
const result = Statistics.geometricMean(values).toFixed(3);
41+
expect(result).to.be('3.663')
42+
});
43+
44+
it('geometricMean with a zero should compute to zero', function() {
45+
const values = [6, 2, 0, 5];
46+
const result = Statistics.geometricMean(values).toFixed(3);
47+
expect(result).to.be('0.000')
48+
});
49+
});
50+
});

0 commit comments

Comments
 (0)