OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // Dart test program for testing native float arrays. | |
6 | |
7 // VMOptions=--optimization_counter_threshold=10 --no-background_compilation | |
8 | |
9 // Library tag to be able to run in html test framework. | |
10 library FloatArrayTest; | |
11 | |
12 import "package:expect/expect.dart"; | |
13 import 'dart:typed_data'; | |
14 | |
15 void testCreateFloat32Array() { | |
16 Float32List floatArray; | |
17 | |
18 floatArray = new Float32List(0); | |
19 Expect.equals(0, floatArray.length); | |
20 | |
21 floatArray = new Float32List(10); | |
22 Expect.equals(10, floatArray.length); | |
23 for (int i = 0; i < 10; i++) { | |
24 Expect.equals(0.0, floatArray[i]); | |
25 } | |
26 } | |
27 | |
28 void testSetRange32() { | |
29 Float32List floatArray = new Float32List(3); | |
30 | |
31 List<double> list = [10.0, 11.0, 12.0]; | |
32 floatArray.setRange(0, 3, list); | |
33 for (int i = 0; i < 3; i++) { | |
34 Expect.equals(10 + i, floatArray[i]); | |
35 } | |
36 | |
37 floatArray[0] = 20.0; | |
38 floatArray[1] = 21.0; | |
39 floatArray[2] = 22.0; | |
40 list.setRange(0, 3, floatArray); | |
41 for (int i = 0; i < 3; i++) { | |
42 Expect.equals(20 + i, list[i]); | |
43 } | |
44 | |
45 // 4.0e40 is larger than the largest representable float. | |
46 floatArray.setRange(1, 3, const [8.0, 4.0e40]); | |
47 Expect.equals(20, floatArray[0]); | |
48 Expect.equals(8, floatArray[1]); | |
49 Expect.equals(double.INFINITY, floatArray[2]); | |
50 } | |
51 | |
52 void testIndexOutOfRange32() { | |
53 Float32List floatArray = new Float32List(3); | |
54 List<double> list = const [0.0, 1.0, 2.0, 3.0]; | |
55 | |
56 Expect.throws(() { | |
57 floatArray[5] = 2.0; | |
58 }); | |
59 Expect.throws(() { | |
60 floatArray.setRange(0, 4, list); | |
61 }); | |
62 | |
63 Expect.throws(() { | |
64 floatArray.setRange(3, 4, list); | |
65 }); | |
66 } | |
67 | |
68 void testIndexOf32() { | |
69 var list = new Float32List(10); | |
70 for (int i = 0; i < list.length; i++) { | |
71 list[i] = i + 10.0; | |
72 } | |
73 Expect.equals(0, list.indexOf(10)); | |
74 Expect.equals(5, list.indexOf(15)); | |
75 Expect.equals(9, list.indexOf(19)); | |
76 Expect.equals(-1, list.indexOf(20)); | |
77 | |
78 list = new Float32List(10); | |
79 for (int i = 0; i < list.length; i++) { | |
80 list[i] = i + 10.0; | |
81 } | |
82 Expect.equals(0, list.indexOf(10.0)); | |
83 Expect.equals(5, list.indexOf(15.0)); | |
84 Expect.equals(9, list.indexOf(19.0)); | |
85 Expect.equals(-1, list.indexOf(20.0)); | |
86 } | |
87 | |
88 void testBadValues32() { | |
89 var list = new Float32List(10); | |
90 list[0] = 2.0; | |
91 Expect.throws(() { | |
92 list[0] = 2; | |
93 }); | |
94 Expect.throws(() { | |
95 list[0] = "hello"; | |
96 }); | |
97 } | |
98 | |
99 void testCreateFloat64Array() { | |
100 Float64List floatArray; | |
101 | |
102 floatArray = new Float64List(0); | |
103 Expect.equals(0, floatArray.length); | |
104 | |
105 floatArray = new Float64List(10); | |
106 Expect.equals(10, floatArray.length); | |
107 for (int i = 0; i < 10; i++) { | |
108 Expect.equals(0.0, floatArray[i]); | |
109 } | |
110 } | |
111 | |
112 void testSetRange64() { | |
113 Float64List floatArray = new Float64List(3); | |
114 | |
115 List<double> list = [10.0, 11.0, 12.0]; | |
116 floatArray.setRange(0, 3, list); | |
117 for (int i = 0; i < 3; i++) { | |
118 Expect.equals(10 + i, floatArray[i]); | |
119 } | |
120 | |
121 floatArray[0] = 20.0; | |
122 floatArray[1] = 21.0; | |
123 floatArray[2] = 22.0; | |
124 list.setRange(0, 3, floatArray); | |
125 for (int i = 0; i < 3; i++) { | |
126 Expect.equals(20 + i, list[i]); | |
127 } | |
128 | |
129 // Unlike Float32Array we can properly represent 4.0e40 | |
130 floatArray.setRange(1, 3, const [8.0, 4.0e40]); | |
131 Expect.equals(20, floatArray[0]); | |
132 Expect.equals(8, floatArray[1]); | |
133 Expect.equals(4.0e40, floatArray[2]); | |
134 } | |
135 | |
136 void testIndexOutOfRange64() { | |
137 Float64List floatArray = new Float64List(3); | |
138 List<double> list = const [0.0, 1.0, 2.0, 3.0]; | |
139 | |
140 Expect.throws(() { | |
141 floatArray[5] = 2.0; | |
142 }); | |
143 Expect.throws(() { | |
144 floatArray.setRange(0, 4, list); | |
145 }); | |
146 | |
147 Expect.throws(() { | |
148 floatArray.setRange(3, 4, list); | |
149 }); | |
150 } | |
151 | |
152 void testIndexOf64() { | |
153 var list = new Float64List(10); | |
154 for (int i = 0; i < list.length; i++) { | |
155 list[i] = i + 10.0; | |
156 } | |
157 Expect.equals(0, list.indexOf(10)); | |
158 Expect.equals(5, list.indexOf(15)); | |
159 Expect.equals(9, list.indexOf(19)); | |
160 Expect.equals(-1, list.indexOf(20)); | |
161 | |
162 list = new Float64List(10); | |
163 for (int i = 0; i < list.length; i++) { | |
164 list[i] = i + 10.0; | |
165 } | |
166 Expect.equals(0, list.indexOf(10.0)); | |
167 Expect.equals(5, list.indexOf(15.0)); | |
168 Expect.equals(9, list.indexOf(19.0)); | |
169 Expect.equals(-1, list.indexOf(20.0)); | |
170 } | |
171 | |
172 void testBadValues64() { | |
173 var list = new Float64List(10); | |
174 list[0] = 2.0; | |
175 Expect.throws(() { | |
176 list[0] = 2; | |
177 }); | |
178 Expect.throws(() { | |
179 list[0] = "hello"; | |
180 }); | |
181 } | |
182 | |
183 storeIt32(Float32List a, int index, value) { | |
184 a[index] = value; | |
185 } | |
186 | |
187 storeIt64(Float64List a, int index, value) { | |
188 a[index] = value; | |
189 } | |
190 | |
191 testPolymorphicLoad(var list) { | |
192 return list[0]; | |
193 } | |
194 | |
195 main() { | |
196 var a32 = new Float32List(5); | |
197 for (int i = 0; i < 20; i++) { | |
198 testCreateFloat32Array(); | |
199 testSetRange32(); | |
200 testIndexOutOfRange32(); | |
201 testIndexOf32(); | |
202 storeIt32(a32, 1, 2.0); | |
203 testPolymorphicLoad(a32); | |
204 } | |
205 var a64 = new Float64List(5); | |
206 for (int i = 0; i < 20; i++) { | |
207 testCreateFloat64Array(); | |
208 testSetRange64(); | |
209 testIndexOutOfRange64(); | |
210 testIndexOf64(); | |
211 storeIt64(a64, 1, 2.0); | |
212 testPolymorphicLoad(a64); | |
213 } | |
214 var f32x4 = new Float32x4List(5); | |
215 for (int i = 0; i < 20; i++) { | |
216 testPolymorphicLoad(f32x4); | |
217 } | |
218 | |
219 // These two take a long time in checked mode. | |
220 testBadValues32(); | |
221 testBadValues64(); | |
222 // Check optimized (inlined) version of []= | |
223 Expect.throws(() { | |
224 storeIt32(a32, 1, 2); | |
225 }); | |
226 Expect.throws(() { | |
227 storeIt64(a64, 1, 2); | |
228 }); | |
229 } | |
OLD | NEW |