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