test: Set up a mocha test harness (#5)

We use [mocha](https://mochajs.org/) as the test framework, as it is
  the tool used by mathjs and we would like to make tests as similar
  as possible. However, to tighten the linkage between source code and
  tests, we adopt a somewhat different file organization: unit tests
  for a given source file `blah/foo.js` are in `blah/__test__/foo.spec.js`.

  To run all unit tests, execute the script `pnpm test`.

  Resolves #3.

Reviewed-on: glen/nanomath#5
Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-committed-by: Glen Whitney <glen@studioinfinity.org>
This commit is contained in:
Glen Whitney 2025-04-07 16:18:46 +00:00 committed by Glen Whitney
parent 79c6d44fda
commit 036def4a0c
9 changed files with 756 additions and 8 deletions

View file

@ -1,3 +1,3 @@
import {Type} from '../core/Type.js'
import {Type} from '#core/Type.js'
export const Number = new Type(n => typeof n === 'number')

View file

@ -0,0 +1,17 @@
import assert from 'assert'
import math from '#nanomath'
describe('number arithmetic', () => {
it('supports basic operations', () => {
assert.strictEqual(math.abs(-Infinity), Infinity)
assert.strictEqual(math.absquare(-2), 4)
assert.strictEqual(math.add(2, 2), 4)
assert.strictEqual(math.divide(6, 4), 1.5)
assert.strictEqual(math.cbrt(-8), -2)
assert.strictEqual(math.invert(-4), -0.25)
assert.strictEqual(math.multiply(4, 1.5), 6)
assert.strictEqual(math.negate(Infinity), -Infinity)
assert.strictEqual(math.subtract(4, 2), 2)
assert.strictEqual(math.quotient(7, 3), 2)
})
})

View file

@ -10,7 +10,7 @@ export const cbrt = plain(a => {
if (negate) a = -a
let result = a
if (isFinite(a)) {
result = Math.exp(Math.log(x) / 3)
result = Math.exp(Math.log(result) / 3)
result = (a / (result * result) + (2 * result)) / 3
}
return negate ? -result : result
@ -18,4 +18,5 @@ export const cbrt = plain(a => {
export const invert = plain(a => 1/a)
export const multiply = plain((a, b) => a * b)
export const negate = plain(a => -a)
export const subtract = plain((a, b) => a - b)
export const quotient = plain((a,b) => Math.floor(a/b))

View file

@ -1,4 +1,4 @@
import {Returns, onType} from '../core/helpers.js'
import {Returns, onType} from '#core/helpers.js'
import {Number} from './Number.js'
export const plain = f => onType(