math-interval-2
Version:
Create math intervals like '(-∞, 100]' and test values
360 lines • 17.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var index_1 = require("./index");
describe('MathInterval', function () {
describe('interval & endpoints & bound types', function () {
var interval = index_1.MathInterval.interval(0, true, 100, false);
it('should set lower endpoint', function () {
expect(interval.lowerEndpoint()).toBe(0);
});
it('should set upper endpoint', function () {
expect(interval.upperEndpoint()).toBe(100);
});
it('should set lower bound type', function () {
expect(interval.isLowerBoundClosed()).toBe(true);
});
it('should set upper bound type', function () {
expect(interval.isUpperBoundClosed()).toBe(false);
});
describe('containsAll', function () { return function () {
it('should contain all if all numbers are within interval', function () {
expect(interval.containsAll([0, 20, 30, 40]));
});
it('should not contain all unless all numbers are within interval', function () {
expect(interval.containsAll([0, 20, 30, 100]));
});
}; });
});
describe('validation', function () {
it('should throw error if lower endpoint greater than upper endpoint', function () {
expect(function () { return index_1.MathInterval.interval(100, true, 0, false); }).toThrowError();
});
it('should throw error if lower endpoint smaller than upper endpoint', function () {
expect(function () { return index_1.MathInterval.interval(0, true, 100, false); }).not.toThrowError();
});
it('should throw error if lower equals to upper endpoint and both bounds are not closed', function () {
expect(function () { return index_1.MathInterval.interval(100, true, 100, false); }).toThrowError();
});
it('should not throw error if lower equals to upper endpoint but both bounds are closed', function () {
expect(function () { return index_1.MathInterval.interval(100, true, 100, true); }).not.toThrowError();
});
});
describe('isConnected', function () {
it('should be connected both ways', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(50, false, 150, false);
expect(a.isConnected(b)).toBe(true);
expect(b.isConnected(a)).toBe(true);
});
it('should not be connected both ways', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(150, false, 250, false);
expect(a.isConnected(b)).toBe(false);
expect(b.isConnected(a)).toBe(false);
});
it('should be connected if includes other', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(25, false, 75, false);
expect(a.isConnected(b)).toBe(true);
});
it('should not be connected even if connection set empty', function () {
var a = index_1.MathInterval.interval(0, true, 50, false);
var b = index_1.MathInterval.interval(50, true, 75, false);
expect(a.isConnected(b)).toBe(false);
});
it('should be connected if connection set singleton', function () {
var a = index_1.MathInterval.interval(0, true, 50, true);
var b = index_1.MathInterval.interval(50, true, 75, false);
expect(a.isConnected(b)).toBe(true);
});
it('should not be connected if connection set is invalid', function () {
var a = index_1.MathInterval.interval(0, true, 50, false);
var b = index_1.MathInterval.interval(50, false, 75, false);
expect(a.isConnected(b)).toBe(false);
});
});
describe('span', function () {
it('should return span for connected', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(50, false, 150, false);
var e = index_1.MathInterval.interval(0, false, 150, false);
expect(a.span(b).equals(e)).toBe(true);
});
it('should return enclosing as span', function () {
var a = index_1.MathInterval.interval(0, true, 100, false);
var b = index_1.MathInterval.interval(25, false, 50, true);
expect(a.span(b).equals(a)).toBe(true);
});
it('should return span for disconnected', function () {
var a = index_1.MathInterval.interval(0, false, 25, false);
var b = index_1.MathInterval.interval(75, false, 100, true);
var e = index_1.MathInterval.interval(0, false, 100, true);
expect(a.span(b).equals(e)).toBe(true);
});
it('should return span for closely disconnected', function () {
var a = index_1.MathInterval.interval(0, true, 25, true);
var b = index_1.MathInterval.interval(25, false, 100, false);
var e = index_1.MathInterval.interval(0, true, 100, false);
expect(a.span(b).equals(e)).toBe(true);
});
});
describe('intersection', function () {
it('should return intersection for connected', function () {
var a = index_1.MathInterval.interval(0, false, 100, true);
var b = index_1.MathInterval.interval(50, false, 150, false);
var e = index_1.MathInterval.interval(50, false, 100, true);
expect(a.intersection(b).equals(e)).toBe(true);
});
it('should return enclosed as intersection', function () {
var a = index_1.MathInterval.interval(0, true, 100, false);
var b = index_1.MathInterval.interval(25, false, 50, true);
expect(a.intersection(b).equals(b)).toBe(true);
});
it('should not return intersection for disconnected', function () {
var a = index_1.MathInterval.interval(0, false, 25, false);
var b = index_1.MathInterval.interval(75, false, 100, true);
var e = index_1.MathInterval.interval(0, false, 100, true);
expect(function () { return a.intersection(b); }).toThrowError();
});
it('should not return intersection for closely disconnected', function () {
var a = index_1.MathInterval.interval(0, true, 25, true);
var b = index_1.MathInterval.interval(25, false, 100, false);
var e = index_1.MathInterval.interval(0, true, 100, false);
expect(function () { return a.intersection(b); }).toThrowError();
});
});
describe('encloses', function () {
it('should enclose', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(25, false, 75, false);
expect(a.encloses(b)).toBe(true);
});
it('should not enclose', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(150, false, 250, false);
expect(a.encloses(b)).toBe(false);
});
it('should not enclose if connected', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(50, false, 150, false);
expect(a.encloses(b)).toBe(false);
});
it('should enclose if same and all bounds are closed', function () {
var a = index_1.MathInterval.interval(0, true, 100, true);
var b = index_1.MathInterval.interval(0, true, 100, true);
expect(a.encloses(b)).toBe(true);
});
it('should enclose if same and all bounds are not closed', function () {
var a = index_1.MathInterval.interval(0, true, 100, false);
var b = index_1.MathInterval.interval(0, true, 100, false);
expect(a.encloses(b)).toBe(true);
});
it('should not enclose if endpoints are same but enclosing is open and enclosed closed', function () {
var a = index_1.MathInterval.interval(0, true, 100, false);
var b = index_1.MathInterval.interval(0, true, 100, true);
expect(a.encloses(b)).toBe(false);
});
it('should enclose if endpoints are same but enclosing is open and enclosed closed', function () {
var a = index_1.MathInterval.interval(0, true, 100, true);
var b = index_1.MathInterval.interval(0, true, 100, false);
expect(a.encloses(b)).toBe(true);
});
it('should enclose if singleton', function () {
var a = index_1.MathInterval.interval(0, false, 100, true);
var b = index_1.MathInterval.interval(100, true, 100, true);
expect(a.encloses(b)).toBe(true);
});
it('should enclose if on the edge', function () {
var a = index_1.MathInterval.interval(0, false, 100, true);
var b = index_1.MathInterval.interval(50, true, 100, true);
expect(a.encloses(b)).toBe(true);
});
it('should not enclose if enclosing open and enclosed close', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(50, true, 100, true);
expect(a.encloses(b)).toBe(false);
});
});
describe('equals', function () {
it('should be equal closed', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(0, false, 100, false);
expect(a.equals(b)).toBe(true);
});
it('should be equal open closed', function () {
var a = index_1.MathInterval.interval(0, true, 100, false);
var b = index_1.MathInterval.interval(0, true, 100, false);
expect(a.equals(b)).toBe(true);
});
it('should not be equal if bounds are not same type', function () {
var a = index_1.MathInterval.interval(0, false, 100, false);
var b = index_1.MathInterval.interval(0, true, 100, false);
expect(a.equals(b)).toBe(false);
});
it('should not be equal if endpoints are not same', function () {
var a = index_1.MathInterval.interval(50, true, 100, true);
var b = index_1.MathInterval.interval(0, true, 100, true);
expect(a.equals(b)).toBe(false);
});
});
describe('open', function () {
var interval = index_1.MathInterval.open(0, 100);
it('should not contain lower endpoint', function () {
expect(interval.contains(0)).toBe(false);
});
it('should not contain upper endpoint', function () {
expect(interval.contains(100)).toBe(false);
});
it('should contain middle', function () {
expect(interval.contains(50)).toBe(true);
});
it('should not contain less than lower endpoint', function () {
expect(interval.contains(-1)).toBe(false);
});
it('should not contain greater than upper endpoint', function () {
expect(interval.contains(101)).toBe(false);
});
it('toString', function () {
expect(interval.toString()).toBe('(0, 100)');
});
});
describe('closed', function () {
var interval = index_1.MathInterval.closed(0, 100);
it('should contain lower endpoint', function () {
expect(interval.contains(0)).toBe(true);
});
it('should contain upper endpoint', function () {
expect(interval.contains(100)).toBe(true);
});
it('should contain middle', function () {
expect(interval.contains(50)).toBe(true);
});
it('should not contain less than lower endpoint', function () {
expect(interval.contains(-1)).toBe(false);
});
it('should not contain greater than upper endpoint', function () {
expect(interval.contains(101)).toBe(false);
});
it('toString', function () {
expect(interval.toString()).toBe('[0, 100]');
});
});
describe('closedOpen', function () {
var interval = index_1.MathInterval.closedOpen(0, 100);
it('should contain lower endpoint', function () {
expect(interval.contains(0)).toBe(true);
});
it('should not contain upper endpoint', function () {
expect(interval.contains(100)).toBe(false);
});
it('should contain middle', function () {
expect(interval.contains(50)).toBe(true);
});
it('should not contain less than lower endpoint', function () {
expect(interval.contains(-1)).toBe(false);
});
it('should not contain greater than upper endpoint', function () {
expect(interval.contains(101)).toBe(false);
});
it('toString', function () {
expect(interval.toString()).toBe('[0, 100)');
});
});
describe('openClosed', function () {
var interval = index_1.MathInterval.openClosed(0, 100);
it('should not contain lower endpoint', function () {
expect(interval.contains(0)).toBe(false);
});
it('should contain upper endpoint', function () {
expect(interval.contains(100)).toBe(true);
});
it('should contain middle', function () {
expect(interval.contains(50)).toBe(true);
});
it('should not contain less than lower endpoint', function () {
expect(interval.contains(-1)).toBe(false);
});
it('should not contain greater than upper endpoint', function () {
expect(interval.contains(101)).toBe(false);
});
it('toString', function () {
expect(interval.toString()).toBe('(0, 100]');
});
});
describe('greaterThan', function () {
var interval = index_1.MathInterval.greaterThan(100);
it('should not contain less than limit', function () {
expect(interval.contains(99)).toBe(false);
});
it('should not contain limit', function () {
expect(interval.contains(100)).toBe(false);
});
it('should contain greater than limit', function () {
expect(interval.contains(101)).toBe(true);
});
it('toString', function () {
expect(interval.toString()).toBe('(100, +∞)');
});
});
describe('atLeast', function () {
var interval = index_1.MathInterval.atLeast(100);
it('should not contain less than limit', function () {
expect(interval.contains(99)).toBe(false);
});
it('should contain limit', function () {
expect(interval.contains(100)).toBe(true);
});
it('should contain greater than limit', function () {
expect(interval.contains(101)).toBe(true);
});
it('toString', function () {
expect(interval.toString()).toBe('[100, +∞)');
});
});
describe('lessThan', function () {
var interval = index_1.MathInterval.lessThan(100);
it('should contain less than limit', function () {
expect(interval.contains(99)).toBe(true);
});
it('should not contain limit', function () {
expect(interval.contains(100)).toBe(false);
});
it('should not contain greater than limit', function () {
expect(interval.contains(101)).toBe(false);
});
it('toString', function () {
expect(interval.toString()).toBe('(-∞, 100)');
});
});
describe('atMost', function () {
var interval = index_1.MathInterval.atMost(100);
it('should contain less than limit', function () {
expect(interval.contains(99)).toBe(true);
});
it('should contain limit', function () {
expect(interval.contains(100)).toBe(true);
});
it('should not contain greater than limit', function () {
expect(interval.contains(101)).toBe(false);
});
it('toString', function () {
expect(interval.toString()).toBe('(-∞, 100]');
});
});
describe('all', function () {
var interval = index_1.MathInterval.all();
it('should not contain Infinity', function () {
expect(interval.contains(Infinity)).toBe(false);
});
it('should not contain -Inifinity', function () {
expect(interval.contains(-Infinity)).toBe(false);
});
it('should contain 0', function () {
expect(interval.contains(0)).toBe(true);
});
it('toString', function () {
expect(interval.toString()).toBe('(-∞, +∞)');
});
});
});
//# sourceMappingURL=/home/travis/build/harunurhan/math-interval/src/index.spec.js.map