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 Uint16Array.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   * @author Michael Rimov
28   */
29  public class Uint16ArrayTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"64302", "46", "-5"})
36      public void arrayConstruction() 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 Uint16Array([-1234.11234]);\n"
44              + "    log(array[0]);\n"
45              + "    var array2 = new Int8Array(array.buffer);\n"
46              + "    for (var i = 0; i < array2.length; i++)\n"
47              + "      log(array2[i]);\n"
48              + "  } catch(e) {logEx(e);}\n"
49              + "}\n"
50              + "</script></head><body onload='test()'>\n"
51              + "</body></html>";
52  
53          loadPageVerifyTitle2(html);
54      }
55  
56      /**
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts({"undefined", "815", "undefined", "undefined"})
61      public void index() throws Exception {
62          final String html = DOCTYPE_HTML
63              + "<html><head>\n"
64              + "<script>\n"
65              + LOG_TITLE_FUNCTION
66              + "function test() {\n"
67              + "  var array = new Uint16Array([815]);\n"
68              + "  log(array[-1]);\n"
69              + "  log(array[0]);\n"
70              + "  log(array[1]);\n"
71              + "  log(array[21]);\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({"false", "true", "false", "false"})
84      public void in() throws Exception {
85          final String html = DOCTYPE_HTML
86              + "<html><head>\n"
87              + "<script>\n"
88              + LOG_TITLE_FUNCTION
89              + "function test() {\n"
90              + "  var array = new Uint16Array([815]);\n"
91              + "  log(-1 in array);\n"
92              + "  log(0 in array);\n"
93              + "  log(1 in array);\n"
94              + "  log(42 in array);\n"
95              + "}\n"
96              + "</script></head><body onload='test()'>\n"
97              + "</body></html>";
98  
99          loadPageVerifyTitle2(html);
100     }
101 
102     /**
103      * @throws Exception if the test fails
104      */
105     @Test
106     @Alerts({"undefined", "6", "0", "0", "0", "0", "0", "4", "undefined"})
107     public void undefinedValueInArray() throws Exception {
108         final String html = DOCTYPE_HTML
109             + "<html><head>\n"
110             + "<script>\n"
111             + LOG_TITLE_FUNCTION
112             + "function test() {\n"
113             + "  var array = [];\n"
114             + "  array[1] = null;\n"
115             + "  array[2] = Number.NaN;\n"
116             + "  array[3] = Number.POSITIVE_INFINITY;\n"
117             + "  array[4] = Number.NEGATIVE_INFINITY;\n"
118             + "  array[5] = 4;\n"
119             + "  log(array[0]);\n"
120 
121             + "  var nativeArray = new Uint16Array(array);\n"
122             + "  log(nativeArray.length);\n"
123             + "  log(nativeArray[0]);\n"
124             + "  log(nativeArray[1]);\n"
125             + "  log(nativeArray[2]);\n"
126             + "  log(nativeArray[3]);\n"
127             + "  log(nativeArray[4]);\n"
128             + "  log(nativeArray[5]);\n"
129             + "  log(nativeArray[6]);\n"
130             + "}\n"
131             + "</script></head><body onload='test()'>\n"
132             + "</body></html>";
133 
134         loadPageVerifyTitle2(html);
135     }
136 
137     /**
138      * @throws Exception if the test fails
139      */
140     @Test
141     @Alerts({"0", "1", "0", "17"})
142     public void specialValueInArray() throws Exception {
143         final String html = DOCTYPE_HTML
144             + "<html><head>\n"
145             + "<script>\n"
146             + LOG_TITLE_FUNCTION
147             + "function test() {\n"
148             + "  var array = [];\n"
149             + "  array[0] = NaN;\n"
150             + "  array[1] = true;\n"
151             + "  array[2] = false;\n"
152             + "  array[3] = '17';\n"
153             + "  var nativeArray = new Uint16Array(array);\n"
154             + "  log(nativeArray[0]);\n"
155             + "  log(nativeArray[1]);\n"
156             + "  log(nativeArray[2]);\n"
157             + "  log(nativeArray[3]);\n"
158             + "}\n"
159             + "</script></head><body onload='test()'>\n"
160             + "</body></html>";
161 
162         loadPageVerifyTitle2(html);
163     }
164 
165     /**
166      * @throws Exception if the test fails
167      */
168     @Test
169     @Alerts("0")
170     public void nullConstructor() throws Exception {
171         final String html = DOCTYPE_HTML
172             + "<html><head>\n"
173             + "<script>\n"
174             + LOG_TITLE_FUNCTION
175             + "function test() {\n"
176             + "  try {\n"
177             + "    var array = new Uint16Array(null);\n"
178             + "    log(array.length);\n"
179             + "  } catch(e) {logEx(e);}\n"
180             + "}\n"
181             + "</script></head><body onload='test()'>\n"
182             + "</body></html>";
183 
184         loadPageVerifyTitle2(html);
185     }
186 
187     /**
188      * @throws Exception if the test fails
189      */
190     @Test
191     @Alerts({"", "0", "1", "1,3", "1,3,4,7,11,0,123"})
192     public void asString() throws Exception {
193         final String html = DOCTYPE_HTML
194             + "<html><head>\n"
195             + "<script>\n"
196             + LOG_TITLE_FUNCTION
197             + "function test() {\n"
198             + "  var array = new Uint16Array(0);\n"
199             + "  log(array.toString());\n"
200 
201             + "  array = new Uint16Array(1);\n"
202             + "  log(array.toString());\n"
203 
204             + "  array = new Uint16Array([1]);\n"
205             + "  log(array.toString());\n"
206 
207             + "  array = new Uint16Array([1,3]);\n"
208             + "  log(array.toString());\n"
209 
210             + "  array = new Uint16Array([1,3,4,7,11,0,123]);\n"
211             + "  log(array.toString());\n"
212             + "}\n"
213             + "</script></head><body onload='test()'>\n"
214             + "</body></html>";
215 
216         loadPageVerifyTitle2(html);
217     }
218 
219     /**
220      * @throws Exception if the test fails
221      */
222     @Test
223     @Alerts("Uint16Array")
224     public void name() throws Exception {
225         final String html = DOCTYPE_HTML
226             + "<html><head>\n"
227             + "<script>\n"
228             + LOG_TITLE_FUNCTION
229             + "function test() {\n"
230             + "  log(Uint16Array.name);\n"
231             + "}\n"
232             + "</script></head><body onload='test()'>\n"
233             + "</body></html>";
234 
235         loadPageVerifyTitle2(html);
236     }
237 }