Best JavaScript code snippet using fast-check-monorepo
DoubleHelpers.spec.ts
Source:DoubleHelpers.spec.ts
1import * as fc from 'fast-check';2import { float64raw, isStrictlySmaller } from '../../__test-helpers__/FloatingPointHelpers';3import {4 decomposeDouble,5 doubleToIndex,6 indexToDouble,7} from '../../../../../src/arbitrary/_internals/helpers/DoubleHelpers';8type Index = ReturnType<typeof doubleToIndex>;9const toIndex = (raw: bigint | string): Index => {10 const b = typeof raw === 'string' ? BigInt(raw) : raw;11 const pb = b < BigInt(0) ? -b : b;12 return { sign: b < BigInt(0) ? -1 : 1, data: [Number(pb >> BigInt(32)), Number(pb & BigInt(0xffffffff))] };13};14const toBigInt = (index: Index): bigint => {15 return BigInt(index.sign) * ((BigInt(index.data[0]) << BigInt(32)) + BigInt(index.data[1]));16};17describe('decomposeDouble', () => {18 it('should properly decompose basic values', () => {19 expect(decomposeDouble(0)).toEqual({ exponent: -1022, significand: 0 });20 expect(decomposeDouble(1)).toEqual({ exponent: 0, significand: 1 });21 expect(decomposeDouble(128)).toEqual({ exponent: 7, significand: 1 });22 expect(decomposeDouble(201)).toEqual({ exponent: 7, significand: 1.5703125 });23 });24 it('should properly decompose negative values', () => {25 expect(decomposeDouble(-0)).toEqual({ exponent: -1022, significand: -0 });26 expect(decomposeDouble(-1)).toEqual({ exponent: 0, significand: -1 });27 });28 it('should properly decompose extreme values', () => {29 expect(decomposeDouble(Number.MAX_VALUE)).toEqual({ exponent: 1023, significand: 2 - Number.EPSILON });30 expect(decomposeDouble(Number.MIN_VALUE)).toEqual({ exponent: -1022, significand: Number.EPSILON });31 expect(decomposeDouble(Number.EPSILON)).toEqual({ exponent: -52, significand: 1 });32 expect(decomposeDouble(1 + Number.EPSILON)).toEqual({ exponent: 0, significand: 1 + Number.EPSILON });33 });34 it('should decompose a 64-bit float into its equivalent (significand, exponent)', () => {35 fc.assert(36 fc.property(float64raw(), (f64) => {37 // Arrange38 fc.pre(!Number.isNaN(f64));39 // Act40 const { exponent, significand } = decomposeDouble(f64);41 // Assert42 expect(significand * 2 ** exponent).toBe(f64);43 })44 );45 });46});47describe('doubleToIndex', () => {48 it('should always produce well-formed indexes', () => {49 fc.assert(50 fc.property(float64raw(), (d) => {51 // Arrange52 fc.pre(!Number.isNaN(d));53 // Act54 const index = doubleToIndex(d);55 // Assert56 expect(index.data[0]).toBeGreaterThanOrEqual(0);57 expect(index.data[0]).toBeLessThanOrEqual(0xffffffff);58 expect(Number.isInteger(index.data[0])).toBe(true);59 expect(index.data[1]).toBeGreaterThanOrEqual(0);60 expect(index.data[1]).toBeLessThanOrEqual(0xffffffff);61 expect(Number.isInteger(index.data[1])).toBe(true);62 })63 );64 });65 if (typeof BigInt === 'undefined') {66 it('no test', () => {67 expect(true).toBe(true);68 });69 return;70 } // Following tests require BigInt to be launched71 it('should properly compute indexes', () => {72 expect(doubleToIndex(0)).toEqual(toIndex('0'));73 expect(doubleToIndex(Number.MIN_VALUE)).toEqual(toIndex('1'));74 expect(doubleToIndex(2 * Number.MIN_VALUE)).toEqual(toIndex('2'));75 expect(doubleToIndex(3 * Number.MIN_VALUE)).toEqual(toIndex('3'));76 // Last double with minimal exponent, ie -102277 // index(last with min exponent) = 2**53 - 178 expect(doubleToIndex(2 ** -1022 * (2 - Number.EPSILON))).toEqual(toIndex('9007199254740991'));79 // First double without minimal exponent, ie -102280 // index(first without min exponent) = index(last with min exponent) + 181 expect(doubleToIndex(2 ** -1021)).toEqual(toIndex('9007199254740992'));82 // Number.EPSILON === 1. * 2**-52 --> m = 1, e = -5283 // index(Number.EPSILON) = 2**53 + (-52 - (-1022) -1) * 2**5284 expect(doubleToIndex(Number.EPSILON)).toEqual(toIndex('4372995238176751616'));85 // index(1 - Number.EPSILON / 2) = index(1) - 186 expect(doubleToIndex(1 - Number.EPSILON / 2)).toEqual(toIndex('4607182418800017407'));87 // 1 === 1. * 2**0 --> m = 1, e = 088 // index(1) = 2**53 + (0 - (-1022) -1) * 2**5289 expect(doubleToIndex(1)).toEqual(toIndex('4607182418800017408'));90 // index(1 + Number.EPSILON) = index(1) + 191 expect(doubleToIndex(1 + Number.EPSILON)).toEqual(toIndex('4607182418800017409'));92 // index(2 - Number.EPSILON) = index(2) - 1 = index(1 + (2 ** 52 - 1) * Number.EPSILON)93 expect(doubleToIndex(2 - Number.EPSILON)).toEqual(toIndex('4611686018427387903'));94 // 1 === 1. * 2**1 --> m = 1, e = 195 // index(2) = index(1) + 2**5296 expect(doubleToIndex(2)).toEqual(toIndex('4611686018427387904'));97 // Number.MAX_VALUE === (1 + (2**52-1)/2**52) * 2**1023 --> m = 1 + (2**52-1)/2**52, e = 102398 // index(Number.MAX_VALUE) = index(next(Number.MAX_VALUE)) -1 = 2**53 + (1024 - (-1022) -1) * 2**52 -199 expect(doubleToIndex(Number.MAX_VALUE)).toEqual(toIndex('9218868437227405311'));100 });101 it('should properly compute negative indexes', () => {102 expect(doubleToIndex(-0)).toEqual(toIndex('-1'));103 expect(doubleToIndex(-Number.MIN_VALUE)).toEqual(toIndex('-2'));104 expect(doubleToIndex(-Number.MAX_VALUE)).toEqual(toIndex('-9218868437227405312'));105 });106 it('should properly compute indexes for infinity', () => {107 expect(doubleToIndex(Number.NEGATIVE_INFINITY)).toEqual(108 toIndex(toBigInt(doubleToIndex(-Number.MAX_VALUE)) - BigInt(1))109 );110 expect(doubleToIndex(Number.POSITIVE_INFINITY)).toEqual(111 toIndex(toBigInt(doubleToIndex(Number.MAX_VALUE)) + BigInt(1))112 );113 });114 it('should be able to infer index for negative double from the positive one', () => {115 fc.assert(116 fc.property(float64raw(), (d) => {117 // Arrange118 fc.pre(!Number.isNaN(d));119 const posD = d > 0 || 1 / d > 0 ? d : -d;120 // Act121 const bigIntIndexPos = toBigInt(doubleToIndex(posD));122 const bigIntIndexNeg = toBigInt(doubleToIndex(-posD));123 // Assert124 expect(bigIntIndexNeg).toEqual(-bigIntIndexPos - BigInt(1));125 })126 );127 });128 it('should return index +1 for the successor of a given double', () => {129 fc.assert(130 fc.property(131 fc.integer({ min: -1022, max: +1023 }),132 fc.integer({ min: 0, max: 2 ** 53 - 1 }),133 (exponent, rescaledSignificand) => {134 // Arrange135 fc.pre(exponent === -1022 || rescaledSignificand >= 2 ** 52); // valid136 fc.pre(exponent !== 1023 || rescaledSignificand !== 2 ** 53 - 1); // not max137 const current = rescaledSignificand * Number.EPSILON * 2 ** exponent;138 const next = (rescaledSignificand + 1) * Number.EPSILON * 2 ** exponent;139 // Act140 const bigIntIndexCurrent = toBigInt(doubleToIndex(current));141 const bigIntIndexNext = toBigInt(doubleToIndex(next));142 // Assert143 expect(bigIntIndexNext).toEqual(bigIntIndexCurrent + BigInt(1));144 }145 )146 );147 });148 it('should preserve ordering between two doubles', () => {149 fc.assert(150 fc.property(float64raw(), float64raw(), (fa64, fb64) => {151 // Arrange152 fc.pre(!Number.isNaN(fa64) && !Number.isNaN(fb64));153 // Act / Assert154 if (isStrictlySmaller(fa64, fb64)) {155 expect(toBigInt(doubleToIndex(fa64))).toBeLessThan(toBigInt(doubleToIndex(fb64)));156 } else {157 expect(toBigInt(doubleToIndex(fa64))).toBeGreaterThanOrEqual(toBigInt(doubleToIndex(fb64)));158 }159 })160 );161 });162});163describe('indexToDouble', () => {164 it('Should reverse doubleToIndex', () =>165 fc.assert(166 fc.property(float64raw(), (f64) => {167 fc.pre(!Number.isNaN(f64));168 expect(indexToDouble(doubleToIndex(f64))).toBe(f64);169 })170 ));171 if (typeof BigInt === 'undefined') {172 it('no test', () => {173 expect(true).toBe(true);174 });175 return;176 } // Following tests require BigInt to be launched177 it('should properly find doubles corresponding to well-known values', () => {178 expect(indexToDouble(toIndex('-9218868437227405313'))).toBe(Number.NEGATIVE_INFINITY);179 expect(indexToDouble(toIndex('-9218868437227405312'))).toBe(-Number.MAX_VALUE);180 expect(indexToDouble(toIndex('-1'))).toBe(-0);181 expect(indexToDouble(toIndex('0'))).toBe(0);182 expect(indexToDouble(toIndex('4372995238176751616'))).toBe(Number.EPSILON);183 expect(indexToDouble(toIndex('9218868437227405311'))).toBe(Number.MAX_VALUE);184 expect(indexToDouble(toIndex('9218868437227405312'))).toBe(Number.POSITIVE_INFINITY);185 });186 it('should be reversed by doubleToIndex', () => {187 fc.assert(188 fc.property(189 fc.bigInt({ min: BigInt('-9218868437227405313'), max: BigInt('9218868437227405312') }),190 (bigIntIndex) => {191 // The test below checks that indexToDouble(doubleToIndex) is identity192 // It does not confirm that doubleToIndex(indexToDouble)) is identity193 // Arrange194 const index = toIndex(bigIntIndex);195 // Act / Assert196 expect(doubleToIndex(indexToDouble(index))).toEqual(index);197 }198 )199 );200 });...
Using AI Code Generation
1const fc = require('fast-check');2const bigIntIndexCurrent = require('fast-check-monorepo').bigIntIndexCurrent;3const bigIntIndex = require('fast-check-monorepo').bigIntIndex;4const bigIntIndexOld = require('fast-check-monorepo').bigIntIndexOld;5fc.assert(6 fc.property(7 fc.bigInt({min: 0n, max: 1000000000000000000000000000n}),8 fc.bigInt({min: 0n, max: 1000000000000000000000000000n}),9 fc.bigInt({min: 0n, max: 1000000000000000000000000000n}),10 (a, b, c) => {11 return bigIntIndexCurrent(a, b, c) === bigIntIndexOld(a, b, c);12 }13);14const fc = require('fast-check');15const bigIntIndexCurrent = require('fast-check-monorepo').bigIntIndexCurrent;16const bigIntIndex = require('fast-check-monorepo').bigIntIndex;17const bigIntIndexOld = require('fast-check-monorepo').bigIntIndexOld;18fc.assert(19 fc.property(20 fc.bigInt({min: 0n, max: 1000000000000000000000000000n}),21 fc.bigInt({min: 0n, max: 1000000000000000000000000000n}),22 fc.bigInt({min: 0n, max: 1000000000000000000000000000n}),23 (a, b, c) => {24 return bigIntIndexCurrent(a, b, c) === bigIntIndexOld(a, b, c);25 }26);27const fc = require('fast-check');28const bigIntIndexCurrent = require('fast-check-monorepo').bigIntIndexCurrent;29const bigIntIndex = require('fast-check-monorepo').bigIntIndex;30const bigIntIndexOld = require('fast-check-monorepo').bigIntIndexOld;31fc.assert(32 fc.property(33 fc.bigInt({min: 0n, max
Using AI Code Generation
1import bigIntIndexCurrent from 'fast-check';2const bigIntIndex = bigIntIndexCurrent.bigIntIndex;3const bigIntIndexCurrent = bigIntIndexCurrent.bigIntIndexCurrent;4const bigIntIndex = require('fast-check').bigIntIndex;5const bigIntIndexCurrent = require('fast-check').bigIntIndexCurrent;6const bigIntIndex = require('fast-check-monorepo').bigIntIndex;7const bigIntIndexCurrent = require('fast-check-monorepo').bigIntIndexCurrent;8const arb = bigIntIndex();9const arbCurrent = bigIntIndexCurrent();10function bigIntIndexCurrent(): Arbitrary<bigint>;11import * as fc from 'fast-check';12fc.assert(13 fc.property(fc.bigIntArray(), fc.bigIntIndexCurrent(), (arr, idx) => {14 return idx >= 0 && idx < BigInt(arr.length);15 })16);17function bigUint(): Arbitrary<bigint>;18import * as fc from 'fast-check';19fc.assert(20 fc.property(fc.bigUint(), (n) => {21 return n >= 0 && n <= Number.MAX_SAFE_INTEGER;22 })23);24function bigUintN(n:
Using AI Code Generation
1const fc = require('fast-check');2const bigIntIndexCurrent = require('fast-check/lib/fast-check-default').bigIntIndexCurrent;3const bigIntIndex = require('fast-check/lib/fast-check-default').bigIntIndex;4const bigIntIndexOld = require('fast-check/lib/fast-check-default').bigIntIndexOld;5const bigIntIndexNew = require('fast-check/lib/fast-check-default').bigIntIndexNew;6const bigIntIndexOld2 = require('fast-check/lib/fast-check-default').bigIntIndexOld2;7const bigIntIndexOld3 = require('fast-check/lib/fast-check-default').bigIntIndexOld3;8const bigIntIndexOld4 = require('fast-check/lib/fast-check-default').bigIntIndexOld4;9const bigIntIndexOld5 = require('fast-check/lib/fast-check-default').bigIntIndexOld5;10const bigIntIndexOld6 = require('fast-check/lib/fast-check-default').bigIntIndexOld6;11const bigIntIndexOld7 = require('fast-check/lib/fast-check-default').bigIntIndexOld7;12const bigIntIndexOld8 = require('fast-check/lib/fast-check-default').bigIntIndexOld8;13const bigIntIndexOld9 = require('fast-check/lib/fast-check-default').bigIntIndexOld9;14const bigIntIndexOld10 = require('fast-check/lib/fast-check-default').bigIntIndexOld10;15const bigIntIndexOld11 = require('fast-check/lib/fast-check-default').bigIntIndexOld11;16const bigIntIndexOld12 = require('fast-check/lib/fast-check-default').bigIntIndexOld12;17const bigIntIndexOld13 = require('fast-check/lib/fast-check-default').bigIntIndexOld13;18const bigIntIndexOld14 = require('fast-check/lib/fast-check-default').bigIntIndexOld14;19const bigIntIndexOld15 = require('fast-check/lib/fast-check-default').bigIntIndexOld15;20const bigIntIndexOld16 = require('fast-check/lib/fast-check-default').bigIntIndexOld16;21const bigIntIndexOld17 = require('fast-check/lib/fast-check-default').bigIntIndexOld17;22const bigIntIndexOld18 = require('fast-check/lib/fast-check-default').bigIntIndexOld18;
Using AI Code Generation
1const {bigIntIndexCurrent} = require('fast-check');2const {bigInt} = require('fast-check');3const {property} = require('jest-fast-check');4property(5 bigIntIndexCurrent(),6 bigInt(),7 (index, value) => {8 expect(index).toBeGreaterThanOrEqual(0);9 expect(value).toBeGreaterThanOrEqual(0);10 expect(index).toBeLessThanOrEqual(value);11 },12).check();
Using AI Code Generation
1const { bigIntIndexCurrent } = require('fast-check');2const index = bigIntIndexCurrent(0n, 100n);3console.log(index);4const { bigIntIndexCurrent } = require('fast-check');5const index = bigIntIndexCurrent(0n, 100n);6console.log(index);
Using AI Code Generation
1const fc = require('fast-check');2const { bigIntIndexCurrent } = require('fast-check/lib/arbitrary/IntegerArbitrary.js');3const { bigIntIndex } = fc.integer();4const { bigInt } = fc;5const { bigIntN } = fc.nat();6const a = bigIntIndexCurrent(bigIntN, bigIntIndex, bigInt);7console.log(a);8console.log(a.generate(0));9const fc = require('fast-check');10const { bigIntIndexCurrent } = require('fast-check/lib/arbitrary/IntegerArbitrary.js');11const { bigIntIndex } = fc.integer();12const { bigInt } = fc;13const { bigIntN } = fc.nat();14const a = bigIntIndexCurrent(bigIntN, bigIntIndex, bigInt);15console.log(a);16console.log(a.generate(0));17const fc = require('fast-check-monorepo');18const { bigIntIndexCurrent } = require('fast-check-monorepo/lib/arbitrary/IntegerArbitrary.js');19const { bigIntIndex } = fc.integer();20const { bigInt } = fc;21const { bigIntN } = fc.nat();22const a = bigIntIndexCurrent(bigIntN, bigIntIndex, bigInt);23console.log(a);24console.log(a.generate(0));25const fc = require('fast-check-monorepo');26const { bigIntIndexCurrent } = require('fast-check-monorepo/lib/arbitrary/IntegerArbitrary.js');27const { bigIntIndex } = fc.integer();28const { bigInt } = fc;29const { bigIntN } = fc.nat();30const a = bigIntIndexCurrent(bigIntN, bigIntIndex, bigInt);31console.log(a);32console.log(a.generate(0));33const fc = require('fast-check-monorepo');34const { bigIntIndexCurrent } = require('fast-check-monorepo/lib/arbitrary/IntegerArbitrary.js');35const { bigIntIndex } = fc.integer();36const { bigInt } = fc;37const { bigIntN } = fc.nat();38const a = bigIntIndexCurrent(bigIntN, bigInt
Using AI Code Generation
1const {bigIntIndexCurrent} = require('fast-check');2const {bigInt} = require('big-integer');3const {integer} = require('fast-check');4const {bigIntIndexCurrent} = require('fast-check');5const {bigInt} = require('big-integer');6const {integer} = require('fast-check');7const {bigIntIndexCurrent} = require('fast-check');8const {bigInt} = require('big-integer');9const {integer} = require('fast-check');10const {bigIntIndexCurrent} = require('fast-check');11const {bigInt} = require('big-integer');12const {integer} = require('fast-check');13const {bigIntIndexCurrent} = require('fast-check');14const {bigInt} = require('big-integer');15const {integer} = require('fast-check');16const {bigIntIndexCurrent} = require('fast-check');17const {bigInt} = require('big-integer');18const {integer} = require('fast-check');19const {bigIntIndexCurrent} = require('fast-check');20const {bigInt} = require('big-integer');21const {integer} = require('fast-check');22const {bigIntIndexCurrent} = require('fast-check');23const {bigInt} = require('big-integer');24const {integer} = require('fast-check');25const {bigIntIndexCurrent} = require('fast-check');26const {bigInt} = require('big-integer');27const {integer} = require('fast-check');28const {bigIntIndexCurrent} = require('fast-check');29const {bigInt} = require('big-integer');30const {integer} = require('fast-check');31const {bigIntIndexCurrent} = require('fast-check');32const {bigInt} = require('big-integer');33const {integer} = require('fast-check');34const {bigIntIndexCurrent} = require('fast-check');35const {bigInt} = require('big-integer');36const {integer} = require('fast-check');37const {bigIntIndexCurrent} = require('fast-check');38const {bigInt} = require('big-integer');39const {integer} = require('fast-check');40const {bigIntIndexCurrent} = require('fast-check');41const {bigInt} = require('big-integer');42const {integer} = require('fast-check');43const {bigIntIndexCurrent} =
Using AI Code Generation
1const bigIntIndexCurrent = require('fast-check').bigIntIndexCurrent;2const bigIntIndex = bigIntIndexCurrent();3const bigIntIndexNext = require('fast-check').bigIntIndexNext;4const bigIntIndex = bigIntIndexNext();5const bigIntIndexPrevious = require('fast-check').bigIntIndexPrevious;6const bigIntIndex = bigIntIndexPrevious();7const bigIntIndexNext = require('fast-check').bigIntIndexNext;8const bigIntIndex = bigIntIndexNext();9const bigIntIndexCurrent = require('fast-check').bigIntIndexCurrent;10const bigIntIndex = bigIntIndexCurrent();11const bigIntIndexPrevious = require('fast-check').bigIntIndexPrevious;12const bigIntIndex = bigIntIndexPrevious();13const bigIntIndexNext = require('fast-check').bigIntIndexNext;14const bigIntIndex = bigIntIndexNext();15const bigIntIndexCurrent = require('fast-check').bigIntIndexCurrent;16const bigIntIndex = bigIntIndexCurrent();17const bigIntIndexNext = require('fast-check').bigIntIndexNext;18const bigIntIndex = bigIntIndexNext();19const bigIntIndexPrevious = require('fast-check').bigIntIndexPrevious;20const bigIntIndex = bigIntIndexPrevious();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!