Best JavaScript code snippet using fast-check-monorepo
main.spec.ts
Source:main.spec.ts
...47 ...neighboorsFor(pt)48 // We do not go back on our tracks49 .filter((nPt) => nPt.x !== src.x || nPt.y !== src.y)50 .filter((nPt) => {51 const cell = cellTypeAt(maze, nPt);52 return cell !== null && cell !== CellType.Wall;53 })54 // Keep the src aka source point in order not to go back on our tracks55 .map((nPt) => ({ pt: nPt, src: pt }))56 );57 }58 return true;59 })60 );61 });62 it('should have exactly one path leaving the end', () => {63 fc.assert(64 fc.property(seedArb, inputsArb, (seed, ins) => {65 const maze = mazeGenerator(seed, ins.dim, ins.startPt, ins.endPt);66 const numPathsLeavingEnd = neighboorsFor(ins.endPt).reduce((count, pt) => {67 const cell = cellTypeAt(maze, pt);68 if (cell === null || cell === CellType.Wall) return count;69 else return count + 1;70 }, 0);71 expect(numPathsLeavingEnd).toBe(1);72 })73 );74 });75 it('should have only non-isolated path, start, end', () => {76 fc.assert(77 fc.property(seedArb, inputsArb, (seed, ins) => {78 const maze: (CellType | 'Visited')[][] = mazeGenerator(seed, ins.dim, ins.startPt, ins.endPt);79 const ptsToVisit = [ins.startPt, ins.endPt];80 while (ptsToVisit.length > 0) {81 const [pt] = ptsToVisit.splice(ptsToVisit.length - 1);82 maze[pt.y][pt.x] = 'Visited';83 ptsToVisit.push(84 ...neighboorsFor(pt).filter((nPt) => {85 const cell = cellTypeAt(maze, nPt);86 return cell !== null && cell !== CellType.Wall && cell !== 'Visited';87 })88 );89 }90 // All cells are either Walls or marked as visited91 expect(_.flatten(maze).filter((c) => c !== CellType.Wall && c !== 'Visited')).toHaveLength(0);92 })93 );94 });95});96// Helpers97const seedArb = fc.integer().noBias().noShrink();98const dimensionArb = fc.record({99 width: fc.integer({ min: 2, max: 20 }),100 height: fc.integer({ min: 2, max: 20 }),101});102const inputsArb = dimensionArb103 .chain((dim) => {104 return fc.record({105 dim: fc.constant(dim),106 startPt: fc.record({ x: fc.nat(dim.width - 1), y: fc.nat(dim.height - 1) }),107 endPt: fc.record({ x: fc.nat(dim.width - 1), y: fc.nat(dim.height - 1) }),108 });109 })110 .filter((ins) => ins.startPt.x !== ins.endPt.x || ins.startPt.y !== ins.endPt.y);111const neighboorsFor = (pt: Point): Point[] => {112 return [113 { x: pt.x - 1, y: pt.y },114 { x: pt.x + 1, y: pt.y },115 { x: pt.x, y: pt.y - 1 },116 { x: pt.x, y: pt.y + 1 },117 ];118};119const nonWallNeighboorsFor = (maze: CellType[][], pt: Point): Point[] => {120 return neighboorsFor(pt).filter((nPt) => {121 const cell = cellTypeAt(maze, nPt);122 return cell !== null && cell !== CellType.Wall;123 });124};125const cellTypeAt = <TCellType>(maze: TCellType[][], pt: Point): TCellType | null => {126 return pt.x < 0 || pt.x >= maze[0].length || pt.y < 0 || pt.y >= maze.length ? null : maze[pt.y][pt.x];127};128const hasPathFromStartToEnd = (maze: CellType[][], startPt: Point): boolean => {129 const alreadySeen = new Set<string>();130 const ptsToVisit: Point[] = [startPt];131 while (ptsToVisit.length > 0) {132 const [pt] = ptsToVisit.splice(ptsToVisit.length - 1);133 if (maze[pt.y][pt.x] === CellType.End) return true;134 const ptString = `x:${pt.x},y:${pt.y}`;135 if (alreadySeen.has(ptString)) continue;...
map.js
Source:map.js
...29 this.cellByteSize = 130 console.log("Recreate map", this.width, this.height)31 this.game.scene.cameras.main.setBounds(0, 0, this.width * this.cellSize, this.height * this.cellSize)32 }33 cellTypeAt(x, y) {34 if (!this.cellsDataView) {35 return null36 }37 if (x < 0 || y < 0) {38 return null39 }40 if (x >= this.width || y >= this.height) {41 return null42 }43 var index = x + y * this.width44 var byteOffset = this.cellByteSize * index45 var cellType = this.cellsDataView.getUint8(byteOffset)46 return cellType47 }48 redraw(offsetX, offsetY, width, height) {49 var minX = Math.floor(offsetX / this.texSize)50 var minY = Math.floor(offsetY / this.texSize)51 var maxX = Math.floor((offsetX + width) / this.texSize)52 var maxY = Math.floor((offsetY + height) / this.texSize)53 // console.log("Redraw map", offsetX, offsetY, width, height, "-", minX, maxX)54 for (var x = minX; x <= maxX; ++x) {55 for (var y = minY; y <= maxY; ++y) {56 this._drawTexture(x, y, x * this.texSize, y * this.texSize)57 }58 }59 this.cachedTextures.forEach(tex => {60 tex.drawCallsWithoutBeingUsed += 161 })62 }63 _textureKey(x, y) {64 var key = `${x}_${y}`65 return key66 }67 _drawTexture(texX, texY, offsetX, offsetY) {68 if (!this.cellsDataView) {69 return70 }71 var tex = null72 var key = this._textureKey(texX, texY)73 tex = this.cachedTexturesMap[key]74 if (!tex) {75 tex = this.cachedTextures.filter(tex => { tex.drawCallsWithoutBeingUsed > 10 })[0]76 var oldKey = null77 if (tex) {78 oldKey = this._textureKey(tex.x, tex.y)79 } else {80 console.log("Create texture", texX, texY)81 tex = new CachedMapTexture(this.game.scene, this.texSize)82 this.cachedTextures.push(tex)83 }84 console.log("Draw texture", texX, texY)85 tex.texture.clear();86 for (var y = 0; y < this.cellsPerTex; ++y) {87 for (var x = 0; x < this.cellsPerTex; ++x) {88 var cellX = x + texX * this.cellsPerTex89 var cellY = y + texY * this.cellsPerTex90 var cellType = this.cellTypeAt(cellX, cellY)91 var dx = this.cellSize * x92 var dy = this.cellSize * y93 if (x == this.cellsPerTex - 1) {94 dx -= 295 }96 if (y == this.cellsPerTex - 1) {97 dy -= 298 }99 if (cellType !== null) {100 var texName = cellType == 1 ? "grass" : "earth"101 tex.texture.draw(texName, dx, dy)102 }103 }104 }...
mazeGenerator.ts
Source:mazeGenerator.ts
...39 const cellTypeAt = (pt: Point): CellType | null => {40 return pt.x < 0 || pt.x >= dim.width || pt.y < 0 || pt.y >= dim.height ? null : maze[pt.y][pt.x];41 };42 const addInPtsToScanIfWall = (pt: Point) => {43 if (cellTypeAt(pt) === CellType.Wall) ptsToScan.push(pt);44 };45 const neighboorsFor = (pt: Point) => {46 return [47 { x: pt.x - 1, y: pt.y },48 { x: pt.x + 1, y: pt.y },49 { x: pt.x, y: pt.y - 1 },50 { x: pt.x, y: pt.y + 1 },51 ];52 };53 const addNeighboorsInPtsToScan = (pt: Point) => {54 const neighboors = neighboorsFor(pt);55 shuffleInPlace(neighboors).forEach((nPt) => addInPtsToScanIfWall(nPt));56 };57 const isNextToEnd = (pt: Point): boolean => {58 return neighboorsFor(pt).find((nPt) => cellTypeAt(nPt) === CellType.End) !== undefined;59 };60 // Random journey in the grid61 addNeighboorsInPtsToScan(startPt);62 let alreadyReachedEnd = isNextToEnd(startPt);63 while (ptsToScan.length > 0) {64 const [pt] = ptsToScan.splice(ptsToScan.length - 1);65 if (cellTypeAt(pt) !== CellType.Wall) continue;66 const numRoads = neighboorsFor(pt).reduce((count, nPt) => {67 const cell = cellTypeAt(nPt);68 if (cell !== CellType.Wall && cell !== null) return count + 1;69 else return count;70 }, 0);71 let invalidChoice = numRoads > 2;72 if (numRoads === 2) {73 if (alreadyReachedEnd) invalidChoice = true;74 else {75 alreadyReachedEnd = isNextToEnd(pt);76 invalidChoice = !alreadyReachedEnd;77 }78 }79 if (invalidChoice) {80 // we reach the end of ongoing path -- shuffling of previously possible points81 shuffleInPlace(ptsToScan);...
Using AI Code Generation
1const fc = require('fast-check');2const { cellTypeAt } = require('fast-check/lib/check/arbitrary/CellArbitrary');3const cellType = cellTypeAt(0, 0);4console.log(cellType);5const fc = require('fast-check');6const { cellTypeAt } = require('fast-check/lib/check/arbitrary/CellArbitrary');7const cellType = cellTypeAt(1, 0);8console.log(cellType);9const fc = require('fast-check');10const { cellTypeAt } = require('fast-check/lib/check/arbitrary/CellArbitrary');11const cellType = cellTypeAt(1, 1);12console.log(cellType);13const fc = require('fast-check');14const { cellTypeAt } = require('fast-check/lib/check/arbitrary/CellArbitrary');15const cellType = cellTypeAt(1, 2);16console.log(cellType);17const fc = require('fast-check');18const { cellTypeAt } = require('fast-check/lib/check/arbitrary/CellArbitrary');19const cellType = cellTypeAt(1, 3);20console.log(cellType);21const fc = require('fast-check');22const { cellTypeAt } = require('fast-check/lib/check/arbitrary/CellArbitrary');23const cellType = cellTypeAt(1, 4);24console.log(cellType);
Using AI Code Generation
1import { cellTypeAt } from 'fast-check';2const cellType = cellTypeAt(0, 0);3console.log(cellType);4import { cellTypeAt } from 'fast-check';5const cellType = cellTypeAt(0, 0);6console.log(cellType);7import { cellTypeAt } from 'fast-check';8const cellType = cellTypeAt(0, 0);9console.log(cellType);10import { cellTypeAt } from 'fast-check';11const cellType = cellTypeAt(0, 0);12console.log(cellType);13import { cellTypeAt } from 'fast-check';14const cellType = cellTypeAt(0, 0);15console.log(cellType);16import { cellTypeAt } from 'fast-check';17const cellType = cellTypeAt(0, 0);18console.log(cellType);19import { cellTypeAt } from 'fast-check';20const cellType = cellTypeAt(0, 0);21console.log(cellType);22import { cellTypeAt } from 'fast-check';23const cellType = cellTypeAt(0, 0);24console.log(cellType);
Using AI Code Generation
1const fc = require('fast-check');2const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');3const cellArbitrary = fc.cell();4const cell = cellArbitrary.generate();5console.log(cellTypeAt(cell));6const fc = require('fast-check');7const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');8const cellArbitrary = fc.cell();9const cell = cellArbitrary.generate();10console.log(cellTypeAt(cell));11const fc = require('fast-check');12const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');13const cellArbitrary = fc.cell();14const cell = cellArbitrary.generate();15console.log(cellTypeAt(cell));16const fc = require('fast-check');17const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');18const cellArbitrary = fc.cell();19const cell = cellArbitrary.generate();20console.log(cellTypeAt(cell));21const fc = require('fast-check');22const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');23const cellArbitrary = fc.cell();24const cell = cellArbitrary.generate();25console.log(cellTypeAt(cell));26const fc = require('fast-check');27const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');28const cellArbitrary = fc.cell();29const cell = cellArbitrary.generate();30console.log(cellTypeAt(cell));31const fc = require('fast-check');32const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');33const cellArbitrary = fc.cell();34const cell = cellArbitrary.generate();35console.log(cellTypeAt(cell));36const fc = require('fast-check');37const { cellTypeAt } = require('fast-check/src/check/arbitrary/CellArbitrary');38const cellArbitrary = fc.cell();
Using AI Code Generation
1const fc = require('fast-check');2const { CellArbitrary } = require('fast-check');3fc.assert(4 fc.property(fc.cellTypeAt(), (cell) => {5 return true;6 })7);8const fc = require('fast-check');9const { CellArbitrary } = require('fast-check');10fc.assert(11 fc.property(CellArbitrary.cellTypeAt(), (cell) => {12 return true;13 })14);15const fc = require('fast-check');16const { CellArbitrary } = require('fast-check');17fc.assert(18 fc.property(CellArbitrary.cellTypeAt(), (cell) => {19 return true;20 })21);22const fc = require('fast-check');23const { CellArbitrary } = require('fast-check');24fc.assert(25 fc.property(CellArbitrary.cellTypeAt(), (cell) => {26 return true;27 })28);
Using AI Code Generation
1const fc = require("fast-check");2const { cellTypeAt } = require("./CellArbitrary");3const { cellType } = require("./CellType");4fc.assert(fc.property(fc.integer(), fc.integer(), fc.integer(), (x, y, z) => {5 const result = cellTypeAt(x, y, z);6 return result === cellType("dead") || result === cellType("alive");7}));8const { cellTypeAt } = require("./CellArbitrary");9const { cellType } = require("./CellType");10module.exports = {11};12const { cellType } = require("./CellType");13module.exports = {14};15import { cellTypeAt } from "./CellArbitrary";16import { cellType } from "./CellType";17export {18};19import { cellType } from "./CellType";20export {21};22import { cellTypeAt } from "./CellArbitrary";23import { cellType } from "./CellType";24export {25};26import { cellType } from "./CellType";27export {28};29import { cellTypeAt
Using AI Code Generation
1var fastCheck = require('fast-check');2 [fastCheck.cellTypeAt(0, 0), fastCheck.cellTypeAt(0, 1), fastCheck.cellTypeAt(0, 2)],3 [fastCheck.cellTypeAt(1, 0), fastCheck.cellTypeAt(1, 1), fastCheck.cellTypeAt(1, 2)],4 [fastCheck.cellTypeAt(2, 0), fastCheck.cellTypeAt(2, 1), fastCheck.cellTypeAt(2, 2)]5];6console.log(grid);7var fastCheck = require('fast-check');8 [fastCheck.cellTypeAt(0, 0), fastCheck.cellTypeAt(0, 1), fastCheck.cellTypeAt(0, 2)],9 [fastCheck.cellTypeAt(1, 0), fastCheck.cellTypeAt(1, 1), fastCheck.cellTypeAt(1, 2)],10 [fastCheck.cellTypeAt(2, 0), fastCheck.cellTypeAt(2, 1), fastCheck.cellTypeAt(2, 2)]11];12console.log(grid);13var fastCheck = require('fast-check');14 [fastCheck.cellTypeAt(0, 0), fastCheck.cellTypeAt(0, 1), fastCheck.cellTypeAt(0, 2)],15 [fastCheck.cellTypeAt(1, 0), fastCheck.cellTypeAt(1, 1), fastCheck.cellTypeAt(1, 2)],16 [fastCheck.cellTypeAt(2, 0), fastCheck.cellTypeAt(2, 1), fastCheck.cellTypeAt(2, 2)]17];18console.log(grid);19var fastCheck = require('fast-check');
Using AI Code Generation
1import { cellTypeAt } from 'fast-check';2const mine = cellTypeAt(0, 0);3const empty = cellTypeAt(1, 1);4const number = cellTypeAt(2, 2);5console.log(mine);6console.log(empty);7console.log(number);8import { cellTypeAt } from 'fast-check';9const mine = cellTypeAt(0, 0);10const empty = cellTypeAt(1, 1);11const number = cellTypeAt(2, 2);12console.log(mine);13console.log(empty);14console.log(number);15import { cellTypeAt } from 'fast-check';16const mine = cellTypeAt(0, 0);17const empty = cellTypeAt(1, 1);18const number = cellTypeAt(2, 2);19console.log(mine);20console.log(empty);21console.log(number);22import { cellTypeAt } from 'fast-check';23const mine = cellTypeAt(0, 0);24const empty = cellTypeAt(1, 1);25const number = cellTypeAt(2, 2);26console.log(mine);27console.log(empty);28console.log(number);
Using AI Code Generation
1import { cellTypeAt, CellType } from "fast-check-monorepo";2import { generateBoard } from "fast-check-monorepo";3const board = generateBoard(10, 10, 10);4const cellType = cellTypeAt(board, 0, 0);5if (cellType === CellType.Mine) {6 console.log("Mine!");7} else {8 console.log("Not a mine!");9}10import { cellTypeAt, CellType } from "fast-check-monorepo";11import { generateBoard } from "fast-check-monorepo";12const board = generateBoard(10, 10, 10);13const cellType = cellTypeAt(board, 0, 0);14if (cellType === CellType.Mine) {15 console.log("Mine!");16} else {17 console.log("Not a mine!");18}19import { cellTypeAt, CellType } from "fast-check-monorepo";20import { generateBoard } from "fast-check-monorepo";21const board = generateBoard(10, 10, 10);22const cellType = cellTypeAt(board, 0, 0);23if (cellType === CellType.Mine) {24 console.log("Mine!");25} else {26 console.log("Not a mine!");27}28import { cellTypeAt, CellType } from "fast-check-monorepo";29import { generateBoard } from "fast-check-monorepo";30const board = generateBoard(10, 10, 10);31const cellType = cellTypeAt(board, 0, 0);32if (cellType === CellType.Mine) {33 console.log("Mine!");34} else {35 console.log("Not a mine!");36}
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!!