-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sacha Froment <[email protected]>
- Loading branch information
Showing
5 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
ActionType, | ||
type DRP, | ||
type ResolveConflictsType, | ||
SemanticsType, | ||
type Vertex, | ||
} from "@ts-drp/object"; | ||
|
||
export class RecMulDRP implements DRP { | ||
semanticsType = SemanticsType.pair; | ||
|
||
private _value: number; | ||
|
||
constructor(initialValue?: number) { | ||
if (typeof initialValue === "number") { | ||
this._value = initialValue; | ||
} else { | ||
this._value = 1; | ||
} | ||
} | ||
|
||
recursive_mul(value: number): void { | ||
if (typeof value !== "number") { | ||
return; | ||
} | ||
if (value === 0) { | ||
return; | ||
} | ||
this._value = this._multiply(value, this._value); | ||
} | ||
|
||
private _multiply(value: number, base: number): number { | ||
if (value === 1) return base; // Base case | ||
return base + this._multiply(value - 1, base); | ||
} | ||
|
||
query_value(): number { | ||
return this._value; | ||
} | ||
|
||
resolveConflicts(_: Vertex[]): ResolveConflictsType { | ||
return { action: ActionType.Nop }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./AddMul/index.js"; | ||
export * from "./RecMul/index.js"; | ||
export * from "./Set/index.js"; | ||
export * from "./Map/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { ActionType, Vertex } from "@ts-drp/object"; | ||
import { beforeEach, describe, expect, test } from "vitest"; | ||
|
||
import { RecMulDRP } from "../src/RecMul/index.js"; | ||
|
||
describe("RecMulDRP tests", () => { | ||
let drp: RecMulDRP; | ||
|
||
beforeEach(() => { | ||
drp = new RecMulDRP(); | ||
}); | ||
|
||
test("Test: recursive_mul (Basic)", () => { | ||
drp.recursive_mul(3); | ||
let val = drp.query_value(); | ||
expect(val).toEqual(3); // 1 * 3 | ||
|
||
drp.recursive_mul(4); | ||
val = drp.query_value(); | ||
expect(val).toEqual(12); // 3 * 4 | ||
|
||
drp.recursive_mul(0); | ||
expect(drp.query_value()).toEqual(12); // Should not change for 0 | ||
}); | ||
|
||
test("Test: recursive_mul (Type Safety)", () => { | ||
drp.recursive_mul(2); | ||
expect(drp.query_value()).toEqual(2); // 1 * 2 | ||
|
||
// @ts-expect-error Testing invalid input | ||
drp.recursive_mul(""); | ||
expect(drp.query_value()).toEqual(2); | ||
|
||
// @ts-expect-error Testing invalid input | ||
drp.recursive_mul(true); | ||
expect(drp.query_value()).toEqual(2); | ||
|
||
// @ts-expect-error Testing invalid input | ||
drp.recursive_mul({}); | ||
expect(drp.query_value()).toEqual(2); | ||
}); | ||
|
||
test("Test: initialValue (Basic)", () => { | ||
drp = new RecMulDRP(10); | ||
expect(drp.query_value()).toEqual(10); | ||
|
||
drp = new RecMulDRP(-10); | ||
expect(drp.query_value()).toEqual(-10); | ||
|
||
drp = new RecMulDRP(0); | ||
expect(drp.query_value()).toEqual(0); | ||
|
||
drp = new RecMulDRP(); | ||
expect(drp.query_value()).toEqual(1); // Default value is 1 for multiplication | ||
}); | ||
|
||
test("Test: initialValue (Type Safety)", () => { | ||
// @ts-expect-error Testing invalid input | ||
drp = new RecMulDRP("10"); | ||
expect(drp.query_value()).toEqual(1); | ||
|
||
// @ts-expect-error Testing invalid input | ||
drp = new RecMulDRP(true); | ||
expect(drp.query_value()).toEqual(1); | ||
|
||
// @ts-expect-error Testing invalid input | ||
drp = new RecMulDRP({}); | ||
expect(drp.query_value()).toEqual(1); | ||
|
||
// @ts-expect-error Testing invalid input | ||
drp = new RecMulDRP([]); | ||
expect(drp.query_value()).toEqual(1); | ||
}); | ||
|
||
test("Test: resolveConflicts (Basic)", () => { | ||
const vertex1: Vertex = { | ||
hash: "1", | ||
peerId: "1", | ||
operation: { | ||
drpType: "DRP", | ||
opType: "recursive_mul", | ||
value: [3], | ||
}, | ||
dependencies: [], | ||
timestamp: 0, | ||
signature: new Uint8Array(), | ||
}; | ||
const vertex2: Vertex = { | ||
hash: "2", | ||
peerId: "2", | ||
operation: { | ||
drpType: "DRP", | ||
opType: "recursive_mul", | ||
value: [2], | ||
}, | ||
dependencies: [], | ||
timestamp: 0, | ||
signature: new Uint8Array(), | ||
}; | ||
|
||
let action = drp.resolveConflicts([]); | ||
expect(action).toEqual({ action: ActionType.Nop }); | ||
|
||
action = drp.resolveConflicts([vertex1]); | ||
expect(action).toEqual({ action: ActionType.Nop }); | ||
|
||
action = drp.resolveConflicts([vertex1, vertex2]); | ||
expect(action).toEqual({ action: ActionType.Nop }); | ||
}); | ||
|
||
test("Test: resolveConflicts (Type Safety)", () => { | ||
const vertex1: Vertex = { | ||
hash: "1", | ||
peerId: "1", | ||
operation: { | ||
drpType: "DRP", | ||
opType: "recursive_mul", | ||
value: [2], | ||
}, | ||
dependencies: [], | ||
timestamp: 0, | ||
signature: new Uint8Array(), | ||
}; | ||
|
||
const vertex2 = {}; | ||
|
||
let action = drp.resolveConflicts([vertex1, vertex2]); | ||
expect(action).toEqual({ action: ActionType.Nop }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters