Implement methods to compare objects in classes representing unit
Methods are added to the following class.
- Bandwidth
- Frequency
To reduce redundant code, RichComparable interface is defined.
Change-Id: Iec96bee9754c6dfca62255b8b4068925d3be13a1
diff --git a/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java b/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java
index f8c17fc..f9566c9 100644
--- a/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java
+++ b/utils/misc/src/test/java/org/onlab/util/BandwidthTest.java
@@ -18,10 +18,19 @@
import com.google.common.testing.EqualsTester;
import org.junit.Test;
+import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThan;
+import static org.junit.Assert.assertThat;
+
/**
* Unit tests for Bandwidth.
*/
public class BandwidthTest {
+
+ private final Bandwidth small = Bandwidth.kbps(100.0);
+ private final Bandwidth large = Bandwidth.mbps(1.0);
+
/**
* Tests equality of Bandwidth instances.
*/
@@ -33,4 +42,21 @@
.testEquals();
}
+ /**
+ * Tests if the first object is less than the second object.
+ */
+ @Test
+ public void testLessThan() {
+ assertThat(small, is(lessThan(large)));
+ assertThat(small.isLessThan(large), is(true));
+ }
+
+ /**
+ * Tests if the first object is greater than the second object.
+ */
+ @Test
+ public void testGreaterThan() {
+ assertThat(large, is(greaterThan(small)));
+ assertThat(large.isGreaterThan(small), is(true));
+ }
}
diff --git a/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java b/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java
index 2c78f74..727c0f7 100644
--- a/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java
+++ b/utils/misc/src/test/java/org/onlab/util/FrequencyTest.java
@@ -20,7 +20,9 @@
import org.onlab.junit.ImmutableClassChecker;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.lessThan;
public class FrequencyTest {
@@ -53,6 +55,21 @@
}
/**
+ * Tests the first object is less than the second object.
+ */
+ @Test
+ public void testLessThan() {
+ assertThat(frequency1, is(lessThan(frequency2)));
+ assertThat(frequency1.isLessThan(frequency2), is(true));
+ }
+
+ @Test
+ public void testGreaterThan() {
+ assertThat(frequency2, is(greaterThan(frequency1)));
+ assertThat(frequency2.isGreaterThan(frequency1), is(true));
+ }
+
+ /**
* Tests add operation of two Frequencies.
*/
@Test