LightJason - AgentSpeak(L++)
TestCActionCollectionList.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.google.common.collect.Lists;
27 import org.apache.commons.lang3.RandomStringUtils;
28 import org.junit.Assert;
29 import org.junit.Test;
48 
49 import java.util.AbstractMap;
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.List;
53 import java.util.Random;
54 import java.util.stream.Collectors;
55 import java.util.stream.IntStream;
56 import java.util.stream.Stream;
57 
58 
62 public final class TestCActionCollectionList extends IBaseTest
63 {
64 
68  @Test
69  public final void createempty()
70  {
71  final List<ITerm> l_return = new ArrayList<>();
72 
73  new CCreate().execute(
74  false, IContext.EMPTYPLAN,
75  Collections.emptyList(),
76  l_return
77  );
78 
79  Assert.assertEquals( l_return.size(), 1 );
80  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
81  Assert.assertTrue( l_return.get( 0 ).<List<?>>raw().isEmpty() );
82  }
83 
84 
88  @Test
89  public final void createemptysynchronize()
90  {
91  final List<ITerm> l_return = new ArrayList<>();
92 
93  new CCreate().execute(
94  true, IContext.EMPTYPLAN,
95  Collections.emptyList(),
96  l_return
97  );
98 
99  Assert.assertEquals( l_return.size(), 1 );
100  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
101  Assert.assertTrue( l_return.get( 0 ).<List<?>>raw().isEmpty() );
102  Assert.assertEquals( l_return.get( 0 ).raw().getClass(), Collections.synchronizedList( Collections.emptyList() ).getClass() );
103  }
104 
105 
109  @Test
110  public final void create()
111  {
112  final List<ITerm> l_return = new ArrayList<>();
113 
114  new CCreate().execute(
115  false, IContext.EMPTYPLAN,
116  Stream.of( "a", 1, "b", true ).map( CRawTerm::from ).collect( Collectors.toList() ),
117  l_return
118  );
119 
120  Assert.assertEquals( l_return.size(), 1 );
121  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
122  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().size(), 4 );
123 
124  final List<?> l_list = l_return.get( 0 ).raw();
125 
126  Assert.assertEquals( l_list.get( 0 ), "a" );
127  Assert.assertEquals( l_list.get( 1 ), 1 );
128  Assert.assertEquals( l_list.get( 2 ), "b" );
129  Assert.assertEquals( l_list.get( 3 ), true );
130  }
131 
132 
136  @Test
137  public final void complement()
138  {
139  final List<ITerm> l_return = new ArrayList<>();
140 
141  new CComplement().execute(
142  false, IContext.EMPTYPLAN,
143  Stream.of(
144  CRawTerm.from( Stream.of( "a", "b", 1, 2 ).collect( Collectors.toList() ) ),
145  CRawTerm.from( Stream.of( "x", "y", 4, "a", 5, 1 ).collect( Collectors.toList() ) )
146  ).collect( Collectors.toList() ),
147  l_return
148  );
149 
150  Assert.assertEquals( l_return.size(), 1 );
151  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_return.get( 0 ), List.class ) );
152  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().get( 0 ), "b" );
153  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().get( 1 ), 2 );
154  }
155 
156 
160  @Test
161  public final void get()
162  {
163  final List<ITerm> l_return = new ArrayList<>();
164  final List<?> l_list = Stream.of( "a", 1, "b", true, "foobar", 56.78 ).collect( Collectors.toList() );
165 
166  new CGet().execute(
167  false, IContext.EMPTYPLAN,
168  Stream.of( CRawTerm.from( l_list ), CRawTerm.from( 1 ), CRawTerm.from( 4 ), CRawTerm.from( 5 ) ).collect( Collectors.toList() ),
169  l_return
170  );
171 
172  Assert.assertEquals( l_return.size(), 3 );
173  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 1 );
174  Assert.assertEquals( l_return.get( 1 ).raw(), "foobar" );
175  Assert.assertEquals( l_return.get( 2 ).<Number>raw(), 56.78 );
176  }
177 
181  @Test
182  public final void reverse()
183  {
184  final List<ITerm> l_return = new ArrayList<>();
185  final List<?> l_list = IntStream.range( 0, 10 ).mapToObj( i -> Math.random() ).collect( Collectors.toList() );
186 
187  new CReverse().execute(
188  false, IContext.EMPTYPLAN,
189  Stream.of( CRawTerm.from( l_list ) ).collect( Collectors.toList() ),
190  l_return
191  );
192 
193  Assert.assertArrayEquals( l_return.stream().map( ITerm::raw ).toArray(), Lists.reverse( l_list ).toArray() );
194  }
195 
196 
200  @Test
201  public final void remove()
202  {
203  final Random l_random = new Random();
204 
205  final List<?> l_elements = IntStream.range( 0, l_random.nextInt( 100 ) + 1 ).map( i -> l_random.nextInt() ).boxed().collect( Collectors.toList() );
206  final List<?> l_list = new ArrayList<>( l_elements );
207  final List<Integer> l_index = new ArrayList<>(
208  IntStream.range( 0, l_list.size() / 3 )
209  .map( i -> l_random.nextInt( l_list.size() ) )
210  .boxed()
211  .collect( Collectors.toSet() )
212  );
213 
214  final int l_startsize = l_list.size();
215  final List<ITerm> l_return = new ArrayList<>();
216 
217  new CRemove().execute(
218  false, IContext.EMPTYPLAN,
219  Stream.concat(
220  Stream.of( l_list ),
221  l_index.stream()
222  ).map( CRawTerm::from ).collect( Collectors.toList() ),
223  l_return
224  );
225 
226  Assert.assertEquals( l_startsize, l_list.size() + l_index.size() );
227  Assert.assertTrue(
228  l_index.parallelStream()
229  .map( l_elements::get )
230  .allMatch( i -> l_return.parallelStream().map( ITerm::<Number>raw ).anyMatch( j -> j.equals( i ) ) )
231  );
232  }
233 
234 
238  @Test
239  public final void set()
240  {
241  final List<?> l_list1 = Stream.of( "" ).collect( Collectors.toList() );
242  final List<?> l_list2 = Stream.of( "abc", 123, true ).collect( Collectors.toList() );
243 
244  new CSet().execute(
245  false, IContext.EMPTYPLAN,
246  Stream.of( CRawTerm.from( 0 ), CRawTerm.from( "xxx" ), CRawTerm.from( l_list1 ), CRawTerm.from( l_list2 ) ).collect( Collectors.toList() ),
247  Collections.emptyList()
248  );
249 
250  Assert.assertEquals( l_list1.size(), 1 );
251  Assert.assertEquals( l_list1.get( 0 ), "xxx" );
252 
253  Assert.assertEquals( l_list2.size(), 3 );
254  Assert.assertEquals( l_list2.get( 0 ), "xxx" );
255  }
256 
257 
261  @Test
262  public final void add()
263  {
264  final List<?> l_list = new ArrayList<>();
265 
266  new CAdd().execute(
267  false, IContext.EMPTYPLAN,
268  Stream.of( CRawTerm.from( "xyz" ), CRawTerm.from( l_list ) ).collect( Collectors.toList() ),
269  Collections.emptyList()
270  );
271 
272  Assert.assertEquals( l_list.size(), 1 );
273  Assert.assertEquals( l_list.get( 0 ), "xyz" );
274  }
275 
276 
280  @Test
281  public final void rangeerror()
282  {
283  Assert.assertFalse(
284  new CRange().execute(
285  false, IContext.EMPTYPLAN,
286  Stream.of().map( CRawTerm::from ).collect( Collectors.toList() ),
287  Collections.emptyList()
288  ).value()
289  );
290  }
291 
292 
296  @Test
297  public final void range()
298  {
299  final List<ITerm> l_return = new ArrayList<>();
300 
301  new CRange().execute(
302  false, IContext.EMPTYPLAN,
303  Stream.of( 0, 5, 7, 9 ).map( CRawTerm::from ).collect( Collectors.toList() ),
304  l_return
305  );
306 
307  new CRange().execute(
308  true, IContext.EMPTYPLAN,
309  Stream.of( 1, 7 ).map( CRawTerm::from ).collect( Collectors.toList() ),
310  l_return
311  );
312 
313  Assert.assertEquals( l_return.size(), 3 );
314 
315  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), IntStream.range( 0, 5 ).boxed().toArray() );
316  Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), IntStream.range( 7, 9 ).boxed().toArray() );
317 
318  Assert.assertArrayEquals( l_return.get( 2 ).<List<?>>raw().toArray(), IntStream.range( 1, 7 ).boxed().toArray() );
319  Assert.assertEquals( l_return.get( 2 ).<List<?>>raw().getClass(), Collections.synchronizedList( Collections.emptyList() ).getClass() );
320  }
321 
325  @Test
326  public final void sublisterror()
327  {
328  Assert.assertFalse(
329  new CSubList().execute(
330  false, IContext.EMPTYPLAN,
331  Stream.of( new ArrayList<>() ).map( CRawTerm::from ).collect( Collectors.toList() ),
332  Collections.emptyList()
333  ).value()
334  );
335  }
336 
340  @Test
341  public final void sublist()
342  {
343  final List<ITerm> l_return = new ArrayList<>();
344 
345  new CSubList().execute(
346  false, IContext.EMPTYPLAN,
347  Stream.of( Stream.of( "ax", "bx", "c", 1, 2, 3 ).collect( Collectors.toList() ), 0, 2, 2, 4 ).map( CRawTerm::from ).collect( Collectors.toList() ),
348  l_return
349  );
350 
351  new CSubList().execute(
352  true, IContext.EMPTYPLAN,
353  Stream.of( Stream.of( 8, 9, 10 ).collect( Collectors.toList() ), 1, 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
354  l_return
355  );
356 
357  Assert.assertEquals( l_return.size(), 3 );
358 
359  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( "ax", "bx" ).toArray() );
360  Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), Stream.of( "c", 1 ).toArray() );
361  Assert.assertArrayEquals( l_return.get( 2 ).<List<?>>raw().toArray(), Stream.of( 9 ).toArray() );
362  }
363 
364 
368  @Test
369  public final void flat()
370  {
371  final Random l_random = new Random();
372 
373  final List<ITerm> l_return = new ArrayList<>();
374  final List<?> l_list = IntStream.range( 0, l_random.nextInt( 100 ) + 1 )
375  .mapToObj( i -> RandomStringUtils.random( l_random.nextInt( 100 ) + 1 ) )
376  .collect( Collectors.toList() );
377 
378  new CFlat().execute(
379  false, IContext.EMPTYPLAN,
380  l_list.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
381  l_return
382  );
383 
384  Assert.assertEquals( l_return.size(), l_list.size() );
385  Assert.assertArrayEquals( l_return.stream().map( ITerm::raw ).toArray(), l_list.toArray() );
386  }
387 
388 
392  @Test
393  public final void flatconcat()
394  {
395  final Random l_random = new Random();
396 
397  final List<ITerm> l_return = new ArrayList<>();
398  final List<?> l_list = IntStream.range( 0, l_random.nextInt( 100 ) + 1 )
399  .mapToObj( i -> RandomStringUtils.random( l_random.nextInt( 100 ) + 1 ) )
400  .collect( Collectors.toList() );
401 
402  new CFlatConcat().execute(
403  false, IContext.EMPTYPLAN,
404  l_list.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
405  l_return
406  );
407 
408  Assert.assertEquals( l_return.size(), 1 );
409  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), l_list.toArray() );
410  }
411 
412 
416  @Test
417  public final void ziperror()
418  {
419  Assert.assertFalse(
420  new CZip().execute(
421  false, IContext.EMPTYPLAN,
422  Stream.of( "" ).map( CRawTerm::from ).collect( Collectors.toList() ),
423  Collections.emptyList()
424  ).value()
425  );
426  }
427 
428 
432  @Test
433  public final void zip()
434  {
435  final List<ITerm> l_return = new ArrayList<>();
436 
437  new CZip().execute(
438  false, IContext.EMPTYPLAN,
439  IntStream.range( 0, 6 ).boxed().map( CRawTerm::from ).collect( Collectors.toList() ),
440  l_return
441  );
442 
443  Assert.assertEquals( l_return.size(), 1 );
444  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().size(), 3 );
445 
446  Assert.assertEquals( l_return.get( 0 ).<List<AbstractMap.SimpleEntry<?, ?>>>raw().get( 0 ).getKey(), 0 );
447  Assert.assertEquals( l_return.get( 0 ).<List<AbstractMap.SimpleEntry<?, ?>>>raw().get( 0 ).getValue(), 3 );
448 
449  Assert.assertEquals( l_return.get( 0 ).<List<AbstractMap.SimpleEntry<?, ?>>>raw().get( 1 ).getKey(), 1 );
450  Assert.assertEquals( l_return.get( 0 ).<List<AbstractMap.SimpleEntry<?, ?>>>raw().get( 1 ).getValue(), 4 );
451 
452  Assert.assertEquals( l_return.get( 0 ).<List<AbstractMap.SimpleEntry<?, ?>>>raw().get( 2 ).getKey(), 2 );
453  Assert.assertEquals( l_return.get( 0 ).<List<AbstractMap.SimpleEntry<?, ?>>>raw().get( 2 ).getValue(), 5 );
454 
455 
456 
457  new CZip().execute(
458  true, IContext.EMPTYPLAN,
459  Stream.of( 1, 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
460  l_return
461  );
462 
463  Assert.assertEquals( l_return.size(), 2 );
464  Assert.assertEquals( l_return.get( 1 ).<List<?>>raw().getClass(), Collections.synchronizedList( Collections.emptyList() ).getClass() );
465 
466  }
467 
468 
472  @Test
473  public final void unique()
474  {
475  final List<ITerm> l_return = new ArrayList<>();
476 
477  new CUnique().execute(
478  false, IContext.EMPTYPLAN,
479  Stream.of( 1, 1, 3, 4, 5, 5 ).map( CRawTerm::from ).collect( Collectors.toList() ),
480  l_return
481  );
482 
483  Assert.assertEquals( l_return.size(), 1 );
484  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().size(), 4 );
485  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( 1, 3, 4, 5 ).toArray() );
486 
487  new CUnique().execute(
488  true, IContext.EMPTYPLAN,
489  Stream.of( 1 ).map( CRawTerm::from ).collect( Collectors.toList() ),
490  l_return
491  );
492 
493  Assert.assertEquals( l_return.size(), 2 );
494  Assert.assertEquals( l_return.get( 1 ).<List<?>>raw().getClass(), Collections.synchronizedList( Collections.emptyList() ).getClass() );
495  }
496 
497 }
returns a flat concated list of any term data.
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
common structure for execution definition
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
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
execution context with local data
Definition: IContext.java:42
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
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: CUnique.java:73
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
creates a list of tuples with elements of two lists.
Definition: CZip.java:52
< T > T raw()
cast to any raw value type
final void createemptysynchronize()
test create empty synchronized list
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: list/CFlat.java:64
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
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: CZip.java:76
returns a sublist within the index range.
Definition: CSubList.java:52
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: CSubList.java:76
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
term structure for simple datatypes
Definition: CRawTerm.java:45