1/*
2 * Copyright (C) 2017 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.rsov.test;
18
19import android.content.Context;
20import android.renderscript.Allocation;
21import android.renderscript.Element;
22import android.renderscript.RenderScript;
23import android.renderscript.Type;
24import android.util.Log;
25
26public class UT_multi_input extends UnitTest {
27    private Allocation Ain0;
28    private Allocation Ain1;
29
30    private Allocation Out0;
31
32    private final int Xdim = 100;
33    private final float tolerance = 1e-6f;
34
35    protected UT_multi_input(RSoVTestCore rstc, Context ctx) {
36        super(rstc, "Foreach Multi-input", ctx);
37    }
38
39    private void initializeGlobals(RenderScript RS, ScriptC_multi_input s) {
40        Type.Builder floatBuilder = new Type.Builder(RS, Element.F32(RS));
41
42        floatBuilder.setX(Xdim);
43
44        Ain0 = Allocation.createTyped(RS, floatBuilder.create());
45        Ain1 = Allocation.createTyped(RS, floatBuilder.create());
46        Out0 = Allocation.createTyped(RS, floatBuilder.create());
47        return;
48    }
49
50    public void run() {
51        RenderScript pRS = RenderScript.create(mCtx);
52        ScriptC_multi_input s = new ScriptC_multi_input(pRS);
53
54        initializeGlobals(pRS, s);
55
56        float a[] = new float[Xdim];
57        float b[] = new float[Xdim];
58
59
60        java.util.Random rand = new java.util.Random();
61
62        for (int i = 0; i < Xdim; i++) {
63            a[i] = rand.nextFloat();
64            b[i] = rand.nextFloat();
65        }
66
67        Ain0.copyFrom(a);
68        Ain1.copyFrom(b);
69
70        s.forEach_sum2(Ain0, Ain1, Out0);
71
72        float out0[] = new float[Xdim];
73        float ain0[] = new float[Xdim];
74        float ain1[] = new float[Xdim];
75        Ain0.copyTo(ain0);
76        Ain1.copyTo(ain1);
77        Out0.copyTo(out0);
78
79        pRS.finish();
80        pRS.destroy();
81
82        boolean failed = false;
83        for (int i = 0; i < Xdim; i++) {
84            if (ain0[i] != a[i]) {
85                Log.e(name, "Ain0 was " + a[i] + " but changed to " + ain0[i]);
86                failed = true;
87                break;
88            }
89            if (ain1[i] != b[i]) {
90                Log.e(name, "Ain1 was " + b[i] + " but changed to " + ain1[i]);
91                failed = true;
92                break;
93            }
94            if ((a[i] + b[i] - out0[i]) > tolerance) {
95                float expected = a[i]+b[i];
96                Log.e(name, "expects " + expected + " got " + out0[i]);
97                failed = true;
98                break;
99            }
100        }
101
102        if (failed) {
103            failTest();
104        } else {
105            passTest();
106        }
107    }
108}
109