Rewrite codegen tests which check time in deterministic way

Delete one useless test

 #KT-7621 Fixed
This commit is contained in:
Alexander Udalov
2015-04-29 15:27:21 +03:00
parent 90cdb19324
commit c860f54461
4 changed files with 15 additions and 30 deletions
@@ -17,9 +17,7 @@
package org.jetbrains.kotlin.codegen;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.SystemInfo;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -111,14 +109,6 @@ public class CodegenTestUtil {
return null;
}
public static void assertIsCurrentTime(long returnValue) {
long currentTime = System.currentTimeMillis();
long diff = Math.abs(returnValue - currentTime);
long toleratedDifference = SystemInfo.isWindows ? 15 : 1;
assertTrue("Difference with current time: " + diff + " (this test is a bad one: it may fail even if the generated code is correct)",
diff <= toleratedDifference);
}
@NotNull
public static File compileJava(@NotNull String filename, @NotNull String... additionalClasspath) {
return compileJava(Collections.singletonList(filename), additionalClasspath);
@@ -26,8 +26,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.assertIsCurrentTime;
public class PackageGenTest extends CodegenTestCase {
@Override
@@ -60,8 +58,13 @@ public class PackageGenTest extends CodegenTestCase {
public void testCurrentTime() throws Exception {
loadText("fun f() : Long { return System.currentTimeMillis(); }");
Method main = generateFunction();
long returnValue = (Long) main.invoke(null);
assertIsCurrentTime(returnValue);
long actual = (Long) main.invoke(null);
long expected = System.currentTimeMillis();
long diff = Math.abs(actual - expected);
assertTrue(
"Difference with current time: " + diff + " (this test is a bad one: it may fail even if the generated code is correct)",
diff <= 100
);
}
public void testIdentityHashCode() throws Exception {
@@ -158,13 +158,11 @@ public class PrimitiveTypesTest extends CodegenTestCase {
}
public void testCastOnStack() throws Exception {
loadText("fun foo(): Double = System.currentTimeMillis().toDouble()");
loadText("fun foo(l: Long): Double = l.toDouble()");
Class<?> mainClass = generatePackagePartClass();
Method main = mainClass.getDeclaredMethod("foo");
double currentTimeMillis = (double) System.currentTimeMillis();
double result = (Double) main.invoke(null);
double delta = Math.abs(currentTimeMillis - result);
assertTrue(delta <= 1.0);
Method main = mainClass.getDeclaredMethod("foo", long.class);
double result = (Double) main.invoke(null, 42L);
assertTrue(Math.abs(42L - result) <= 1e-9);
}
public void testNeg() throws Exception {
@@ -23,7 +23,8 @@ import org.jetbrains.org.objectweb.asm.Opcodes;
import java.lang.reflect.*;
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.*;
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.findDeclaredMethodByName;
import static org.jetbrains.kotlin.codegen.CodegenTestUtil.findDeclaredMethodByNameOrNull;
public class PropertyGenTest extends CodegenTestCase {
@Override
@@ -90,9 +91,9 @@ public class PropertyGenTest extends CodegenTestCase {
}
public void testFieldGetter() throws Exception {
loadText("val now: Long get() = System.currentTimeMillis(); fun foo() = now");
loadText("val now: Long get() = 42L; fun foo() = now");
Method method = generateFunction("foo");
assertIsCurrentTime((Long) method.invoke(null));
assertEquals(Long.valueOf(42), method.invoke(null));
}
public void testFieldSetter() throws Exception {
@@ -131,13 +132,6 @@ public class PropertyGenTest extends CodegenTestCase {
assertEquals(610, getFoo.invoke(instance));
}
public void testInitializersForTopLevelProperties() throws Exception {
loadText("val x = System.currentTimeMillis()");
Method method = generateFunction("getX");
method.setAccessible(true);
assertIsCurrentTime((Long) method.invoke(null));
}
public void testPropertyReceiverOnStack() throws Exception {
loadFile();
Class<?> aClass = generateClass("Evaluator");