View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.arrays;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for Int16Array.
25   *
26   * @author Ahmed Ashour
27   * @author Frank Danek
28   * @author Ronald Brill
29   * @author Michael Rimov
30   */
31  @RunWith(BrowserRunner.class)
32  public class Int16ArrayTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts({"[object ArrayBuffer]", "10", "0"})
39      public void buffer() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html><head>\n"
42              + "<script>\n"
43              + LOG_TITLE_FUNCTION
44              + "function test() {\n"
45              + "  try {\n"
46              + "    var array = new Int16Array(5);\n"
47              + "    log(array.buffer);\n"
48              + "    log(array.byteLength);\n"
49              + "    log(array.byteOffset);\n"
50              + "  } catch(e) {logEx(e);}\n"
51              + "}\n"
52              + "</script></head><body onload='test()'>\n"
53              + "</body></html>";
54  
55          loadPageVerifyTitle2(html);
56      }
57  
58      /**
59       * @throws Exception if the test fails
60       */
61      @Test
62      @Alerts({"17", "-45", "2"})
63      public void arrayConstructor() throws Exception {
64          final String html = DOCTYPE_HTML
65              + "<html><head>\n"
66              + "<script>\n"
67              + LOG_TITLE_FUNCTION
68              + "function test() {\n"
69              + "  try {\n"
70              + "    var array = new Int16Array([17, -45.3]);\n"
71              + "    log(array[0]);\n"
72              + "    log(array[1]);\n"
73              + "    log(array.length);\n"
74              + "  } catch(e) {logEx(e);}\n"
75              + "}\n"
76              + "</script></head><body onload='test()'>\n"
77              + "</body></html>";
78  
79          loadPageVerifyTitle2(html);
80      }
81  
82      /**
83       * @throws Exception if the test fails
84       */
85      @Test
86      @Alerts("2")
87      public void constant() throws Exception {
88          final String html = DOCTYPE_HTML
89              + "<html><head>\n"
90              + "<script>\n"
91              + LOG_TITLE_FUNCTION
92              + "function test() {\n"
93              + "  try {\n"
94              + "    log(Int16Array.BYTES_PER_ELEMENT);\n"
95              + "  } catch(e) {logEx(e);}\n"
96              + "}\n"
97              + "</script></head><body onload='test()'>\n"
98              + "</body></html>";
99  
100         loadPageVerifyTitle2(html);
101     }
102 
103     /**
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts({"17", "0", "-45", "-1"})
108     public void bufferConstructor() throws Exception {
109         final String html = DOCTYPE_HTML
110             + "<html><head>\n"
111             + "<script>\n"
112             + LOG_TITLE_FUNCTION
113             + "function test() {\n"
114             + "  try {\n"
115             + "    var array = new Int16Array([17, -45.3]);\n"
116             + "    var array2 = new Int8Array(array.buffer);\n"
117             + "    for (var i = 0; i < array2.length; i++)\n"
118             + "      log(array2[i]);\n"
119             + "  } catch(e) {logEx(e);}\n"
120             + "}\n"
121             + "</script></head><body onload='test()'>\n"
122             + "</body></html>";
123 
124         loadPageVerifyTitle2(html);
125     }
126 
127     /**
128      * @throws Exception if the test fails
129      */
130     @Test
131     @Alerts("undefined")
132     public void outOfRange() throws Exception {
133         final String html = DOCTYPE_HTML
134             + "<html><head>\n"
135             + "<script>\n"
136             + LOG_TITLE_FUNCTION
137             + "function test() {\n"
138             + "  try {\n"
139             + "    var array = new Int16Array(2);\n"
140             + "    array[0] = 11;\n"
141             + "    array[1] = 12;\n"
142             + "    array[2] = 13;\n"
143             + "    log(array[2]);\n"
144             + "  } catch(e) {logEx(e);}\n"
145             + "}\n"
146             + "</script></head><body onload='test()'>\n"
147             + "</body></html>";
148 
149         loadPageVerifyTitle2(html);
150     }
151 
152     /**
153      * @throws Exception if the test fails
154      */
155     @Test
156     @Alerts({"undefined", "815", "undefined", "undefined"})
157     public void index() throws Exception {
158         final String html = DOCTYPE_HTML
159             + "<html><head>\n"
160             + "<script>\n"
161             + LOG_TITLE_FUNCTION
162             + "function test() {\n"
163             + "  var array = new Int16Array([815]);\n"
164             + "  log(array[-1]);\n"
165             + "  log(array[0]);\n"
166             + "  log(array[1]);\n"
167             + "  log(array[21]);\n"
168             + "}\n"
169             + "</script></head><body onload='test()'>\n"
170             + "</body></html>";
171 
172         loadPageVerifyTitle2(html);
173     }
174 
175     /**
176      * @throws Exception if the test fails
177      */
178     @Test
179     @Alerts({"false", "true", "false", "false"})
180     public void in() throws Exception {
181         final String html = DOCTYPE_HTML
182             + "<html><head>\n"
183             + "<script>\n"
184             + LOG_TITLE_FUNCTION
185             + "function test() {\n"
186             + "  var array = new Int16Array([815]);\n"
187             + "  log(-1 in array);\n"
188             + "  log(0 in array);\n"
189             + "  log(1 in array);\n"
190             + "  log(42 in array);\n"
191             + "}\n"
192             + "</script></head><body onload='test()'>\n"
193             + "</body></html>";
194 
195         loadPageVerifyTitle2(html);
196     }
197 
198     /**
199      * @throws Exception if the test fails
200      */
201     @Test
202     @Alerts({"undefined", "6", "0", "0", "0", "0", "0", "4", "undefined"})
203     public void undefinedValueInArray() throws Exception {
204         final String html = DOCTYPE_HTML
205             + "<html><head>\n"
206             + "<script>\n"
207             + LOG_TITLE_FUNCTION
208             + "function test() {\n"
209             + "  var array = [];\n"
210             + "  array[1] = null;\n"
211             + "  array[2] = Number.NaN;\n"
212             + "  array[3] = Number.POSITIVE_INFINITY;\n"
213             + "  array[4] = Number.NEGATIVE_INFINITY;\n"
214             + "  array[5] = 4;\n"
215             + "  log(array[0]);\n"
216 
217             + "  var nativeArray = new Int16Array(array);\n"
218             + "  log(nativeArray.length);\n"
219             + "  log(nativeArray[0]);\n"
220             + "  log(nativeArray[1]);\n"
221             + "  log(nativeArray[2]);\n"
222             + "  log(nativeArray[3]);\n"
223             + "  log(nativeArray[4]);\n"
224             + "  log(nativeArray[5]);\n"
225             + "  log(nativeArray[6]);\n"
226             + "}\n"
227             + "</script></head><body onload='test()'>\n"
228             + "</body></html>";
229 
230         loadPageVerifyTitle2(html);
231     }
232 
233     /**
234      * @throws Exception if the test fails
235      */
236     @Test
237     @Alerts({"0", "1", "0", "17"})
238     public void specialValueInArray() throws Exception {
239         final String html = DOCTYPE_HTML
240             + "<html><head>\n"
241             + "<script>\n"
242             + LOG_TITLE_FUNCTION
243             + "function test() {\n"
244             + "  var array = [];\n"
245             + "  array[0] = NaN;\n"
246             + "  array[1] = true;\n"
247             + "  array[2] = false;\n"
248             + "  array[3] = '17';\n"
249             + "  var nativeArray = new Int16Array(array);\n"
250             + "  log(nativeArray[0]);\n"
251             + "  log(nativeArray[1]);\n"
252             + "  log(nativeArray[2]);\n"
253             + "  log(nativeArray[3]);\n"
254             + "}\n"
255             + "</script></head><body onload='test()'>\n"
256             + "</body></html>";
257 
258         loadPageVerifyTitle2(html);
259     }
260 
261     /**
262      * @throws Exception if the test fails
263      */
264     @Test
265     @Alerts("0")
266     public void nullConstructor() throws Exception {
267         final String html = DOCTYPE_HTML
268             + "<html><head>\n"
269             + "<script>\n"
270             + LOG_TITLE_FUNCTION
271             + "function test() {\n"
272             + "  try {\n"
273             + "    var array = new Int16Array(null);\n"
274             + "    log(array.length);\n"
275             + "  } catch(e) {logEx(e);}\n"
276             + "}\n"
277             + "</script></head><body onload='test()'>\n"
278             + "</body></html>";
279 
280         loadPageVerifyTitle2(html);
281     }
282 
283     /**
284      * @throws Exception if the test fails
285      */
286     @Test
287     @Alerts({"", "0", "1", "1,3", "1,3,4,7,11,0,123"})
288     public void asString() throws Exception {
289         final String html = DOCTYPE_HTML
290             + "<html><head>\n"
291             + "<script>\n"
292             + LOG_TITLE_FUNCTION
293             + "function test() {\n"
294             + "  var array = new Int16Array(0);\n"
295             + "  log(array.toString());\n"
296 
297             + "  array = new Int16Array(1);\n"
298             + "  log(array.toString());\n"
299 
300             + "  array = new Int16Array([1]);\n"
301             + "  log(array.toString());\n"
302 
303             + "  array = new Int16Array([1,3]);\n"
304             + "  log(array.toString());\n"
305 
306             + "  array = new Int16Array([1,3,4,7,11,0,123]);\n"
307             + "  log(array.toString());\n"
308             + "}\n"
309             + "</script></head><body onload='test()'>\n"
310             + "</body></html>";
311 
312         loadPageVerifyTitle2(html);
313     }
314 
315     /**
316      * @throws Exception if the test fails
317      */
318     @Test
319     @Alerts("Int16Array")
320     public void name() throws Exception {
321         final String html = DOCTYPE_HTML
322             + "<html><head>\n"
323             + "<script>\n"
324             + LOG_TITLE_FUNCTION
325             + "function test() {\n"
326             + "  log(Int16Array.name);\n"
327             + "}\n"
328             + "</script></head><body onload='test()'>\n"
329             + "</body></html>";
330 
331         loadPageVerifyTitle2(html);
332     }
333 }