Skip to content

Commit 926b432

Browse files
committed
Add property-base tests to Integer
1 parent 565310f commit 926b432

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

packages/core/test/integer.test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Integer, {
2525
inSafeRange
2626
} from '../src/integer'
2727
import { newError } from '../src/error'
28+
import fc from 'fast-check'
2829

2930
describe('Integer', () => {
3031
forEachToNumberOrInfinityScenarios(({ input, expectedOutput }) =>
@@ -355,6 +356,121 @@ describe('Integer', () => {
355356
Integer.MAX_SAFE_VALUE.toString()
356357
)
357358
})
359+
360+
test('int(BigInt) should be reversed by Integer.toBigInt', () => {
361+
fc.assert(
362+
fc.property(
363+
fc.bigInt({ max: Integer.MAX_SAFE_VALUE.toBigInt(), min: Integer.MIN_SAFE_VALUE.toBigInt() }),
364+
bigint => bigint === int(bigint).toBigInt()
365+
)
366+
)
367+
})
368+
369+
test('int(string) should be reversed by Integer.toString', () => {
370+
fc.assert(fc.property(fc.integer().map(num => num.toString()), str => str === int(str).toString()))
371+
})
372+
373+
test('int(number) should be reversed by Integer.toNumber', () => {
374+
fc.assert(fc.property(fc.integer(), num => num === int(num).toNumber()))
375+
})
376+
377+
test('int(Integer) should be equal Integer', () => {
378+
fc.assert(fc.property(arbitraryInteger(), integer => integer === int(integer)))
379+
})
380+
381+
test('Integer.add should be Commutative', () => {
382+
fc.assert(fc.property(arbitraryInteger(), arbitraryInteger(),
383+
(a, b) => a.add(b).equals(b.add(a))))
384+
})
385+
386+
test('Integer.multiply should be Commutative', () => {
387+
fc.assert(fc.property(arbitraryInteger(), arbitraryInteger(),
388+
(a, b) => a.multiply(b).equals(b.multiply(a))))
389+
})
390+
391+
test('Integer.add should be Associative', () => {
392+
fc.assert(fc.property(arbitraryInteger(), arbitraryInteger(), arbitraryInteger(),
393+
(a, b, c) => a.add(b.add(c)).equals(a.add(b).add(c))))
394+
})
395+
396+
test('Integer.multiply should be Associative', () => {
397+
fc.assert(fc.property(arbitraryInteger(), arbitraryInteger(), arbitraryInteger(),
398+
(a, b, c) => a.multiply(b.multiply(c)).equals(a.multiply(b).multiply(c))))
399+
})
400+
401+
test('Integer.add should be Distributive', () => {
402+
fc.assert(fc.property(arbitraryInteger(), arbitraryInteger(), arbitraryInteger(),
403+
(a, b, c) => a.multiply(b.add(c)).equals(a.multiply(b).add(a.multiply(c)))))
404+
})
405+
406+
test('Integer.subtract should be Distributive', () => {
407+
fc.assert(fc.property(arbitraryInteger(), arbitraryInteger(), arbitraryInteger(),
408+
(a, b, c) => a.multiply(b.subtract(c)).equals(a.multiply(b).subtract(a.multiply(c)))))
409+
})
410+
411+
describe('For Same Signal Integers', () => {
412+
test('Integer.greaterThan should return true if a - b is positive', () => {
413+
fc.assert(fc.property(
414+
arbitrarySameSignalIntegers(),
415+
({ a, b }) => a.subtract(b).isPositive() ? a.greaterThan(b) : !a.greaterThan(b)))
416+
})
417+
418+
test('Integer.greaterThanOrEqual should return true if a - b is positive or ZERO', () => {
419+
fc.assert(fc.property(
420+
arbitrarySameSignalIntegers(),
421+
({ a, b }) => a.subtract(b).isPositive() || a.subtract(b).isZero() ? a.greaterThanOrEqual(b) : !a.greaterThanOrEqual(b)))
422+
})
423+
424+
test('Integer.equals should return true if a - b is ZERO', () => {
425+
fc.assert(fc.property(
426+
arbitrarySameSignalIntegers(),
427+
({ a, b }) => a.subtract(b).isZero() ? a.equals(b) : !a.equals(b)))
428+
})
429+
430+
test('Integer.lessThanOrEqual should return true if a - b is ZERO or negative', () => {
431+
fc.assert(fc.property(
432+
arbitrarySameSignalIntegers(),
433+
({ a, b }) => a.subtract(b).isNegative() || a.subtract(b).isZero() ? a.lessThanOrEqual(b) : !a.lessThanOrEqual(b)))
434+
})
435+
436+
test('Integer.lessThanOrEqual should return true if a - b is ZERO or negative', () => {
437+
fc.assert(fc.property(
438+
arbitrarySameSignalIntegers(),
439+
({ a, b }) => a.subtract(b).isNegative() ? a.lessThan(b) : !a.lessThan(b)))
440+
})
441+
})
442+
443+
describe('For Different Signal Integers', () => {
444+
test('Integer.greaterThan should return true if a is positive', () => {
445+
fc.assert(fc.property(
446+
arbitraryDiffSignalIntegers(),
447+
({ a, b }) => a.isPositive() ? a.greaterThan(b) : !a.greaterThan(b)))
448+
})
449+
450+
test('Integer.greaterThanOrEqual should return true if a is positive or ZERO', () => {
451+
fc.assert(fc.property(
452+
arbitraryDiffSignalIntegers(),
453+
({ a, b }) => a.isPositive() || a.isZero() ? a.greaterThanOrEqual(b) : !a.greaterThanOrEqual(b)))
454+
})
455+
456+
test('Integer.equals should return true if a is ZERO and b is ZERO', () => {
457+
fc.assert(fc.property(
458+
arbitraryDiffSignalIntegers(),
459+
({ a, b }) => a.isZero() && b.isZero() ? a.equals(b) : !a.equals(b)))
460+
})
461+
462+
test('Integer.lessThanOrEqual should return true if a is ZERO or negative', () => {
463+
fc.assert(fc.property(
464+
arbitraryDiffSignalIntegers(),
465+
({ a, b }) => a.isNegative() || a.isZero() ? a.lessThanOrEqual(b) : !a.lessThanOrEqual(b)))
466+
})
467+
468+
test('Integer.lessThanOrEqual should return true if a is ZERO or negative', () => {
469+
fc.assert(fc.property(
470+
arbitraryDiffSignalIntegers(),
471+
({ a, b }) => a.isNegative() ? a.lessThan(b) : !a.lessThan(b)))
472+
})
473+
})
358474
})
359475

360476
function forEachToNumberOrInfinityScenarios (
@@ -1188,6 +1304,20 @@ function wellFormedNumbersAndRadix (): Array<[string, number]> {
11881304
]
11891305
}
11901306

1307+
function arbitraryInteger (): fc.Arbitrary<Integer> {
1308+
return fc.record({ low: fc.integer(), high: fc.integer() })
1309+
.map(({ low, high }) => new Integer(low, high))
1310+
}
1311+
1312+
function arbitrarySameSignalIntegers (): fc.Arbitrary<{ a: Integer, b: Integer }> {
1313+
return fc.record({ a: arbitraryInteger(), b: arbitraryInteger() })
1314+
.map(({ a, b }) => a.isPositive() === b.isPositive() ? { a, b } : { a, b: b.negate() })
1315+
}
1316+
1317+
function arbitraryDiffSignalIntegers (): fc.Arbitrary<{ a: Integer, b: Integer }> {
1318+
return fc.record({ a: arbitraryInteger(), b: arbitraryInteger() })
1319+
.map(({ a, b }) => a.isPositive() === b.isPositive() ? { a, b: b.negate() } : { a, b })
1320+
}
11911321
interface AssertionPair<I, O> {
11921322
input: I
11931323
expectedOutput: O

0 commit comments

Comments
 (0)