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