U128: fix signedness bug in compareTo
compareTo was not correctly handling values that have the signedness bit set.
diff --git a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java
index af76aee..35ef846 100644
--- a/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java
+++ b/java_gen/pre-written/src/main/java/org/projectfloodlight/openflow/types/U128.java
@@ -1,8 +1,11 @@
package org.projectfloodlight.openflow.types;
+import javax.annotation.Nonnull;
+
import org.jboss.netty.buffer.ChannelBuffer;
import com.google.common.hash.PrimitiveSink;
+import com.google.common.primitives.UnsignedLongs;
public class U128 implements OFValueType<U128>, HashValue<U128> {
@@ -85,11 +88,12 @@
}
@Override
- public int compareTo(U128 o) {
- long c = this.raw1 - o.raw1;
- if (c != 0)
- return Long.signum(c);
- return Long.signum(this.raw2 - o.raw2);
+ public int compareTo(@Nonnull U128 o) {
+ int msb = UnsignedLongs.compare(this.raw1, o.raw1);
+ if(msb != 0)
+ return msb;
+ else
+ return UnsignedLongs.compare(this.raw2, o.raw2);
}
@Override
diff --git a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java
index 310dab0..fb5cd23 100644
--- a/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java
+++ b/java_gen/pre-written/src/test/java/org/projectfloodlight/openflow/types/U128Test.java
@@ -9,6 +9,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import org.hamcrest.Matchers;
import org.junit.Test;
import com.google.common.hash.HashCode;
@@ -122,6 +123,45 @@
}
}
+
+ @Test
+ public void testCompare() {
+ U128 u0_0 = U128.of(0, 0);
+ U128 u0_1 = U128.of(0, 1);
+ U128 u0_8 = U128.of(0, 0x8765_4321_8765_4321L);
+ U128 u1_0 = U128.of(0x1234_5678_1234_5678L, 0);
+ U128 u8_0 = U128.of(0x8765_4321_8765_4321L, 0);
+ U128 uf_0 = U128.of(0xFFFF_FFFF_FFFF_FFFFL, 0);
+
+ U128[] us = new U128[] { u0_0, u0_1, u0_8, u1_0, u8_0, uf_0 };
+
+ for(int i = 0; i< us.length; i++) {
+ U128 u_base = us[i];
+ assertThat(
+ String.format("%s should be equal to itself (compareTo)", u_base),
+ u_base.compareTo(u_base), equalTo(0));
+ assertThat(
+ String.format("%s should be equal to itself (equals)", u_base),
+ u_base.equals(u_base), equalTo(true));
+ assertThat(
+ String.format("%s should be equal to itself (equals, by value)", u_base),
+ u_base.equals(U128.of(u_base.getMsb(), u_base.getLsb())), equalTo(true));
+
+ for(int j = i+1; j< us.length; j++) {
+ U128 u_greater = us[j];
+ assertThat(
+ String.format("%s should not be equal to %s", u_base, u_greater),
+ u_base.equals(u_base), equalTo(true));
+ assertThat(
+ String.format("%s should be smaller than %s", u_base, u_greater),
+ u_base.compareTo(u_greater), Matchers.lessThan(0));
+ assertThat(
+ String.format("%s should be greater than %s", u_greater, u_base),
+ u_greater.compareTo(u_base), Matchers.greaterThan(0));
+ }
+ }
+ }
+
@Test
public void testCombine() {
long key = 0x1234567890abcdefL;