commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r555375 - in /jakarta/commons/proper/jxpath/trunk/src: java/org/apache/commons/jxpath/ri/compiler/ test/org/apache/commons/jxpath/ri/compiler/
Date Wed, 11 Jul 2007 19:30:00 GMT
Author: mbenson
Date: Wed Jul 11 12:29:57 2007
New Revision: 555375

URL: http://svn.apache.org/viewvc?view=rev&rev=555375
Log:
[JXPATH-95] part 2: fix NaN inequality comparisons

Modified:
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationCompare.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationEqual.java
    jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationNotEqual.java
    jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationCompare.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationCompare.java?view=diff&rev=555375&r1=555374&r2=555375
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationCompare.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationCompare.java
Wed Jul 11 12:29:57 2007
@@ -34,9 +34,27 @@
  * @version $Revision$ $Date$
  */
 public abstract class CoreOperationCompare extends CoreOperation {
+    private boolean invert;
 
     public CoreOperationCompare(Expression arg1, Expression arg2) {
+        this(arg1, arg2, false);
+    }
+
+    protected CoreOperationCompare(Expression arg1, Expression arg2, boolean invert) {
         super(new Expression[] { arg1, arg2 });
+        this.invert = invert;
+    }
+
+    public Object computeValue(EvalContext context) {
+        return equal(context, args[0], args[1]) ? Boolean.TRUE : Boolean.FALSE;
+    }
+
+    protected int getPrecedence() {
+        return 2;
+    }
+
+    protected boolean isSymmetric() {
+        return true;
     }
 
     /**
@@ -110,12 +128,6 @@
     }
 
     protected boolean equal(Object l, Object r) {
-        if (l instanceof Pointer && r instanceof Pointer) {
-            if (l.equals(r)) {
-                return true;
-            }
-        }
-
         if (l instanceof Pointer) {
             l = ((Pointer) l).getValue();
         }
@@ -124,21 +136,28 @@
             r = ((Pointer) r).getValue();
         }
 
-        if (l == r) {
-            return true;
-        }
-
+        boolean result;
         if (l instanceof Boolean || r instanceof Boolean) {
-            return (InfoSetUtil.booleanValue(l) == InfoSetUtil.booleanValue(r));
-        }
-        if (l instanceof Number || r instanceof Number) {
-            return (InfoSetUtil.doubleValue(l) == InfoSetUtil.doubleValue(r));
-        }
-        if (l instanceof String || r instanceof String) {
-            return (
-                InfoSetUtil.stringValue(l).equals(InfoSetUtil.stringValue(r)));
+            result = l == r || InfoSetUtil.booleanValue(l) == InfoSetUtil.booleanValue(r);
+        } else if (l instanceof Number || r instanceof Number) {
+            //if either side is NaN, no comparison returns true:
+            double ld = InfoSetUtil.doubleValue(l);
+            if (Double.isNaN(ld)) {
+                return false;
+            }
+            double rd = InfoSetUtil.doubleValue(r);
+            if (Double.isNaN(rd)) {
+                return false;
+            }
+            result = ld == rd;
+        } else {
+            if (l instanceof String || r instanceof String) {
+                l = InfoSetUtil.stringValue(l);
+                r = InfoSetUtil.stringValue(r);
+            }
+            result = l == r || l != null && l.equals(r);
         }
-        return l != null && l.equals(r);
+        return result ^ invert;
     }
 
 }

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationEqual.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationEqual.java?view=diff&rev=555375&r1=555374&r2=555375
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationEqual.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationEqual.java
Wed Jul 11 12:29:57 2007
@@ -16,8 +16,6 @@
  */
 package org.apache.commons.jxpath.ri.compiler;
 
-import org.apache.commons.jxpath.ri.EvalContext;
-
 /**
  * Implementation of Expression for the operation "=".
  *
@@ -30,18 +28,6 @@
         super(arg1, arg2);
     }
 
-    public Object computeValue(EvalContext context) {
-        return equal(context, args[0], args[1]) ? Boolean.TRUE : Boolean.FALSE;
-    }
-    
-    protected int getPrecedence() {
-        return 2;
-    }
-
-    protected boolean isSymmetric() {
-        return true;
-    }
-    
     public String getSymbol() {
         return "=";
     }

Modified: jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationNotEqual.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationNotEqual.java?view=diff&rev=555375&r1=555374&r2=555375
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationNotEqual.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/compiler/CoreOperationNotEqual.java
Wed Jul 11 12:29:57 2007
@@ -16,8 +16,6 @@
  */
 package org.apache.commons.jxpath.ri.compiler;
 
-import org.apache.commons.jxpath.ri.EvalContext;
-
 /**
  * Implementation of Expression for the operation "!=".
  *
@@ -27,21 +25,9 @@
 public class CoreOperationNotEqual extends CoreOperationCompare {
 
     public CoreOperationNotEqual(Expression arg1, Expression arg2) {
-        super(arg1, arg2);
+        super(arg1, arg2, true);
     }
 
-    public Object computeValue(EvalContext context) {
-        return equal(context, args[0], args[1]) ? Boolean.FALSE : Boolean.TRUE;
-    }
-    
-    protected int getPrecedence() {
-        return 2;
-    }
-
-    protected boolean isSymmetric() {
-        return true;
-    }
-    
     public String getSymbol() {
         return "!=";
     }

Modified: jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java?view=diff&rev=555375&r1=555374&r2=555375
==============================================================================
--- jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
(original)
+++ jakarta/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/compiler/CoreOperationTest.java
Wed Jul 11 12:29:57 2007
@@ -117,9 +117,8 @@
         assertXPathValue(context, "$nan < $nan", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan >= $nan", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan <= $nan", Boolean.FALSE, Boolean.class);
-        //note that "<= and >=" implies a test of numerical, rather than object, equality.
;)
         assertXPathValue(context, "$nan >= $nan and $nan <= $nan", Boolean.FALSE, Boolean.class);
-        assertXPathValue(context, "$nan = $nan", Boolean.TRUE, Boolean.class);
+        assertXPathValue(context, "$nan = $nan", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan != $nan", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan > 0", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan < 0", Boolean.FALSE, Boolean.class);
@@ -127,13 +126,13 @@
         assertXPathValue(context, "$nan <= 0", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan >= 0 and $nan <= 0", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan = 0", Boolean.FALSE, Boolean.class);
-        assertXPathValue(context, "$nan != 0", Boolean.TRUE, Boolean.class);
+        assertXPathValue(context, "$nan != 0", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan > 1", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan < 1", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan >= 1", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan <= 1", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan >= 1 and $nan <= 1", Boolean.FALSE, Boolean.class);
         assertXPathValue(context, "$nan = 1", Boolean.FALSE, Boolean.class);
-        assertXPathValue(context, "$nan != 1", Boolean.TRUE, Boolean.class);
+        assertXPathValue(context, "$nan != 1", Boolean.FALSE, Boolean.class);
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message