Debian Codegraph @

rhino/testsrc/tests/lc3/JSObject/ToDouble-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 = 'ToDouble-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 = "JavaScript Object to java.lang.String";
29 var VERSION = "1_4";
30 var TITLE = "LiveConnect 3.0 JavaScript to Java Data Type Conversion " +
31 SECTION;
32 startTest();
33
34 var dt = new DT();
35
36 var a = new Array();
37 var i = 0;
38
39 // 3.3.6.4 Other JavaScript Objects
40 // Passing a JavaScript object to a java method that that expects a double
41 // should:
42 // 1. Apply the ToPrimitive operator (ECMA 9.3) to the JavaScript object
43 // with hint Number
44 // 2. Convert Result(1) to Java numeric type using the rules in 3.3.3.
45
46 var bool = new Boolean(true);
47
48 a[i++] = new TestObject(
49 "dt.setDouble( bool )",
50 "dt.PUB_DOUBLE",
51 "dt.getDouble()",
52 "typeof dt.getDouble()",
53 1,
54 "number");
55
56 bool = new Boolean(false);
57
58 a[i++] = new TestObject(
59 "dt.setDouble( bool )",
60 "dt.PUB_DOUBLE",
61 "dt.getDouble()",
62 "typeof dt.getDouble()",
63 0,
64 "number");
65
66 var number = new Number(0);
67
68 a[i++] = new TestObject(
69 "dt.setDouble( number )",
70 "dt.PUB_DOUBLE",
71 "dt.getDouble()",
72 "typeof dt.getDouble()",
73 0,
74 "number");
75
76 nan = new Number(NaN);
77
78 a[i++] = new TestObject(
79 "dt.setDouble( nan )",
80 "dt.PUB_DOUBLE",
81 "dt.getDouble()",
82 "typeof dt.getDouble()",
83 NaN,
84 "number");
85
86 infinity = new Number(Infinity);
87
88 a[i++] = new TestObject(
89 "dt.setDouble( infinity )",
90 "dt.PUB_DOUBLE",
91 "dt.getDouble()",
92 "typeof dt.getDouble()",
93 Infinity,
94 "number");
95
96 var neg_infinity = new Number(-Infinity);
97
98 a[i++] = new TestObject(
99 "dt.setDouble( new Number(neg_infinity))",
100 "dt.PUB_DOUBLE",
101 "dt.getDouble()",
102 "typeof dt.getDouble()",
103 -Infinity,
104 "number");
105
106 var string = new String("JavaScript String Value");
107
108 a[i++] = new TestObject(
109 "dt.setDouble(string)",
110 "dt.PUB_DOUBLE",
111 "dt.getDouble()",
112 "typeof dt.getDouble()",
113 NaN,
114 "number");
115
116 var string = new String("1234567890");
117
118 a[i++] = new TestObject(
119 "dt.setDouble(string)",
120 "dt.PUB_DOUBLE",
121 "dt.getDouble()",
122 "typeof dt.getDouble()",
123 1234567890,
124 "number");
125
126
127 var string = new String("1234567890.123456789");
128
129 a[i++] = new TestObject(
130 "dt.setDouble(string)",
131 "dt.PUB_DOUBLE",
132 "dt.getDouble()",
133 "typeof dt.getDouble()",
134 1234567890.123456789,
135 "number");
136
137 var myobject = new MyObject( "9876543210" );
138
139 a[i++] = new TestObject(
140 "dt.setDouble( myobject )",
141 "dt.PUB_DOUBLE",
142 "dt.getDouble()",
143 "typeof dt.getDouble()",
144 9876543210,
145 "number");
146
147 myobject = new MyOtherObject( "5551212");
148
149 a[i++] = new TestObject(
150 "dt.setDouble( myobject )",
151 "dt.PUB_DOUBLE",
152 "dt.getDouble()",
153 "typeof dt.getDouble()",
154 5551212,
155 "number");
156
157 myobject = new AnotherObject( "6060842");
158
159 a[i++] = new TestObject(
160 "dt.setDouble( myobject )",
161 "dt.PUB_DOUBLE",
162 "dt.getDouble()",
163 "typeof dt.getDouble()",
164 6060842,
165 "number");
166
167 var object = new Object();
168
169 a[i++] = new TestObject(
170 "dt.setDouble( object )",
171 "dt.PUB_DOUBLE",
172 "dt.getDouble()",
173 "typeof dt.getDouble()",
174 NaN,
175 "number");
176
177 a[i++] = new TestObject(
178 "dt.setDouble( MyObject )",
179 "dt.PUB_DOUBLE",
180 "dt.getDouble()",
181 "typeof dt.getDouble()",
182 NaN,
183 "number");
184
185 a[i++] = new TestObject(
186 "dt.setDouble( this )",
187 "dt.PUB_DOUBLE",
188 "dt.getDouble()",
189 "typeof dt.getDouble()",
190 NaN,
191 "number");
192
193 a[i++] = new TestObject(
194 "dt.setDouble( Math )",
195 "dt.PUB_DOUBLE",
196 "dt.getDouble()",
197 "typeof dt.getDouble()",
198 NaN,
199 "number");
200
201 a[i++] = new TestObject(
202 "dt.setDouble( Function )",
203 "dt.PUB_DOUBLE",
204 "dt.getDouble()",
205 "typeof dt.getDouble()",
206 NaN,
207 "number");
208
209 for ( i = 0; i < a.length; i++ ) {
210 new TestCase(
211 a[i].description +"; "+ a[i].javaFieldName,
212 a[i].jsValue,
213 a[i].javaFieldValue );
214
215 new TestCase(
216 a[i].description +"; " + a[i].javaMethodName,
217 a[i].jsValue,
218 a[i].javaMethodValue );
219
220 new TestCase(
221 a[i].javaTypeName,
222 a[i].jsType,
223 a[i].javaTypeValue );
224 }
225
226 test();
227
228 function MyObject( stringValue ) {
229 this.stringValue = String(stringValue);
230 this.toString = new Function( "return this.stringValue" );
231 }
232
233 function MyOtherObject( value ) {
234 this.toString = null;
235 this.value = value;
236 this.valueOf = new Function( "return this.value" );
237 }
238
239 function AnotherObject( value ) {
240 this.toString = new Function( "return new Number(666)" );
241 this.value = value;
242 this.valueOf = new Function( "return this.value" );
243 }
244
245 function TestObject( description, javaField, javaMethod, javaType,
246 jsValue, jsType )
247 {
248 eval (description );
249
250 this.description = description;
251 this.javaFieldName = javaField;
252 this.javaFieldValue = eval( javaField );
253 this.javaMethodName = javaMethod;
254 this.javaMethodValue = eval( javaMethod );
255 this.javaTypeName = javaType,
256 this.javaTypeValue = eval( javaType );
257
258 this.jsValue = jsValue;
259 this.jsType = jsType;
260 }