Add G-Set (Grow-only Set) (#4975)

This commit is contained in:
Niklas Hoefflin
2023-12-05 19:39:18 +01:00
committed by GitHub
parent 3001620c1e
commit e59a3b1ebb
3 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,71 @@
package com.thealgorithms.datastructures.crdt;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class GSetTest {
@Test
void testAddElement() {
GSet<String> gSet = new GSet<>();
gSet.addElement("apple");
gSet.addElement("orange");
assertTrue(gSet.lookup("apple"));
assertTrue(gSet.lookup("orange"));
assertFalse(gSet.lookup("banana"));
}
@Test
void testLookup() {
GSet<Integer> gSet = new GSet<>();
gSet.addElement(1);
gSet.addElement(2);
assertTrue(gSet.lookup(1));
assertTrue(gSet.lookup(2));
assertFalse(gSet.lookup(3));
}
@Test
void testCompare() {
GSet<String> gSet1 = new GSet<>();
GSet<String> gSet2 = new GSet<>();
gSet1.addElement("apple");
gSet1.addElement("orange");
gSet2.addElement("orange");
gSet2.addElement("banana");
assertFalse(gSet1.compare(gSet2));
GSet<String> gSet3 = new GSet<>();
gSet3.addElement("apple");
gSet3.addElement("orange");
assertTrue(gSet1.compare(gSet3));
}
@Test
void testMerge() {
GSet<String> gSet1 = new GSet<>();
GSet<String> gSet2 = new GSet<>();
gSet1.addElement("apple");
gSet1.addElement("orange");
gSet2.addElement("orange");
gSet2.addElement("banana");
GSet<String> mergedSet = new GSet<>();
mergedSet.merge(gSet1);
mergedSet.merge(gSet2);
assertTrue(mergedSet.lookup("apple"));
assertTrue(mergedSet.lookup("orange"));
assertTrue(mergedSet.lookup("banana"));
assertFalse(mergedSet.lookup("grape"));
}
}