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