Skip to content

Commit

Permalink
added tests, removed setters, made everything public
Browse files Browse the repository at this point in the history
  • Loading branch information
Razvan1928 committed Mar 13, 2024
1 parent ac418f3 commit e08e849
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,17 @@ import type { Dimensions, Transform, WaferMapMatrix, WaferMapTypedMatrix } from
* This setup is used in the wafer-map component to perform heavy computational duties
*/
export class MatrixRenderer {
private colIndexes: Uint8Array = Uint8Array.from([]);
private rowIndexes: Uint8Array = Uint8Array.from([]);
private canvas!: OffscreenCanvas;
private context!: OffscreenCanvasRenderingContext2D;
private values = new Float32Array([]);
private scaledColIndex = new Float64Array([]);
private scaledRowIndex = new Float64Array([]);
private dieDimensions: Dimensions = { width: 1, height: 1 };
private transform: Transform = { k: 1, x: 0, y: 0 };
private topLeftCanvasCorner: { x: number, y: number } = { x: 0, y: 0 };
private bottomRightCanvasCorner: { x: number, y: number } = { x: 500, y: 500 };
public setTopLeftCanvasCorner(topLeftCanvasCorner: { x: number, y: number }): void {
this.topLeftCanvasCorner = topLeftCanvasCorner;
}

public setBottomRightCanvasCorner(bottomRightCanvasCorner: { x: number, y: number }): void {
this.bottomRightCanvasCorner = bottomRightCanvasCorner;
}

public setColIndexes(colIndexes: Uint8Array): void {
this.colIndexes = colIndexes;
}

public setRowIndexes(rowIndexes: Uint8Array): void {
this.rowIndexes = rowIndexes;
}

public setValues(values: Float32Array): void {
this.values = values;
}

public setScaledColIndex(scaledColIndex: Float64Array): void {
this.scaledColIndex = scaledColIndex;
}

public setScaledRowIndex(scaledRowIndex: Float64Array): void {
this.scaledRowIndex = scaledRowIndex;
}
public setDieDimensions(dieDimensions: Dimensions): void {
this.dieDimensions = dieDimensions;
}

public setTransform(transform: Transform): void {
this.transform = transform;
}

public setCanvas(canvas: OffscreenCanvas): void {
this.canvas = canvas;
this.context = canvas.getContext('2d')!;
}
public colIndexes: Uint8Array = Uint8Array.from([]);
public rowIndexes: Uint8Array = Uint8Array.from([]);
public canvas!: OffscreenCanvas;
public context!: OffscreenCanvasRenderingContext2D;
public values = new Float32Array([]);
public scaledColIndex = new Float64Array([]);
public scaledRowIndex = new Float64Array([]);
public dieDimensions: Dimensions = { width: 1, height: 1 };
public transform: Transform = { k: 1, x: 0, y: 0 };
public topLeftCanvasCorner: { x: number, y: number } = { x: 0, y: 0 };
public bottomRightCanvasCorner: { x: number, y: number } = { x: 500, y: 500 };

public getMatrix(): WaferMapTypedMatrix {
return {
Expand All @@ -74,7 +35,7 @@ export class MatrixRenderer {
this.values = Float32Array.from([]);
}

private scaleCanvas(): void {
public scaleCanvas(): void {
this.context.translate(
this.transform.x,
this.transform.y
Expand All @@ -98,7 +59,7 @@ export class MatrixRenderer {
this.canvas.height = data.height;
}

private clearCanvas(): void {
public clearCanvas(): void {
this.context.clearRect(
0,
0,
Expand All @@ -123,12 +84,12 @@ export class MatrixRenderer {
}
}

private formatValue(value: number | undefined): string {
public formatValue(value: number | undefined): string {
if (value === undefined) return '';
return parseFloat(value.toFixed(1)) + '...';
}

private addTextOnDie(x: number, y: number, i: number) {
public addTextOnDie(x: number, y: number, i: number) {
const fontSize = Math.floor(this.dieDimensions.height * 0.35);
this.context.font = `${fontSize}px Arial`;
this.context.fillStyle = 'White';
Expand All @@ -143,7 +104,7 @@ export class MatrixRenderer {
this.context.fillText(formattedValue, textX, textY);
}

private isDieVisible(x: number, y: number): boolean {
public isDieVisible(x: number, y: number): boolean {
return x >= this.topLeftCanvasCorner.x &&
x <= this.bottomRightCanvasCorner.x &&
y >= this.topLeftCanvasCorner.y &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,36 @@ describe('MatrixRenderer with MessageChannel', () => {
+ updatedMatrix.rowIndexes.length
+ updatedMatrix.values.length).toEqual(0);
});

it('should get the matrix', async () => {
const testData = {
colIndexes: [4, 1, 2],
rowIndexes: [54, 54, 62],
values: [8.12, 9.0, 0.32]
};

await matrixRenderer.updateMatrix(testData);

const matrix = await matrixRenderer.getMatrix();

expect(matrix).toEqual({
colIndexes: Uint8Array.from(testData.colIndexes),
rowIndexes: Uint8Array.from(testData.rowIndexes),
values: Float32Array.from(testData.values)
});
});

it('should draw the wafer', async () => {
matrixRenderer.scaledColIndex = Promise.resolve(Float64Array.from([0.1, 0.2, 0.3]));
matrixRenderer.scaledRowIndex = Promise.resolve(Float64Array.from([0.4, 0.5, 0.6]));
spyOn(matrixRenderer, 'clearCanvas');
spyOn(matrixRenderer, 'scaleCanvas');
spyOn(matrixRenderer, 'addTextOnDie');

matrixRenderer.drawWafer();

expect(matrixRenderer.clearCanvas).toHaveBeenCalled();
expect(matrixRenderer.scaleCanvas).toHaveBeenCalled();
expect(matrixRenderer.addTextOnDie).toHaveBeenCalledTimes(3);
});
});

0 comments on commit e08e849

Please sign in to comment.