1 /*
2  * Copyright (C) 2011-2012 The Android Open Source Project
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  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.renderscript.cts;
18 
19 import android.renderscript.Allocation;
20 
21 import android.renderscript.Byte2;
22 import android.renderscript.Byte3;
23 import android.renderscript.Byte4;
24 
25 import android.renderscript.Double2;
26 import android.renderscript.Double3;
27 import android.renderscript.Double4;
28 
29 import android.renderscript.Element;
30 
31 import android.renderscript.Float2;
32 import android.renderscript.Float3;
33 import android.renderscript.Float4;
34 
35 import android.renderscript.Int2;
36 import android.renderscript.Int3;
37 import android.renderscript.Int4;
38 
39 import android.renderscript.Long2;
40 import android.renderscript.Long3;
41 import android.renderscript.Long4;
42 
43 import android.renderscript.RSRuntimeException;
44 
45 import android.renderscript.Short2;
46 import android.renderscript.Short3;
47 import android.renderscript.Short4;
48 
49 import android.renderscript.Type;
50 
51 public class ComputeTest extends RSBaseCompute {
52 
testJavaVectorTypes()53     public void testJavaVectorTypes() {
54         Byte2 b2 = new Byte2();
55         b2.x = 1;
56         b2.y = 2;
57         b2 = new Byte2((byte)1, (byte)2);
58         assertTrue(b2.x == 1);
59         assertTrue(b2.y == 2);
60         Byte3 b3 = new Byte3();
61         b3.x = 1;
62         b3.y = 2;
63         b3.z = 2;
64         b3 = new Byte3((byte)1, (byte)2, (byte)3);
65         assertTrue(b3.x == 1);
66         assertTrue(b3.y == 2);
67         assertTrue(b3.z == 3);
68         Byte4 b4 = new Byte4();
69         b4.x = 1;
70         b4.y = 2;
71         b4.x = 3;
72         b4.w = 4;
73         b4 = new Byte4((byte)1, (byte)2, (byte)3, (byte)4);
74         assertTrue(b4.x == 1);
75         assertTrue(b4.y == 2);
76         assertTrue(b4.z == 3);
77         assertTrue(b4.w == 4);
78 
79         Double2 d2 = new Double2();
80         d2.x = 1.0;
81         d2.y = 2.0;
82         d2 = new Double2(1.0, 2.0);
83         assertTrue(d2.x == 1.0);
84         assertTrue(d2.y == 2.0);
85         Double3 d3 = new Double3();
86         d3.x = 1.0;
87         d3.y = 2.0;
88         d3.z = 3.0;
89         d3 = new Double3(1.0, 2.0, 3.0);
90         assertTrue(d3.x == 1.0);
91         assertTrue(d3.y == 2.0);
92         assertTrue(d3.z == 3.0);
93         Double4 d4 = new Double4();
94         d4.x = 1.0;
95         d4.y = 2.0;
96         d4.x = 3.0;
97         d4.w = 4.0;
98         d4 = new Double4(1.0, 2.0, 3.0, 4.0);
99         assertTrue(d4.x == 1.0);
100         assertTrue(d4.y == 2.0);
101         assertTrue(d4.z == 3.0);
102         assertTrue(d4.w == 4.0);
103 
104         Float2 f2 = new Float2();
105         f2.x = 1.0f;
106         f2.y = 2.0f;
107         f2 = new Float2(1.0f, 2.0f);
108         assertTrue(f2.x == 1.0f);
109         assertTrue(f2.y == 2.0f);
110         Float3 f3 = new Float3();
111         f3.x = 1.0f;
112         f3.y = 2.0f;
113         f3.z = 3.0f;
114         f3 = new Float3(1.0f, 2.0f, 3.0f);
115         assertTrue(f3.x == 1.0f);
116         assertTrue(f3.y == 2.0f);
117         assertTrue(f3.z == 3.0f);
118         Float4 f4 = new Float4();
119         f4.x = 1.0f;
120         f4.y = 2.0f;
121         f4.x = 3.0f;
122         f4.w = 4.0f;
123         f4 = new Float4(1.0f, 2.0f, 3.0f, 4.0f);
124         assertTrue(f4.x == 1.0f);
125         assertTrue(f4.y == 2.0f);
126         assertTrue(f4.z == 3.0f);
127         assertTrue(f4.w == 4.0f);
128 
129         Int2 i2 = new Int2();
130         i2.x = 1;
131         i2.y = 2;
132         i2 = new Int2(1, 2);
133         assertTrue(i2.x == 1);
134         assertTrue(i2.y == 2);
135         Int3 i3 = new Int3();
136         i3.x = 1;
137         i3.y = 2;
138         i3.z = 3;
139         i3 = new Int3(1, 2, 3);
140         assertTrue(i3.x == 1);
141         assertTrue(i3.y == 2);
142         assertTrue(i3.z == 3);
143         Int4 i4 = new Int4();
144         i4.x = 1;
145         i4.y = 2;
146         i4.x = 3;
147         i4.w = 4;
148         i4 = new Int4(1, 2, 3, 4);
149         assertTrue(i4.x == 1);
150         assertTrue(i4.y == 2);
151         assertTrue(i4.z == 3);
152         assertTrue(i4.w == 4);
153 
154         Long2 l2 = new Long2();
155         l2.x = 1;
156         l2.y = 2;
157         l2 = new Long2(1, 2);
158         assertTrue(l2.x == 1);
159         assertTrue(l2.y == 2);
160         Long3 l3 = new Long3();
161         l3.x = 1;
162         l3.y = 2;
163         l3.z = 3;
164         l3 = new Long3(1, 2, 3);
165         assertTrue(l3.x == 1);
166         assertTrue(l3.y == 2);
167         assertTrue(l3.z == 3);
168         Long4 l4 = new Long4();
169         l4.x = 1;
170         l4.y = 2;
171         l4.x = 3;
172         l4.w = 4;
173         l4 = new Long4(1, 2, 3, 4);
174         assertTrue(l4.x == 1);
175         assertTrue(l4.y == 2);
176         assertTrue(l4.z == 3);
177         assertTrue(l4.w == 4);
178 
179         Short2 s2 = new Short2();
180         s2.x = 1;
181         s2.y = 2;
182         s2 = new Short2((short)1, (short)2);
183         assertTrue(s2.x == 1);
184         assertTrue(s2.y == 2);
185         Short3 s3 = new Short3();
186         s3.x = 1;
187         s3.y = 2;
188         s3.z = 3;
189         s3 = new Short3((short)1, (short)2, (short)3);
190         assertTrue(s3.x == 1);
191         assertTrue(s3.y == 2);
192         assertTrue(s3.z == 3);
193         Short4 s4 = new Short4();
194         s4.x = 1;
195         s4.y = 2;
196         s4.x = 3;
197         s4.w = 4;
198         s4 = new Short4((short)1, (short)2, (short)3, (short)4);
199         assertTrue(s4.x == 1);
200         assertTrue(s4.y == 2);
201         assertTrue(s4.z == 3);
202         assertTrue(s4.w == 4);
203     }
204 
initializeGlobals(ScriptC_primitives s)205     private boolean initializeGlobals(ScriptC_primitives s) {
206         float pF = s.get_floatTest();
207         if (pF != 1.99f) {
208             return false;
209         }
210         s.set_floatTest(2.99f);
211 
212         double pD = s.get_doubleTest();
213         if (pD != 2.05) {
214             return false;
215         }
216         s.set_doubleTest(3.05);
217 
218         byte pC = s.get_charTest();
219         if (pC != -8) {
220             return false;
221         }
222         s.set_charTest((byte)-16);
223 
224         short pS = s.get_shortTest();
225         if (pS != -16) {
226             return false;
227         }
228         s.set_shortTest((short)-32);
229 
230         int pI = s.get_intTest();
231         if (pI != -32) {
232             return false;
233         }
234         s.set_intTest(-64);
235 
236         long pL = s.get_longTest();
237         if (pL != 17179869184l) {
238             return false;
239         }
240         s.set_longTest(17179869185l);
241 
242         long puL = s.get_ulongTest();
243         if (puL != 4611686018427387904L) {
244             return false;
245         }
246         s.set_ulongTest(4611686018427387903L);
247 
248         long pLL = s.get_longlongTest();
249         if (pLL != 68719476736L) {
250             return false;
251         }
252         s.set_longlongTest(68719476735L);
253 
254         long pu64 = s.get_uint64_tTest();
255         if (pu64 != 117179869184l) {
256             return false;
257         }
258         s.set_uint64_tTest(117179869185l);
259 
260         ScriptField_AllVectorTypes avt;
261         avt = new ScriptField_AllVectorTypes(mRS, 1,
262                                              Allocation.USAGE_SCRIPT);
263         ScriptField_AllVectorTypes.Item avtItem;
264         avtItem = new ScriptField_AllVectorTypes.Item();
265         avtItem.b2.x = 1;
266         avtItem.b2.y = 2;
267         avtItem.b3.x = 1;
268         avtItem.b3.y = 2;
269         avtItem.b3.z = 3;
270         avtItem.b4.x = 1;
271         avtItem.b4.y = 2;
272         avtItem.b4.z = 3;
273         avtItem.b4.w = 4;
274 
275         avtItem.s2.x = 1;
276         avtItem.s2.y = 2;
277         avtItem.s3.x = 1;
278         avtItem.s3.y = 2;
279         avtItem.s3.z = 3;
280         avtItem.s4.x = 1;
281         avtItem.s4.y = 2;
282         avtItem.s4.z = 3;
283         avtItem.s4.w = 4;
284 
285         avtItem.i2.x = 1;
286         avtItem.i2.y = 2;
287         avtItem.i3.x = 1;
288         avtItem.i3.y = 2;
289         avtItem.i3.z = 3;
290         avtItem.i4.x = 1;
291         avtItem.i4.y = 2;
292         avtItem.i4.z = 3;
293         avtItem.i4.w = 4;
294 
295         avtItem.f2.x = 1.0f;
296         avtItem.f2.y = 2.0f;
297         avtItem.f3.x = 1.0f;
298         avtItem.f3.y = 2.0f;
299         avtItem.f3.z = 3.0f;
300         avtItem.f4.x = 1.0f;
301         avtItem.f4.y = 2.0f;
302         avtItem.f4.z = 3.0f;
303         avtItem.f4.w = 4.0f;
304 
305         avt.set(avtItem, 0, true);
306         s.bind_avt(avt);
307 
308         return true;
309     }
310 
311     /**
312      * Test primitive types.
313      */
testPrimitives()314     public void testPrimitives() {
315         ScriptC_primitives t = new ScriptC_primitives(mRS);
316 
317         assertTrue(initializeGlobals(t));
318         t.invoke_test();
319         waitForMessage();
320         checkForErrors();
321         t.destroy();
322     }
323 
checkInit(ScriptC_array_init s)324     private void checkInit(ScriptC_array_init s) {
325         float[] fa = s.get_fa();
326         assertTrue(fa[0] == 1.0);
327         assertTrue(fa[1] == 9.9999f);
328         assertTrue(fa[2] == 0);
329         assertTrue(fa[3] == 0);
330         assertTrue(fa.length == 4);
331 
332         double[] da = s.get_da();
333         assertTrue(da[0] == 7.0);
334         assertTrue(da[1] == 8.88888);
335         assertTrue(da.length == 2);
336 
337         byte[] ca = s.get_ca();
338         assertTrue(ca[0] == 'a');
339         assertTrue(ca[1] == 7);
340         assertTrue(ca[2] == 'b');
341         assertTrue(ca[3] == 'c');
342         assertTrue(ca.length == 4);
343 
344         short[] sa = s.get_sa();
345         assertTrue(sa[0] == 1);
346         assertTrue(sa[1] == 1);
347         assertTrue(sa[2] == 2);
348         assertTrue(sa[3] == 3);
349         assertTrue(sa.length == 4);
350 
351         int[] ia = s.get_ia();
352         assertTrue(ia[0] == 5);
353         assertTrue(ia[1] == 8);
354         assertTrue(ia[2] == 0);
355         assertTrue(ia[3] == 0);
356         assertTrue(ia.length == 4);
357 
358         long[] la = s.get_la();
359         assertTrue(la[0] == 13);
360         assertTrue(la[1] == 21);
361         assertTrue(la.length == 2);
362 
363         long[] lla = s.get_lla();
364         assertTrue(lla[0] == 34);
365         assertTrue(lla[1] == 0);
366         assertTrue(lla[2] == 0);
367         assertTrue(lla[3] == 0);
368         assertTrue(lla.length == 4);
369 
370         boolean[] ba = s.get_ba();
371         assertTrue(ba[0] == true);
372         assertTrue(ba[1] == false);
373         assertTrue(ba[2] == false);
374         assertTrue(ba.length == 3);
375     }
376 
377     /**
378      * Test array initialization.
379      */
testArrayInit()380     public void testArrayInit() {
381         ScriptC_array_init t = new ScriptC_array_init(mRS);
382 
383         checkInit(t);
384         t.invoke_array_init_test();
385         Element[] e = new Element[2];
386         e[0] = Element.FONT(mRS);
387         e[1] = Element.ELEMENT(mRS);
388         t.set_elemArr(e);
389         mRS.finish();
390         waitForMessage();
391         checkForErrors();
392         t.destroy();
393     }
394 
395 
initializeVector(ScriptC_vector s)396     private boolean initializeVector(ScriptC_vector s) {
397         Float2 F2 = s.get_f2();
398         if (F2.x != 1.0f || F2.y != 2.0f) {
399             return false;
400         }
401         F2.x = 2.99f;
402         F2.y = 3.99f;
403         s.set_f2(F2);
404 
405         Float3 F3 = s.get_f3();
406         if (F3.x != 1.0f || F3.y != 2.0f || F3.z != 3.0f) {
407             return false;
408         }
409         F3.x = 2.99f;
410         F3.y = 3.99f;
411         F3.z = 4.99f;
412         s.set_f3(F3);
413 
414         Float4 F4 = s.get_f4();
415         if (F4.x != 1.0f || F4.y != 2.0f || F4.z != 3.0f || F4.w != 4.0f) {
416             return false;
417         }
418         F4.x = 2.99f;
419         F4.y = 3.99f;
420         F4.z = 4.99f;
421         F4.w = 5.99f;
422         s.set_f4(F4);
423 
424         Double2 D2 = s.get_d2();
425         if (D2.x != 1.0 || D2.y != 2.0) {
426             return false;
427         }
428         D2.x = 2.99;
429         D2.y = 3.99;
430         s.set_d2(D2);
431 
432         Double3 D3 = s.get_d3();
433         if (D3.x != 1.0 || D3.y != 2.0 || D3.z != 3.0) {
434             return false;
435         }
436         D3.x = 2.99;
437         D3.y = 3.99;
438         D3.z = 4.99;
439         s.set_d3(D3);
440 
441         Double4 D4 = s.get_d4();
442         if (D4.x != 1.0 || D4.y != 2.0 || D4.z != 3.0 || D4.w != 4.0) {
443             return false;
444         }
445         D4.x = 2.99;
446         D4.y = 3.99;
447         D4.z = 4.99;
448         D4.w = 5.99;
449         s.set_d4(D4);
450 
451         Byte2 B2 = s.get_i8_2();
452         if (B2.x != 1 || B2.y != 2) {
453             return false;
454         }
455         B2.x = 2;
456         B2.y = 3;
457         s.set_i8_2(B2);
458 
459         Byte3 B3 = s.get_i8_3();
460         if (B3.x != 1 || B3.y != 2 || B3.z != 3) {
461             return false;
462         }
463         B3.x = 2;
464         B3.y = 3;
465         B3.z = 4;
466         s.set_i8_3(B3);
467 
468         Byte4 B4 = s.get_i8_4();
469         if (B4.x != 1 || B4.y != 2 || B4.z != 3 || B4.w != 4) {
470             return false;
471         }
472         B4.x = 2;
473         B4.y = 3;
474         B4.z = 4;
475         B4.w = 5;
476         s.set_i8_4(B4);
477 
478         Short2 S2 = s.get_u8_2();
479         if (S2.x != 1 || S2.y != 2) {
480             return false;
481         }
482         S2.x = 2;
483         S2.y = 3;
484         s.set_u8_2(S2);
485 
486         Short3 S3 = s.get_u8_3();
487         if (S3.x != 1 || S3.y != 2 || S3.z != 3) {
488             return false;
489         }
490         S3.x = 2;
491         S3.y = 3;
492         S3.z = 4;
493         s.set_u8_3(S3);
494 
495         Short4 S4 = s.get_u8_4();
496         if (S4.x != 1 || S4.y != 2 || S4.z != 3 || S4.w != 4) {
497             return false;
498         }
499         S4.x = 2;
500         S4.y = 3;
501         S4.z = 4;
502         S4.w = 5;
503         s.set_u8_4(S4);
504 
505         S2 = s.get_i16_2();
506         if (S2.x != 1 || S2.y != 2) {
507             return false;
508         }
509         S2.x = 2;
510         S2.y = 3;
511         s.set_i16_2(S2);
512 
513         S3 = s.get_i16_3();
514         if (S3.x != 1 || S3.y != 2 || S3.z != 3) {
515             return false;
516         }
517         S3.x = 2;
518         S3.y = 3;
519         S3.z = 4;
520         s.set_i16_3(S3);
521 
522         S4 = s.get_i16_4();
523         if (S4.x != 1 || S4.y != 2 || S4.z != 3 || S4.w != 4) {
524             return false;
525         }
526         S4.x = 2;
527         S4.y = 3;
528         S4.z = 4;
529         S4.w = 5;
530         s.set_i16_4(S4);
531 
532         Int2 I2 = s.get_u16_2();
533         if (I2.x != 1 || I2.y != 2) {
534             return false;
535         }
536         I2.x = 2;
537         I2.y = 3;
538         s.set_u16_2(I2);
539 
540         Int3 I3 = s.get_u16_3();
541         if (I3.x != 1 || I3.y != 2 || I3.z != 3) {
542             return false;
543         }
544         I3.x = 2;
545         I3.y = 3;
546         I3.z = 4;
547         s.set_u16_3(I3);
548 
549         Int4 I4 = s.get_u16_4();
550         if (I4.x != 1 || I4.y != 2 || I4.z != 3 || I4.w != 4) {
551             return false;
552         }
553         I4.x = 2;
554         I4.y = 3;
555         I4.z = 4;
556         I4.w = 5;
557         s.set_u16_4(I4);
558 
559         I2 = s.get_i32_2();
560         if (I2.x != 1 || I2.y != 2) {
561             return false;
562         }
563         I2.x = 2;
564         I2.y = 3;
565         s.set_i32_2(I2);
566 
567         I3 = s.get_i32_3();
568         if (I3.x != 1 || I3.y != 2 || I3.z != 3) {
569             return false;
570         }
571         I3.x = 2;
572         I3.y = 3;
573         I3.z = 4;
574         s.set_i32_3(I3);
575 
576         I4 = s.get_i32_4();
577         if (I4.x != 1 || I4.y != 2 || I4.z != 3 || I4.w != 4) {
578             return false;
579         }
580         I4.x = 2;
581         I4.y = 3;
582         I4.z = 4;
583         I4.w = 5;
584         s.set_i32_4(I4);
585 
586         Long2 L2 = s.get_u32_2();
587         if (L2.x != 1 || L2.y != 2) {
588             return false;
589         }
590         L2.x = 2;
591         L2.y = 3;
592         s.set_u32_2(L2);
593 
594         Long3 L3 = s.get_u32_3();
595         if (L3.x != 1 || L3.y != 2 || L3.z != 3) {
596             return false;
597         }
598         L3.x = 2;
599         L3.y = 3;
600         L3.z = 4;
601         s.set_u32_3(L3);
602 
603         Long4 L4 = s.get_u32_4();
604         if (L4.x != 1 || L4.y != 2 || L4.z != 3 || L4.w != 4) {
605             return false;
606         }
607         L4.x = 2;
608         L4.y = 3;
609         L4.z = 4;
610         L4.w = 5;
611         s.set_u32_4(L4);
612 
613         L2 = s.get_i64_2();
614         if (L2.x != 1 || L2.y != 2) {
615             return false;
616         }
617         L2.x = 2;
618         L2.y = 3;
619         s.set_i64_2(L2);
620 
621         L3 = s.get_i64_3();
622         if (L3.x != 1 || L3.y != 2 || L3.z != 3) {
623             return false;
624         }
625         L3.x = 2;
626         L3.y = 3;
627         L3.z = 4;
628         s.set_i64_3(L3);
629 
630         L4 = s.get_i64_4();
631         if (L4.x != 1 || L4.y != 2 || L4.z != 3 || L4.w != 4) {
632             return false;
633         }
634         L4.x = 2;
635         L4.y = 3;
636         L4.z = 4;
637         L4.w = 5;
638         s.set_i64_4(L4);
639 
640         L2 = s.get_u64_2();
641         if (L2.x != 1 || L2.y != 2) {
642             return false;
643         }
644         L2.x = 2;
645         L2.y = 3;
646         s.set_u64_2(L2);
647 
648         L3 = s.get_u64_3();
649         if (L3.x != 1 || L3.y != 2 || L3.z != 3) {
650             return false;
651         }
652         L3.x = 2;
653         L3.y = 3;
654         L3.z = 4;
655         s.set_u64_3(L3);
656 
657         L4 = s.get_u64_4();
658         if (L4.x != 1 || L4.y != 2 || L4.z != 3 || L4.w != 4) {
659             return false;
660         }
661         L4.x = 2;
662         L4.y = 3;
663         L4.z = 4;
664         L4.w = 5;
665         s.set_u64_4(L4);
666 
667         return true;
668     }
669 
testVector()670     public void testVector() {
671         ScriptC_vector s = new ScriptC_vector(mRS);
672         if (!initializeVector(s)) {
673             fail("Failed to init vector components");
674         } else {
675             s.invoke_vector_test();
676             mRS.finish();
677             waitForMessage();
678         }
679         checkForErrors();
680         s.destroy();
681     }
682 
testMatrix()683     public void testMatrix() {
684         ScriptC_MatrixTest s = new ScriptC_MatrixTest(mRS);
685         s.invoke_matrixTests();
686         mRS.finish();
687         waitForMessage();
688         checkForErrors();
689         s.destroy();
690     }
691 
initializeStructObject(ScriptC_struct_object s)692     private boolean initializeStructObject(ScriptC_struct_object s) {
693         ScriptField_objects_rs.Item i = new ScriptField_objects_rs.Item();
694         i.e = Element.FONT(mRS);
695         i.i = 1;
696         s.set_myStruct(i);
697         return true;
698     }
699 
testStructObject()700     public void testStructObject() {
701         ScriptC_struct_object s =
702                 new ScriptC_struct_object(mRS);
703         if (!initializeStructObject(s)) {
704             fail("Failed to init structure with RS objects");
705         } else {
706             s.invoke_struct_object_test();
707             mRS.finish();
708             waitForMessage();
709         }
710         checkForErrors();
711         s.destroy();
712     }
713 
714     /**
715      * Test utility functions.
716      */
testUtilityFunctions()717     public void testUtilityFunctions() {
718         ScriptC_utils t = new ScriptC_utils(mRS);
719         t.invoke_test();
720         waitForMessage();
721         checkForErrors();
722         t.destroy();
723     }
724 
setUpAllocation(Allocation a, int val)725     void setUpAllocation(Allocation a, int val) {
726         Type t = a.getType();
727         int x = t.getX();
728 
729         int[] arr = new int[x];
730         for (int i = 0; i < x; i++) {
731             arr[i] = val;
732         }
733         a.copyFrom(arr);
734     }
735 
checkAllocation(Allocation a, int val)736     void checkAllocation(Allocation a, int val) {
737         Type t = a.getType();
738         int x = t.getX();
739 
740         int[] arr = new int[x];
741         a.copyTo(arr);
742         for (int i = 0; i < x; i++) {
743             assertTrue(arr[i] == val);
744         }
745     }
746 
747     /**
748      * Test support for reflected forEach() as well as validation of parameters.
749      */
testForEach()750     public void testForEach() {
751         ScriptC_negate s = new ScriptC_negate(mRS);
752 
753         int x = 7;
754         Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(x).create();
755         Allocation in = Allocation.createTyped(mRS, t);
756         Allocation out = Allocation.createTyped(mRS, t);
757 
758         int val = 5;
759         setUpAllocation(in, val);
760         s.forEach_root(in, out);
761         checkAllocation(out, -val);
762 
763         Type badT = new Type.Builder(mRS, Element.I32(mRS)).setX(x-1).create();
764         Allocation badOut = Allocation.createTyped(mRS, badT);
765         try {
766             s.forEach_root(in, badOut);
767             fail("should throw RSRuntimeException");
768         } catch (RSRuntimeException e) {
769         }
770         checkForErrors();
771 
772         in.destroy();
773         out.destroy();
774         badOut.destroy();
775         s.destroy();
776     }
777 
778     /**
779      * Test script instancing.
780      */
testInstance()781     public void testInstance() {
782         ScriptC_instance instance_1 = new ScriptC_instance(mRS);
783         ScriptC_instance instance_2 = new ScriptC_instance(mRS);
784 
785         Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(1).create();
786         Allocation ai1 = Allocation.createTyped(mRS, t);
787         Allocation ai2 = Allocation.createTyped(mRS, t);
788 
789         instance_1.set_i(1);
790         instance_2.set_i(2);
791         instance_1.set_ai(ai1);
792         instance_2.set_ai(ai2);
793 
794         // We now check to ensure that the global is not being shared across
795         // our separate script instances. Our invoke here merely sets the
796         // instanced allocation with the instanced global variable's value.
797         // If globals are being shared (i.e. not instancing scripts), then
798         // both instanced allocations will have the same resulting value
799         // (depending on the order in which the invokes complete).
800         instance_1.invoke_instance_test();
801         instance_2.invoke_instance_test();
802 
803         int i1[] = new int[1];
804         int i2[] = new int[1];
805 
806         ai1.copyTo(i1);
807         ai2.copyTo(i2);
808 
809         // 3-step check ensures that a fortunate race condition wouldn't let us
810         // pass accidentally.
811         assertEquals(2, i2[0]);
812         assertEquals(1, i1[0]);
813         assertEquals(2, i2[0]);
814 
815         checkForErrors();
816 
817         ai1.destroy();
818         ai2.destroy();
819         instance_1.destroy();
820         instance_2.destroy();
821     }
822 }
823