aboutsummaryrefslogtreecommitdiff
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
commitd98e9c4d0e83b9c55826bc8b79f38d36846df1e3 (patch)
treed9433ce498dd94b1bbbef1993b6929b3a51f1d25 /java/src
parentc01060c8ca30bab1420d653a4d8541b764bd9c99 (diff)
downloadprotobuf-d98e9c4d0e83b9c55826bc8b79f38d36846df1e3.tar.gz
protobuf-d98e9c4d0e83b9c55826bc8b79f38d36846df1e3.tar.bz2
protobuf-d98e9c4d0e83b9c55826bc8b79f38d36846df1e3.zip
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/NanoTest.java26
1 files changed, 26 insertions, 0 deletions
diff --git a/java/src/test/java/com/google/protobuf/NanoTest.java b/java/src/test/java/com/google/protobuf/NanoTest.java
index 5a04b6f1..92ddda62 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.
*/