Debian Codegraph @

rhino/testsrc/tests/lc3/JSNumber/ToInt-001.js

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8 -*-
2 * Copyright (C) 1997, 1998 Netscape Communications Corporation,
3 * All Rights Reserved.
4 */
5 /* This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8
9 gTestfile = 'ToInt-001.js';
10
11 /**
12 * JavaScript to Java type conversion.
13 *
14 * This test passes JavaScript number values to several Java methods
15 * that expect arguments of various types, and verifies that the value is
16 * converted to the correct value and type.
17 *
18 * This tests instance methods, and not static methods.
19 *
20 * Running these tests successfully requires you to have
21 * com.netscape.javascript.qa.liveconnect.DataTypeClass on your classpath.
22 *
23 * Specification: Method Overloading Proposal for Liveconnect 3.0
24 *
25 * @author: [email protected]
26 *
27 */
28 var SECTION = "number conversion to int";
29 var VERSION = "1_4";
30 var TITLE = "LiveConnect 3.0 JavaScript to Java Data Type Conversion " +
31 SECTION;
32 var BUGNUMBER="335589";
33
34 startTest();
35
36 var dt = new DT();
37
38 var a = new Array();
39 var i = 0;
40
41 // passing a JS number to a method that expects a int:
42 // round JS number to integral value using round-to-negative-infinity mode
43 // numbers with a magnitude too large to be represented in the target integral
44 // type result in a runtime error.
45 // Nan converts to 0.
46
47 // Special cases: 0, -0, Infinity, -Infinity, and NaN
48
49 a[i++] = new TestObject(
50 "dt.setInteger( 0 )",
51 "dt.PUB_INT",
52 "dt.getInteger()",
53 "typeof dt.getInteger()",
54 0,
55 "number" );
56
57 // only doubles and floats know about -0
58
59 a[i++] = new TestObject(
60 "dt.setInteger( -0 )",
61 "Infinity / dt.PUB_INT",
62 "Infinity / dt.getInteger()",
63 "typeof dt.getInteger()",
64 Infinity,
65 "number" );
66
67 a[i++] = new TestObject(
68 "dt.setInteger( java.lang.Integer.MAX_VALUE )",
69 "dt.PUB_INT",
70 "dt.getInteger()",
71 "typeof dt.getInteger()",
72 java.lang.Integer.MAX_VALUE,
73 "number" );
74
75 a[i++] = new TestObject(
76 "dt.setInteger( java.lang.Integer.MIN_VALUE )",
77 "dt.PUB_INT",
78 "dt.getInteger()",
79 "typeof dt.getInteger()",
80 java.lang.Integer.MIN_VALUE,
81 "number" );
82
83 a[i++] = new TestObject(
84 "dt.setInteger( -java.lang.Integer.MAX_VALUE )",
85 "dt.PUB_INT",
86 "dt.getInteger()",
87 "typeof dt.getInteger()",
88 -java.lang.Integer.MAX_VALUE,
89 "number" );
90
91 a[i++] = new TestObject(
92 "dt.setInteger(1e-2000)",
93 "dt.PUB_INT",
94 "dt.getInteger()",
95 "typeof dt.getInteger()",
96 0,
97 "number" );
98
99 a[i++] = new TestObject(
100 "dt.setInteger(-1e-2000)",
101 "dt.PUB_INT",
102 "dt.getInteger()",
103 "typeof dt.getInteger()",
104 0,
105 "number" );
106
107 for ( i = 0; i < a.length; i++ ) {
108 new TestCase(
109 a[i].description +"; "+ a[i].javaFieldName,
110 a[i].jsValue,
111 a[i].javaFieldValue );
112
113 new TestCase(
114 a[i].description +"; " + a[i].javaMethodName,
115 a[i].jsValue,
116 a[i].javaMethodValue );
117
118 new TestCase(
119 a[i].javaTypeName,
120 a[i].jsType,
121 a[i].javaTypeValue );
122 }
123
124 test();
125
126 function TestObject( description, javaField, javaMethod, javaType,
127 jsValue, jsType )
128 {
129 eval (description );
130
131 this.description = description;
132 this.javaFieldName = javaField;
133 this.javaFieldValue = eval( javaField );
134 this.javaMethodName = javaMethod;
135 this.javaMethodValue = eval( javaMethod );
136 this.javaTypeName = javaType,
137 this.javaTypeValue = eval( javaType );
138
139 this.jsValue = jsValue;
140 this.jsType = jsType;
141 }