Skip to content
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
62bdfea
large random tests
izulin Jun 11, 2020
9d30698
this test fails sometimes
izulin Jun 13, 2020
bc3b8f3
removal of unnecessary nulls/undefined in serialization
izulin Jun 13, 2020
b4348e2
simplified tests
izulin Jun 13, 2020
cbf0092
Remove redundant tests
voodoo11 Jun 18, 2020
7745ceb
Merge branch 'develop' into pu/large-tests
izulin Jul 10, 2020
20af2f6
cleanup
izulin Jul 10, 2020
794fd07
restoring old version of a file
izulin Jul 10, 2020
f3acafd
columns
izulin Jul 10, 2020
84ce252
large test
izulin Jul 16, 2020
faabaa3
moveCells
izulin Jul 16, 2020
4387006
Fix error when moving only part of a range. Handle cycles when moving
voodoo11 Jul 17, 2020
d5ce0c6
Restore old data when undo move rows/columns
voodoo11 Jul 18, 2020
b81ab28
Batch transformations
voodoo11 Jul 22, 2020
dfc5cca
Changelog
voodoo11 Jul 23, 2020
bbb20da
Merge branch 'develop' into jk/move-cells-transformer-fix
voodoo11 Jul 23, 2020
318dd24
Use ??
voodoo11 Jul 24, 2020
dd8f002
Rename method
voodoo11 Jul 24, 2020
2c62e23
Update CHANGELOG.md
voodoo11 Jul 24, 2020
c75721b
Update CHANGELOG.md
voodoo11 Jul 24, 2020
354bf80
Merge branch 'develop' into jk/move-cells-transformer-fix
voodoo11 Jul 24, 2020
44546f0
Merge branch 'pu/large-tests' into jk/move-cells-transformer-fix
izulin Jul 24, 2020
0b339b2
.
izulin Jul 24, 2020
5664d14
Merge branch 'develop' into pu/large-tests
izulin Aug 8, 2020
5a00a79
fails
izulin Aug 8, 2020
fab3f7e
.
izulin Aug 8, 2020
b2a0d2b
Merge branch 'develop' into pu/large-tests
izulin Sep 4, 2020
f58cc61
cleanup
izulin Sep 4, 2020
6f511e4
Merge branch 'develop' into pu/large-tests
izulin Sep 4, 2020
743f616
Merge branch 'develop' into pu/large-tests
izulin Jul 2, 2021
f85785c
Merge branch 'develop' into pu/large-tests
izulin Jul 6, 2021
8bf1cb0
minor cleanup
izulin Jul 6, 2021
8abfc59
Update CHANGELOG.md
izulin Jul 6, 2021
033bd17
changelog
izulin Jul 6, 2021
9c41eea
Merge branch 'pu/large-tests' of https://github.com/handsontable/hype…
izulin Jul 6, 2021
346d42f
less iterations
izulin Jul 6, 2021
d8d1f16
Merge branch 'develop' into pu/large-tests
izulin Jul 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Operation `moveCells` creating cyclic dependencies does not cause losing original formula. (#479)
- Simplified adding new function modules, reworked (simplified) implementations of existing modules. (#480)

### Changed
- Operation `moveCells` creating cyclic dependencies does not cause losing original formula. (#479)

Comment thread
izulin marked this conversation as resolved.
Resolved
### Fixed
- Fixed hardcoding of languages in i18n tests. (#471)
- Fixed many compilation warnings based on LGTM analysis. (#473)
Expand Down
267 changes: 267 additions & 0 deletions test/crud-random.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
import {HyperFormula} from '../src'
import {AbsoluteCellRange} from '../src/AbsoluteCellRange'
import {verifyValues} from './testUtils'


/**
* random int from global variable 'state'
*/

const getInt = (function() {
let state = 1
return function() {
state ^= state << 13
state ^= state >> 17
state ^= state << 5
state &= 0x7FFFFFFF
return state
}
})()

/**
* random float from global variable 'state'
*/
function getFloat(): number {
return getInt()/0x80000000
}

/**
* boolean flag for outputing crud operations
*/
const outputLog = false

/**
* random int in range between min and max
*
* @param min
* @param max
*/
function randomInteger(min: number, max: number) {
return Math.floor(getFloat() * (max - min)) + min
}

type Pts = { x: number, y: number }

type Rectangle = { topleft: Pts, bottomright: Pts }

/**
* picks a random range as a subset of rectangle
* and builds a formula =SUM(range)
* @param engine
* @param rect
*/
function randomRange(engine: HyperFormula, rect: Rectangle): string {
const x1 = randomInteger(rect.topleft.x, rect.bottomright.x)
const x2 = randomInteger(rect.topleft.x, rect.bottomright.x)
const y1 = randomInteger(rect.topleft.y, rect.bottomright.y)
const y2 = randomInteger(rect.topleft.y, rect.bottomright.y)
const startAddress = engine.simpleCellAddressToString({
sheet: 0,
col: Math.min(x1, x2),
row: Math.min(y1, y2),
}, 0)
const endAddress = engine.simpleCellAddressToString({
sheet: 0,
col: Math.max(x1, x2),
row: Math.max(y1, y2)
}, 0)
return '=SUM(' + startAddress + ':' + endAddress + ')'
}

function undoRedo(engine: HyperFormula) {
if(outputLog) {
console.log('engine.undo()')
}
engine.undo()
if(outputLog) {
console.log('engine.redo()')
}
engine.redo()
}

/**
* Fills a rectangle of cells with random formula sums with a random ranges from another rectangle.
* @param engine
* @param rectFormulas
* @param rectValues
*/
function randomSums(engine: HyperFormula, rectFormulas: Rectangle, rectValues: Rectangle) {
allPts(rectFormulas).forEach((pts) => {
const formula = randomRange(engine, rectValues)
if(outputLog) {
console.log(`engine.setCellContents({sheet: 0, col: ${pts.x}, row: ${pts.y}}, '${formula}')`)
}
engine.setCellContents({sheet: 0, col: pts.x, row: pts.y}, formula)
undoRedo(engine)
})
}

/**
* Fills a rectangle of cells with random values.
* @param engine
* @param rectValues
*/
function randomVals(engine: HyperFormula, rectValues: Rectangle) {
allPts(rectValues).forEach((pts) => {
const val = randomInteger(-10, 10)
if(outputLog) {
console.log(`engine.setCellContents({sheet: 0, col:${pts.x}, row:${pts.y}}, ${val})`)
}
engine.setCellContents({sheet: 0, col: pts.x, row: pts.y}, val)
undoRedo(engine)
})
}

/**
* builds a rectangle from corner + X side length + Y side length
* @param pts
* @param sideX
* @param sideY
*/
function rectangleFromCorner(pts: Pts, sideX: number, sideY: number): Rectangle {
return {topleft: pts, bottomright: {x: pts.x + sideX, y: pts.y + sideY}}
}

/**
* all addresses from a rectangle
*
* @param rect
*/
function allPts(rect: Rectangle): Pts[] {
const ret: Pts[] = []
for (let x = rect.topleft.x; x < rect.bottomright.x; x++) {
for (let y = rect.topleft.y; y < rect.bottomright.y; y++) {
ret.push({x, y})
}
}
return ret
}

/**
* random shuffle of an array
*
* @param array
*/
function shuffleArray<T>(array: T[]): T[] {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]
}
return array
}

/**
* swaps two rectangles in-place
* @param engine
* @param corner1
* @param corner2
* @param sideX
* @param sideY
*/
function swapTwoRectangles(engine: HyperFormula, pts1: Pts, pts2: Pts, sideX: number, sideY: number) {
if(outputLog) {
console.log(`engine.moveCells( AbsoluteCellRange.spanFrom({sheet: 0, col: ${pts1.x}, row: ${pts1.y}}, ${sideX}, ${sideY}), {sheet: 0, col: 1000, row: 1000})`)
}
engine.moveCells( AbsoluteCellRange.spanFrom({sheet: 0, col: pts1.x, row: pts1.y}, sideX, sideY), {sheet: 0, col: 1000, row: 1000})
undoRedo(engine)
if(outputLog) {
console.log(`engine.moveCells( AbsoluteCellRange.spanFrom({sheet: 0, col: ${pts2.x}, row: ${pts2.y}}, ${sideX}, ${sideY}), {sheet: 0, col: ${pts1.x}, row: ${pts1.y}})`)
}
engine.moveCells( AbsoluteCellRange.spanFrom({sheet: 0, col: pts2.x, row: pts2.y}, sideX, sideY), {sheet: 0, col: pts1.x, row: pts1.y})
undoRedo(engine)
if(outputLog) {
console.log(`engine.moveCells( AbsoluteCellRange.spanFrom({sheet: 0, col: 1000, row: 1000}, ${sideX}, ${sideY}), {sheet: 0, col: ${pts2.x}, row: ${pts2.y}})`)
}
engine.moveCells( AbsoluteCellRange.spanFrom({sheet: 0, col: 1000, row: 1000}, sideX, sideY), {sheet: 0, col: pts2.x, row: pts2.y})
undoRedo(engine)
}

/**
* Empties the engine using .setCellContents()
* operates only on sheet: 0
*
* @param engine - engine to be emptied
* @param rect - rectangle of adresses we expect nonempty cell to be in
*
* The outcome should be that there are no cells in the engine.
*/
function randomCleanup(engine: HyperFormula, rect: Rectangle) {
shuffleArray(allPts(rect)).forEach((pts) => {
if(outputLog) {
console.log(`engine.setCellContents({sheet: 0, col:${pts.x}, row:${pts.y}}, null)`)
}
engine.setCellContents({sheet: 0, col: pts.x, row: pts.y}, null)
undoRedo(engine)
}
)
}

describe('large psuedo-random test', () => {
it('growing rectangle + addRows + addColumns + removeRows + removeColumns should produce the same sheet as static sheet', () => {
const engine = HyperFormula.buildFromArray([])
let sideX = 3
const n = 4
let sideY = 3
for(let rep=0;rep<5;rep++) {
randomVals(engine, rectangleFromCorner({x: 0, y: 0}, sideX, sideY))
verifyValues(engine)
for (let i = 0; i < n; i++) {
randomSums(engine,
rectangleFromCorner({x: sideX * (i + 1), y: 0}, sideX, sideY),
rectangleFromCorner({x: sideX * i, y: 0}, sideX, sideY)
)
verifyValues(engine)
}
for (let i = 0; i < n; i++) {
randomSums(engine,
rectangleFromCorner({x: sideX * (i + 1), y: 0}, sideX, sideY),
rectangleFromCorner({x: 0, y: 0}, sideX * (i+1), sideY)
)
verifyValues(engine)
}
for (let i=0; i<n; i++) {
const columnPositionToAdd = randomInteger(0, sideX*(n+1)+1)
if(outputLog) {
console.log(`engine.addColumns(0, [${columnPositionToAdd},2])`)
}
engine.addColumns(0, [columnPositionToAdd, 2])
undoRedo(engine)
verifyValues(engine)
const columnPositionToRemove = randomInteger(0, sideX*(n+1))
if(outputLog) {
console.log(`engine.removeColumns(0, [${columnPositionToRemove},1])`)
}
engine.removeColumns(0, [columnPositionToRemove, 1])
undoRedo(engine)
}
sideX += 1

const rowPositionToAdd = randomInteger(0, sideY + 1)
if(outputLog) {
console.log(`engine.addRows(0, [${rowPositionToAdd},2])`)
}
engine.addRows(0, [rowPositionToAdd, 2])
undoRedo(engine)
sideY += 2
verifyValues(engine)
const rowPositionToRemove = randomInteger(0, sideY)
if(outputLog) {
console.log(`engine.removeRows(0, [${rowPositionToRemove},1])`)
}
engine.removeRows(0, [rowPositionToRemove, 1])
undoRedo(engine)
sideY -= 1
verifyValues(engine)
const x1 = randomInteger(0, n*sideX)
const x2 = randomInteger(0, n*sideX)
const y1 = randomInteger(0, sideY)
const y2 = randomInteger(0, sideY)
swapTwoRectangles(engine, {x: x1, y: y1}, {x: x2, y: y2}, sideX, sideY)
verifyValues(engine)
}
randomCleanup(engine, rectangleFromCorner({x: 0, y: 0}, 2 * (n + 1) * sideX, 2 * sideY))
expect(engine.dependencyGraph.graph.nodesCount()).toBe(0)
expect(engine.dependencyGraph.rangeMapping.getMappingSize(0)).toBe(0)
})
})