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