LightJason - AgentSpeak(L++)
TestCActionCollectionMap.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 org.junit.Assert;
28 import org.junit.Test;
43 
44 import java.util.AbstractMap;
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.concurrent.ConcurrentHashMap;
51 import java.util.stream.Collectors;
52 import java.util.stream.Stream;
53 
54 
58 public final class TestCActionCollectionMap extends IBaseTest
59 {
60 
64  @Test
65  public final void create()
66  {
67  final List<ITerm> l_return = new ArrayList<>();
68 
69  new CCreate().execute(
70  false, IContext.EMPTYPLAN,
71  Collections.emptyList(),
72  l_return
73  );
74 
75  Assert.assertEquals( l_return.size(), 1 );
76  Assert.assertTrue( l_return.get( 0 ).<Map<?, ?>>raw().isEmpty() );
77 
78 
79  new CCreate().execute(
80  false, IContext.EMPTYPLAN,
81  Stream.of( "a", 1, "b", 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
82  l_return
83  );
84 
85  Assert.assertEquals( l_return.size(), 2 );
86  Assert.assertFalse( l_return.get( 1 ).<Map<?, ?>>raw().isEmpty() );
87 
88  Assert.assertArrayEquals( l_return.get( 1 ).<Map<?, ?>>raw().keySet().toArray(), Stream.of( "a", "b" ).toArray() );
89  Assert.assertArrayEquals( l_return.get( 1 ).<Map<?, ?>>raw().values().toArray(), Stream.of( 1, 2 ).toArray() );
90 
91 
92  new CCreate().execute(
93  true, IContext.EMPTYPLAN,
94  Collections.emptyList(),
95  l_return
96  );
97 
98  Assert.assertEquals( l_return.size(), 3 );
99  Assert.assertTrue( l_return.get( 2 ).<Map<?, ?>>raw() instanceof ConcurrentHashMap<?, ?> );
100  }
101 
102 
106  @Test
107  public final void keysvalues()
108  {
109  final Map<?, ?> l_map = StreamUtils.zip(
110  Stream.of( "foo", "bar", "yyy", "xxx" ),
111  Stream.of( 1, 2, 3, 4 ),
112  AbstractMap.SimpleImmutableEntry::new
113  ).collect( Collectors.toMap( AbstractMap.SimpleImmutableEntry::getKey, AbstractMap.SimpleImmutableEntry::getValue ) );
114 
115 
116  final List<ITerm> l_return = new ArrayList<>();
117 
118  new CKeys().execute(
119  false, IContext.EMPTYPLAN,
120  Stream.of( CRawTerm.from( l_map ) ).collect( Collectors.toList() ),
121  l_return
122  );
123 
124  new CValues().execute(
125  false, IContext.EMPTYPLAN,
126  Stream.of( CRawTerm.from( l_map ) ).collect( Collectors.toList() ),
127  l_return
128  );
129 
130 
131  Assert.assertEquals( l_return.size(), 2 );
132 
133  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().size(), 4 );
134  Assert.assertEquals( l_return.get( 1 ).<List<?>>raw().size(), 4 );
135 
136  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( "bar", "foo", "xxx", "yyy" ).toArray() );
137  Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), Stream.of( 2, 1, 3, 4 ).toArray() );
138  }
139 
140 
144  @Test
145  public final void putsingle()
146  {
147  final Map<?, ?> l_map = new HashMap<>();
148 
149  new CPutSingle().execute(
150  false, IContext.EMPTYPLAN,
151  Stream.of( "v", 1, l_map ).map( CRawTerm::from ).collect( Collectors.toList() ),
152 
153  Collections.emptyList()
154  );
155 
156  Assert.assertEquals( l_map.size(), 1 );
157  Assert.assertArrayEquals( l_map.keySet().toArray(), Stream.of( "v" ).toArray() );
158  Assert.assertArrayEquals( l_map.values().toArray(), Stream.of( 1 ).toArray() );
159 
160 
161 
162 
164  false, IContext.EMPTYPLAN,
165  Stream.of( "v", 666, l_map ).map( CRawTerm::from ).collect( Collectors.toList() ),
166 
167  Collections.emptyList()
168  );
169 
170  Assert.assertEquals( l_map.size(), 1 );
171  Assert.assertArrayEquals( l_map.keySet().toArray(), Stream.of( "v" ).toArray() );
172  Assert.assertArrayEquals( l_map.values().toArray(), Stream.of( 1 ).toArray() );
173  }
174 
175 
179  @Test
180  public final void putmultiple()
181  {
182  final Map<?, ?> l_map = new HashMap<>();
183 
184  new CPutMultiple().execute(
185  false, IContext.EMPTYPLAN,
186  Stream.of( l_map, "xx", 2, "yyy", 3 ).map( CRawTerm::from ).collect( Collectors.toList() ),
187  Collections.emptyList()
188  );
189 
190  Assert.assertEquals( l_map.size(), 2 );
191  Assert.assertArrayEquals( l_map.keySet().toArray(), Stream.of( "xx", "yyy" ).toArray() );
192  Assert.assertArrayEquals( l_map.values().toArray(), Stream.of( 2, 3 ).toArray() );
193 
194 
196  false, IContext.EMPTYPLAN,
197  Stream.of( l_map, "xx", 100, "zz", 4 ).map( CRawTerm::from ).collect( Collectors.toList() ),
198  Collections.emptyList()
199  );
200 
201  Assert.assertEquals( l_map.size(), 3 );
202  Assert.assertArrayEquals( l_map.keySet().toArray(), Stream.of( "xx", "zz", "yyy" ).toArray() );
203  Assert.assertArrayEquals( l_map.values().toArray(), Stream.of( 2, 4, 3 ).toArray() );
204  }
205 
206 
210  @Test
211  public final void remove()
212  {
213  final List<ITerm> l_return = new ArrayList<>();
214  final Map<Object, Object> l_map = new HashMap<>();
215 
216  l_map.put( "a", 1 );
217  l_map.put( "y", 2 );
218  l_map.put( "z", 3 );
219 
220  new CRemove().execute(
221  false, IContext.EMPTYPLAN,
222  Stream.of( l_map, "a", "z" ).map( CRawTerm::from ).collect( Collectors.toList() ),
223  l_return
224  );
225 
226  Assert.assertEquals( l_map.size(), 1 );
227  Assert.assertEquals( l_return.size(), 2 );
228  Assert.assertArrayEquals( l_return.stream().map( ITerm::raw ).toArray(), Stream.of( 1, 3 ).toArray() );
229  }
230 
231 
235  @Test
236  public final void getmultiple()
237  {
238  final List<ITerm> l_return = new ArrayList<>();
239  final Map<Object, Object> l_map = new HashMap<>();
240 
241  l_map.put( "i", 1 );
242  l_map.put( "j", 2 );
243  l_map.put( "k", 3 );
244 
245  new CGetMultiple().execute(
246  false, IContext.EMPTYPLAN,
247  Stream.of( l_map, "i", "j", "o" ).map( CRawTerm::from ).collect( Collectors.toList() ),
248  l_return
249  );
250 
251  Assert.assertEquals( l_return.size(), 3 );
252  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 1 );
253  Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 2 );
254  Assert.assertNull( l_return.get( 2 ).<Number>raw() );
255  }
256 
257 
261  @Test
262  public final void getsingle()
263  {
264  final List<ITerm> l_return = new ArrayList<>();
265 
266  final Map<Object, Object> l_map1 = new HashMap<>();
267  l_map1.put( "g", 123 );
268 
269  final Map<Object, Object> l_map2 = new HashMap<>();
270  l_map2.put( "g", "text" );
271 
272  new CGetSingle().execute(
273  false, IContext.EMPTYPLAN,
274  Stream.of( "g", l_map1, l_map2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
275  l_return
276  );
277 
278  Assert.assertEquals( l_return.size(), 2 );
279  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 123 );
280  Assert.assertEquals( l_return.get( 1 ).raw(), "text" );
281  }
282 
283 }
adds an single element to multiple map arguments.
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
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
adds an single element pair to all map iif not exists.
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: map/CKeys.java:72
< 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
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