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