LightJason - AgentSpeak(L++)
TestCActionCollection.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.google.common.collect.HashMultimap;
28 import com.google.common.collect.Multimap;
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.ImmutablePair;
33 import org.apache.commons.lang3.tuple.Pair;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
44 
45 import java.text.MessageFormat;
46 import java.util.AbstractMap;
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Set;
53 import java.util.stream.Collectors;
54 import java.util.stream.IntStream;
55 import java.util.stream.Stream;
56 
57 
61 @RunWith( DataProviderRunner.class )
62 public final class TestCActionCollection extends IBaseTest
63 {
64 
69  @DataProvider
70  public static Object[] generate()
71  {
72  return Stream.of(
73 
74  new ImmutablePair<>(
75  Stream.of( CRawTerm.from( new Object() ) ).collect( Collectors.toList() ),
76  new int[]{0}
77  ),
78 
79  new ImmutablePair<>(
80  Stream.of( CRawTerm.from( new ArrayList<>() ) ).collect( Collectors.toList() ),
81  new int[]{0}
82  ),
83 
84  new ImmutablePair<>(
85  Stream.of( CRawTerm.from( Stream.of( 1, "test" ).collect( Collectors.toList() ) ) ).collect( Collectors.toList() ),
86  new int[]{2}
87  ),
88 
89  new ImmutablePair<>(
90  Stream.of( CRawTerm.from( new AbstractMap.SimpleEntry<>( "a", 1 ) ) ).collect( Collectors.toList() ),
91  new int[]{2}
92  ),
93 
94  new ImmutablePair<>(
95  Stream.of( CRawTerm.from( Stream.of( 1, 1, "test" ).collect( Collectors.toSet() ) ) ).collect( Collectors.toList() ),
96  new int[]{2}
97  ),
98 
99  new ImmutablePair<>(
100  Stream.of(
101  CRawTerm.from( Stream.of( "abcd", "xyz", 12, 12 ).collect( Collectors.toSet() ) ),
102  CRawTerm.from( Stream.of( 1, 2, 3, 3, 4, 4 ).collect( Collectors.toList() ) )
103  ).collect( Collectors.toList() ),
104  new int[]{3, 6}
105  ),
106 
107  new ImmutablePair<>(
108  Stream.of( CRawTerm.from(
109  StreamUtils.windowed( Stream.of( 1, 2, 3, 4 ), 2 ).collect( Collectors.toMap( i -> i.get( 0 ), i -> i.get( 1 ) ) )
110  ) ).collect( Collectors.toList() ),
111  new int[]{3}
112  ),
113 
114  new ImmutablePair<>(
115  Stream.of( CRawTerm.from(
116  StreamUtils.windowed( Stream.of( 1, 2, 3, 4 ), 2 ).collect( Collectors.toMap( i -> i.get( 0 ), i -> i.get( 1 ) ) )
117  ) ).collect( Collectors.toList() ),
118  new int[]{3}
119  )
120 
121  ).toArray();
122  }
123 
129  @Test
130  @UseDataProvider( "generate" )
131  public final void size( final Pair<List<ITerm>, int[]> p_input )
132  {
133  final List<ITerm> l_return = new ArrayList<>();
134 
135  new CSize().execute(
136  false, IContext.EMPTYPLAN,
137  p_input.getLeft(),
138  l_return
139  );
140 
141  Assert.assertArrayEquals(
142  MessageFormat.format( "elements {0}", p_input.getLeft() ),
143  l_return.stream().map( ITerm::<Number>raw ).mapToInt( Number::intValue ).toArray(),
144  p_input.getRight()
145  );
146  }
147 
148 
152  @Test
153  public final void empty()
154  {
155  final List<ITerm> l_return = new ArrayList<>();
156 
157  new CIsEmpty().execute(
158  false, IContext.EMPTYPLAN,
159  Stream.of( new ArrayList<>(), HashMultimap.create(), new HashMap<>(), Stream.of( "1", 2 ).collect( Collectors.toList() ), new Object() )
160  .map( CRawTerm::from )
161  .collect( Collectors.toList() ),
162  l_return
163  );
164 
165  Assert.assertEquals( l_return.size(), 5 );
166  Assert.assertArrayEquals( l_return.stream().map( ITerm::<Boolean>raw ).toArray(), Stream.of( true, true, true, false, false ).toArray() );
167  }
168 
169 
173  @Test
174  public final void clear()
175  {
176  final List<Integer> l_list = IntStream.range( 0, 10 ).boxed().collect( Collectors.toList() );
177  final Set<Integer> l_set = IntStream.range( 10, 20 ).boxed().collect( Collectors.toSet() );
178  final Map<Integer, Integer> l_map = StreamUtils.windowed( IntStream.range( 100, 120 ).boxed(), 2 )
179  .collect( Collectors.toMap( i -> i.get( 0 ), i -> i.get( 1 ) ) );
180 
181  final Multimap<Integer, Integer> l_multimap = HashMultimap.create();
182  IntStream.range( 0, 5 ).forEach( i -> IntStream.range( i, i + 5 ).forEach( j -> l_map.put( i, j ) ) );
183 
184  new CClear().execute(
185  false, IContext.EMPTYPLAN,
186  Stream.of( l_list, l_set, l_map, l_multimap ).map( CRawTerm::from ).collect( Collectors.toList() ),
187  Collections.emptyList()
188  );
189 
190  Assert.assertTrue( l_list.isEmpty() );
191  Assert.assertTrue( l_set.isEmpty() );
192  Assert.assertTrue( l_map.isEmpty() );
193  }
194 
195 }
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.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: CIsEmpty.java:66
execution context with local data
Definition: IContext.java:42
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
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