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 Uint32Array.
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 Uint32ArrayTest extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts({"4294967251", "-45", "-1", "-1", "-1"})
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 Uint32Array([-45.3]);\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("4")
64      public void constant() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><head>\n"
67              + "<script>\n"
68              + LOG_TITLE_FUNCTION
69              + "function test() {\n"
70              + "  try {\n"
71              + "    log(Uint32Array.BYTES_PER_ELEMENT);\n"
72              + "  } catch(e) {logEx(e);}\n"
73              + "}\n"
74              + "</script></head><body onload='test()'>\n"
75              + "</body></html>";
76  
77          loadPageVerifyTitle2(html);
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      @Alerts({"undefined", "815", "undefined", "undefined"})
85      public void index() throws Exception {
86          final String html = DOCTYPE_HTML
87              + "<html><head>\n"
88              + "<script>\n"
89              + LOG_TITLE_FUNCTION
90              + "function test() {\n"
91              + "  var array = new Uint32Array([815]);\n"
92              + "  log(array[-1]);\n"
93              + "  log(array[0]);\n"
94              + "  log(array[1]);\n"
95              + "  log(array[21]);\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({"false", "true", "false", "false"})
108     public void in() throws Exception {
109         final String html = DOCTYPE_HTML
110             + "<html><head>\n"
111             + "<script>\n"
112             + LOG_TITLE_FUNCTION
113             + "function test() {\n"
114             + "  var array = new Uint32Array([815]);\n"
115             + "  log(-1 in array);\n"
116             + "  log(0 in array);\n"
117             + "  log(1 in array);\n"
118             + "  log(42 in array);\n"
119             + "}\n"
120             + "</script></head><body onload='test()'>\n"
121             + "</body></html>";
122 
123         loadPageVerifyTitle2(html);
124     }
125 
126     /**
127      * @throws Exception if the test fails
128      */
129     @Test
130     @Alerts({"undefined", "6", "0", "0", "0", "0", "0", "4", "undefined"})
131     public void undefinedValueInArray() throws Exception {
132         final String html = DOCTYPE_HTML
133             + "<html><head>\n"
134             + "<script>\n"
135             + LOG_TITLE_FUNCTION
136             + "function test() {\n"
137             + "  var array = [];\n"
138             + "  array[1] = null;\n"
139             + "  array[2] = Number.NaN;\n"
140             + "  array[3] = Number.POSITIVE_INFINITY;\n"
141             + "  array[4] = Number.NEGATIVE_INFINITY;\n"
142             + "  array[5] = 4;\n"
143             + "  log(array[0]);\n"
144 
145             + "  var nativeArray = new Uint32Array(array);\n"
146             + "  log(nativeArray.length);\n"
147             + "  log(nativeArray[0]);\n"
148             + "  log(nativeArray[1]);\n"
149             + "  log(nativeArray[2]);\n"
150             + "  log(nativeArray[3]);\n"
151             + "  log(nativeArray[4]);\n"
152             + "  log(nativeArray[5]);\n"
153             + "  log(nativeArray[6]);\n"
154             + "}\n"
155             + "</script></head><body onload='test()'>\n"
156             + "</body></html>";
157 
158         loadPageVerifyTitle2(html);
159     }
160 
161     /**
162      * @throws Exception if the test fails
163      */
164     @Test
165     @Alerts({"0", "1", "0", "17"})
166     public void specialValueInArray() throws Exception {
167         final String html = DOCTYPE_HTML
168             + "<html><head>\n"
169             + "<script>\n"
170             + LOG_TITLE_FUNCTION
171             + "function test() {\n"
172             + "  var array = [];\n"
173             + "  array[0] = NaN;\n"
174             + "  array[1] = true;\n"
175             + "  array[2] = false;\n"
176             + "  array[3] = '17';\n"
177             + "  var nativeArray = new Uint32Array(array);\n"
178             + "  log(nativeArray[0]);\n"
179             + "  log(nativeArray[1]);\n"
180             + "  log(nativeArray[2]);\n"
181             + "  log(nativeArray[3]);\n"
182             + "}\n"
183             + "</script></head><body onload='test()'>\n"
184             + "</body></html>";
185 
186         loadPageVerifyTitle2(html);
187     }
188 
189     /**
190      * @throws Exception if the test fails
191      */
192     @Test
193     @Alerts("0")
194     public void nullConstructor() throws Exception {
195         final String html = DOCTYPE_HTML
196             + "<html><head>\n"
197             + "<script>\n"
198             + LOG_TITLE_FUNCTION
199             + "function test() {\n"
200             + "  try {\n"
201             + "    var array = new Uint32Array(null);\n"
202             + "    log(array.length);\n"
203             + "  } catch(e) {logEx(e);}\n"
204             + "}\n"
205             + "</script></head><body onload='test()'>\n"
206             + "</body></html>";
207 
208         loadPageVerifyTitle2(html);
209     }
210 
211     /**
212      * @throws Exception if the test fails
213      */
214     @Test
215     @Alerts({"", "0", "1", "1,3", "1,3,4,7,11,0,123"})
216     public void asString() throws Exception {
217         final String html = DOCTYPE_HTML
218             + "<html><head>\n"
219             + "<script>\n"
220             + LOG_TITLE_FUNCTION
221             + "function test() {\n"
222             + "  var array = new Uint32Array(0);\n"
223             + "  log(array.toString());\n"
224 
225             + "  array = new Uint32Array(1);\n"
226             + "  log(array.toString());\n"
227 
228             + "  array = new Uint32Array([1]);\n"
229             + "  log(array.toString());\n"
230 
231             + "  array = new Uint32Array([1,3]);\n"
232             + "  log(array.toString());\n"
233 
234             + "  array = new Uint32Array([1,3,4,7,11,0,123]);\n"
235             + "  log(array.toString());\n"
236             + "}\n"
237             + "</script></head><body onload='test()'>\n"
238             + "</body></html>";
239 
240         loadPageVerifyTitle2(html);
241     }
242 
243     /**
244      * @throws Exception if the test fails
245      */
246     @Test
247     @Alerts("Uint32Array")
248     public void name() throws Exception {
249         final String html = DOCTYPE_HTML
250             + "<html><head>\n"
251             + "<script>\n"
252             + LOG_TITLE_FUNCTION
253             + "function test() {\n"
254             + "  log(Uint32Array.name);\n"
255             + "}\n"
256             + "</script></head><body onload='test()'>\n"
257             + "</body></html>";
258 
259         loadPageVerifyTitle2(html);
260     }
261 }