LightJason - AgentSpeak(L++)
TestCActionMath.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
26 import com.codepoetics.protonpack.StreamUtils;
27 import com.tngtech.java.junit.dataprovider.DataProvider;
28 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
29 import com.tngtech.java.junit.dataprovider.UseDataProvider;
30 import org.apache.commons.lang3.tuple.ImmutableTriple;
31 import org.apache.commons.lang3.tuple.Triple;
32 import org.apache.commons.math3.primes.Primes;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
78 
79 import java.lang.reflect.InvocationTargetException;
80 import java.util.ArrayList;
81 import java.util.Arrays;
82 import java.util.List;
83 import java.util.Random;
84 import java.util.function.Function;
85 import java.util.stream.Collectors;
86 import java.util.stream.IntStream;
87 import java.util.stream.Stream;
88 
89 
93 @RunWith( DataProviderRunner.class )
94 public final class TestCActionMath extends IBaseTest
95 {
96 
101  @DataProvider
102  public static Object[] singlevaluegenerate()
103  {
104  return Stream.concat(
105 
106  singlevaluetestcase(
107 
108  Stream.of( 2.5, 9.1, 111.7, 889.9 ),
109 
110  Stream.of(
111  CNextPrime.class
112  ),
113 
114  i -> (double) Primes.nextPrime( i.intValue() )
115  ),
116 
117  singlevaluetestcase(
118 
119  Stream.of( -2, -6, 4, -1, -5, 3, 49, 30, 6, 5, 1.3, 2.8, 9.7, 1, 8, 180, Math.PI ),
120 
121  Stream.of(
122  CAbs.class,
123  CACos.class,
124  CASin.class,
125  CATan.class,
126  CCeil.class,
127  CCos.class,
128  CCosh.class,
129  CDegrees.class,
130  CExp.class,
131  CIsPrime.class,
132  CLog.class,
133  CLog10.class,
134  CFloor.class,
135  CRadians.class,
136  CRound.class,
137  CSignum.class,
138  CSin.class,
139  CSinh.class,
140  CSqrt.class,
141  CTan.class,
142  CTanh.class
143  ),
144 
145  i -> Math.abs( i.doubleValue() ),
146  i -> Math.acos( i.doubleValue() ),
147  i -> Math.asin( i.doubleValue() ),
148  i -> Math.atan( i.doubleValue() ),
149  i -> Math.ceil( i.doubleValue() ),
150  i -> Math.cos( i.doubleValue() ),
151  i -> Math.cosh( i.doubleValue() ),
152  i -> Math.toDegrees( i.doubleValue() ),
153  i -> Math.exp( i.doubleValue() ),
154  i -> Primes.isPrime( i.intValue() ),
155  i -> Math.log( i.doubleValue() ),
156  i -> Math.log10( i.doubleValue() ),
157  i -> Math.floor( i.doubleValue() ),
158  i -> Math.toRadians( i.doubleValue() ),
159  i -> Math.round( i.doubleValue() ),
160  i -> Math.signum( i.doubleValue() ),
161  i -> Math.sin( i.doubleValue() ),
162  i -> Math.sinh( i.doubleValue() ),
163  i -> Math.sqrt( i.doubleValue() ),
164  i -> Math.tan( i.doubleValue() ),
165  i -> Math.tanh( i.doubleValue() )
166  )
167 
168  ).toArray();
169  }
170 
171 
176  @DataProvider
177  public static Object[] aggregationvaluegenerate()
178  {
179  final Random l_random = new Random();
180 
181  return aggregationvaluetestcase(
182 
183  IntStream.range( 0, 100 ).boxed().map( i -> l_random.nextGaussian() ),
184 
185  Stream.of(
186  CAverage.class,
187  CSum.class,
188  CMin.class,
189  CMax.class
190  ),
191 
192  i -> i.mapToDouble( Number::doubleValue ).average().getAsDouble(),
193  i -> i.mapToDouble( Number::doubleValue ).sum(),
194  i -> i.mapToDouble( Number::doubleValue ).min().getAsDouble(),
195  i -> i.mapToDouble( Number::doubleValue ).max().getAsDouble()
196 
197  ).toArray();
198  }
199 
200 
209  @SafeVarargs
210  @SuppressWarnings( "varargs" )
211  private static Stream<Object> singlevaluetestcase( final Stream<Number> p_input, final Stream<Class<? extends IAction>> p_class,
212  final Function<Number, ?>... p_result )
213  {
214  final List<ITerm> l_input = p_input.map( CRawTerm::from ).collect( Collectors.toList() );
215 
216  return StreamUtils.zip(
217  p_class,
218  Arrays.stream( p_result ),
219  ( i, j ) -> new ImmutableTriple<>( l_input, i, j )
220  );
221  }
222 
223 
232  @SafeVarargs
233  @SuppressWarnings( "varargs" )
234  private static Stream<Object> aggregationvaluetestcase( final Stream<Number> p_input, final Stream<Class<? extends IAction>> p_class,
235  final Function<Stream<Number>, ?>... p_result )
236  {
237  final List<ITerm> l_input = p_input.map( CRawTerm::from ).collect( Collectors.toList() );
238 
239  return StreamUtils.zip(
240  p_class,
241  Arrays.stream( p_result ),
242  ( i, j ) -> new ImmutableTriple<>( l_input, i, j )
243  );
244  }
245 
246 
257  @Test
258  @UseDataProvider( "aggregationvaluegenerate" )
259  public final void aggregationvalueaction( final Triple<List<ITerm>, Class<? extends IAction>, Function<Stream<Number>, ?>> p_input )
260  throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
261  {
262  final List<ITerm> l_return = new ArrayList<>();
263 
264  p_input.getMiddle().getConstructor().newInstance().execute(
265  false, IContext.EMPTYPLAN,
266  p_input.getLeft(),
267  l_return
268  );
269 
270  Assert.assertEquals( l_return.size(), 1 );
271  Assert.assertEquals( l_return.get( 0 ).raw(), p_input.getRight().apply( p_input.getLeft().stream().map( ITerm::<Number>raw ) ) );
272  }
273 
274 
285  @Test
286  @UseDataProvider( "singlevaluegenerate" )
287  public final void singlevalueaction( final Triple<List<ITerm>, Class<? extends IAction>, Function<Number, ?>> p_input )
288  throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
289  {
290  final List<ITerm> l_return = new ArrayList<>();
291 
292  p_input.getMiddle().getConstructor().newInstance().execute(
293  false, IContext.EMPTYPLAN,
294  p_input.getLeft(),
295  l_return
296  );
297 
298  Assert.assertArrayEquals(
299  p_input.getMiddle().toGenericString(),
300  l_return.stream().map( ITerm::raw ).toArray(),
301  p_input.getLeft().stream().map( ITerm::<Number>raw ).map( p_input.getRight() ).toArray()
302  );
303  }
304 
305 
309  @Test
310  public final void binomial()
311  {
312  final List<ITerm> l_return = new ArrayList<>();
313 
314  new CBinomial().execute(
315  false, IContext.EMPTYPLAN,
316  Stream.of( 49, 30, 6, 5 ).map( CRawTerm::from ).collect( Collectors.toList() ),
317  l_return
318  );
319 
320  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 18851684897584L );
321  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 6L );
322  }
323 
324 
328  @Test
329  public final void factorial()
330  {
331  final List<ITerm> l_return = new ArrayList<>();
332 
333  new CFactorial().execute(
334  false, IContext.EMPTYPLAN,
335  Stream.of( 5, 1, 2, 3, 4 ).map( CRawTerm::from ).collect( Collectors.toList() ),
336  l_return
337  );
338 
339  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 120L );
340  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 1L );
341  Assert.assertEquals( l_return.get( 2 ).<Number>raw(), 2L );
342  Assert.assertEquals( l_return.get( 3 ).<Number>raw(), 6L );
343  Assert.assertEquals( l_return.get( 4 ).<Number>raw(), 24L );
344  }
345 
346 
350  @Test
351  public final void primefactors()
352  {
353  final List<ITerm> l_return = new ArrayList<>();
354 
355  new CPrimeFactors().execute(
356  false, IContext.EMPTYPLAN,
357  Stream.of( 8, 120 ).map( CRawTerm::from ).collect( Collectors.toList() ),
358  l_return
359  );
360 
361  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( 2D, 2D, 2D ).toArray() );
362  Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), Stream.of( 2D, 2D, 2D, 3D, 5D ).toArray() );
363  }
364 
365 
369  @Test
370  public final void sigmoid()
371  {
372  final List<ITerm> l_return = new ArrayList<>();
373 
374  new CSigmoid().execute(
375  false, IContext.EMPTYPLAN,
376  Stream.of( 1, 1, 1, 10, 20, 30 ).map( CRawTerm::from ).collect( Collectors.toList() ),
377  l_return
378  );
379 
380  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 0.9999546021312976 );
381  Assert.assertEquals( l_return.get( 2 ).<Number>raw(), 0.9999999979388463 );
382  Assert.assertEquals( l_return.get( 3 ).<Number>raw(), 0.9999999999999065 );
383  }
384 
385 
389  @Test
390  public final void stirling()
391  {
392  final List<ITerm> l_return = new ArrayList<>();
393 
394  new CStirling().execute(
395  false, IContext.EMPTYPLAN,
396  Stream.of( 3, 2, 8, 3 ).map( CRawTerm::from ).collect( Collectors.toList() ),
397  l_return
398  );
399 
400  Assert.assertArrayEquals( l_return.stream().map( ITerm::<Number>raw ).toArray(), Stream.of( 3L, 966L ).toArray() );
401  }
402 
403 
407  @Test
408  public final void pow()
409  {
410  final List<ITerm> l_return = new ArrayList<>();
411 
412  new CPow().execute(
413  false, IContext.EMPTYPLAN,
414  Stream.of( 2, 3, 4, 0.5 ).map( CRawTerm::from ).collect( Collectors.toList() ),
415  l_return
416  );
417 
418  Assert.assertArrayEquals(
419  l_return.stream().map( ITerm::raw ).toArray(),
420  Stream.of( 9.0, 16.0, 0.25 ).toArray()
421  );
422  }
423 
424 
428  @Test
429  public final void geometricmean( )
430  {
431  final List<ITerm> l_return = new ArrayList<>();
432 
433  new CGeometricMean().execute(
434  false, IContext.EMPTYPLAN,
435  Stream.of( 1.05, 1.03, 0.94, 1.02, 1.04 ).map( CRawTerm::from ).collect( Collectors.toList() ),
436  l_return
437  );
438 
439  Assert.assertEquals( l_return.size(), 1 );
440  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 1.0152139522031014 );
441  }
442 
443 
447  @Test
448  public final void harmonicmean()
449  {
450  final List<ITerm> l_return = new ArrayList<>();
451 
452  new CHarmonicMean().execute(
453  false, IContext.EMPTYPLAN,
454  Stream.of( 150, 50 ).map( CRawTerm::from ).collect( Collectors.toList() ),
455  l_return
456  );
457 
458  Assert.assertEquals( l_return.size(), 1 );
459  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 75.0 );
460  }
461 
465  @Test
466  public final void hypot()
467  {
468  final Random l_random = new Random();
469  final List<Double> l_input = IntStream.range( 0, 100 ).mapToDouble( i -> l_random.nextGaussian() ).boxed().collect( Collectors.toList() );
470 
471  final List<ITerm> l_return = new ArrayList<>();
472 
473  new CHypot().execute(
474  false, IContext.EMPTYPLAN,
475  l_input.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
476  l_return
477  );
478 
479  Assert.assertArrayEquals(
480  l_return.stream().map( ITerm::<Number>raw ).toArray(),
481  StreamUtils.windowed( l_input.stream(), 2, 2 )
482  .mapToDouble( i -> Math.hypot( i.get( 0 ), i.get( 1 ) ) )
483  .boxed()
484  .toArray()
485  );
486  }
487 
488 
492  @Test
493  public final void maxIndex()
494  {
495  final List<ITerm> l_return = new ArrayList<>();
496 
497  new CMaxIndex().execute(
498  false, IContext.EMPTYPLAN,
499  Stream.of( 3, 4, 9, 1, 7, 8, 4 ).map( CRawTerm::from ).collect( Collectors.toList() ),
500  l_return
501  );
502 
503  Assert.assertEquals( l_return.size(), 1 );
504  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 2D );
505  }
506 
510  @Test
511  public final void minIndex()
512  {
513  final List<ITerm> l_return = new ArrayList<>();
514 
515  new CMinIndex().execute(
516  false, IContext.EMPTYPLAN,
517  Stream.of( 3, 4, 9, 1, 7, 8, 4 ).map( CRawTerm::from ).collect( Collectors.toList() ),
518  l_return
519  );
520 
521  Assert.assertEquals( l_return.size(), 1 );
522  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 3D );
523  }
524 
525 }
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CBinomial.java:66
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CSigmoid.java:67
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for checking for a prime number.
Definition: CIsPrime.java:49
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
action for sinus hyperbolic value.
Definition: CSinh.java:47
action for converting degree value into radian.
Definition: CRadians.java:47
static Object [] aggregationvaluegenerate()
data provider generator for aggregation-value tests
action for calculating a parameterized sigmoid function.
Definition: CSigmoid.java:51
action for calculating the euclidian length.
Definition: CHypot.java:47
action for converting angle value to degree value.
Definition: CDegrees.java:47
action for exponential value.
Definition: CExp.java:47
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CMaxIndex.java:66
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for calculating binomial coefficient.
Definition: CBinomial.java:50
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CStirling.java:67
action for calculating stirling number.
Definition: CStirling.java:51
action for tangens hyperbolic value.
Definition: CTanh.java:47
action for cosinus hyperbolic value.
Definition: CCosh.java:47
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CFactorial.java:64
< T > T raw()
cast to any raw value type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
action for create the next prime number.
Definition: CNextPrime.java:49
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CHypot.java:63
action for logarithm (base 10) value.
Definition: CLog10.java:47
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CPow.java:64
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CMinIndex.java:66
static Object [] singlevaluegenerate()
data provider generator for single-value tests
term structure for simple datatypes
Definition: CRawTerm.java:45
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation