Debian Codegraph @

rhino/testsrc/tests/lc3/JSNumber/ToShort-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 = 'ToShort-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 startTest();
33
34 var dt = new DT();
35
36 var a = new Array();
37 var i = 0;
38
39 // passing a JS number to a method that expects a int:
40 // round JS number to integral value using round-to-negative-infinity mode
41 // numbers with a magnitude too large to be represented in the target integral
42 // type result in a runtime error.
43 // Nan converts to 0.
44
45 // Special cases: 0, -0, Infinity, -Infinity, and NaN
46
47 a[i++] = new TestObject(
48 "dt.setShort( 0 )",
49 "dt.PUB_SHORT",
50 "dt.getShort()",
51 "typeof dt.getShort()",
52 0,
53 "number" );
54
55 // only doubles and floats know about -0
56
57 a[i++] = new TestObject(
58 "dt.setShort( -0 )",
59 "Infinity / dt.PUB_SHORT",
60 "Infinity / dt.getShort()",
61 "typeof dt.getShort()",
62 Infinity,
63 "number" );
64
65 a[i++] = new TestObject(
66 "dt.setShort( java.lang.Short.MAX_VALUE )",
67 "dt.PUB_SHORT",
68 "dt.getShort()",
69 "typeof dt.getShort()",
70 java.lang.Short.MAX_VALUE,
71 "number" );
72
73 a[i++] = new TestObject(
74 "dt.setShort( java.lang.Short.MIN_VALUE )",
75 "dt.PUB_SHORT",
76 "dt.getShort()",
77 "typeof dt.getShort()",
78 java.lang.Short.MIN_VALUE,
79 "number" );
80
81 a[i++] = new TestObject(
82 "dt.setShort( -java.lang.Short.MAX_VALUE )",
83 "dt.PUB_SHORT",
84 "dt.getShort()",
85 "typeof dt.getShort()",
86 -java.lang.Short.MAX_VALUE,
87 "number" );
88
89 a[i++] = new TestObject(
90 "dt.setShort(1e-2000)",
91 "dt.PUB_SHORT",
92 "dt.getShort()",
93 "typeof dt.getShort()",
94 0,
95 "number" );
96
97 a[i++] = new TestObject(
98 "dt.setShort(-1e-2000)",
99 "dt.PUB_SHORT",
100 "dt.getShort()",
101 "typeof dt.getShort()",
102 0,
103 "number" );
104
105 for ( i = 0; i < a.length; i++ ) {
106 new TestCase(
107 a[i].description +"; "+ a[i].javaFieldName,
108 a[i].jsValue,
109 a[i].javaFieldValue );
110
111 new TestCase(
112 a[i].description +"; " + a[i].javaMethodName,
113 a[i].jsValue,
114 a[i].javaMethodValue );
115
116 new TestCase(
117 a[i].javaTypeName,
118 a[i].jsType,
119 a[i].javaTypeValue );
120 }
121
122 test();
123
124 function TestObject( description, javaField, javaMethod, javaType,
125 jsValue, jsType )
126 {
127 eval (description );
128
129 this.description = description;
130 this.javaFieldName = javaField;
131 this.javaFieldValue = eval( javaField );
132 this.javaMethodName = javaMethod;
133 this.javaMethodValue = eval( javaMethod );
134 this.javaTypeName = javaType,
135 this.javaTypeValue = eval( javaType );
136
137 this.jsValue = jsValue;
138 this.jsType = jsType;
139 }