Skip to content

Add tests for statistics functions in test.js #21

Mergedsyntaqx merged into mainrequested by syntaqx

Approved by g1t

2 files+3−340/2 viewed
+0−34
11 # automation lab
2−
3−## Functions
4−
5−All functions are exported from `math.js`.
6−
7−```js
8−const { add, multiply, power, mean, median, standard_deviation } = require("./math");
9−```
10−
11−- `add(a, b)`: returns the sum of `a` and `b`.
12− ```js
13− add(2, 3); // 5
14− ```
15−- `multiply(a, b)`: returns the product of `a` and `b`.
16− ```js
17− multiply(4, 5); // 20
18− ```
19−- `power(base, exponent)`: returns `base` raised to a whole-number `exponent`; throws a `RangeError` if `exponent` is negative.
20− ```js
21− power(2, 3); // 8
22− ```
23−- `mean(numbers)`: returns the average of an array of numbers; throws a `TypeError` if the array is empty.
24− ```js
25− mean([1, 2, 3, 4]); // 2.5
26− ```
27−- `median(numbers)`: returns the middle value (the average of the two middle values when the length is even); throws a `TypeError` if the array is empty.
28− ```js
29− median([3, 1, 2]); // 2
30− median([1, 2, 3, 4]); // 2.5
31− ```
32−- `standard_deviation(numbers)`: returns the population standard deviation; throws a `TypeError` if the array is empty.
33− ```js
34− standard_deviation([2, 4, 4, 4, 5, 5, 7, 9]); // 2
35− ```
+3−0
66 assert.strictEqual(math.power(2, 10), 1024);
77 assert.strictEqual(math.power(5, 0), 1);
88 assert.throws(() => math.power(2, -1), RangeError);
9+assert.strictEqual(math.mean([1, 2, 3]), 2);
910 assert.strictEqual(math.mean([1, 2, 3, 4]), 2.5);
11+assert.strictEqual(math.median([1, 2, 3]), 2);
1012 assert.strictEqual(math.median([3, 1, 2]), 2);
1113 assert.strictEqual(math.median([4, 1, 3, 2]), 2.5);
14+assert.strictEqual(math.standard_deviation([1, 2, 3]), Math.sqrt(2 / 3));
1215 assert.strictEqual(math.standard_deviation([2, 4, 4, 4, 5, 5, 7, 9]), 2);
1316 for (const fn of [math.mean, math.median, math.standard_deviation]) {
1417 assert.throws(() => fn([]), TypeError);