1/* 2 * Copyright (C) 2013 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 17package com.android.rs.test_compatlegacy; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.support.v8.renderscript.*; 22 23public class UT_instance extends UnitTest { 24 private Resources mRes; 25 26 protected UT_instance(RSTestCore rstc, Resources res, Context ctx) { 27 super(rstc, "Instance", ctx); 28 mRes = res; 29 } 30 31 void assertEquals(int e, int v) { 32 if (e != v) { 33 RSTest.log("Assertion failed! Expected: <" + e + "> Got: <" + v + ">"); 34 failTest(); 35 } 36 } 37 38 public void run() { 39 RenderScript mRS = RenderScript.create(mCtx); 40 mRS.setMessageHandler(mRsMessage); 41 42 ScriptC_instance instance_1 = new ScriptC_instance(mRS); 43 ScriptC_instance instance_2 = new ScriptC_instance(mRS); 44 ScriptC_instance instance_3 = new ScriptC_instance(mRS); 45 ScriptC_instance instance_4 = new ScriptC_instance(mRS); 46 ScriptC_instance instance_5 = new ScriptC_instance(mRS); 47 48 Type t = new Type.Builder(mRS, Element.I32(mRS)).setX(1).create(); 49 Allocation ai1 = Allocation.createTyped(mRS, t); 50 Allocation ai2 = Allocation.createTyped(mRS, t); 51 Allocation ai3 = Allocation.createTyped(mRS, t); 52 Allocation ai4 = Allocation.createTyped(mRS, t); 53 Allocation ai5 = Allocation.createTyped(mRS, t); 54 55 instance_1.set_i(1); 56 instance_2.set_i(2); 57 instance_3.set_i(3); 58 instance_4.set_i(4); 59 instance_5.set_i(5); 60 instance_1.set_ai(ai1); 61 instance_2.set_ai(ai2); 62 instance_3.set_ai(ai3); 63 instance_4.set_ai(ai4); 64 instance_5.set_ai(ai5); 65 66 // We now check to ensure that the global is not being shared across 67 // our separate script instances. Our invoke here merely sets the 68 // instanced allocation with the instanced global variable's value. 69 // If globals are being shared (i.e. not instancing scripts), then 70 // both instanced allocations will have the same resulting value 71 // (depending on the order in which the invokes complete). 72 instance_1.invoke_instance_test(); 73 instance_2.invoke_instance_test(); 74 instance_3.invoke_instance_test(); 75 instance_4.invoke_instance_test(); 76 instance_5.invoke_instance_test(); 77 78 int i1[] = new int[1]; 79 int i2[] = new int[1]; 80 int i3[] = new int[1]; 81 int i4[] = new int[1]; 82 int i5[] = new int[1]; 83 84 ai1.copyTo(i1); 85 ai2.copyTo(i2); 86 ai3.copyTo(i3); 87 ai4.copyTo(i4); 88 ai5.copyTo(i5); 89 90 assertEquals(1, i1[0]); 91 assertEquals(2, i2[0]); 92 assertEquals(3, i3[0]); 93 assertEquals(4, i4[0]); 94 assertEquals(5, i5[0]); 95 assertEquals(1, i1[0]); 96 assertEquals(2, i2[0]); 97 assertEquals(3, i3[0]); 98 assertEquals(4, i4[0]); 99 assertEquals(5, i5[0]); 100 passTest(); // Set to pass (as long as existing checks didn't fail). 101 } 102} 103