# HG changeset patch # User Peter Mehlitz # Date 1422982173 28800 # Node ID fdc263e5806b978214186891ec5856e9275e8c1e # Parent b920e6b1be83f7ebb20193f49415684b27d54eef added inverse matching in StringSetMatcher. Since this is not easy to do in regexes, it's at the next hight level in StringSetMatcher added a optional CG accessor interface (geChoice(i), getAllChoices(), getProcessedChoices() getUnprocessedChoices()) that can be used from listeners and peers to enumerate/analyse choice sets. Note that not all CGs have to support this as there is no requirement that CGs actually use pre-computed choice sets. The low level accessor is getChoice(i), ChoiceGeneratorBase provides generic (not very efficient) set accessor implementations. Note that ChoiceGeneratorBase.getChoice() has to be overridden in subclasses in order to support choice enumeration, the default impl is just there so that we don't break subclass compilation diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/util/StringSetMatcher.java --- a/src/main/gov/nasa/jpf/util/StringSetMatcher.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/util/StringSetMatcher.java Tue Feb 03 08:49:33 2015 -0800 @@ -22,18 +22,23 @@ /** * simple utility that can be used to check for string matches in - * sets with '*' wildcards, e.g. to check for class name lists like + * sets with '*' wildcards, e.g. to check for class name lists such as * * vm.halt_on_throw=java.lang.reflect.*:my.own.Exception * - * Only meta chars in patterns are '*', i.e. '.' is a regular char to match + * Only meta chars in patterns are '*' and '!', i.e. '.' is a regular char to match + * A '!' prefix inverts the match */ public class StringSetMatcher { + public static final char WILDCARD = '*'; + public static final char INVERTED = '!'; + boolean hasAnyPattern; // do we have a universal '*' pattern? Pattern[] pattern; Matcher[] matcher; + boolean[] inverted; /** * convenience method for matcher pairs containing of explicit excludes and @@ -67,6 +72,7 @@ int n = set.length; pattern = new Pattern[n]; matcher = new Matcher[n]; + inverted = new boolean[n]; for (int i=0; i0) { sb.append(','); } + if (inverted[i]){ + sb.append(INVERTED); + } sb.append(pattern[i]); } } - sb.append(']'); + sb.append('}'); return sb.toString(); } public void addPattern (String s){ if (s.equals("*")) { // no need to compile + // note that this doesn't include the - pointless - "!*", which would match nothing hasAnyPattern = true; } else { @@ -122,18 +133,33 @@ System.arraycopy(matcher, 0, mNew, 0, n); mNew[n] = pNew[n].matcher(""); + boolean[] iNew = new boolean[pNew.length]; + System.arraycopy( inverted, 0, iNew, 0, n); + iNew[n] = isInverted(s); + pattern = pNew; matcher = mNew; + inverted = iNew; } } - Pattern createPattern (String s){ + public static boolean isInverted (String s){ + return (!s.isEmpty() && s.charAt(0) == INVERTED); + } + + protected Pattern createPattern (String s){ Pattern p; + int j = 0; + int len = s.length(); + // inversion is better done outside of regex + if ((len > 0) && s.charAt(0) == INVERTED){ + j++; // skip INVERTED char + } + StringBuilder sb = new StringBuilder(); - - int len = s.length(); - for (int j=0; j= 0){ + Event e = base; + for (int i=0; i getPreviousChoiceGenerator(); int getNumberOfParents(); diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/ChoiceGeneratorBase.java --- a/src/main/gov/nasa/jpf/vm/ChoiceGeneratorBase.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/ChoiceGeneratorBase.java Tue Feb 03 08:49:33 2015 -0800 @@ -335,6 +335,67 @@ setDone(); } + // override this to support explicit CG enumeration from listeners etc. + + /** + * explicit choice enumeration. Override if supported + * @return choice value or null if not supported + */ + @Override + public T getChoice (int idx){ + return null; + } + + //--- generic choice set getter implementation + // Note - this requires an overloaded getChoice() and can be very slow (depending on CG implementation) + + @Override + public T[] getAllChoices(){ + int n = getTotalNumberOfChoices(); + T[] a = (T[]) new Object[n]; + for (int i=0; i= 0 && idx < 3){ + return values[idx]; + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } + + @Override public void reset () { count = -1; diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/choice/IntIntervalGenerator.java --- a/src/main/gov/nasa/jpf/vm/choice/IntIntervalGenerator.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/choice/IntIntervalGenerator.java Tue Feb 03 08:49:33 2015 -0800 @@ -95,6 +95,16 @@ } @Override + public Integer getChoice (int idx){ + int nChoices = getTotalNumberOfChoices(); + if (idx >= 0 && idx < nChoices){ + return min + idx*delta; + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } + + @Override public Integer getNextChoice () { return new Integer(next); } diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/choice/InvocationCG.java --- a/src/main/gov/nasa/jpf/vm/choice/InvocationCG.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/choice/InvocationCG.java Tue Feb 03 08:49:33 2015 -0800 @@ -44,6 +44,15 @@ } @Override + public Invocation getChoice (int idx){ + if (idx >=0 && idx < invokes.size()){ + return invokes.get(idx); + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } + + @Override public void advance () { cur = it.next(); } diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/choice/NumberChoiceFromList.java --- a/src/main/gov/nasa/jpf/vm/choice/NumberChoiceFromList.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/choice/NumberChoiceFromList.java Tue Feb 03 08:49:33 2015 -0800 @@ -76,6 +76,14 @@ } } + @Override + public T getChoice (int idx){ + if (idx >=0 && idx < values.length){ + return values[idx]; + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } @Override public void reset () { diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/choice/RandomIntIntervalGenerator.java --- a/src/main/gov/nasa/jpf/vm/choice/RandomIntIntervalGenerator.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/choice/RandomIntIntervalGenerator.java Tue Feb 03 08:49:33 2015 -0800 @@ -72,6 +72,21 @@ random = new Random(seed); } + @Override + public Integer getChoice (int idx){ + if (idx >= 0 && idx < nChoices){ + // Ok, this is really not efficient - use only for non-performance critical operations + Random r = new Random(seed); + int v=0; + for (int i=0; i= 0 && idx < choices.length){ + return choices[idx]; + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } + + @Override public Integer getNextChoice() { return new Integer(choices[nextIdx]); } diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/choice/RandomOrderLongCG.java --- a/src/main/gov/nasa/jpf/vm/choice/RandomOrderLongCG.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/choice/RandomOrderLongCG.java Tue Feb 03 08:49:33 2015 -0800 @@ -45,6 +45,16 @@ } nextIdx = -1; } + + @Override + public Long getChoice (int idx){ + if (idx >= 0 && idx < choices.length){ + return choices[idx]; + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } + @Override public Long getNextChoice() { diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/choice/ThreadChoiceFromSet.java --- a/src/main/gov/nasa/jpf/vm/choice/ThreadChoiceFromSet.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/choice/ThreadChoiceFromSet.java Tue Feb 03 08:49:33 2015 -0800 @@ -59,6 +59,16 @@ } @Override + public ThreadInfo getChoice (int idx){ + if (idx >= 0 && idx < values.length){ + return values[idx]; + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } + + + @Override public void reset () { count = -1; diff -r b920e6b1be83 -r fdc263e5806b src/main/gov/nasa/jpf/vm/choice/TypedObjectChoice.java --- a/src/main/gov/nasa/jpf/vm/choice/TypedObjectChoice.java Sat Jan 24 18:19:08 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/choice/TypedObjectChoice.java Tue Feb 03 08:49:33 2015 -0800 @@ -73,6 +73,16 @@ } @Override + public Integer getChoice (int idx){ + if (idx >= 0 && idx < values.length){ + return values[idx]; + } else { + throw new IllegalArgumentException("choice index out of range: " + idx); + } + } + + + @Override public void advance () { count++; } diff -r b920e6b1be83 -r fdc263e5806b src/tests/gov/nasa/jpf/util/StringSetMatcherTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tests/gov/nasa/jpf/util/StringSetMatcherTest.java Tue Feb 03 08:49:33 2015 -0800 @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2015, United States Government, as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All rights reserved. + * + * The Java Pathfinder core (jpf-core) platform is licensed under the + * Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package gov.nasa.jpf.util; + +import gov.nasa.jpf.util.test.TestJPF; +import org.junit.Test; + +/** + * regression test for .util.StringSetMatcher + */ +public class StringSetMatcherTest extends TestJPF { + + @Test + public void testInversion (){ + StringSetMatcher ssm = new StringSetMatcher("!failure-*", "failure-10"); + + assertTrue( ssm.matchesAny("blah")); + assertFalse( ssm.matchesAny("failure-0")); + + assertTrue( ssm.matchesAny("failure-10")); + assertFalse( ssm.matchesAll("failure-10")); + } + + @Test + public void testMatchesAll (){ + StringSetMatcher ssm = new StringSetMatcher("a*", "*blah"); + + assertTrue( ssm.matchesAll("aXXblah")); + assertFalse( ssm.matchesAll("xblah")); + } + + @Test + public void testMatchesAny (){ + StringSetMatcher ssm = new StringSetMatcher("blah", "gna"); + + assertTrue( ssm.matchesAny("blah")); + assertFalse( ssm.matchesAny("xblah")); + } + + @Test + public void testHasAnyPattern(){ + StringSetMatcher ssm = new StringSetMatcher("*", "gna"); + assertTrue( ssm.matchesAny("blubb")); + assertTrue( ssm.matchesAll("gna")); + + ssm = new StringSetMatcher("*"); // single pattern optimization + assertTrue(ssm.matchesAll("gna")); + assertTrue(ssm.matchesAny("gulp")); + } + +}