View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.numbers.field;
18  
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.params.ParameterizedTest;
21  import org.junit.jupiter.params.provider.MethodSource;
22  
23  import java.util.stream.Stream;
24  
25  import org.apache.commons.numbers.core.Addition;
26  import org.apache.commons.numbers.core.Multiplication;
27  
28  /**
29   * Tests for fields.
30   */
31  class FieldParametricTest {
32  
33      private static Stream<FieldTestData<?>> getList() {
34          return FieldsList.list().stream();
35      }
36  
37      @ParameterizedTest
38      @MethodSource("getList")
39      <T> void testAdditionAssociativity(FieldTestData<T> data) {
40          Field<T> field = data.getField();
41          T a = data.getA();
42          T b = data.getB();
43          T c = data.getC();
44          final T r1 = field.add(field.add(a, b), c);
45          final T r2 = field.add(a, field.add(b, c));
46          assertEquals(r1, r2);
47      }
48  
49      @ParameterizedTest
50      @MethodSource("getList")
51      <T> void testAdditionCommutativity(FieldTestData<T> data) {
52          Field<T> field = data.getField();
53          T a = data.getA();
54          T b = data.getB();
55          final T r1 = field.add(a, b);
56          final T r2 = field.add(b, a);
57          assertEquals(r1, r2);
58      }
59  
60      @ParameterizedTest
61      @MethodSource("getList")
62      <T> void testAdditiveIdentity(FieldTestData<T> data) {
63          Field<T> field = data.getField();
64          T a = data.getA();
65          final T r1 = field.add(a, field.zero());
66          final T r2 = a;
67          assertEquals(r1, r2);
68      }
69  
70      @ParameterizedTest
71      @MethodSource("getList")
72      <T> void testAdditiveInverse(FieldTestData<T> data) {
73          Field<T> field = data.getField();
74          T a = data.getA();
75          final T r1 = field.add(a, field.negate(a));
76          final T r2 = field.zero();
77          assertEquals(r1, r2);
78      }
79  
80      @ParameterizedTest
81      @MethodSource("getList")
82      <T> void testMultiplicationAssociativity(FieldTestData<T> data) {
83          Field<T> field = data.getField();
84          T a = data.getA();
85          T b = data.getB();
86          T c = data.getC();
87          final T r1 = field.multiply(field.multiply(a, b), c);
88          final T r2 = field.multiply(a, field.multiply(b, c));
89          assertEquals(r1, r2);
90      }
91  
92      @ParameterizedTest
93      @MethodSource("getList")
94      <T> void testMultiplicationCommutativity(FieldTestData<T> data) {
95          Field<T> field = data.getField();
96          T a = data.getA();
97          T b = data.getB();
98          final T r1 = field.multiply(a, b);
99          final T r2 = field.multiply(b, a);
100         assertEquals(r1, r2);
101     }
102 
103     @ParameterizedTest
104     @MethodSource("getList")
105     <T> void testMultiplicativeIdentity(FieldTestData<T> data) {
106         Field<T> field = data.getField();
107         T a = data.getA();
108         final T r1 = field.multiply(a, field.one());
109         final T r2 = a;
110         assertEquals(r1, r2);
111     }
112 
113     @ParameterizedTest
114     @MethodSource("getList")
115     <T> void testMultiplicativeInverse(FieldTestData<T> data) {
116         Field<T> field = data.getField();
117         T a = data.getA();
118         final T r1 = field.multiply(a, field.reciprocal(a));
119         final T r2 = field.one();
120         assertEquals(r1, r2);
121     }
122 
123     @ParameterizedTest
124     @MethodSource("getList")
125     <T> void testDistributivity(FieldTestData<T> data) {
126         Field<T> field = data.getField();
127         T a = data.getA();
128         T b = data.getB();
129         T c = data.getC();
130         final T r1 = field.multiply(a, field.add(b, c));
131         final T r2 = field.add(field.multiply(a, b), field.multiply(a, c));
132         assertEquals(r1, r2);
133     }
134 
135     @ParameterizedTest
136     @MethodSource("getList")
137     <T extends Addition<T>> void testAdd(FieldTestData<T> data) {
138         Field<T> field = data.getField();
139         T a = data.getA();
140         T b = data.getB();
141 
142         final T r1 = field.add(a, b);
143         final T r2 = a.add(b);
144         assertEquals(r1, r2);
145     }
146 
147     @ParameterizedTest
148     @MethodSource("getList")
149     <T extends Addition<T>> void testSubtract(FieldTestData<T> data) {
150         Field<T> field = data.getField();
151         T a = data.getA();
152         T b = data.getB();
153 
154         final T r1 = field.subtract(a, b);
155         final T r2 = a.add(b.negate());
156         assertEquals(r1, r2);
157     }
158 
159     @ParameterizedTest
160     @MethodSource("getList")
161     <T extends Addition<T>> void testMultiplyInt(FieldTestData<T> data) {
162         Field<T> field = data.getField();
163         T a = data.getA();
164         final int n = 5;
165 
166         final T r1 = field.multiply(n, a);
167 
168         T r2 = field.zero();
169         for (int i = 0; i < n; i++) {
170             r2 = r2.add(a);
171         }
172 
173         assertEquals(r1, r2);
174     }
175 
176     @ParameterizedTest
177     @MethodSource("getList")
178     <T extends Addition<T>> void testZero(FieldTestData<T> data) {
179         Field<T> field = data.getField();
180         T a = data.getA();
181 
182         final T r1 = field.zero();
183         final T r2 = a.zero();
184         assertEquals(r1, r2);
185     }
186 
187     @ParameterizedTest
188     @MethodSource("getList")
189     <T extends Multiplication<T>> void testMultiply(FieldTestData<T> data) {
190         Field<T> field = data.getField();
191         T a = data.getA();
192         T b = data.getB();
193 
194         final T r1 = field.multiply(a, b);
195         final T r2 = a.multiply(b);
196         assertEquals(r1, r2);
197     }
198 
199     @ParameterizedTest
200     @MethodSource("getList")
201     <T extends Multiplication<T>> void testDivide(FieldTestData<T> data) {
202         Field<T> field = data.getField();
203         T a = data.getA();
204         T b = data.getB();
205 
206         final T r1 = field.divide(a, b);
207         final T r2 = a.multiply(b.reciprocal());
208         assertEquals(r1, r2);
209     }
210 
211     @ParameterizedTest
212     @MethodSource("getList")
213     <T extends Multiplication<T>> void testOne(FieldTestData<T> data) {
214         Field<T> field = data.getField();
215         T a = data.getA();
216 
217         final T r1 = field.one();
218         final T r2 = a.one();
219         assertEquals(r1, r2);
220     }
221 
222     /**
223      * @param a Instance.
224      * @param b Instance.
225      */
226     private static void assertEquals(Object a,
227                                      Object b) {
228         Assertions.assertEquals(a, b, a + " != " + b);
229     }
230 }