24 package org.lightjason.agentspeak.action.builtin;
26 import com.google.common.collect.Lists;
27 import org.apache.commons.lang3.RandomStringUtils;
28 import org.junit.Assert;
29 import org.junit.Test;
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;
71 final List<ITerm> l_return =
new ArrayList<>();
75 Collections.emptyList(),
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() );
91 final List<ITerm> l_return =
new ArrayList<>();
95 Collections.emptyList(),
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() );
112 final List<ITerm> l_return =
new ArrayList<>();
116 Stream.of(
"a", 1,
"b",
true ).map(
CRawTerm::from ).collect( Collectors.toList() ),
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 );
124 final List<?> l_list = l_return.get( 0 ).raw();
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 );
139 final List<ITerm> l_return =
new ArrayList<>();
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() ),
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 );
161 public final void get()
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() );
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 );
184 final List<ITerm> l_return =
new ArrayList<>();
185 final List<?> l_list = IntStream.range( 0, 10 ).mapToObj( i -> Math.random() ).collect( Collectors.toList() );
189 Stream.of(
CRawTerm.
from( l_list ) ).collect( Collectors.toList() ),
193 Assert.assertArrayEquals( l_return.stream().map(
ITerm::raw ).toArray(), Lists.reverse( l_list ).toArray() );
201 public final void remove()
203 final Random l_random =
new Random();
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() ) )
211 .collect( Collectors.toSet() )
214 final int l_startsize = l_list.size();
215 final List<ITerm> l_return =
new ArrayList<>();
226 Assert.assertEquals( l_startsize, l_list.size() + l_index.size() );
228 l_index.parallelStream()
229 .map( l_elements::get )
230 .allMatch( i -> l_return.parallelStream().map(
ITerm::<Number>raw ).anyMatch( j -> j.equals( i ) ) )
239 public final void set()
241 final List<?> l_list1 = Stream.of(
"" ).collect( Collectors.toList() );
242 final List<?> l_list2 = Stream.of(
"abc", 123,
true ).collect( Collectors.toList() );
247 Collections.emptyList()
250 Assert.assertEquals( l_list1.size(), 1 );
251 Assert.assertEquals( l_list1.get( 0 ),
"xxx" );
253 Assert.assertEquals( l_list2.size(), 3 );
254 Assert.assertEquals( l_list2.get( 0 ),
"xxx" );
264 final List<?> l_list =
new ArrayList<>();
269 Collections.emptyList()
272 Assert.assertEquals( l_list.size(), 1 );
273 Assert.assertEquals( l_list.get( 0 ),
"xyz" );
287 Collections.emptyList()
299 final List<ITerm> l_return =
new ArrayList<>();
303 Stream.of( 0, 5, 7, 9 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
309 Stream.of( 1, 7 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
313 Assert.assertEquals( l_return.size(), 3 );
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() );
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() );
331 Stream.of(
new ArrayList<>() ).map(
CRawTerm::from ).collect( Collectors.toList() ),
332 Collections.emptyList()
343 final List<ITerm> l_return =
new ArrayList<>();
347 Stream.of( Stream.of(
"ax",
"bx",
"c", 1, 2, 3 ).collect( Collectors.toList() ), 0, 2, 2, 4 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
353 Stream.of( Stream.of( 8, 9, 10 ).collect( Collectors.toList() ), 1, 2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
357 Assert.assertEquals( l_return.size(), 3 );
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() );
371 final Random l_random =
new Random();
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() );
380 l_list.stream().map(
CRawTerm::from ).collect( Collectors.toList() ),
384 Assert.assertEquals( l_return.size(), l_list.size() );
385 Assert.assertArrayEquals( l_return.stream().map(
ITerm::raw ).toArray(), l_list.toArray() );
395 final Random l_random =
new Random();
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() );
404 l_list.stream().map(
CRawTerm::from ).collect( Collectors.toList() ),
408 Assert.assertEquals( l_return.size(), 1 );
409 Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), l_list.toArray() );
422 Stream.of(
"" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
423 Collections.emptyList()
435 final List<ITerm> l_return =
new ArrayList<>();
439 IntStream.range( 0, 6 ).boxed().map(
CRawTerm::from ).collect( Collectors.toList() ),
443 Assert.assertEquals( l_return.size(), 1 );
444 Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().size(), 3 );
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 );
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 );
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 );
459 Stream.of( 1, 2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
463 Assert.assertEquals( l_return.size(), 2 );
464 Assert.assertEquals( l_return.get( 1 ).<List<?>>raw().getClass(), Collections.synchronizedList( Collections.emptyList() ).getClass() );
475 final List<ITerm> l_return =
new ArrayList<>();
479 Stream.of( 1, 1, 3, 4, 5, 5 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
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() );
489 Stream.of( 1 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
493 Assert.assertEquals( l_return.size(), 2 );
494 Assert.assertEquals( l_return.get( 1 ).<List<?>>raw().getClass(), Collections.synchronizedList( Collections.emptyList() ).getClass() );
final void reverse()
test reverse action
returns a flat concated list of any term data.
base test class with helpers
returns an unique list of the list.
IContext EMPTYPLAN
empty context with plan
final void sublisterror()
test sublist error
common structure for execution definition
creates the complement between lists.
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
unflats any list structure.
final void rangeerror()
test range error
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 void unique()
test unique action
final void createempty()
test create empty list
execution context with local data
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
adds an element to the 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
final void flat()
test flat action
final void zip()
test zip action
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 void range()
test range
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.
returns an element of the list by the index.
< T > T raw()
cast to any raw value type
final void createemptysynchronize()
test create empty synchronized list
final void ziperror()
test zip action error
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
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
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
class for any helper calls
returns a sublist within the index range.
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 void sublist()
test sublist
final void flatconcat()
test flatconcat action
removes an element of the list by the index.
action to add elements to a list.
creates a list with a integer ranged list.
final void add()
test add action
final void create()
test create non-empty 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
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
final void complement()
test complement action