test-fuzzy-array

tap/tape utility to compare arrays with an epislon

Usage no npm install needed!

<script type="module">
  import testFuzzyArray from 'https://cdn.skypack.dev/test-fuzzy-array';
</script>

README

test-fuzzy-array

stable

A tap/tape utility to compare arrays with an epislon value. Defaults to float epsilon, and provides clear logging on test pass/failure.

var test = require('tape')
var Fuzz = require('test-fuzzy-array')

test('compares arrays with an epislon', function(t) {
    var epsilon = 0.01
    var almostEqual = Fuzz(t, epsilon)

    almostEqual(['foo', 1, 0.0025], ['foo', 1, 0.0026])
    t.end()
})

Result:
img

Usage

NPM

almostEqual = Fuzz(t[, epsilon[, relativeTolerance]])

Returns an almostEqual function that operates on the given test instance of tape, tap and similar interfaces. Epsilon defaults to float precision. Relative tolerance defaults to epsilon value.

The function has the signature:

almostEqual(value, expected, msg)

notAlmostEqual = almostEqual.not

The return value of Fuzz has a not function which is the negated version.

Examples:

var Fuzzy = require('test-fuzzy-array')

test('arrays are almost equal', function(t) {
    var almostEqual = Fuzzy(t)
    var notAlmostEqual = almostEqual.not
    
    almostEqual(['foo', 1, 1], ['foo', 1, 1 + 1e-12])
    notAlmostEqual([], ['foo', 1, 0.0026])
    t.end()
})

License

MIT, see LICENSE.md for details.