|
| 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 | +} |
0 commit comments