Fixes class methods errors. Adds string tests

This commit is contained in:
konsoletyper 2013-12-03 22:29:50 +04:00
parent 9722a6e863
commit d3063e7811
8 changed files with 75 additions and 24 deletions

View File

@ -12,14 +12,14 @@ import org.teavm.model.MethodReference;
public class ClassNativeGenerator implements Generator {
@Override
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) {
switch (methodRef.getClassName()) {
switch (methodRef.getName()) {
case "isInstance":
generateIsInstance(context, writer);
break;
case "isAssignable":
generateIsAssignableFrom(context, writer);
break;
case "getComponentType":
case "getComponentType0":
generateGetComponentType(context, writer);
break;
}

View File

@ -33,6 +33,11 @@ class TClassTests {
assertEquals(Object.class, Object[].class.getComponentType());
}
@Test
public void arrayOfArraysComponentTypeDetected() {
assertEquals(Object[].class, Object[][].class.getComponentType());
}
@Test
public void nonArrayComponentTypeIsNull() {
assertNull(Object.class.getComponentType());

View File

@ -0,0 +1,24 @@
package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
class TStringTests {
@Test
public void charsExtracted() {
String str = "123";
assertEquals('1', str.charAt(0));
assertEquals('2', str.charAt(1));
assertEquals('3', str.charAt(2));
}
@Test
public void lengthComputed() {
String str = "123";
assertEquals(3, str.length());
}
}

View File

@ -29,4 +29,14 @@ class TSystemTests {
public void failsToCopyArraysWithIncompatibleElements() {
TSystem.arraycopy(TObject.wrap(new TObject[1]), 0, TObject.wrap(new int[1]), 0, 1);
}
@Test(expected = NullPointerException.class)
public void failsToCopyFromNullSource() {
TSystem.arraycopy(null, 0, TObject.wrap(new int[1]), 0, 1);
}
@Test(expected = NullPointerException.class)
public void failsToCopyToNullTarget() {
TSystem.arraycopy(TObject.wrap(new TObject[1]), 0, null, 0, 1);
}
}

View File

@ -34,6 +34,18 @@ public class Assert {
}
}
public static void assertEquals(long expected, long actual) {
if (expected != actual) {
fail();
}
}
public static void assertNotEquals(long expected, long actual) {
if (expected == actual) {
fail();
}
}
public static void assertNotNull(Object object) {
if (object == null) {
fail();

View File

@ -32,7 +32,7 @@ public class ClasslibTestGenerator {
private static List<MethodReference> testMethods = new ArrayList<>();
private static Map<String, List<MethodReference>> groupedMethods = new HashMap<>();
private static String[] testClasses = { "java.lang.ObjectTests", "java.lang.SystemTests",
"java.lang.StringBuilderTests", "java.lang.ClassTests" };
"java.lang.StringBuilderTests", "java.lang.ClassTests", "java.lang.StringTests" };
public static void main(String[] args) throws IOException {
out = System.out;

View File

@ -170,7 +170,7 @@ public class DependencyChecker {
if (method != null) {
return methodCache.map(new MethodReference(cls.getName(), methodRef.getDescriptor()));
}
cls = classSource.getClassHolder(cls.getParent());
cls = cls.getParent() != null ? classSource.getClassHolder(cls.getParent()) : null;
}
throw new RuntimeException("Method not found: " + methodRef);
}

View File

@ -218,17 +218,17 @@ Long = function(lo, hi) {
this.lo = lo | 0;
this.hi = hi | 0;
}
Long.ZERO = new Long(0, 0);
Long.fromInt = function(val) {
Long_ZERO = new Long(0, 0);
Long_fromInt = function(val) {
return new Long(val, 0);
}
Long.fromNumber = function(val) {
Long_fromNumber = function(val) {
return new Long(val | 0, (val / 0x100000000) | 0);
}
Long.toNumber = function(val) {
Long_toNumber = function(val) {
return val.lo + 0x100000000 * val.hi;
}
Long.add = function(a, b) {
Long_add = function(a, b) {
var a_lolo = a.lo & 0xFFFF;
var a_lohi = a.lo >>> 16;
var a_hilo = a.hi & 0xFFFF;
@ -245,7 +245,7 @@ Long.add = function(a, b) {
return new Long((lolo & 0xFFFF) | ((lohi & 0xFFFF) << 16),
(hilo & 0xFFFF) | ((hihi & 0xFFFF) << 16));
}
Long.inc = function(a) {
Long_inc = function(a) {
var lo = (a.lo + 1) | 0;
var hi = a.hi;
if (lo === 0) {
@ -253,7 +253,7 @@ Long.inc = function(a) {
}
return new Long(lo, hi);
}
Long.dec = function(a) {
Long_dec = function(a) {
var lo = (a.lo - 1) | 0;
var hi = a.hi;
if (lo === -1) {
@ -261,10 +261,10 @@ Long.dec = function(a) {
}
return new Long(lo, hi);
}
Long.neg = function(a) {
Long_neg = function(a) {
return Long.inc(new Long(a.lo ^ 0xFFFFFFFF, a.hi ^ 0xFFFFFFFF));
}
Long.sub = function(a, b) {
Long_sub = function(a, b) {
var a_lolo = a.lo & 0xFFFF;
var a_lohi = a.lo >>> 16;
var a_hilo = a.hi & 0xFFFF;
@ -281,17 +281,17 @@ Long.sub = function(a, b) {
return new Long((lolo & 0xFFFF) | ((lohi & 0xFFFF) << 16),
(hilo & 0xFFFF) | ((hihi & 0xFFFF) << 16));
}
Long.compare = function(a, b) {
Long_compare = function(a, b) {
var r = a.hi - a.hi;
if (r != 0) {
return r;
}
return a.lo - b.lo;
}
Long.isNegative = function(a) {
Long_isNegative = function(a) {
return a.hi < 0;
}
Long.mul = function(a, b) {
Long_mul = function(a, b) {
var a_lolo = a.lo & 0xFFFF;
var a_lohi = a.lo >>> 16;
var a_hilo = a.hi & 0xFFFF;
@ -309,38 +309,38 @@ Long.mul = function(a, b) {
return new Long((lolo & 0xFFFF) | ((lohi & 0xFFFF) << 16),
(hilo & 0xFFFF) | ((hihi & 0xFFFF) << 16));
}
Long.div = function(a, b) {
Long_div = function(a, b) {
var result = (a.hi * 0x100000000 + a.lo) / (b.hi * 0x100000000 + b.lo);
return new Long(result | 0, (result / 0x100000000) | 0);
}
Long.rem = function(a, b) {
Long_rem = function(a, b) {
var result = (a.hi * 0x100000000 + a.lo) % (b.hi * 0x100000000 + b.lo);
return new Long(result | 0, (result / 0x100000000) | 0);
}
Long.and = function(a, b) {
Long_and = function(a, b) {
return new Long(a.lo & b.lo, a.hi & b.hi);
}
Long.or = function(a, b) {
Long_or = function(a, b) {
return new Long(a.lo | b.lo, a.hi | b.hi);
}
Long.xor = function(a, b) {
Long_xor = function(a, b) {
return new Long(a.lo ^ b.lo, a.hi ^ b.hi);
}
Long.shl = function(a, b) {
Long_shl = function(a, b) {
if (b < 32) {
return new Long(a.lo << b, (a.lo >>> (32 - b)) | (a.hi << b));
} else {
return new Long(0, a.lo << (b - 32));
}
}
Long.shr = function(a, b) {
Long_shr = function(a, b) {
if (b < 32) {
return new Long((a.lo >>> b) | (a.hi << (32 - b)), a.hi >> b);
} else {
return new Long((a.hi >> (b - 32)), -1);
}
}
Long.shru = function(a, b) {
Long_shru = function(a, b) {
if (b < 32) {
return new Long((a.lo >>> b) | (a.hi << (32 - b)), a.hi >>> b);
} else {