Skip to content

Commit

Permalink
Tests for GSet & 2PSet CRDT
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopereira12 committed Jul 29, 2024
1 parent a42f14b commit 8ff0fdc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/crdt/tests/GSet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { GSet } from "../src/builtins/GSet";

describe("G-Set Tests", () => {

let set1: GSet<string>;
let set2: GSet<string>;

beforeEach(() => {
set1 = new GSet<string>(new Set<string>(["walter", "jesse", "mike"]));
set2 = new GSet<string>(new Set<string>(["walter", "jesse", "mike"]));
});

test("Test Add", () => {
set1.add("gustavo");
set2.add("gustavo");

expect(set1.lookup("gustavo")).toBe(true);
expect(set2.lookup("gustavo")).toBe(true);
});

test("Test Compare", () => {
expect(set1.compare(set2)).toBe(true);

set1.add("gustavo");

expect(set1.compare(set2)).toBe(false);

set2.add("gustavo");

expect(set1.compare(set2)).toBe(true);
});

test("Test Merge", () => {
set1.add("gustavo");
set2.add("lalo");

expect(set1.compare(set2)).toBe(false);

set1.merge(set2);
set2.merge(set1);

expect(set1.compare(set2)).toBe(true);

});
});
55 changes: 55 additions & 0 deletions packages/crdt/tests/TwoPSet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { describe, test, expect, beforeEach } from "vitest";
import { TwoPSet } from "../src/builtins/2PSet";
import { GSet } from "../src/builtins/GSet";

describe("2P-Set Tests", () => {

let set1: TwoPSet<string>;
let set2: TwoPSet<string>;

beforeEach(() => {
set1 = new TwoPSet<string>(new GSet<string>(new Set<string>(["walter", "jesse", "mike"])), new GSet<string>(new Set<string>()));
set2 = new TwoPSet<string>(new GSet<string>(new Set<string>(["walter", "jesse", "mike"])), new GSet<string>(new Set<string>()));
});

test("Test Add Element", () => {
expect(set1.lookup("gustavo")).toBe(false);

set1.add("gustavo");

expect(set1.lookup("gustavo")).toBe(true);
});

test("Test Remove Element", () => {
expect(set1.lookup("mike")).toBe(true);

set1.remove("mike");

expect(set1.lookup("mike")).toBe(false);
});

test("Test Compare Elements", () => {
expect(set1.compare(set2)).toBe(true);

set1.remove("mike");

expect(set1.compare(set2)).toBe(false);

set2.remove("mike");

expect(set1.compare(set2)).toBe(true);
});

test("Test Merge Elements", () => {
set1.remove("mike");
set2.add("gustavo");

expect(set1.compare(set2)).toBe(false);

set1.merge(set2);
set2.merge(set1);

expect(set1.compare(set2)).toBe(true);
});

});

0 comments on commit 8ff0fdc

Please sign in to comment.