Skip to content

Commit beab1cd

Browse files
author
Philipp Kraus
committedMar 26, 2019
fix test
1 parent 9906be4 commit beab1cd

File tree

4 files changed

+181
-1
lines changed

4 files changed

+181
-1
lines changed
 

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# LightJason - Bit Action
1+
# LightJason - Map Action
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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.map;
25+
26+
import com.codepoetics.protonpack.StreamUtils;
27+
import com.google.common.collect.HashMultimap;
28+
import com.google.common.collect.Multimap;
29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
import org.lightjason.agentspeak.language.CRawTerm;
32+
import org.lightjason.agentspeak.language.ITerm;
33+
import org.lightjason.agentspeak.language.execution.IContext;
34+
35+
import java.util.ArrayList;
36+
import java.util.Collections;
37+
import java.util.HashMap;
38+
import java.util.HashSet;
39+
import java.util.List;
40+
import java.util.Map;
41+
import java.util.stream.Collectors;
42+
import java.util.stream.IntStream;
43+
import java.util.stream.Stream;
44+
45+
46+
/**
47+
* generic map actions
48+
*/
49+
public final class TestCAction
50+
{
51+
/**
52+
* test clear map
53+
*/
54+
@Test
55+
public void clearmap()
56+
{
57+
final Map<Integer, Integer> l_map = StreamUtils.windowed( IntStream.range( 100, 120 ).boxed(), 2 )
58+
.collect( Collectors.toMap( i -> i.get( 0 ), i -> i.get( 1 ) ) );
59+
final Multimap<Integer, Integer> l_multimap = HashMultimap.create();
60+
IntStream.range( 0, 5 ).forEach( i -> IntStream.range( i, i + 5 ).forEach( j -> l_map.put( i, j ) ) );
61+
62+
new CMapClear().execute(
63+
false, IContext.EMPTYPLAN,
64+
Stream.of( l_map, l_multimap ).map( CRawTerm::of ).collect( Collectors.toList() ),
65+
Collections.emptyList()
66+
);
67+
68+
Assert.assertTrue( l_map.isEmpty() );
69+
Assert.assertTrue( l_multimap.isEmpty() );
70+
71+
}
72+
73+
/**
74+
* test empty map
75+
*/
76+
@Test
77+
public void emptymap()
78+
{
79+
final List<ITerm> l_return = new ArrayList<>();
80+
81+
new CMapIsEmpty().execute(
82+
false, IContext.EMPTYPLAN,
83+
Stream.of( new ArrayList<>(), new HashSet<>(), HashMultimap.create(), new HashMap<>(), Stream.of( "1", 2 ).collect( Collectors.toList() ), new Object() )
84+
.map( CRawTerm::of )
85+
.collect( Collectors.toList() ),
86+
l_return
87+
);
88+
89+
Assert.assertEquals( 6, l_return.size() );
90+
Assert.assertArrayEquals( Stream.of( false, false, true, true, false, false ).toArray(), l_return.stream().map( ITerm::<Boolean>raw ).toArray() );
91+
}
92+
93+
/**
94+
* test map size
95+
*/
96+
@Test
97+
public void mapsize()
98+
{
99+
final Map<String, String> l_map = new HashMap<>();
100+
l_map.put( "a", "b" );
101+
l_map.put( "c", "d" );
102+
103+
final HashMultimap<String, String> l_multimap = HashMultimap.create();
104+
l_multimap.put( "a", "b" );
105+
l_multimap.put( "a", "x" );
106+
l_multimap.put( "c", "d" );
107+
108+
final List<ITerm> l_return = new ArrayList<>();
109+
110+
111+
new CMapSize().execute(
112+
false, IContext.EMPTYPLAN,
113+
Stream.of( new ArrayList<>(), new HashSet<>(), l_multimap, l_map, Stream.of( "1", 2 ).collect( Collectors.toList() ), new Object() )
114+
.map( CRawTerm::of )
115+
.collect( Collectors.toList() ),
116+
l_return
117+
);
118+
119+
Assert.assertArrayEquals( Stream.of( 0L, 0L, 3L, 2L, 0L, 0L ).toArray(), l_return.stream().map( ITerm::raw ).toArray() );
120+
}
121+
}

‎src/test/java/org/lightjason/agentspeak/action/map/TestCActionCollectionMap.java

+21
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.lightjason.agentspeak.action.map.map.CGetMultiple;
3131
import org.lightjason.agentspeak.action.map.map.CGetSingle;
3232
import org.lightjason.agentspeak.action.map.map.CKeys;
33+
import org.lightjason.agentspeak.action.map.map.CLambdaStreaming;
3334
import org.lightjason.agentspeak.action.map.map.CPutMultiple;
3435
import org.lightjason.agentspeak.action.map.map.CPutMultipleIfAbsent;
3536
import org.lightjason.agentspeak.action.map.map.CPutSingle;
@@ -280,4 +281,24 @@ public void getsingle()
280281
Assert.assertEquals( "text", l_return.get( 1 ).raw() );
281282
}
282283

284+
/**
285+
* test lambda
286+
*/
287+
@Test
288+
@SuppressWarnings( "unchecked" )
289+
public void lambda()
290+
{
291+
final Map<Integer, String> l_map = new HashMap<>();
292+
l_map.put( 1, "foo123" );
293+
l_map.put( 2, "bar123" );
294+
295+
296+
Assert.assertArrayEquals(
297+
Stream.of( 1, "foo123", 2, "bar123" ).toArray(),
298+
new CLambdaStreaming().apply( l_map )
299+
.map( i -> (Map.Entry<Integer, String>) i )
300+
.flatMap( i -> Stream.of( i.getKey(), i.getValue() ) )
301+
.toArray()
302+
);
303+
}
283304
}

‎src/test/java/org/lightjason/agentspeak/action/map/TestCActionCollectionMultimap.java

+38
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
import org.lightjason.agentspeak.action.map.multimap.CGetMultiple;
3636
import org.lightjason.agentspeak.action.map.multimap.CGetSingle;
3737
import org.lightjason.agentspeak.action.map.multimap.CKeys;
38+
import org.lightjason.agentspeak.action.map.multimap.CLambdaStreaming;
3839
import org.lightjason.agentspeak.action.map.multimap.CPutMultiple;
3940
import org.lightjason.agentspeak.action.map.multimap.CPutSingle;
4041
import org.lightjason.agentspeak.action.map.multimap.CValues;
42+
import org.lightjason.agentspeak.error.context.CExecutionIllegealArgumentException;
4143
import org.lightjason.agentspeak.language.CCommon;
4244
import org.lightjason.agentspeak.language.CRawTerm;
4345
import org.lightjason.agentspeak.language.ITerm;
@@ -49,6 +51,7 @@
4951
import java.util.List;
5052
import java.util.Map;
5153
import java.util.Random;
54+
import java.util.Set;
5255
import java.util.stream.Collectors;
5356
import java.util.stream.IntStream;
5457
import java.util.stream.Stream;
@@ -97,6 +100,19 @@ public void createsynchronized()
97100
Assert.assertEquals( Multimaps.synchronizedSetMultimap( HashMultimap.create() ).getClass(), l_return.get( 0 ).raw().getClass() );
98101
}
99102

103+
/**
104+
* test create error
105+
*/
106+
@Test( expected = CExecutionIllegealArgumentException.class )
107+
public void createerror()
108+
{
109+
new CCreate().execute(
110+
true, IContext.EMPTYPLAN,
111+
Stream.of( "x" ).map( CRawTerm::of ).collect( Collectors.toList() ),
112+
Collections.emptyList()
113+
);
114+
}
115+
100116

101117
/**
102118
* test key and values
@@ -256,4 +272,26 @@ public void getsingle()
256272
Assert.assertArrayEquals( Stream.of( "foobar" ).toArray(), l_return.get( 1 ).<List<?>>raw().toArray() );
257273
}
258274

275+
/**
276+
* test lambda
277+
*/
278+
@Test
279+
@SuppressWarnings( "unchecked" )
280+
public void lambda()
281+
{
282+
final Multimap<Integer, String> l_map = HashMultimap.create();
283+
l_map.put( 1, "foo123" );
284+
l_map.put( 1, "foo456" );
285+
l_map.put( 2, "bar123" );
286+
287+
288+
Assert.assertArrayEquals(
289+
Stream.of( 1, "foo123", "foo456", 2, "bar123" ).toArray(),
290+
new CLambdaStreaming().apply( l_map )
291+
.map( i -> (Map.Entry<Integer, Set<String>>) i )
292+
.flatMap( i -> Stream.concat( Stream.of( i.getKey() ), i.getValue().stream() ) )
293+
.toArray()
294+
);
295+
}
296+
259297
}

0 commit comments

Comments
 (0)
Please sign in to comment.