Commit 34a1cf73 authored by Evan W. Patton's avatar Evan W. Patton Committed by Jeffrey Schiller

Correctly convert bignums to strings in YailList (#1546)

Change-Id: Ia3b29e30091e88e1db0fb5ec02c6b0d270c17f15
parent 9c6edf75
// -*- mode: java; c-basic-offset: 2; -*- // -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved // Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved // Copyright 2011-2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0 // Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
...@@ -10,6 +10,7 @@ import com.google.appinventor.components.runtime.errors.YailRuntimeError; ...@@ -10,6 +10,7 @@ import com.google.appinventor.components.runtime.errors.YailRuntimeError;
import gnu.lists.LList; import gnu.lists.LList;
import gnu.lists.Pair; import gnu.lists.Pair;
import gnu.math.IntNum;
import org.json.JSONException; import org.json.JSONException;
...@@ -119,7 +120,11 @@ public class YailList extends Pair { ...@@ -119,7 +120,11 @@ public class YailList extends Pair {
* @return the string * @return the string
*/ */
public static String YailListElementToString(Object element) { public static String YailListElementToString(Object element) {
if (Number.class.isInstance(element)) { if (element instanceof IntNum) {
return ((IntNum) element).toString(10);
} else if (element instanceof Long) {
return Long.toString((Long) element);
} else if (Number.class.isInstance(element)) {
return YailNumberToString.format(((Number) element).doubleValue()); return YailNumberToString.format(((Number) element).doubleValue());
} else { } else {
return String.valueOf(element); return String.valueOf(element);
......
...@@ -58,7 +58,7 @@ public final class YailNumberToString { ...@@ -58,7 +58,7 @@ public final class YailNumberToString {
public static String format(double number) { public static String format(double number) {
// We will print integer values without a decimal point. // We will print integer values without a decimal point.
if (number == Math.rint(number)) { if (number == Math.rint(number)) {
return String.valueOf((int) number); return String.valueOf((long) number);
} else { } else {
double mag = Math.abs(number); double mag = Math.abs(number);
if (mag < BIGBOUND && mag > SMALLBOUND) { if (mag < BIGBOUND && mag > SMALLBOUND) {
......
// -*- mode: java; c-basic-offset: 2; -*- // -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved // Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved // Copyright 2011-2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0 // Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0 // http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.runtime.util; package com.google.appinventor.components.runtime.util;
import gnu.lists.FString; import gnu.lists.FString;
import gnu.math.IntNum;
import junit.framework.TestCase; import junit.framework.TestCase;
...@@ -203,4 +204,12 @@ public class YailListTest extends TestCase { ...@@ -203,4 +204,12 @@ public class YailListTest extends TestCase {
// this is the intended behavior // this is the intended behavior
} }
} }
public void testBigNumsInStringArray() {
YailList list = YailList.makeList(new Object[] { IntNum.make(Long.MAX_VALUE), (Long) Long.MAX_VALUE });
String[] strings = list.toStringArray();
assertEquals(2, strings.length);
assertEquals(Long.toString(Long.MAX_VALUE), strings[0]);
assertEquals(Long.toString(Long.MAX_VALUE), strings[1]);
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment