LightJason - AgentSpeak(L++)
TestCActionCollectionSet.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 org.junit.Assert;
27 import org.junit.Test;
37 
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Set;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45 
46 
50 public final class TestCActionCollectionSet extends IBaseTest
51 {
52 
56  @Test
57  public final void create()
58  {
59  final List<ITerm> l_return = new ArrayList<>();
60 
61  new CCreate().execute(
62  false, IContext.EMPTYPLAN,
63  Stream.of( 1, 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
64  l_return
65  );
66 
67  Assert.assertEquals( l_return.size(), 1 );
68  Assert.assertTrue( l_return.get( 0 ).raw() instanceof Set<?> );
69  Assert.assertArrayEquals( l_return.get( 0 ).<Set<?>>raw().toArray(), Stream.of( 1, 2 ).toArray() );
70  }
71 
75  @Test
76  public final void createsynchronized()
77  {
78  final List<ITerm> l_return = new ArrayList<>();
79 
80  new CCreate().execute(
81  true, IContext.EMPTYPLAN,
82  Stream.of( 10, 20 ).map( CRawTerm::from ).collect( Collectors.toList() ),
83  l_return
84  );
85 
86  Assert.assertEquals( l_return.size(), 1 );
87  Assert.assertEquals( l_return.get( 0 ).raw().getClass(), Collections.synchronizedSet( Collections.emptySet() ).getClass() );
88  Assert.assertArrayEquals( l_return.get( 0 ).<Set<?>>raw().toArray(), Stream.of( 20, 10 ).toArray() );
89  }
90 
91 
95  @Test
96  public final void add()
97  {
98  final Set<Object> l_set = new HashSet<>();
99 
100  new CAdd().execute(
101  false, IContext.EMPTYPLAN,
102  Stream.of( l_set, 1, 5, 7 ).map( CRawTerm::from ).collect( Collectors.toList() ),
103  Collections.emptyList()
104  );
105 
106  Assert.assertArrayEquals( l_set.toArray(), Stream.of( 1, 5, 7 ).toArray() );
107  }
108 
112  @Test
113  public final void contains()
114  {
115  final List<ITerm> l_return = new ArrayList<>();
116  final Set<Object> l_set = Stream.of( "foo", 1, 2 ).collect( Collectors.toSet() );
117 
118  new CContains().execute(
119  false, IContext.EMPTYPLAN,
120  Stream.of( l_set, "foo", 1, "bar" ).map( CRawTerm::from ).collect( Collectors.toList() ),
121  l_return
122  );
123 
124  Assert.assertArrayEquals( l_return.stream().map( ITerm::raw ).toArray(), Stream.of( true, true, false ).toArray() );
125  }
126 
130  @Test
131  public final void remove()
132  {
133  final List<ITerm> l_return = new ArrayList<>();
134  final Set<Object> l_set = Stream.of( "foo", 1, 2 ).collect( Collectors.toSet() );
135 
136  new CRemove().execute(
137  false, IContext.EMPTYPLAN,
138  Stream.of( l_set, "foo", 1, 5 ).map( CRawTerm::from ).collect( Collectors.toList() ),
139  l_return
140  );
141 
142  Assert.assertArrayEquals( l_return.stream().map( ITerm::raw ).toArray(), Stream.of( true, true, false ).toArray() );
143  Assert.assertArrayEquals( l_set.toArray(), Stream.of( 2 ).toArray() );
144  }
145 
149  @Test
150  public final void tolist()
151  {
152  final List<ITerm> l_return = new ArrayList<>();
153  final Set<Object> l_set = Stream.of( "foobar", "x", "y" ).collect( Collectors.toSet() );
154 
155  new CToList().execute(
156  false, IContext.EMPTYPLAN,
157  Stream.of( l_set ).map( CRawTerm::from ).collect( Collectors.toList() ),
158  l_return
159  );
160 
161  Assert.assertEquals( l_return.size(), 1 );
162  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
163  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), l_set.toArray() );
164  }
165 
166 }
removes any argument from the set and returns it.
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
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
< 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
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