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 bigIntIndexNeg = require('fast-check-monorepo').bigIntIndexNeg;3fc.assert(fc.property(fc.bigIntIndexNeg(), (a) => a < 0));4{5 "scripts": {6 },7 "dependencies": {8 }9}10"paths": {11}12"paths": {13}
Using AI Code Generation
1import { bigIntIndexNeg } from 'fast-check';2describe('bigIntIndexNeg', () => {3 it('should generate negative BigInts', () => {4 const index = bigIntIndexNeg();5 expect(index).toBeLessThan(0);6 });7});8import { bigIntIndexPos } from 'fast-check';9describe('bigIntIndexPos', () => {10 it('should generate positive BigInts', () => {11 const index = bigIntIndexPos();12 expect(index).toBeGreaterThan(0);13 });14});15import { bigIntIndex } from 'fast-check';16describe('bigIntIndex', () => {17 it('should generate BigInts', () => {18 const index = bigIntIndex();19 expect(index).toBeGreaterThanOrEqual(0);20 });21});22import { bigUint } from 'fast-check';23describe('bigUint', () => {24 it('should generate bigUint', () => {25 const bigUint = bigUint();26 expect(bigUint).toBeGreaterThanOrEqual(0);27 });28});29import { bigUintArray } from 'fast-check';30describe('bigUintArray', () => {31 it('should generate bigUintArray', () => {32 const bigUintArray = bigUintArray();33 expect(bigUintArray).toBeGreaterThanOrEqual(0);34 });35});36import { bigUintArray } from 'fast-check';37describe('bigUintArray', () => {38 it('should generate bigUintArray', () => {39 const bigUintArray = bigUintArray();40 expect(bigUintArray).toBeGreaterThanOrEqual(0);41 });42});43import { bigUintArray } from 'fast-check';44describe('bigUintArray', () => {45 it('should generate bigUintArray', () => {
Using AI Code Generation
1const fc = require('fast-check');2const bigIntIndexNeg = require('fast-check-monorepo').bigIntIndexNeg;3fc.assert(4 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {5 return bigIntIndexNeg(a, b) === a - b;6 })7);8fc.assert(9 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {10 return bigIntIndexNeg(a, b) === a - b;11 })12);13fc.assert(14 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {15 return bigIntIndexNeg(a, b) === a - b;16 })17);18fc.assert(19 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {20 return bigIntIndexNeg(a, b) === a - b;21 })22);23fc.assert(24 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {25 return bigIntIndexNeg(a, b) === a - b;26 })27);28fc.assert(29 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {30 return bigIntIndexNeg(a, b) === a - b;31 })32);33fc.assert(34 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {35 return bigIntIndexNeg(a, b) === a - b;36 })37);38fc.assert(39 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {40 return bigIntIndexNeg(a, b) === a - b;41 })42);43fc.assert(44 fc.property(fc.bigInt(), fc.bigInt(), (a, b) => {45 return bigIntIndexNeg(a, b) === a - b;46 })47);48fc.assert(49 fc.property(fc.bigInt(), fc.bigInt(), (a, b) =>
Using AI Code Generation
1import { bigIntIndexNeg } from 'fast-check'2const index = bigIntIndexNeg()3import { bigIntIndexNeg } from 'fast-check/lib/check/arbitrary/BigIntArbitrary'4const index = bigIntIndexNeg()5Is there a way to import the function from the fast-check-monorepo?6Is there a way to import the function from the fast-check-monorepo?7I am trying to use fast-check to generate a random string for a test. I don’t want to use the stringArbitrary() function because I want to be able to control the number of characters in the string. I’ve read the documentation and it says that I should use the buildStringArbitrary() function, but I can’t find it in
Using AI Code Generation
1const fc = require('fast-check');2const bigIntIndexNeg = fc.integer(-10, 10);3console.log(bigIntIndexNeg);4const fc = require('fast-check');5const bigIntIndexNeg = fc.integer(-10, 10);6console.log(bigIntIndexNeg);7const fc = require('fast-check');8const bigIntIndexNeg = fc.integer(-10, 10);9console.log(bigIntIndexNeg);10const fc = require('fast-check');11const bigIntIndexNeg = fc.integer(-10, 10);12console.log(bigIntIndexNeg);13const fc = require('fast-check');14const bigIntIndexNeg = fc.integer(-10, 10);15console.log(bigIntIndexNeg);16const fc = require('fast-check');17const bigIntIndexNeg = fc.integer(-10, 10);18console.log(bigIntIndexNeg);
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!!