LightJason - AgentSpeak(L++)
TestCActionBool.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.junit.Assert;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
50 
51 import java.lang.reflect.InvocationTargetException;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Map;
57 import java.util.stream.Collectors;
58 import java.util.stream.IntStream;
59 import java.util.stream.Stream;
60 
61 
65 @RunWith( DataProviderRunner.class )
66 public final class TestCActionBool extends IBaseTest
67 {
68 
73  @DataProvider
74  public static Object[] generate()
75  {
76  return Stream.concat(
77 
78  // -- first test-case ---
79  testcase(
80 
81  // input data
82  Stream.of( true, false, true, false, false, true ),
83 
84  // testing classes / test-methods
85  Stream.of(
86  CAllMatch.class,
87  CAnyMatch.class,
88  CAnd.class,
89  COr.class,
90  CXor.class,
91  CNot.class,
92  CCountTrue.class,
93  CCountFalse.class
94  ),
95 
96  // results for each class test
97  Stream.of( false ),
98  Stream.of( true ),
99  Stream.of( false ),
100  Stream.of( true ),
101  Stream.of( true ),
102  Stream.of( false, true, false, true, true, false ),
103  Stream.of( 3L ),
104  Stream.of( 3L )
105  ),
106 
107 
108 
109  // --- second test-case ---
110  testcase(
111 
112  // input data
113  Stream.of( true, true ),
114 
115  // testing classes / test-methods
116  Stream.of(
117  CAllMatch.class,
118  CAnyMatch.class,
119  CAnd.class,
120  COr.class,
121  CXor.class,
122  CNot.class,
123  CCountTrue.class,
124  CCountFalse.class
125 
126  ),
127 
128  // results for each class test
129  Stream.of( true ),
130  Stream.of( true ),
131  Stream.of( true ),
132  Stream.of( true ),
133  Stream.of( false ),
134  Stream.of( false, false ),
135  Stream.of( 2L ),
136  Stream.of( 0L )
137  )
138 
139  ).toArray();
140  }
141 
150  @SafeVarargs
151  @SuppressWarnings( "varargs" )
152  private static Stream<Object> testcase( final Stream<Object> p_input, final Stream<Class<?>> p_classes, final Stream<Object>... p_classresult )
153  {
154  final List<ITerm> l_input = p_input.map( CRawTerm::from ).collect( Collectors.toList() );
155 
156  return StreamUtils.zip(
157  p_classes,
158  Arrays.stream( p_classresult ),
159  ( i, j ) -> new ImmutableTriple<>( l_input, i, j )
160  );
161  }
162 
172  @Test
173  @UseDataProvider( "generate" )
174  public final void execute( 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  l_return.stream().map( ITerm::raw ).toArray(),
187  p_input.getRight().toArray()
188  );
189  }
190 
194  @Test
195  public final void equal()
196  {
197  final List<ITerm> l_return = new ArrayList<>();
198 
199  new CEqual().execute(
200  false, IContext.EMPTYPLAN,
201  Stream.of( l_return, l_return, new Object() ).map( CRawTerm::from ).collect( Collectors.toList() ),
202  l_return
203  );
204 
205  Assert.assertEquals( l_return.size(), 2 );
206  Assert.assertTrue( l_return.get( 0 ).<Boolean>raw() );
207  Assert.assertFalse( l_return.get( 1 ).<Boolean>raw() );
208 
209 
210  final List<Integer> l_list1 = IntStream.range( 0, 5 ).boxed().collect( Collectors.toList() );
211  final List<Integer> l_list2 = IntStream.range( 0, 5 ).boxed().collect( Collectors.toList() );
212 
213  new CEqual().execute(
214  false, IContext.EMPTYPLAN,
215  Stream.of( l_list1, l_list2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
216  l_return
217  );
218 
219  Assert.assertEquals( l_return.size(), 3 );
220  Assert.assertTrue( l_return.get( 2 ).<Boolean>raw() );
221 
222 
223  final Map<Integer, Integer> l_map1 = new HashMap<>();
224  l_map1.put( 1, 2 );
225  final Map<Integer, Integer> l_map2 = new HashMap<>();
226  l_map2.put( 1, 1 );
227 
228  new CEqual().execute(
229  false, IContext.EMPTYPLAN,
230  Stream.of( l_map1, l_map2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
231  l_return
232  );
233 
234  Assert.assertEquals( l_return.size(), 4 );
235  Assert.assertFalse( l_return.get( 3 ).<Boolean>raw() );
236  }
237 
241  @Test
242  public final void notequal()
243  {
244  final Object l_object = new Object();
245  final List<ITerm> l_return = new ArrayList<>();
246 
247  new CNotEqual().execute(
248  false, IContext.EMPTYPLAN,
249  Stream.of( l_object, l_object, new Object() ).map( CRawTerm::from ).collect( Collectors.toList() ),
250  l_return
251  );
252 
253  Assert.assertEquals( l_return.size(), 2 );
254  Assert.assertFalse( l_return.get( 0 ).<Boolean>raw() );
255  Assert.assertTrue( l_return.get( 1 ).<Boolean>raw() );
256  }
257 
258 }
base test class with helpers
Definition: IBaseTest.java:33
static Object [] generate()
data provider generator
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
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
Definition: CEqual.java:71
< T > T raw()
cast to any raw value type
checks any elements are equal to the first argument.
Definition: CAnyMatch.java:49
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
checks all elements are equal to the first argument.
Definition: CAllMatch.java:49
combines all arguments to a single result with the or-operator.
Definition: bool/COr.java:48
term structure for simple datatypes
Definition: CRawTerm.java:45