Debian Codegraph @

rhino/testsrc/tests/lc3/JavaArray/ToArray-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 = 'ToArray-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 = "JavaArray to 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 // Passing a JavaArray to a method that expects a java.lang.String should
40 // call the unwrapped array's toString method and return the result as a
41 // new java.lang.String.
42
43 // pass a byte array to a method that expects a string
44
45 a[i++] = new TestObject (
46 "dt.setStringObject(java.lang.String(new java.lang.String(\"hello\").getBytes()))",
47 "dt.PUB_STRING +''",
48 "dt.getStringObject() +''",
49 "typeof dt.getStringObject()",
50 "hello",
51 "object" );
52
53 // pass a char array to a method that expects a string
54
55 a[i++] = new TestObject (
56 "dt.setStringObject(java.lang.String(new java.lang.String(\"goodbye\").toCharArray()))",
57 "dt.PUB_STRING +''",
58 "dt.getStringObject() +''",
59 "typeof dt.getStringObject()",
60 "goodbye",
61 "object" );
62
63 a[i++] = new TestObject (
64 "dt.setStringObject(java.lang.String(new java.lang.String(\"goodbye\").toCharArray()))",
65 "dt.PUB_STRING +''",
66 "dt.getStringObject() +''",
67 "typeof dt.getStringObject()",
68 "goodbye",
69 "object" );
70
71 // Vector.copyInto expects an object array
72
73 a[i++] = new TestObject(
74 "var vector = new java.util.Vector(); "+
75 "vector.addElement( \"a\" ); vector.addElement( \"b\" ); "+
76 "vector.copyInto( DT.PUB_STATIC_ARRAY_OBJECT )",
77 "DT.PUB_STATIC_ARRAY_OBJECT[0] +''",
78 "DT.staticGetObjectArray()[0] +''",
79 "typeof DT.staticGetObjectArray()[0]",
80 "a",
81 "object" );
82
83 a[i++] = new TestObject(
84 "var vector = new java.util.Vector(); "+
85 "vector.addElement( \"a\" ); vector.addElement( 3 ); "+
86 "vector.copyInto( DT.PUB_STATIC_ARRAY_OBJECT )",
87 "DT.PUB_STATIC_ARRAY_OBJECT[1] +''",
88 "DT.staticGetObjectArray()[1] +''",
89 "DT.staticGetObjectArray()[1].getClass().getName() +''",
90 "3.0",
91 "java.lang.Double" );
92
93 // byte array
94
95 var random = Math.random() +"";
96
97 for ( counter = 0; counter < random.length; counter ++ ) {
98 a[i++] = new TestObject (
99 "dt.setByteArray(java.lang.String(\""+random+"\").getBytes());",
100 "dt.PUB_STATIC_ARRAY_BYTE["+counter+"]",
101 "dt.getByteArray()["+counter+"]",
102 "typeof dt.getByteArray()["+counter+"]",
103 random[counter].charCodeAt(0),
104 "number" );
105 }
106
107 // char array
108
109 random = Math.random() +"";
110
111 for ( counter = 0; counter < random.length; counter ++ ) {
112 a[i++] = new TestObject (
113 "dt.setCharArray(java.lang.String(\""+random+"\").toCharArray());",
114 "dt.PUB_STATIC_ARRAY_CHAR["+counter+"]",
115 "dt.getCharArray()["+counter+"]",
116 "typeof dt.getCharArray()["+counter+"]",
117 random[counter].charCodeAt(0),
118 "number" );
119 }
120
121 // int array
122
123 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_INT.length))) +"";
124 for ( counter = 0; counter < random.length; counter++ ) {
125 dt.PUB_ARRAY_INT[counter] = random[counter];
126 }
127 for ( counter = 0; counter < random.length; counter ++ ) {
128 a[i++] = new TestObject (
129 "dt.setIntArray(dt.PUB_ARRAY_INT)",
130 "DT.PUB_STATIC_ARRAY_INT["+counter+"]",
131 "dt.getIntArray()["+counter+"]",
132 "typeof dt.getIntArray()["+counter+"]",
133 Number(random[counter]),
134 "number" );
135 }
136
137 // short array
138
139 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_SHORT.length))) +"";
140 for ( counter = 0; counter < random.length; counter++ ) {
141 dt.PUB_ARRAY_SHORT[counter] = random[counter];
142 }
143 for ( counter = 0; counter < random.length; counter ++ ) {
144 a[i++] = new TestObject (
145 "dt.setShortArray(dt.PUB_ARRAY_SHORT)",
146 "DT.PUB_STATIC_ARRAY_SHORT["+counter+"]",
147 "dt.getShortArray()["+counter+"]",
148 "typeof dt.getShortArray()["+counter+"]",
149 Number(random[counter]),
150 "number" );
151 }
152
153 // long array
154
155 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_LONG.length))) +"";
156 for ( counter = 0; counter < random.length; counter++ ) {
157 dt.PUB_ARRAY_LONG[counter] = random[counter];
158 }
159 for ( counter = 0; counter < random.length; counter ++ ) {
160 a[i++] = new TestObject (
161 "dt.setLongArray(dt.PUB_ARRAY_LONG)",
162 "DT.PUB_STATIC_ARRAY_LONG["+counter+"]",
163 "dt.getLongArray()["+counter+"]",
164 "typeof dt.getLongArray()["+counter+"]",
165 Number(random[counter]),
166 "number" );
167 }
168
169
170 // double array
171
172 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_DOUBLE.length))) +"";
173 for ( counter = 0; counter < random.length; counter++ ) {
174 dt.PUB_ARRAY_DOUBLE[counter] = random[counter];
175 }
176 for ( counter = 0; counter < random.length; counter ++ ) {
177 a[i++] = new TestObject (
178 "dt.setDoubleArray(dt.PUB_ARRAY_DOUBLE)",
179 "DT.PUB_STATIC_ARRAY_DOUBLE["+counter+"]",
180 "dt.getDoubleArray()["+counter+"]",
181 "typeof dt.getDoubleArray()["+counter+"]",
182 Number(random[counter]),
183 "number" );
184 }
185
186 // float array
187
188 random = ( Math.round(Math.random() * Math.pow(10,dt.PUB_ARRAY_FLOAT.length))) +"";
189 for ( counter = 0; counter < random.length; counter++ ) {
190 dt.PUB_ARRAY_FLOAT[counter] = random[counter];
191 }
192 for ( counter = 0; counter < random.length; counter ++ ) {
193 a[i++] = new TestObject (
194 "dt.setFloatArray(dt.PUB_ARRAY_FLOAT)",
195 "DT.PUB_STATIC_ARRAY_FLOAT["+counter+"]",
196 "dt.getFloatArray()["+counter+"]",
197 "typeof dt.getFloatArray()["+counter+"]",
198 Number(random[counter]),
199 "number" );
200 }
201
202 for ( i = 0; i < a.length; i++ ) {
203 new TestCase(
204 a[i].description +"; "+ a[i].javaFieldName,
205 a[i].jsValue,
206 a[i].javaFieldValue );
207
208 new TestCase(
209 a[i].description +"; " + a[i].javaMethodName,
210 a[i].jsValue,
211 a[i].javaMethodValue );
212
213 new TestCase(
214 a[i].javaTypeName,
215 a[i].jsType,
216 a[i].javaTypeValue );
217
218 }
219
220 test();
221
222 function TestObject( description, javaField, javaMethod, javaType,
223 jsValue, jsType )
224 {
225 eval (description );
226
227 this.description = description;
228 this.javaFieldName = javaField;
229 this.javaFieldValue = eval( javaField );
230 this.javaMethodName = javaMethod;
231 this.javaMethodValue = eval( javaMethod );
232 this.javaTypeName = javaType,
233 this.javaTypeValue = eval(javaType);
234
235 this.jsValue = jsValue;
236 this.jsType = jsType;
237 }