Hi

# dont works!!
highway=*&  (maxheight<  3.5) {add goods=no; add delivery=no}
highway=*&  (maxweight<  7.5) {add goods=no; add delivery=no}

I have a patch for this, it wasn't as involved as I expected.

However (and this is probably why it wasn't done to begin with) equality is still always string based and never numeric.

Eg 3.5=3.5 but 3.5 is not equal to 3.50 or anything else which has the same numeric value but different string representation.

If that is important I can probably make it work.

..Steve
Index: test/uk/me/parabola/mkgmap/osmstyle/eval/ValueWithUnitTest.java
===================================================================
--- test/uk/me/parabola/mkgmap/osmstyle/eval/ValueWithUnitTest.java	(revision )
+++ test/uk/me/parabola/mkgmap/osmstyle/eval/ValueWithUnitTest.java	(revision )
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2011.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 or
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+package uk.me.parabola.mkgmap.osmstyle.eval;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests for values that may or may not have an associated unit (eg 70mph).
+ */
+public class ValueWithUnitTest {
+
+	@Test
+	public void testBareInteger() {
+		ValueWithUnit val = new ValueWithUnit("23");
+		assertTrue("valid value", val.isValid());
+	}
+
+	@Test
+	public void testInvalidNumber() {
+		ValueWithUnit val = new ValueWithUnit("abc");
+		assertFalse("invalid value", val.isValid());
+	}
+
+	@Test
+	public void testIntegerCompare() {
+		ValueWithUnit val1 = new ValueWithUnit("23");
+		ValueWithUnit val2 = new ValueWithUnit("24");
+
+		assertEquals(-1, val1.compareTo(val2));
+		assertEquals(1, val2.compareTo(val1));
+	}
+
+	@Test
+	public void testDecimalNumber() {
+		ValueWithUnit val = new ValueWithUnit("23.3");
+		assertTrue("valid value", val.isValid());
+	}
+
+	/**
+	 * Test that 23.5 is not the same as 23 alone. (Checks that the decimal part
+	 * is not being stripped off as the unit.)
+	 */
+	@Test
+	public void testDecimalNotEqualToInteger() {
+		ValueWithUnit val1 = new ValueWithUnit("23.5");
+		ValueWithUnit val2 = new ValueWithUnit("23");
+
+		assertEquals(1, val1.compareTo(val2));
+	}
+
+	@Test
+	public void testDecimalCompare() {
+		ValueWithUnit val1 = new ValueWithUnit("23.45");
+		ValueWithUnit val2 = new ValueWithUnit("23.46");
+
+		assertEquals(-1, val1.compareTo(val2));
+		assertEquals(1, val2.compareTo(val1));
+	}
+}
Index: src/uk/me/parabola/mkgmap/osmstyle/eval/ValueWithUnit.java
===================================================================
--- src/uk/me/parabola/mkgmap/osmstyle/eval/ValueWithUnit.java	(revision 2020)
+++ src/uk/me/parabola/mkgmap/osmstyle/eval/ValueWithUnit.java	(revision )
@@ -16,9 +16,11 @@
  */
 package uk.me.parabola.mkgmap.osmstyle.eval;
 
+import java.math.BigDecimal;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+
 /**
  * Represents a number and the units it is in. We want ultimately to be
  * able to do things like: is 10km/h > 8mph, and get the right answer
@@ -30,9 +32,10 @@
  */
 public class ValueWithUnit implements Comparable<ValueWithUnit> {
 	private static final Pattern EXTRACT_NUMBER_UNIT
-			= Pattern.compile("[ \t]*(-?[0-9]+)[ \t]*(.*)");
+			= Pattern.compile("[ \t]*(-?[0-9.]+)[ \t]*(.*)");
+	private static final BigDecimal ZERO = new BigDecimal(0);
 
-	private final int value;
+	private final BigDecimal value;
 	private final String unit;
 
 	private final boolean valid;
@@ -41,13 +44,11 @@
 		Matcher m = EXTRACT_NUMBER_UNIT.matcher(val);
 		boolean found = m.find();
 		if (found) {
-			// If found is true, then the first group is an integer, so no need
-			// to check.
-			value = Integer.parseInt(m.group(1));
+			value = new BigDecimal(m.group(1));
 			unit = m.group(2).trim();
 			valid = true;
 		} else {
-			value = 0;
+			value = ZERO;
 			unit = val;
 			valid = false;
 		}
@@ -61,8 +62,7 @@
 	 * To start with, just compare the value and ignore the unit.
 	 */
 	public int compareTo(ValueWithUnit o) {
-		if (value == o.value) return 0;
-		else return value < o.value ? -1 : 1;
+		return value.compareTo(o.value);
 	}
 
 	public boolean isValid() {
Index: src/uk/me/parabola/mkgmap/osmstyle/RuleFileReader.java
===================================================================
--- src/uk/me/parabola/mkgmap/osmstyle/RuleFileReader.java	(revision 2020)
+++ src/uk/me/parabola/mkgmap/osmstyle/RuleFileReader.java	(revision )
@@ -72,7 +72,7 @@
 
 	void load(Reader r, String name) {
 		scanner = new TokenScanner(name, r);
-		scanner.setExtraWordChars("-:");
+		scanner.setExtraWordChars("-:.");
 
 		ExpressionReader expressionReader = new ExpressionReader(scanner);
 		ActionReader actionReader = new ActionReader(scanner);
Index: test/uk/me/parabola/mkgmap/osmstyle/RuleFileReaderTest.java
===================================================================
--- test/uk/me/parabola/mkgmap/osmstyle/RuleFileReaderTest.java	(revision 2020)
+++ test/uk/me/parabola/mkgmap/osmstyle/RuleFileReaderTest.java	(revision )
@@ -411,6 +411,44 @@
 	}
 
 	/**
+	 * Test values such as 3.5 in comparisons.
+	 * Originally non-integer values were not allowed and were not even recognised.
+	 */
+	@Test
+	public void testDecimalValues() {
+		RuleSet rs = makeRuleSet("z=yes & a < 3.5 [0x1]");
+
+		Way el = new Way(1);
+		el.addTag("z", "yes");
+
+		// Is less than so
+		el.addTag("a", "2");
+		GType type = getFirstType(rs, el);
+		assertNotNull("a is less than 3.5", type);
+
+		el.addTag("a", "4");
+		type = getFirstType(rs, el);
+		assertNull("a is greater than 3.5", type);
+	}
+
+	@Test
+	public void testDecimalAndDecimalCompare() {
+		RuleSet rs = makeRuleSet("z=yes & a < 3.5 [0x1]");
+
+		Way el = new Way(1);
+		el.addTag("z", "yes");
+
+		// Is less than so
+		el.addTag("a", "3.49");
+		GType type = getFirstType(rs, el);
+		assertNotNull("a is less than 3.5", type);
+
+		el.addTag("a", "3.55");
+		type = getFirstType(rs, el);
+		assertNull("a is greater than 3.5", type);
+	}
+
+	/**
 	 * A moderately complex set of conditions and substitutions.
 	 */
 	@Test
_______________________________________________
mkgmap-dev mailing list
[email protected]
http://www.mkgmap.org.uk/mailman/listinfo/mkgmap-dev

Reply via email to