LightJason - AgentSpeak(L++)
TestCActionMathBitVector.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 cern.colt.matrix.tbit.BitVector;
27 import cern.colt.matrix.tdouble.DoubleMatrix1D;
28 import com.codepoetics.protonpack.StreamUtils;
29 import com.tngtech.java.junit.dataprovider.DataProvider;
30 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
31 import com.tngtech.java.junit.dataprovider.UseDataProvider;
32 import org.apache.commons.lang3.tuple.ImmutableTriple;
33 import org.apache.commons.lang3.tuple.Triple;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
61 
62 import java.lang.reflect.InvocationTargetException;
63 import java.util.ArrayList;
64 import java.util.Arrays;
65 import java.util.Collections;
66 import java.util.List;
67 import java.util.stream.Collectors;
68 import java.util.stream.Stream;
69 
70 
74 @RunWith( DataProviderRunner.class )
75 public final class TestCActionMathBitVector extends IBaseTest
76 {
81  private static final BitVector VECTOR1 = new BitVector( 3 );
86  private static final BitVector VECTOR2 = new BitVector( 3 );
87 
88 
92  @Before
93  public final void initialize()
94  {
95  VECTOR1.put( 0, true );
96  VECTOR1.put( 1, false );
97  VECTOR1.put( 2, false );
98 
99  VECTOR2.put( 0, false );
100  VECTOR2.put( 1, false );
101  VECTOR2.put( 2, true );
102  }
103 
108  @DataProvider
109  public static Object[] generator()
110  {
111  return testcase(
112 
113  Stream.of( VECTOR1, VECTOR2 ),
114 
115  Stream.of(
116  CFalseCount.class,
117  CCopy.class,
118  CTrueCount.class,
119  CSize.class,
120  CNot.class,
121  COr.class,
122  CAnd.class,
123  CNAnd.class,
124  CHammingDistance.class,
125  CXor.class
126  ),
127  Stream.of( 2D, 2D ),
128  Stream.of( VECTOR1, VECTOR2 ),
129  Stream.of( 1D, 1D ),
130  Stream.of( 3, 3 ),
131  Stream.of(),
132  Stream.of(),
133  Stream.of(),
134  Stream.of(),
135  Stream.of( 2D ),
136  Stream.of()
137 
138  ).toArray();
139  }
140 
149  @SafeVarargs
150  @SuppressWarnings( "varargs" )
151  private static Stream<Object> testcase( final Stream<Object> p_input, final Stream<Class<?>> p_classes, final Stream<Object>... p_classresult )
152  {
153  final List<ITerm> l_input = p_input.map( CRawTerm::from ).collect( Collectors.toList() );
154 
155  return StreamUtils.zip(
156  p_classes,
157  Arrays.stream( p_classresult ),
158  ( i, j ) -> new ImmutableTriple<>( l_input, i, j )
159  );
160  }
161 
162 
172  @Test
173  @UseDataProvider( "generator" )
174  public final void action( final Triple<List<ITerm>, Class<? extends IAction>, Stream<Object>> p_input )
175  throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
176  {
177  final List<ITerm> l_return = new ArrayList<>();
178 
179  p_input.getMiddle().getConstructor().newInstance().execute(
180  false, IContext.EMPTYPLAN,
181  p_input.getLeft(),
182  l_return
183  );
184 
185  Assert.assertArrayEquals(
186  p_input.getMiddle().toGenericString(),
187  l_return.stream().map( ITerm::raw ).toArray(),
188  p_input.getRight().toArray()
189  );
190  }
191 
195  @Test
196  public final void create()
197  {
198  final List<ITerm> l_return = new ArrayList<>();
199 
200  new CCreate().execute(
201  false, IContext.EMPTYPLAN,
202  Stream.of( 3 ).map( CRawTerm::from ).collect( Collectors.toList() ),
203  l_return
204  );
205 
206  Assert.assertEquals( l_return.size(), 1 );
207  Assert.assertTrue( l_return.get( 0 ).raw() instanceof BitVector );
208  Assert.assertEquals( l_return.get( 0 ).<BitVector>raw().size(), 3 );
209  }
210 
214  @Test
215  public final void boolValue()
216  {
217  final List<ITerm> l_return = new ArrayList<>();
218 
219  new CBoolValue().execute(
220  false, IContext.EMPTYPLAN,
221  Stream.of( VECTOR2, 0 ).map( CRawTerm::from ).collect( Collectors.toList() ),
222  l_return
223  );
224 
225  Assert.assertEquals( l_return.get( 0 ).<Boolean>raw(), false );
226  }
227 
231  @Test
232  public final void set()
233  {
234  new CSet().execute(
235  false, IContext.EMPTYPLAN,
236  Stream.of( VECTOR2, true, 0, 1 ).map( CRawTerm::from ).collect( Collectors.toList() ),
237  Collections.emptyList()
238  );
239 
240  Assert.assertEquals( VECTOR2.get( 0 ), true );
241  Assert.assertEquals( VECTOR2.get( 1 ), true );
242  }
243 
247  @Test
248  public final void clear()
249  {
250  new CClear().execute(
251  false, IContext.EMPTYPLAN,
252  Stream.of( VECTOR2, 0 ).map( CRawTerm::from ).collect( Collectors.toList() ),
253  Collections.emptyList()
254  );
255 
256  Assert.assertEquals( VECTOR2.get( 0 ), false );
257  }
258 
262  @Test
263  public final void range()
264  {
265  final List<ITerm> l_return = new ArrayList<>();
266 
267  new CRange().execute(
268  false, IContext.EMPTYPLAN,
269  Stream.of( VECTOR2, 0, 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
270  l_return
271  );
272 
273  Assert.assertEquals( l_return.size(), 1 );
274  Assert.assertEquals( l_return.get( 0 ).<BitVector>raw(), VECTOR2 );
275  }
276 
280  @Test
281  public final void numericvalue()
282  {
283  final List<ITerm> l_return = new ArrayList<>();
284 
285  new CNumericValue().execute(
286  false, IContext.EMPTYPLAN,
287  Stream.of( VECTOR1, 1 ).map( CRawTerm::from ).collect( Collectors.toList() ),
288  l_return
289  );
290 
291  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 0D );
292  }
293 
297  @Test
298  public final void tolist()
299  {
300  final List<ITerm> l_return = new ArrayList<>();
301 
302  new CToList().execute(
303  false, IContext.EMPTYPLAN,
304  Stream.of( VECTOR1 ).map( CRawTerm::from ).collect( Collectors.toList() ),
305  l_return
306  );
307 
308  Assert.assertEquals( l_return.size(), 1 );
309  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
310  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( 1D, 0D, 0D ).toArray() );
311  }
312 
316  @Test
317  public final void toblas()
318  {
319  final List<ITerm> l_return = new ArrayList<>();
320 
321  new CToBlas().execute(
322  false, IContext.EMPTYPLAN,
323  Stream.of( VECTOR2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
324  l_return
325  );
326 
327  Assert.assertEquals( l_return.size(), 1 );
328  Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix1D );
329  Assert.assertArrayEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().toArray(), Stream.of( 0, 0, 1 ).mapToDouble( i -> i ).toArray(), 0 );
330  }
331 
332 }
performs the logical not-and operation to all bit vectors.
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
performs the logical and operation to all bit vectors.
performs the logical not operation to all bit vectors.
sets the indexed bit to false within the bit vector.
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
combines all arguments to a single result with the and-operator.
Definition: bool/CAnd.java:48
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
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
combines all arguments to a single result with the xor-operator.
Definition: bool/CXor.java:48
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
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
performs the logical xor operation to all bit vectors.
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
< 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
performs the logical or operation to all bit vectors.
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
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
combines all arguments to a single result with the or-operator.
Definition: bool/COr.java:48
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
performs the logical not-and operation to all bit matrices.
term structure for simple datatypes
Definition: CRawTerm.java:45