/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2018 The eXist Project
 * http://exist-db.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.exist.xquery.value;

import org.exist.xquery.XPathException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DoubleValueTest {

    @Test
    public void convertToInteger() throws XPathException {
        final double value = -2147483649d;
        final DoubleValue doubleValue = new DoubleValue(value);

        final IntegerValue integerValue = (IntegerValue)doubleValue.convertTo(Type.INTEGER);

        assertEquals(2147483647, integerValue.getInt());
        assertEquals(-2147483649l, integerValue.getLong());
    }

    @Test
    public void getInt() throws XPathException {
        final double value = -2147483649d;
        final DoubleValue doubleValue = new DoubleValue(value);

        assertEquals(2147483647, doubleValue.getInt());
    }

    @Test
    public void getLong() throws XPathException {
        final double value = -2147483649d;
        final DoubleValue doubleValue = new DoubleValue(value);

        assertEquals(-2147483649l, doubleValue.getLong());
    }

    @Test
    public void toJavaObject_int() throws XPathException {
        final double value = -2147483649d;
        final DoubleValue doubleValue = new DoubleValue(value);

        final int i = doubleValue.toJavaObject(int.class);

        assertEquals(2147483647, i);     //TODO(AR) is this correct?

    }

    @Test
    public void toJavaObject_long() throws XPathException {
        final double value = -2147483649d;
        final DoubleValue doubleValue = new DoubleValue(value);

        final long l = doubleValue.toJavaObject(long.class);

        assertEquals(-2147483649l, l);

    }
}

@@ -945,16 +945,60 @@
 .Test;%0A%0A
+import static com.ibm.icu.impl.Assert.fail;%0A
 import s
@@ -1034,16 +1034,16 @@
 Equals;%0A
-
 %0Apublic 
@@ -1918,32 +1918,855 @@
 Long());%0A    %7D%0A%0A
+    @Test(expected=XPathException.class)%0A    public void toJavaObject_int_lowerBound() throws XPathException %7B%0A        final double value = -2147483649d;  // NOTE: this is out of bounds for an XDM xs:int, so should generate an error%0A        final DoubleValue doubleValue = new DoubleValue(value);%0A%0A        doubleValue.toJavaObject(int.class);%0A%0A        fail(%22xs:double value is out of bounds for xs:int%22);%0A    %7D%0A%0A    @Test(expected=XPathException.class)%0A    public void toJavaObject_int_upperBound() throws XPathException %7B%0A        final double value = 2147483649d;  // NOTE: this is out of bounds for an XDM xs:int, so should generate an error%0A        final DoubleValue doubleValue = new DoubleValue(value);%0A%0A        doubleValue.toJavaObject(int.class);%0A%0A        fail(%22xs:double value is out of bounds for xs:int%22);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2849,33 +2849,33 @@
 lue = -214748364
-9
+8
 d;%0A        final
@@ -2999,32 +2999,33 @@
    assertEquals(
+-
 2147483647, i); 
@@ -3021,46 +3021,14 @@
 8364
-7
+8
 , i);
-     //TODO(AR) is this correct?
 %0A%0A  

Fix incorrect xs:double -> xs:int conversion test