From 06b21b2b73dc9f622f6fe415d8b8a9df450ffdb8 Mon Sep 17 00:00:00 2001 From: Elton Cardoso do Nascimento <43186596+EltonCN@users.noreply.github.com> Date: Wed, 11 Dec 2024 17:03:41 -0300 Subject: [PATCH] LamportTime 100% coverage --- .../cst/memorystorage/LamportTime.java | 2 +- .../cst/memorystorage/LamportTimeTest.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) 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)); + } }