diff --git a/src/main/java/br/unicamp/cst/memorystorage/LamportTime.java b/src/main/java/br/unicamp/cst/memorystorage/LamportTime.java index 57531b4..8a95df0 100644 --- a/src/main/java/br/unicamp/cst/memorystorage/LamportTime.java +++ b/src/main/java/br/unicamp/cst/memorystorage/LamportTime.java @@ -51,7 +51,7 @@ public String toString() public static LamportTime synchronize(LogicalTime time0, LogicalTime time1) { - if(!(LogicalTime.class.isInstance(time0) && LogicalTime.class.isInstance(time1))){ + if(!(LamportTime.class.isInstance(time0) && LamportTime.class.isInstance(time1))){ throw new IllegalArgumentException("LamportTime can only synchonize LamportTime instances"); } diff --git a/src/test/java/br/unicamp/cst/memorystorage/LamportTimeTest.java b/src/test/java/br/unicamp/cst/memorystorage/LamportTimeTest.java index e832882..596cb00 100644 --- a/src/test/java/br/unicamp/cst/memorystorage/LamportTimeTest.java +++ b/src/test/java/br/unicamp/cst/memorystorage/LamportTimeTest.java @@ -1,6 +1,9 @@ package br.unicamp.cst.memorystorage; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -53,5 +56,57 @@ public void synchronizeTest() assertTrue(time0.lessThan(timeS)); assertTrue(time1.lessThan(timeS)); assertEquals(56, timeS.getTime()); + + LamportTime timeS2 = LamportTime.synchronize(time1, time0); + + assertTrue(time0.lessThan(timeS2)); + assertTrue(time1.lessThan(timeS2)); + assertEquals(56, timeS2.getTime()); + } + + @Test + public void equalsTest() + { + LamportTime time0 = new LamportTime(0); + LamportTime time1 = new LamportTime(1); + LamportTime time2 = new LamportTime(1); + + assertNotEquals(time0, time1); + assertEquals(time1, time2); + } + + @Test + public void lessThanTest() + { + LamportTime time0 = new LamportTime(0); + LamportTime time1 = new LamportTime(1); + LamportTime time2 = new LamportTime(2); + + assertTrue(time0.lessThan(time1)); + assertFalse(time2.lessThan(time1)); + } + + @Test + public void synchonizeNonLamportTest() + { + LogicalTime logicalTime = new LogicalTime() { + + @Override + public LogicalTime increment() { + throw new UnsupportedOperationException("Unimplemented method 'increment'"); + } + + @Override + public boolean lessThan(Object o) { + throw new UnsupportedOperationException("Unimplemented method 'lessThan'"); + } + + }; + + LamportTime lamportTime = new LamportTime(); + + assertThrows(IllegalArgumentException.class, () -> LamportTime.synchronize(lamportTime, logicalTime)); + assertThrows(IllegalArgumentException.class, () -> LamportTime.synchronize(logicalTime, lamportTime)); + } }