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 Float32Array.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   * @author Michael Rimov
28   */
29  public class Float32ArrayTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"63", "-76", "-106", "73", "79", "97", "60", "-53"})
36      public void bufferConstructor() 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 Float32Array([1234567.8901, -12345678.90123]);\n"
44              + "    var array2 = new Int8Array(array.buffer);\n"
45              + "    for (var i = 0; i < array2.length; i++)\n"
46              + "      log(array2[i]);\n"
47              + "  } catch(e) {logEx(e);}\n"
48              + "}\n"
49              + "</script></head><body onload='test()'>\n"
50              + "</body></html>";
51  
52          loadPageVerifyTitle2(html);
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      @Alerts({"undefined", "1234567.875", "undefined", "undefined"})
60      public void index() throws Exception {
61          final String html = DOCTYPE_HTML
62              + "<html><head>\n"
63              + "<script>\n"
64              + LOG_TITLE_FUNCTION
65              + "function test() {\n"
66              + "  var array = new Float32Array([1234567.8901]);\n"
67              + "  log(array[-1]);\n"
68              + "  log(array[0]);\n"
69              + "  log(array[1]);\n"
70              + "  log(array[21]);\n"
71              + "}\n"
72              + "</script></head><body onload='test()'>\n"
73              + "</body></html>";
74  
75          loadPageVerifyTitle2(html);
76      }
77  
78      /**
79       * @throws Exception if the test fails
80       */
81      @Test
82      @Alerts({"false", "true", "false", "false"})
83      public void in() throws Exception {
84          final String html = DOCTYPE_HTML
85              + "<html><head>\n"
86              + "<script>\n"
87              + LOG_TITLE_FUNCTION
88              + "function test() {\n"
89              + "  var array = new Float32Array([1234567.8901]);\n"
90              + "  log(-1 in array);\n"
91              + "  log(0 in array);\n"
92              + "  log(1 in array);\n"
93              + "  log(42 in array);\n"
94              + "}\n"
95              + "</script></head><body onload='test()'>\n"
96              + "</body></html>";
97  
98          loadPageVerifyTitle2(html);
99      }
100 
101     /**
102      * @throws Exception if the test fails
103      */
104     @Test
105     @Alerts({"undefined", "6", "NaN", "0", "NaN", "Infinity", "-Infinity", "4", "undefined"})
106     public void undefinedValueInArray() throws Exception {
107         final String html = DOCTYPE_HTML
108             + "<html><head>\n"
109             + "<script>\n"
110             + LOG_TITLE_FUNCTION
111             + "function test() {\n"
112             + "  var array = [];\n"
113             + "  array[1] = null;\n"
114             + "  array[2] = Number.NaN;\n"
115             + "  array[3] = Number.POSITIVE_INFINITY;\n"
116             + "  array[4] = Number.NEGATIVE_INFINITY;\n"
117             + "  array[5] = 4;\n"
118             + "  log(array[0]);\n"
119 
120             + "  var nativeArray = new Float32Array(array);\n"
121             + "  log(nativeArray.length);\n"
122             + "  log(nativeArray[0]);\n"
123             + "  log(nativeArray[1]);\n"
124             + "  log(nativeArray[2]);\n"
125             + "  log(nativeArray[3]);\n"
126             + "  log(nativeArray[4]);\n"
127             + "  log(nativeArray[5]);\n"
128             + "  log(nativeArray[6]);\n"
129             + "}\n"
130             + "</script></head><body onload='test()'>\n"
131             + "</body></html>";
132 
133         loadPageVerifyTitle2(html);
134     }
135 
136     /**
137      * @throws Exception if the test fails
138      */
139     @Test
140     @Alerts({"NaN", "1", "0", "17"})
141     public void specialValueInArray() throws Exception {
142         final String html = DOCTYPE_HTML
143             + "<html><head>\n"
144             + "<script>\n"
145             + LOG_TITLE_FUNCTION
146             + "function test() {\n"
147             + "  var array = [];\n"
148             + "  array[0] = NaN;\n"
149             + "  array[1] = true;\n"
150             + "  array[2] = false;\n"
151             + "  array[3] = '17';\n"
152             + "  var nativeArray = new Float32Array(array);\n"
153             + "  log(nativeArray[0]);\n"
154             + "  log(nativeArray[1]);\n"
155             + "  log(nativeArray[2]);\n"
156             + "  log(nativeArray[3]);\n"
157             + "}\n"
158             + "</script></head><body onload='test()'>\n"
159             + "</body></html>";
160 
161         loadPageVerifyTitle2(html);
162     }
163 
164     /**
165      * @throws Exception if the test fails
166      */
167     @Test
168     @Alerts("0")
169     public void nullConstructor() throws Exception {
170         final String html = DOCTYPE_HTML
171             + "<html><head>\n"
172             + "<script>\n"
173             + LOG_TITLE_FUNCTION
174             + "function test() {\n"
175             + "  try {\n"
176             + "    var array = new Float32Array(null);\n"
177             + "    log(array.length);\n"
178             + "  } catch(e) {logEx(e);}\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", "1", "1,3", "1,3,4,7,11,0,123"})
191     public void asString() throws Exception {
192         final String html = DOCTYPE_HTML
193             + "<html><head>\n"
194             + "<script>\n"
195             + LOG_TITLE_FUNCTION
196             + "function test() {\n"
197             + "  var array = new Float32Array(0);\n"
198             + "  log(array.toString());\n"
199 
200             + "  array = new Float32Array(1);\n"
201             + "  log(array.toString());\n"
202 
203             + "  array = new Float32Array([1]);\n"
204             + "  log(array.toString());\n"
205 
206             + "  array = new Float32Array([1,3]);\n"
207             + "  log(array.toString());\n"
208 
209             + "  array = new Float32Array([1,3,4,7,11,0,123]);\n"
210             + "  log(array.toString());\n"
211             + "}\n"
212             + "</script></head><body onload='test()'>\n"
213             + "</body></html>";
214 
215         loadPageVerifyTitle2(html);
216     }
217 
218     /**
219      * @throws Exception if the test fails
220      */
221     @Test
222     @Alerts("Float32Array")
223     public void name() throws Exception {
224         final String html = DOCTYPE_HTML
225             + "<html><head>\n"
226             + "<script>\n"
227             + LOG_TITLE_FUNCTION
228             + "function test() {\n"
229             + "  log(Float32Array.name);\n"
230             + "}\n"
231             + "</script></head><body onload='test()'>\n"
232             + "</body></html>";
233 
234         loadPageVerifyTitle2(html);
235     }
236 }