Skip to content

Commit

Permalink
Unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles Sadowski committed Oct 1, 2019
1 parent c3c7c1d commit 9ad130f
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.numbers.field;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link FP64}.
*/
public class FP64Test {
@Test
public void testConsistencyWithDouble() {
final double v = -5.67e89;
final Double a = Double.valueOf(v);
final FP64 b = FP64.of(v);

Assertions.assertEquals(a.doubleValue(), b.doubleValue(), 0d);
Assertions.assertEquals(a.floatValue(), b.floatValue(), 0f);
Assertions.assertEquals(a.intValue(), b.intValue());
Assertions.assertEquals(a.longValue(), b.longValue());
Assertions.assertEquals(a.byteValue(), b.byteValue());
Assertions.assertEquals(a.hashCode(), b.hashCode());
}

@Test
public void testOne() {
Assertions.assertEquals(1d, FP64.of(-3.4).one().doubleValue(), 0d);
}
@Test
public void testZero() {
Assertions.assertEquals(0d, FP64.of(-3.4).zero().doubleValue(), 0d);
}

@Test
public void testSubtract() {
final double a = 123.4;
final double b = 5678.9;

Assertions.assertEquals(a - b, FP64.of(a).subtract(FP64.of(b)).doubleValue(), 0d);
}
@Test
public void testDivide() {
final double a = 123.4;
final double b = 5678.9;

Assertions.assertEquals(a / b, FP64.of(a).divide(FP64.of(b)).doubleValue(), 0d);
}

@Test
public void testMultiplyInt() {
final double a = 123.4;
final int n = 3456789;

Assertions.assertEquals(n * a, FP64.of(a).multiply(n).doubleValue(), 0d);
}

@Test
public void testPowInt() {
final double a = 123.4;
final int n = 5;

Assertions.assertEquals(Math.pow(a, n), FP64.of(a).pow(n).doubleValue(), 0d);
}
@Test
public void testZeroPow() {
Assertions.assertSame(FP64.of(9876.5).one(), FP64.of(2.3456).pow(0));
}

@Test
public void testCompare() {
Assertions.assertTrue(FP64.of(0).compareTo(FP64.of(-1)) > 0);
Assertions.assertTrue(FP64.of(1).compareTo(FP64.of(2)) < 0);

final double v = 123.45;
Assertions.assertTrue(FP64.of(v).compareTo(FP64.of(v)) == 0);
}
}
Expand Up @@ -22,6 +22,9 @@

import java.util.stream.Stream;

import org.apache.commons.numbers.core.Addition;
import org.apache.commons.numbers.core.Multiplication;

/**
* Tests for fields.
*/
Expand Down Expand Up @@ -129,6 +132,93 @@ public <T> void testDistributivity(FieldTestData<T> data) {
assertEquals(r1, r2);
}

@ParameterizedTest
@MethodSource("getList")
public <T extends Addition<T>> void testAdd(FieldTestData<T> data) {
Field<T> field = data.getField();
T a = data.getA();
T b = data.getB();

final T r1 = field.add(a, b);
final T r2 = a.add(b);
assertEquals(r1, r2);
}

@ParameterizedTest
@MethodSource("getList")
public <T extends Addition<T>> void testSubtract(FieldTestData<T> data) {
Field<T> field = data.getField();
T a = data.getA();
T b = data.getB();

final T r1 = field.subtract(a, b);
final T r2 = a.add(b.negate());
assertEquals(r1, r2);
}

@ParameterizedTest
@MethodSource("getList")
public <T extends Addition<T>> void testMultiplyInt(FieldTestData<T> data) {
Field<T> field = data.getField();
T a = data.getA();
final int n = 5;

final T r1 = field.multiply(n, a);

T r2 = field.zero();
for (int i = 0; i < n; i++) {
r2 = r2.add(a);
}

assertEquals(r1, r2);
}

@ParameterizedTest
@MethodSource("getList")
public <T extends Addition<T>> void testZero(FieldTestData<T> data) {
Field<T> field = data.getField();
T a = data.getA();

final T r1 = field.zero();
final T r2 = a.zero();
assertEquals(r1, r2);
}

@ParameterizedTest
@MethodSource("getList")
public <T extends Multiplication<T>> void testMultiply(FieldTestData<T> data) {
Field<T> field = data.getField();
T a = data.getA();
T b = data.getB();

final T r1 = field.multiply(a, b);
final T r2 = a.multiply(b);
assertEquals(r1, r2);
}

@ParameterizedTest
@MethodSource("getList")
public <T extends Multiplication<T>> void testDivide(FieldTestData<T> data) {
Field<T> field = data.getField();
T a = data.getA();
T b = data.getB();

final T r1 = field.divide(a, b);
final T r2 = a.multiply(b.reciprocal());
assertEquals(r1, r2);
}

@ParameterizedTest
@MethodSource("getList")
public <T extends Multiplication<T>> void testOne(FieldTestData<T> data) {
Field<T> field = data.getField();
T a = data.getA();

final T r1 = field.one();
final T r2 = a.one();
assertEquals(r1, r2);
}

/**
* @param a Instance.
* @param b Instance.
Expand Down

0 comments on commit 9ad130f

Please sign in to comment.