Skip to content
Snippets Groups Projects
Commit dab41646 authored by stepan's avatar stepan
Browse files

TypeConvert external: tests + few fixes

parent c3867b9a
No related branches found
No related tags found
No related merge requests found
......@@ -58,7 +58,7 @@ public abstract class TypeConvert extends RExternalBuiltinNode.Arg5 {
private static RIntVector readIntVector(RAbstractStringVector x, int firstPos, int firstVal, RAbstractStringVector naStrings) {
int[] data = new int[x.getLength()];
Arrays.fill(data, 0, firstPos, RRuntime.INT_NA);
boolean complete = RDataFactory.COMPLETE_VECTOR;
boolean complete = canBeComplete(firstPos);
data[firstPos] = firstVal;
for (int i = firstPos + 1; i < data.length; i++) {
String s = x.getDataAt(i);
......@@ -72,7 +72,7 @@ public abstract class TypeConvert extends RExternalBuiltinNode.Arg5 {
private static RDoubleVector readDoubleVector(RAbstractStringVector x, int firstPos, double firstVal, RAbstractStringVector naStrings) {
double[] data = new double[x.getLength()];
Arrays.fill(data, 0, firstPos, RRuntime.DOUBLE_NA);
boolean complete = RDataFactory.COMPLETE_VECTOR;
boolean complete = canBeComplete(firstPos);
data[firstPos] = firstVal;
for (int i = firstPos + 1; i < data.length; i++) {
String s = x.getDataAt(i);
......@@ -86,7 +86,7 @@ public abstract class TypeConvert extends RExternalBuiltinNode.Arg5 {
private static RLogicalVector readLogicalVector(RAbstractStringVector x, int firstPos, byte firstVal, RAbstractStringVector naStrings) {
byte[] data = new byte[x.getLength()];
Arrays.fill(data, 0, firstPos, RRuntime.LOGICAL_NA);
boolean complete = RDataFactory.COMPLETE_VECTOR;
boolean complete = canBeComplete(firstPos);
data[firstPos] = firstVal;
for (int i = firstPos + 1; i < data.length; i++) {
String s = x.getDataAt(i);
......@@ -97,6 +97,10 @@ public abstract class TypeConvert extends RExternalBuiltinNode.Arg5 {
return RDataFactory.createLogicalVector(data, complete);
}
private static boolean canBeComplete(int firstNonNAPos) {
return firstNonNAPos == 0 ? RDataFactory.COMPLETE_VECTOR : RDataFactory.INCOMPLETE_VECTOR;
}
@Specialization
protected Object typeConvert(RAbstractStringVector x, RAbstractStringVector naStrings, byte asIs, @SuppressWarnings("unused") Object dec, @SuppressWarnings("unused") Object numeral) {
if (x.getLength() == 0) {
......@@ -104,7 +108,7 @@ public abstract class TypeConvert extends RExternalBuiltinNode.Arg5 {
}
int i = 0;
while (isNA(x.getDataAt(i), naStrings) && i < x.getLength()) {
while (i < x.getLength() && isNA(x.getDataAt(i), naStrings)) {
i++;
}
......
......@@ -339,14 +339,20 @@ public class RRuntime {
@TruffleBoundary
public static int string2intNoCheck(String s, boolean exceptionOnFail) {
// FIXME use R rules
int result;
try {
return Integer.decode(s); // decode supports hex constants
result = Integer.decode(s); // decode supports hex constants
} catch (NumberFormatException e) {
if (exceptionOnFail) {
throw e;
}
return INT_NA;
}
if (result == INT_NA && exceptionOnFail) {
throw new NumberFormatException();
}
return INT_NA;
return result;
}
@TruffleBoundary
......
......@@ -111811,6 +111811,38 @@ Tracing f(100) on entry
[1] 100
NULL
 
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert('-2147483648')
[1] -2147483648
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert('42')
[1] 42
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert(c('42', '42.42'))
[1] 42.00 42.42
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert(c('NA', '42'))
[1] NA 42
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert(c('NA', '44.5'))
[1] NA 44.5
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert(c('NA', 'NA'))
[1] NA NA
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert(c('NA', 'TRUE'))
[1] NA TRUE
##com.oracle.truffle.r.test.library.utils.TestTypeConvert.basicTests
#type.convert(c(NA, '44.5'))
[1] NA 44.5
##com.oracle.truffle.r.test.library.utils.TestUtils.testHeadNTail
#{head(letters)}
[1] "a" "b" "c" "d" "e" "f"
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.test.library.utils;
import org.junit.Test;
import com.oracle.truffle.r.test.TestBase;
public class TestTypeConvert extends TestBase {
@Test
public void basicTests() {
assertEval("type.convert('42')");
assertEval("type.convert(c('42', '42.42'))"); // convert to double vector
assertEval("type.convert(c('NA', 'NA'))");
assertEval("type.convert(c('NA', 'TRUE'))");
assertEval("type.convert(c('NA', '42'))");
assertEval("type.convert(c('NA', '44.5'))");
assertEval("type.convert(c(NA, '44.5'))"); // string NA
// looks like integer, but is double (because it would be INT_NA)
assertEval("type.convert('-2147483648')");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment