aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
authorChris Smith <cjs@google.com>2013-07-22 09:00:43 +0100
committerChris Smith <cjs@google.com>2013-07-23 19:13:36 +0100
commit0f2ca89132ab81441f7eb351c7a053a8c8d9d1c3 (patch)
treed30df349a57d0279cb3ff8b8b34459f3ebf22669 /java/src
parenta400007ed570bbcc638c05c59727b3527238ec70 (diff)
downloadexternal_protobuf-0f2ca89132ab81441f7eb351c7a053a8c8d9d1c3.zip
external_protobuf-0f2ca89132ab81441f7eb351c7a053a8c8d9d1c3.tar.gz
external_protobuf-0f2ca89132ab81441f7eb351c7a053a8c8d9d1c3.tar.bz2
Allow NaN/+inf/-inf defaults in micro/nano.
Adds support for default values of NaN, infinity and negative infinity for floats and doubles in both the nano and micro java compiler. Change-Id: Ibc43e5ebb073e51d9a8181f3aa23b72e10015dca
Diffstat (limited to 'java/src')
-rw-r--r--java/src/test/java/com/google/protobuf/MicroTest.java12
-rw-r--r--java/src/test/java/com/google/protobuf/NanoTest.java26
2 files changed, 38 insertions, 0 deletions
diff --git a/java/src/test/java/com/google/protobuf/MicroTest.java b/java/src/test/java/com/google/protobuf/MicroTest.java
index afca135..9e18feb 100644
--- a/java/src/test/java/com/google/protobuf/MicroTest.java
+++ b/java/src/test/java/com/google/protobuf/MicroTest.java
@@ -2139,6 +2139,18 @@ public class MicroTest extends TestCase {
assertEquals(MicroOuterClass.FOREIGN_MICRO_BAR, msg.getDefaultForeignEnum());
assertFalse(msg.hasDefaultImportEnum());
assertEquals(UnittestImportMicro.IMPORT_MICRO_BAR, msg.getDefaultImportEnum());
+ assertFalse(msg.hasDefaultFloatInf());
+ assertEquals(Float.POSITIVE_INFINITY, msg.getDefaultFloatInf());
+ assertFalse(msg.hasDefaultFloatNegInf());
+ assertEquals(Float.NEGATIVE_INFINITY, msg.getDefaultFloatNegInf());
+ assertFalse(msg.hasDefaultFloatNan());
+ assertEquals(Float.NaN, msg.getDefaultFloatNan());
+ assertFalse(msg.hasDefaultDoubleInf());
+ assertEquals(Double.POSITIVE_INFINITY, msg.getDefaultDoubleInf());
+ assertFalse(msg.hasDefaultDoubleNegInf());
+ assertEquals(Double.NEGATIVE_INFINITY, msg.getDefaultDoubleNegInf());
+ assertFalse(msg.hasDefaultDoubleNan());
+ assertEquals(Double.NaN, msg.getDefaultDoubleNan());
}
/**
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 5a04b6f..92ddda6 100644
--- a/java/src/test/java/com/google/protobuf/NanoTest.java
+++ b/java/src/test/java/com/google/protobuf/NanoTest.java
@@ -2062,6 +2062,12 @@ public class NanoTest extends TestCase {
assertEquals(TestAllTypesNano.BAR, msg.defaultNestedEnum);
assertEquals(NanoOuterClass.FOREIGN_NANO_BAR, msg.defaultForeignEnum);
assertEquals(UnittestImportNano.IMPORT_NANO_BAR, msg.defaultImportEnum);
+ assertEquals(Float.POSITIVE_INFINITY, msg.defaultFloatInf);
+ assertEquals(Float.NEGATIVE_INFINITY, msg.defaultFloatNegInf);
+ assertEquals(Float.NaN, msg.defaultFloatNan);
+ assertEquals(Double.POSITIVE_INFINITY, msg.defaultDoubleInf);
+ assertEquals(Double.NEGATIVE_INFINITY, msg.defaultDoubleNegInf);
+ assertEquals(Double.NaN, msg.defaultDoubleNan);
// Default values are not output, except for required fields.
byte [] result = MessageNano.toByteArray(msg);
@@ -2074,6 +2080,26 @@ public class NanoTest extends TestCase {
}
/**
+ * Tests that fields with a default value of NaN are not serialized when
+ * set to NaN. This is a special case as NaN != NaN, so normal equality
+ * checks don't work.
+ */
+ public void testNanoNotANumberDefaults() throws Exception {
+ TestAllTypesNano msg = new TestAllTypesNano();
+ msg.defaultDoubleNan = 0;
+ msg.defaultFloatNan = 0;
+ byte[] result = MessageNano.toByteArray(msg);
+ int msgSerializedSize = msg.getSerializedSize();
+ assertTrue(msgSerializedSize > 3);
+
+ msg.defaultDoubleNan = Double.NaN;
+ msg.defaultFloatNan = Float.NaN;
+ result = MessageNano.toByteArray(msg);
+ msgSerializedSize = msg.getSerializedSize();
+ assertEquals(3, msgSerializedSize);
+ }
+
+ /**
* Test that a bug in skipRawBytes() has been fixed: if the skip skips
* exactly up to a limit, this should not break things.
*/