LightJason - AgentSpeak(L++)
TestCActionString.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.tngtech.java.junit.dataprovider.DataProvider;
28 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
29 import com.tngtech.java.junit.dataprovider.UseDataProvider;
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
50 
51 import java.io.UnsupportedEncodingException;
52 import java.util.AbstractMap;
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.List;
56 import java.util.Locale;
57 import java.util.stream.Collectors;
58 import java.util.stream.Stream;
59 
60 
64 @RunWith( DataProviderRunner.class )
65 public final class TestCActionString extends IBaseTest
66 {
67 
72  @DataProvider
73  public static Object[] generate()
74  {
75  return Stream.of(
76  Stream.of( "fooo", "#!$foo", "1234o097", "AboCDef", "foo", "BARo" ).collect( Collectors.toList() )
77  ).toArray();
78  }
79 
85  @Test
86  @UseDataProvider( "generate" )
87  public final void base64( final List<String> p_input )
88  {
89  final List<ITerm> l_return = new ArrayList<>();
90  final List<ITerm> l_result = new ArrayList<>();
91 
92  new CBase64Encode().execute(
93  false, IContext.EMPTYPLAN,
94  p_input.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
95  l_return
96  );
97 
98  new CBase64Decode().execute(
99  false, IContext.EMPTYPLAN,
100  l_return,
101  l_result
102  );
103 
104  StreamUtils.zip(
105  p_input.stream(),
106  l_result.stream().map( ITerm::<String>raw ),
107  AbstractMap.SimpleImmutableEntry::new
108  ).forEach( i -> Assert.assertEquals( i.getKey(), i.getValue() ) );
109  }
110 
111 
117  @Test
118  public final void base64decodeerror() throws UnsupportedEncodingException
119  {
120  Assert.assertFalse(
121  new CBase64Decode().execute(
122  false, IContext.EMPTYPLAN,
123  Stream.of( new String( "test encodingwith german additional character: öäß".getBytes( "UTF-16" ), "UTF-16" ) )
124  .map( CRawTerm::from )
125  .collect( Collectors.toList() ),
126  Collections.emptyList()
127  ).value()
128  );
129  }
130 
131 
137  @Test
138  @UseDataProvider( "generate" )
139  public final void concat( final List<String> p_input )
140  {
141  final List<ITerm> l_return = new ArrayList<>();
142 
143  new CConcat().execute(
144  false, IContext.EMPTYPLAN,
145  p_input.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
146  l_return
147  );
148 
149  Assert.assertEquals(
150  l_return.get( 0 ).<String>raw(),
151  p_input.stream().collect( Collectors.joining() )
152  );
153  }
154 
155 
161  @Test
162  @UseDataProvider( "generate" )
163  public final void contains( final List<String> p_input )
164  {
165  final List<ITerm> l_return = new ArrayList<>();
166 
167  new CContains().execute(
168  false, IContext.EMPTYPLAN,
169  Stream.concat(
170  Stream.of( p_input.stream().collect( Collectors.joining() ) ),
171  p_input.stream()
172  ).map( CRawTerm::from ).collect( Collectors.toList() ),
173  l_return
174  );
175 
176  Assert.assertTrue(
177  l_return.stream()
178  .allMatch( ITerm::<Boolean>raw )
179  );
180  }
181 
182 
188  @Test
189  @UseDataProvider( "generate" )
190  public final void lower( final List<String> p_input )
191  {
192  final List<ITerm> l_return = new ArrayList<>();
193 
194  new CLower().execute(
195  false, IContext.EMPTYPLAN,
196  p_input.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
197  l_return
198  );
199 
200 
201  StreamUtils.zip(
202  p_input.stream().map( i -> i.toLowerCase( Locale.ROOT ) ),
203  l_return.stream().map( ITerm::<String>raw ),
204  AbstractMap.SimpleImmutableEntry::new
205  ).forEach( i -> Assert.assertEquals( i.getKey(), i.getValue() ) );
206  }
207 
208 
214  @Test
215  @UseDataProvider( "generate" )
216  public final void reverse( final List<String> p_input )
217  {
218  final List<ITerm> l_return = new ArrayList<>();
219 
220  new CReverse().execute(
221  false, IContext.EMPTYPLAN,
222  p_input.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
223  l_return
224  );
225 
226 
227  StreamUtils.zip(
228  p_input.stream().map( i -> new StringBuilder( i ).reverse().toString() ),
229  l_return.stream().map( ITerm::<String>raw ),
230  AbstractMap.SimpleImmutableEntry::new
231  ).forEach( i -> Assert.assertEquals( i.getKey(), i.getValue() ) );
232  }
233 
234 
240  @Test
241  @UseDataProvider( "generate" )
242  public final void size( final List<String> p_input )
243  {
244  final List<ITerm> l_return = new ArrayList<>();
245 
246  new CSize().execute(
247  false, IContext.EMPTYPLAN,
248  p_input.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
249  l_return
250  );
251 
252 
253  StreamUtils.zip(
254  p_input.stream().mapToLong( String::length ).boxed(),
255  l_return.stream().map( ITerm::<Number>raw ).map( Number::longValue ),
256  AbstractMap.SimpleImmutableEntry::new
257  ).forEach( i -> Assert.assertEquals( i.getKey(), i.getValue() ) );
258  }
259 
260 
266  @Test
267  @UseDataProvider( "generate" )
268  public final void random( final List<String> p_input )
269  {
270  final List<ITerm> l_return = new ArrayList<>();
271 
272  new CRandom().execute(
273  false, IContext.EMPTYPLAN,
274  Stream.concat(
275  Stream.of( p_input.stream().collect( Collectors.joining() ) ),
276  p_input.stream().mapToInt( String::length ).boxed()
277  ).map( CRawTerm::from ).collect( Collectors.toList() ),
278  l_return
279  );
280 
281  StreamUtils.zip(
282  p_input.stream().mapToInt( String::length ).boxed(),
283  l_return.stream().map( ITerm::<String>raw ).mapToInt( String::length ).boxed(),
284  AbstractMap.SimpleImmutableEntry::new
285  ).forEach( i -> Assert.assertEquals( i.getKey(), i.getValue() ) );
286  }
287 
288 
294  @Test
295  @UseDataProvider( "generate" )
296  public final void upper( final List<String> p_input )
297  {
298  final List<ITerm> l_return = new ArrayList<>();
299 
300  new CUpper().execute(
301  false, IContext.EMPTYPLAN,
302  p_input.stream().map( CRawTerm::from ).collect( Collectors.toList() ),
303  l_return
304  );
305 
306 
307  StreamUtils.zip(
308  p_input.stream().map( i -> i.toUpperCase( Locale.ROOT ) ),
309  l_return.stream().map( ITerm::<String>raw ),
310  AbstractMap.SimpleImmutableEntry::new
311  ).forEach( i -> Assert.assertEquals( i.getKey(), i.getValue() ) );
312  }
313 
314 
318  @Test
319  public final void startswith()
320  {
321  final List<ITerm> l_return = new ArrayList<>();
322 
323  new CStartsWith().execute(
324  false, IContext.EMPTYPLAN,
325  Stream.of( "this is an input text", "this", "th", "is" ).map( CRawTerm::from ).collect( Collectors.toList() ),
326  l_return
327  );
328 
329  Assert.assertEquals( l_return.size(), 3 );
330  Assert.assertTrue( l_return.get( 0 ).<Boolean>raw() );
331  Assert.assertTrue( l_return.get( 1 ).<Boolean>raw() );
332  Assert.assertFalse( l_return.get( 2 ).<Boolean>raw() );
333  }
334 
335 
339  @Test
340  public final void endswidth()
341  {
342  final List<ITerm> l_return = new ArrayList<>();
343 
344  new CEndsWith().execute(
345  false, IContext.EMPTYPLAN,
346  Stream.of( "this is a new input text with a cool ending", "ing", "this", "g" ).map( CRawTerm::from ).collect( Collectors.toList() ),
347  l_return
348  );
349 
350  Assert.assertEquals( l_return.size(), 3 );
351  Assert.assertTrue( l_return.get( 0 ).<Boolean>raw() );
352  Assert.assertFalse( l_return.get( 1 ).<Boolean>raw() );
353  Assert.assertTrue( l_return.get( 2 ).<Boolean>raw() );
354  }
355 
356 
360  @Test
361  public final void levenshteinerror()
362  {
363  Assert.assertFalse(
364  new CLevenshtein().execute(
365  false, IContext.EMPTYPLAN,
366  Collections.emptyList(),
367  Collections.emptyList()
368  ).value()
369  );
370  }
371 
372 
376  @Test
377  public final void levenshtein()
378  {
379  final List<ITerm> l_return = new ArrayList<>();
380 
381  new CLevenshtein().execute(
382  false, IContext.EMPTYPLAN,
383  Stream.of( "kitten", "sitting", "singing" ).map( CRawTerm::from ).collect( Collectors.toList() ),
384  l_return
385  );
386 
387  Assert.assertEquals( l_return.size(), 2 );
388  Assert.assertEquals( l_return.get( 0 ).<Number>raw().intValue(), 3 );
389  Assert.assertEquals( l_return.get( 1 ).<Number>raw().intValue(), 5 );
390  }
391 
392 
396  @Test
397  public final void ncd()
398  {
399  final List<ITerm> l_return = new ArrayList<>();
400 
401  new CNCD().execute(
402  false, IContext.EMPTYPLAN,
403  Stream.of( "test", "tests", "this a complete other string", "test" ).map( CRawTerm::from ).collect( Collectors.toList() ),
404  l_return
405  );
406 
407  Assert.assertEquals( l_return.size(), 3 );
408  Assert.assertEquals( l_return.get( 0 ).<Number>raw().doubleValue(), 0.04878048780487805, 0.0001 );
409  Assert.assertEquals( l_return.get( 1 ).<Number>raw().doubleValue(), 0.38333333333333336, 0.0001 );
410  Assert.assertEquals( l_return.get( 2 ).<Number>raw().doubleValue(), 0, 0 );
411 
412 
413  new CNCD().execute(
414  false, IContext.EMPTYPLAN,
415  Stream.of( "GZIP", "test", "tests", "this a complete other string", "test" ).map( CRawTerm::from ).collect( Collectors.toList() ),
416  l_return
417  );
418 
419  Assert.assertEquals( l_return.size(), 6 );
420  Assert.assertEquals( l_return.get( 3 ).<Number>raw().doubleValue(), 0.12, 0 );
421  Assert.assertEquals( l_return.get( 4 ).<Number>raw().doubleValue(), 0.5833333333333334, 0.0001 );
422  Assert.assertEquals( l_return.get( 5 ).<Number>raw().doubleValue(), 0, 0 );
423  }
424 
425 
429  @Test
430  public final void ncderror()
431  {
432  Assert.assertFalse(
433  new CNCD().execute(
434  false, IContext.EMPTYPLAN,
435  Collections.emptyList(),
436  Collections.emptyList()
437  ).value()
438  );
439  }
440 
441 }
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: CLower.java:62
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
action to check string for starts-with.
base test class with helpers
Definition: IBaseTest.java:33
action to create an upper-case string.
Definition: CUpper.java:46
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
action to concat / join all strings.
Definition: CConcat.java:48
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: CConcat.java:64
final void levenshteinerror()
tets for levenshtein distance error
final void ncderror()
test normalized compression distance error
execution context with local data
Definition: IContext.java:42
action to check string for containing another string.
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: CRandom.java:65
calculates the normalized-compression-distance.
action to create random strings, with a definied length.
Definition: CRandom.java:49
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
action to check string for ends-with.
Definition: CEndsWith.java:47
action to create a lower-case string.
Definition: CLower.java:46
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
Definition: CUpper.java:62
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: CEndsWith.java:63
final void base64decodeerror()
test base64 decode with errors
static Object [] generate()
data provider generator
final void ncd()
test normalized compression distance
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