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