From 91c5101c3352dae3da0f2d73762d5a89258a20ac Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Tue, 16 Jul 2013 09:15:32 +0000 Subject: [PATCH] Add some tests of NaNs git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1503623 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/math3/stat/FrequencyTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java b/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java index edf61788c..980384bc3 100644 --- a/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java +++ b/src/test/java/org/apache/commons/math3/stat/FrequencyTest.java @@ -24,6 +24,7 @@ import java.util.List; import org.apache.commons.math3.TestUtils; +import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -357,5 +358,63 @@ public final class FrequencyTest { Assert.assertEquals("2", mode.get(0)); Assert.assertFalse(mode.contains("1")); Assert.assertTrue(mode.contains("2")); + + try { + f.addValue(Double.valueOf(Double.NaN)); + Assert.fail("Expected MathIllegalArgumentException"); + } catch (MathIllegalArgumentException e) { + // expected + } } + + @Test + public void testModeDoubleNan() { + List> mode; + f.addValue(Double.valueOf(Double.NaN)); + f.addValue(Double.valueOf(Double.NaN)); + f.addValue(Double.valueOf(Double.NaN)); + f.addValue(Double.valueOf(Double.NEGATIVE_INFINITY)); + f.addValue(Double.valueOf(Double.POSITIVE_INFINITY)); + f.addValue(Double.valueOf(Double.NEGATIVE_INFINITY)); + f.addValue(Double.valueOf(Double.POSITIVE_INFINITY)); + f.addValue(Double.valueOf(Double.NEGATIVE_INFINITY)); + f.addValue(Double.valueOf(Double.POSITIVE_INFINITY)); + mode = f.getMode(); + Assert.assertEquals(3, mode.size()); + Assert.assertEquals(Double.valueOf(Double.NEGATIVE_INFINITY), mode.get(0)); + Assert.assertEquals(Double.valueOf(Double.POSITIVE_INFINITY), mode.get(1)); + Assert.assertEquals(Double.valueOf(Double.NaN), mode.get(2)); + try { + f.addValue(Float.valueOf(Float.NaN)); + Assert.fail("Expected MathIllegalArgumentException"); + } catch (MathIllegalArgumentException e) { + // expected + } + } + + @Test + public void testModeFloatNan() { + List> mode; + f.addValue(Float.valueOf(Float.NaN)); + f.addValue(Float.valueOf(Float.NaN)); + f.addValue(Float.valueOf(Float.NaN)); + f.addValue(Float.valueOf(Float.NEGATIVE_INFINITY)); + f.addValue(Float.valueOf(Float.POSITIVE_INFINITY)); + f.addValue(Float.valueOf(Float.NEGATIVE_INFINITY)); + f.addValue(Float.valueOf(Float.POSITIVE_INFINITY)); + f.addValue(Float.valueOf(Float.NEGATIVE_INFINITY)); + f.addValue(Float.valueOf(Float.POSITIVE_INFINITY)); + mode = f.getMode(); + Assert.assertEquals(3, mode.size()); + Assert.assertEquals(Float.valueOf(Float.NEGATIVE_INFINITY), mode.get(0)); + Assert.assertEquals(Float.valueOf(Float.POSITIVE_INFINITY), mode.get(1)); + Assert.assertEquals(Float.valueOf(Float.NaN), mode.get(2)); + try { + f.addValue(Double.valueOf(Double.NaN)); + Assert.fail("Expected MathIllegalArgumentException"); + } catch (MathIllegalArgumentException e) { + // expected + } + } + }