|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import * as fc from 'fast-check'; |
| 3 | +import { doubleToIndex, indexToDouble } from '../../../../../src/arbitrary/_internals/helpers/DoubleHelpers'; |
| 4 | +import { |
| 5 | + maxNonIntegerValue, |
| 6 | + onlyIntegersAfterThisValue, |
| 7 | + refineConstraintsForDoubleOnly, |
| 8 | +} from '../../../../../src/arbitrary/_internals/helpers/DoubleOnlyHelpers'; |
| 9 | +import { add64 } from '../../../../../src/arbitrary/_internals/helpers/ArrayInt64'; |
| 10 | + |
| 11 | +describe('maxNonIntegerValue', () => { |
| 12 | + it('should be immediately followed by an integer', () => { |
| 13 | + // Arrange / Act |
| 14 | + const next = nextDouble(maxNonIntegerValue); |
| 15 | + |
| 16 | + // Assert |
| 17 | + expect(Number.isInteger(next)).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should be followed by a number immediatelly followed by an integer', () => { |
| 21 | + // Arrange / Act |
| 22 | + const next = nextDouble(maxNonIntegerValue); |
| 23 | + const nextNext = nextDouble(next); |
| 24 | + |
| 25 | + // Assert |
| 26 | + expect(Number.isInteger(nextNext)).toBe(true); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should be immediately followed by onlyIntegersAfterThisValue', () => { |
| 30 | + // Arrange / Act / Assert |
| 31 | + expect(nextDouble(maxNonIntegerValue)).toBe(onlyIntegersAfterThisValue); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('refineConstraintsForDoubleOnly', () => { |
| 36 | + describe('no excluded', () => { |
| 37 | + it('should properly refine default constraints', () => { |
| 38 | + // Arrange / Act / Assert |
| 39 | + expect(refineConstraintsForDoubleOnly({})).toEqual({ |
| 40 | + minExcluded: false, |
| 41 | + min: -onlyIntegersAfterThisValue, // min included, but its value will be replaced by -inf in mapper |
| 42 | + maxExcluded: false, |
| 43 | + max: onlyIntegersAfterThisValue, // max included, but its value will be replaced by +inf in mapper |
| 44 | + noDefaultInfinity: false, |
| 45 | + noNaN: false, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should properly refine when constraints reject infinities', () => { |
| 50 | + // Arrange / Act / Assert |
| 51 | + expect(refineConstraintsForDoubleOnly({ noDefaultInfinity: true })).toEqual({ |
| 52 | + minExcluded: false, |
| 53 | + min: -maxNonIntegerValue, |
| 54 | + maxExcluded: false, |
| 55 | + max: maxNonIntegerValue, |
| 56 | + noDefaultInfinity: false, |
| 57 | + noNaN: false, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should properly refine when constraints ask for onlyIntegersAfterThisValue or above (excluding infinite)', () => { |
| 62 | + fc.assert( |
| 63 | + fc.property( |
| 64 | + fc.double({ noDefaultInfinity: true, noNaN: true, min: onlyIntegersAfterThisValue }), |
| 65 | + (boundary) => { |
| 66 | + // Arrange / Act / Assert |
| 67 | + expect(refineConstraintsForDoubleOnly({ min: -boundary, max: boundary })).toEqual({ |
| 68 | + minExcluded: false, |
| 69 | + min: -maxNonIntegerValue, // min has been adapted to better fit the float range |
| 70 | + maxExcluded: false, |
| 71 | + max: maxNonIntegerValue, // max has been adapted to better fit the float range |
| 72 | + noDefaultInfinity: false, |
| 73 | + noNaN: false, |
| 74 | + }); |
| 75 | + }, |
| 76 | + ), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should properly refine when constraints ask for maxNonIntegerValue or below', () => { |
| 81 | + fc.assert( |
| 82 | + fc.property(fc.double({ noNaN: true, min: 1, max: maxNonIntegerValue }), (boundary) => { |
| 83 | + // Arrange / Act / Assert |
| 84 | + expect(refineConstraintsForDoubleOnly({ min: -boundary, max: boundary })).toEqual({ |
| 85 | + minExcluded: false, |
| 86 | + min: -boundary, // min was already in the accepted range |
| 87 | + maxExcluded: false, |
| 88 | + max: boundary, // max was already in the accepted range |
| 89 | + noDefaultInfinity: false, |
| 90 | + noNaN: false, |
| 91 | + }); |
| 92 | + }), |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('with excluded', () => { |
| 98 | + const excluded = { minExcluded: true, maxExcluded: true }; |
| 99 | + |
| 100 | + it('should properly refine default constraints', () => { |
| 101 | + // Arrange / Act / Assert |
| 102 | + expect(refineConstraintsForDoubleOnly({ ...excluded })).toEqual({ |
| 103 | + minExcluded: true, |
| 104 | + min: -onlyIntegersAfterThisValue, // min excluded so it only starts at -maxNonIntegerValue |
| 105 | + maxExcluded: true, |
| 106 | + max: onlyIntegersAfterThisValue, /// min excluded so it only starts at -maxNonIntegerValue |
| 107 | + noDefaultInfinity: false, |
| 108 | + noNaN: false, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should properly refine when constraints reject infinities', () => { |
| 113 | + // Arrange / Act / Assert |
| 114 | + expect(refineConstraintsForDoubleOnly({ ...excluded, noDefaultInfinity: true })).toEqual({ |
| 115 | + minExcluded: true, |
| 116 | + min: -onlyIntegersAfterThisValue, // min excluded so it only starts at -maxNonIntegerValue |
| 117 | + maxExcluded: true, |
| 118 | + max: onlyIntegersAfterThisValue, // min excluded so it only starts at -maxNonIntegerValue |
| 119 | + noDefaultInfinity: false, |
| 120 | + noNaN: false, |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should properly refine when constraints ask for onlyIntegersAfterThisValue or above (excluding infinite)', () => { |
| 125 | + fc.assert( |
| 126 | + fc.property( |
| 127 | + fc.double({ noDefaultInfinity: true, noNaN: true, min: onlyIntegersAfterThisValue }), |
| 128 | + (boundary) => { |
| 129 | + // Arrange / Act / Assert |
| 130 | + expect(refineConstraintsForDoubleOnly({ ...excluded, min: -boundary, max: boundary })).toEqual({ |
| 131 | + minExcluded: true, |
| 132 | + min: -onlyIntegersAfterThisValue, // min has been adapted to better fit the float range, values only starts at -maxNonIntegerValue |
| 133 | + maxExcluded: true, |
| 134 | + max: onlyIntegersAfterThisValue, // max has been adapted to better fit the float range, values only starts at maxNonIntegerValue |
| 135 | + noDefaultInfinity: false, |
| 136 | + noNaN: false, |
| 137 | + }); |
| 138 | + }, |
| 139 | + ), |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should properly refine when constraints ask for maxNonIntegerValue or below', () => { |
| 144 | + fc.assert( |
| 145 | + fc.property(fc.double({ noNaN: true, min: 1, max: maxNonIntegerValue }), (boundary) => { |
| 146 | + // Arrange / Act / Assert |
| 147 | + expect(refineConstraintsForDoubleOnly({ ...excluded, min: -boundary, max: boundary })).toEqual({ |
| 148 | + minExcluded: true, |
| 149 | + min: -boundary, // min was already in the accepted range |
| 150 | + maxExcluded: true, |
| 151 | + max: boundary, // max was already in the accepted range |
| 152 | + noDefaultInfinity: false, |
| 153 | + noNaN: false, |
| 154 | + }); |
| 155 | + }), |
| 156 | + ); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +// Helpers |
| 162 | + |
| 163 | +function nextDouble(value: number): number { |
| 164 | + const index = doubleToIndex(value); |
| 165 | + const nextIndex = add64(index, { sign: 1, data: [0, 1] }); |
| 166 | + return indexToDouble(nextIndex); |
| 167 | +} |
0 commit comments