aboutsummaryrefslogtreecommitdiff
path: root/android/guava-tests/test/com/google/common/math/StatsTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'android/guava-tests/test/com/google/common/math/StatsTest.java')
-rw-r--r--android/guava-tests/test/com/google/common/math/StatsTest.java135
1 files changed, 25 insertions, 110 deletions
diff --git a/android/guava-tests/test/com/google/common/math/StatsTest.java b/android/guava-tests/test/com/google/common/math/StatsTest.java
index 76de5b55c..9fe9a96b1 100644
--- a/android/guava-tests/test/com/google/common/math/StatsTest.java
+++ b/android/guava-tests/test/com/google/common/math/StatsTest.java
@@ -69,6 +69,7 @@ import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.NaN;
import static java.lang.Double.POSITIVE_INFINITY;
import static java.lang.Math.sqrt;
+import static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.math.StatsTesting.ManyValues;
@@ -104,16 +105,8 @@ public class StatsTest extends TestCase {
}
public void testMean() {
- try {
- EMPTY_STATS_VARARGS.mean();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- EMPTY_STATS_ITERABLE.mean();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.mean());
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.mean());
assertThat(ONE_VALUE_STATS.mean()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
assertThat(Stats.of(POSITIVE_INFINITY).mean()).isPositiveInfinity();
assertThat(Stats.of(NEGATIVE_INFINITY).mean()).isNegativeInfinity();
@@ -196,16 +189,8 @@ public class StatsTest extends TestCase {
}
public void testPopulationVariance() {
- try {
- EMPTY_STATS_VARARGS.populationVariance();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- EMPTY_STATS_ITERABLE.populationVariance();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationVariance());
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationVariance());
assertThat(ONE_VALUE_STATS.populationVariance()).isWithin(0.0).of(0.0);
assertThat(Stats.of(POSITIVE_INFINITY).populationVariance()).isNaN();
assertThat(Stats.of(NEGATIVE_INFINITY).populationVariance()).isNaN();
@@ -256,16 +241,10 @@ public class StatsTest extends TestCase {
}
public void testPopulationStandardDeviation() {
- try {
- EMPTY_STATS_VARARGS.populationStandardDeviation();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- EMPTY_STATS_ITERABLE.populationStandardDeviation();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
+ assertThrows(
+ IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationStandardDeviation());
+ assertThrows(
+ IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationStandardDeviation());
assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isWithin(0.0).of(0.0);
assertThat(TWO_VALUES_STATS.populationStandardDeviation())
.isWithin(ALLOWED_ERROR)
@@ -297,21 +276,9 @@ public class StatsTest extends TestCase {
}
public void testSampleVariance() {
- try {
- EMPTY_STATS_VARARGS.sampleVariance();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- EMPTY_STATS_ITERABLE.sampleVariance();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- ONE_VALUE_STATS.sampleVariance();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.sampleVariance());
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.sampleVariance());
+ assertThrows(IllegalStateException.class, () -> ONE_VALUE_STATS.sampleVariance());
assertThat(TWO_VALUES_STATS.sampleVariance())
.isWithin(ALLOWED_ERROR)
.of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS);
@@ -342,21 +309,9 @@ public class StatsTest extends TestCase {
}
public void testSampleStandardDeviation() {
- try {
- EMPTY_STATS_VARARGS.sampleStandardDeviation();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- EMPTY_STATS_ITERABLE.sampleStandardDeviation();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- ONE_VALUE_STATS.sampleStandardDeviation();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.sampleStandardDeviation());
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.sampleStandardDeviation());
+ assertThrows(IllegalStateException.class, () -> ONE_VALUE_STATS.sampleStandardDeviation());
assertThat(TWO_VALUES_STATS.sampleStandardDeviation())
.isWithin(ALLOWED_ERROR)
.of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS));
@@ -387,16 +342,8 @@ public class StatsTest extends TestCase {
}
public void testMax() {
- try {
- EMPTY_STATS_VARARGS.max();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- EMPTY_STATS_ITERABLE.max();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.max());
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.max());
assertThat(ONE_VALUE_STATS.max()).isEqualTo(ONE_VALUE);
assertThat(Stats.of(POSITIVE_INFINITY).max()).isPositiveInfinity();
assertThat(Stats.of(NEGATIVE_INFINITY).max()).isNegativeInfinity();
@@ -424,16 +371,8 @@ public class StatsTest extends TestCase {
}
public void testMin() {
- try {
- EMPTY_STATS_VARARGS.min();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
- try {
- EMPTY_STATS_ITERABLE.min();
- fail("Expected IllegalStateException");
- } catch (IllegalStateException expected) {
- }
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.min());
+ assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.min());
assertThat(ONE_VALUE_STATS.min()).isEqualTo(ONE_VALUE);
assertThat(Stats.of(POSITIVE_INFINITY).min()).isPositiveInfinity();
assertThat(Stats.of(NEGATIVE_INFINITY).min()).isNegativeInfinity();
@@ -505,16 +444,8 @@ public class StatsTest extends TestCase {
}
public void testMeanOf() {
- try {
- Stats.meanOf();
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException expected) {
- }
- try {
- Stats.meanOf(ImmutableList.<Number>of());
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException expected) {
- }
+ assertThrows(IllegalArgumentException.class, () -> Stats.meanOf());
+ assertThrows(IllegalArgumentException.class, () -> Stats.meanOf(ImmutableList.<Number>of()));
assertThat(Stats.meanOf(ONE_VALUE)).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
assertThat(Stats.meanOf(POSITIVE_INFINITY)).isPositiveInfinity();
assertThat(Stats.meanOf(NEGATIVE_INFINITY)).isNegativeInfinity();
@@ -565,19 +496,11 @@ public class StatsTest extends TestCase {
}
public void testFromByteArray_withNullInputThrowsNullPointerException() {
- try {
- Stats.fromByteArray(null);
- fail("Expected NullPointerException");
- } catch (NullPointerException expected) {
- }
+ assertThrows(NullPointerException.class, () -> Stats.fromByteArray(null));
}
public void testFromByteArray_withEmptyArrayInputThrowsIllegalArgumentException() {
- try {
- Stats.fromByteArray(new byte[0]);
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException expected) {
- }
+ assertThrows(IllegalArgumentException.class, () -> Stats.fromByteArray(new byte[0]));
}
public void testFromByteArray_withTooLongArrayInputThrowsIllegalArgumentException() {
@@ -588,11 +511,7 @@ public class StatsTest extends TestCase {
.put(buffer)
.putChar('.')
.array();
- try {
- Stats.fromByteArray(tooLongByteArray);
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException expected) {
- }
+ assertThrows(IllegalArgumentException.class, () -> Stats.fromByteArray(tooLongByteArray));
}
public void testFromByteArrayWithTooShortArrayInputThrowsIllegalArgumentException() {
@@ -602,10 +521,6 @@ public class StatsTest extends TestCase {
.order(ByteOrder.LITTLE_ENDIAN)
.put(buffer, 0, Stats.BYTES - 1)
.array();
- try {
- Stats.fromByteArray(tooShortByteArray);
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException expected) {
- }
+ assertThrows(IllegalArgumentException.class, () -> Stats.fromByteArray(tooShortByteArray));
}
}