Debian Codegraph @

rhino/testsrc/tests/lc2/Arrays/array-003.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'array-003.js';
7
8 /**
9 File Name: array-003.js
10 Description:
11
12 JavaArray elements should be enumerable using a for/in loop.
13
14 @version 1.00
15 */
16 var SECTION = "LiveConnect";
17 var VERSION = "1_3";
18 var TITLE = "Java Array to JavaScript JavaArray object";
19
20 startTest();
21 writeHeaderToLog( SECTION + " "+ TITLE);
22
23 // In all test cases, the expected type is "object, and the expected
24 // class is "Number"
25
26 var E_TYPE = "object";
27 var E_CLASS = "[object JavaArray]";
28
29 // Create arrays of actual results (java_array) and expected results
30 // (test_array).
31
32 var java_array = new Array();
33 var test_array = new Array();
34
35 var i = 0;
36
37 // byte[]
38
39 var byte_array = ( new java.lang.String("hello") ).getBytes();
40
41 java_array[i] = new JavaValue( byte_array );
42 test_array[i] = new TestValue( "( new java.lang.String('hello') ).getBytes()",
43 ["h".charCodeAt(0),
44 "e".charCodeAt(0),
45 "l".charCodeAt(0),
46 "l".charCodeAt(0),
47 "o".charCodeAt(0) ],
48 "[B"
49 );
50 i++;
51
52
53 // char[]
54 var char_array = ( new java.lang.String("rhino") ).toCharArray();
55
56 java_array[i] = new JavaValue( char_array );
57 test_array[i] = new TestValue( "( new java.lang.String('rhino') ).toCharArray()",
58 [ "r".charCodeAt(0),
59 "h".charCodeAt(0),
60 "i".charCodeAt(0),
61 "n".charCodeAt(0),
62 "o".charCodeAt(0) ],
63 "[C" );
64 i++;
65
66 for ( i = 0; i < java_array.length; i++ ) {
67 CompareValues( java_array[i], test_array[i] );
68 }
69
70 test();
71
72 function CompareValues( javaval, testval ) {
73 // Check value
74
75 var p;
76 var e = 0;
77
78 for ( p in javaval.value ) {
79 new TestCase( SECTION,
80 "("+ testval.description +")["+p+"]",
81 testval.value[p],
82 javaval.value[p] );
83 e++;
84
85 }
86
87 /* Number of elements enumerated should be same as number of elements in
88 * the array
89 */
90
91 new TestCase( SECTION,
92 "number of elements enumerated:",
93 testval.length,
94 e );
95
96
97 // Check type
98
99 new TestCase( SECTION,
100 "typeof (" + testval.description +")",
101 testval.type,
102 javaval.type );
103
104 // Check class.
105 new TestCase(SECTION,
106 "The Java Class of ( "+ testval.description +" )",
107 testval.lcclass,
108 javaval.lcclass );
109
110 }
111 function JavaValue( value ) {
112 this.value = value;
113 this.type = typeof value;
114 this.classname = this.value.toString();
115
116 jlo_class = java.lang.Class.forName("java.lang.Object")
117 jlo_getClass_method = jlo_class.getMethod("getClass", null)
118 this.lcclass = jlo_getClass_method.invoke(value, null );
119
120 return this;
121 }
122 function TestValue( description, value, lcclass ) {
123 this.lcclass = java.lang.Class.forName( lcclass );
124 this.description = description;
125 this.length = value.length;
126 this.value = value;
127 this.type = E_TYPE;
128 this.classname = E_CLASS;
129 return this;
130 }