Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: tests/language/type_cast_vm_test.dart

Issue 10659005: Implement type cast in the VM. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« runtime/vm/flow_graph_builder.cc ('K') | « runtime/vm/token.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/language/type_cast_vm_test.dart
===================================================================
--- tests/language/type_cast_vm_test.dart (revision 0)
+++ tests/language/type_cast_vm_test.dart (revision 0)
@@ -0,0 +1,158 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+//
+// Dart test program testing type casts.
+
+class TypeTest {
+ static test() {
+ int result = 0;
+ try {
+ var i = "hello" as int; // Throws a CastException
+ } catch (TypeError error) {
+ result = 1;
+ Expect.isTrue(error is CastException);
+ Expect.equals("int", error.dstType);
+ Expect.equals("OneByteString", error.srcType);
+ Expect.equals("type cast", error.dstName);
+ int pos = error.url.lastIndexOf("/", error.url.length);
+ if (pos == -1) {
+ pos = error.url.lastIndexOf("\\", error.url.length);
+ }
+ String subs = error.url.substring(pos + 1, error.url.length);
+ Expect.equals("type_cast_vm_test.dart", subs);
+ Expect.equals(11, error.line);
+ Expect.equals(23, error.column);
+ }
+ return result;
+ }
+
+ static testSideEffect() {
+ int result = 0;
+ int index() {
+ result++;
+ return 0;
+ }
+ try {
+ var a = new List<int>(1) as List<int>;
+ a[0] = 0;
+ a[index()]++; // Type check succeeds, but does not create side effects.
+ assert(a[0] == 1);
+ } catch (TypeError error) {
+ result = 100;
+ }
+ return result;
+ }
+
+ static testArgument() {
+ int result = 0;
+ int f(int i) {
+ return i;
+ }
+ try {
+ int i = f("hello" as int); // Throws a CastException
+ } catch (TypeError error) {
+ result = 1;
+ Expect.isTrue(error is CastException);
+ Expect.equals("int", error.dstType);
+ Expect.equals("OneByteString", error.srcType);
+ Expect.equals("type cast", error.dstName);
+ int pos = error.url.lastIndexOf("/", error.url.length);
+ if (pos == -1) {
+ pos = error.url.lastIndexOf("\\", error.url.length);
+ }
+ String subs = error.url.substring(pos + 1, error.url.length);
+ Expect.equals("type_cast_vm_test.dart", subs);
+ Expect.equals(53, error.line);
+ Expect.equals(25, error.column);
+ }
+ return result;
+ }
+
+ static testReturn() {
+ int result = 0;
+ int f(String s) {
+ return s as int; // Throws a CastException
+ }
+ try {
+ int i = f("hello");
+ } catch (TypeError error) {
+ result = 1;
+ Expect.isTrue(error is CastException);
+ Expect.equals("int", error.dstType);
+ Expect.equals("OneByteString", error.srcType);
+ Expect.equals("type cast", error.dstName);
+ int pos = error.url.lastIndexOf("/", error.url.length);
+ if (pos == -1) {
+ pos = error.url.lastIndexOf("\\", error.url.length);
+ }
+ String subs = error.url.substring(pos + 1, error.url.length);
+ Expect.equals("type_cast_vm_test.dart", subs);
+ Expect.equals(75, error.line);
+ Expect.equals(16, error.column);
+ }
+ return result;
+ }
+
+ static var field = "hello";
+ static testField() {
+ int result = 0;
+ Expect.equals(5, (field as String).length);
+ try {
+ field as int; // Throws a CastException
+ } catch (TypeError error) {
+ result = 1;
+ Expect.equals("int", error.dstType);
+ Expect.equals("OneByteString", error.srcType);
+ Expect.equals("type cast", error.dstName);
+ int pos = error.url.lastIndexOf("/", error.url.length);
+ if (pos == -1) {
+ pos = error.url.lastIndexOf("\\", error.url.length);
+ }
+ String subs = error.url.substring(pos + 1, error.url.length);
+ Expect.equals("type_cast_vm_test.dart", subs);
+ Expect.equals(102, error.line);
+ Expect.equals(13, error.column);
+ }
+ return result;
+ }
+
+ static testAnyFunction() {
+ int result = 0;
+ Function anyFunction;
+ f() { };
+ anyFunction = f as Function; // No error.
+ anyFunction = null as Function; // No error.
+ try {
+ var i = f as int; // Throws a TypeError if type checks are enabled.
+ } catch (TypeError error) {
+ result = 1;
+ Expect.equals("int", error.dstType);
+ Expect.equals("() => Dynamic", error.srcType);
+ Expect.equals("type cast", error.dstName);
+ int pos = error.url.lastIndexOf("/", error.url.length);
+ if (pos == -1) {
+ pos = error.url.lastIndexOf("\\", error.url.length);
+ }
+ String subs = error.url.substring(pos + 1, error.url.length);
+ Expect.equals("type_cast_vm_test.dart", subs);
+ Expect.equals(127, error.line);
+ Expect.equals(17, error.column);
+ }
+ return result;
+ }
+
+ static testMain() {
+ Expect.equals(1, test());
+ Expect.equals(1, testSideEffect());
+ Expect.equals(1, testArgument());
+ Expect.equals(1, testReturn());
+ Expect.equals(1, testField());
+ Expect.equals(1, testAnyFunction());
+ }
+}
+
+
+main() {
+ TypeTest.testMain();
+}
« runtime/vm/flow_graph_builder.cc ('K') | « runtime/vm/token.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698