Repository: commons-numbers Updated Branches: refs/heads/feature__NUMBERS-51__field 15b7b2f10 -> 797e40250
NUMBERS-51: Unit tests for "Field". Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/b15facf8 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/b15facf8 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/b15facf8 Branch: refs/heads/feature__NUMBERS-51__field Commit: b15facf8a0f3fbc8c8e661f1e22ba25a3834563c Parents: 15b7b2f Author: Gilles Sadowski <gil...@harfang.homelinux.org> Authored: Tue Dec 26 21:35:44 2017 +0100 Committer: Gilles Sadowski <gil...@harfang.homelinux.org> Committed: Tue Dec 26 21:35:44 2017 +0100 ---------------------------------------------------------------------- .../numbers/field/FieldParametricTest.java | 113 +++++++++++++++++++ .../commons/numbers/field/FieldTestData.java | 60 ++++++++++ .../commons/numbers/field/FieldsList.java | 70 ++++++++++++ 3 files changed, 243 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/b15facf8/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java new file mode 100644 index 0000000..fc123b8 --- /dev/null +++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldParametricTest.java @@ -0,0 +1,113 @@ +/* + * 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 java.util.Arrays; +import java.util.List; +import java.util.ArrayList; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Tests for fields. + */ +@RunWith(value=Parameterized.class) +public class FieldParametricTest { + /** Field under test. */ + private final Field field; + private final Object a; + private final Object b; + private final Object c; + + /** + * Initializes data instance. + * + * @param data Field data to be tested. + */ + public FieldParametricTest(FieldTestData data) { + this.field = data.getField(); + this.a = data.getA(); + this.b = data.getB(); + this.c = data.getC(); + } + + @Parameters(name = "{index}: data={0}") + public static Iterable<FieldTestData[]> getList() { + return FieldsList.list(); + } + + @Test + public void testAdditionAssociativity() { + final Object r1 = field.add(field.add(a, b), c); + final Object r2 = field.add(a, field.add(b, c)); + Assert.assertTrue(r1.equals(r2)); + } + @Test + public void testAdditionCommutativity() { + final Object r1 = field.add(a, b); + final Object r2 = field.add(b, a); + Assert.assertTrue(r1.equals(r2)); + } + @Test + public void testAdditiveIdentity() { + final Object r1 = field.add(a, field.zero()); + final Object r2 = a; + Assert.assertTrue(r1.equals(r2)); + } + @Test + public void testAdditiveInverse() { + final Object r1 = field.add(a, field.negate(a)); + final Object r2 = field.zero(); + Assert.assertTrue(r1.equals(r2)); + } + + @Test + public void testMultiplicationAssociativity() { + final Object r1 = field.multiply(field.multiply(a, b), c); + final Object r2 = field.multiply(a, field.multiply(b, c)); + Assert.assertTrue(r1.equals(r2)); + } + @Test + public void testMultiplicationCommutativity() { + final Object r1 = field.multiply(a, b); + final Object r2 = field.multiply(b, a); + Assert.assertTrue(r1.equals(r2)); + } + @Test + public void testMultiplicativeIdentity() { + final Object r1 = field.multiply(a, field.one()); + final Object r2 = a; + Assert.assertTrue(r1.equals(r2)); + } + @Test + public void testMultiplicativeInverse() { + final Object r1 = field.multiply(a, field.reciprocal(a)); + final Object r2 = field.one(); + Assert.assertTrue(r1.equals(r2)); + } + + @Test + public void testDistributivity() { + final Object r1 = field.multiply(a, field.add(b, c)); + final Object r2 = field.add(field.multiply(a, b), field.multiply(a, c)); + Assert.assertTrue(r1.equals(r2)); + } +} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/b15facf8/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldTestData.java ---------------------------------------------------------------------- diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldTestData.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldTestData.java new file mode 100644 index 0000000..82dc1a0 --- /dev/null +++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldTestData.java @@ -0,0 +1,60 @@ +/* + * 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 java.util.Arrays; + +/** + * Data store for {@link FieldParametricTest}. + */ +class FieldTestData { + private final Field<?> field; + private final Object a; + private final Object b; + private final Object c; + + public FieldTestData(Field<?> field, + Object a, + Object b, + Object c) { + this.field = field; + this.a = a; + this.b = b; + this.c = c; + } + + public Field<?> getField() { + return field; + } + + public Object getA() { + return a; + } + + public Object getB() { + return b; + } + + public Object getC() { + return c; + } + + @Override + public String toString() { + return field.toString() + " [a=" + a + ", b=" + b + ", c=" + c + "]"; + } +} http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/b15facf8/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java ---------------------------------------------------------------------- diff --git a/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java new file mode 100644 index 0000000..e937e0c --- /dev/null +++ b/commons-numbers-field/src/test/java/org/apache/commons/numbers/field/FieldsList.java @@ -0,0 +1,70 @@ +/* + * 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 java.util.Arrays; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + +import org.apache.commons.numbers.fraction.Fraction; + +/** + * List of samplers. + */ +public class FieldsList { + /** List of all fields implemented in the library. */ + private static final List<FieldTestData[]> LIST = + new ArrayList<FieldTestData[]>(); + + static { + try { + // List of fields to test. + add(new FractionField(), + new Fraction(13, 4), + new Fraction(5, 29), + new Fraction(-279, 11)); + + } catch (Exception e) { + System.err.println("Unexpected exception while creating the list of fields: " + e); + e.printStackTrace(System.err); + throw new RuntimeException(e); + } + } + + /** + * @param field Field. + * @param a Field element. + * @param b Field element. + */ + private static <T> void add(Field<T> field, + T a, + T b, + T c) { + LIST.add(new FieldTestData[] { new FieldTestData(field, a, b, c) }); + } + + /** + * Subclasses that are "parametric" tests can forward the call to + * the "@Parameters"-annotated method to this method. + * + * @return the list of all fields. + */ + public static Iterable<FieldTestData[]> list() { + return Collections.unmodifiableList(LIST); + } +}