1a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung/*
2a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * Copyright (C) 2017 The Android Open Source Project
3a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung *
4a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * Licensed under the Apache License, Version 2.0 (the "License");
5a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * you may not use this file except in compliance with the License.
6a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * You may obtain a copy of the License at
7a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung *
8a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung *      http://www.apache.org/licenses/LICENSE-2.0
9a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung *
10a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * Unless required by applicable law or agreed to in writing, software
11a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * distributed under the License is distributed on an "AS IS" BASIS,
12a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * See the License for the specific language governing permissions and
14a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung * limitations under the License.
15a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung */
16a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
17a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungpackage com.android.rs.rsov.test;
18a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
19a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungimport android.content.Context;
20a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungimport android.renderscript.Allocation;
21a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungimport android.renderscript.Element;
22a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungimport android.renderscript.RenderScript;
23a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungimport android.renderscript.Type;
24a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungimport android.util.Log;
25a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
26a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sungpublic class UT_multi_input extends UnitTest {
27a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    private Allocation Ain0;
28a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    private Allocation Ain1;
29a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
30a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    private Allocation Out0;
31a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
32a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    private final int Xdim = 100;
33a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    private final float tolerance = 1e-6f;
34a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
35a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    protected UT_multi_input(RSoVTestCore rstc, Context ctx) {
36a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        super(rstc, "Foreach Multi-input", ctx);
37a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    }
38a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
39a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    private void initializeGlobals(RenderScript RS, ScriptC_multi_input s) {
40a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Type.Builder floatBuilder = new Type.Builder(RS, Element.F32(RS));
41a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
42a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        floatBuilder.setX(Xdim);
43a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
44a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Ain0 = Allocation.createTyped(RS, floatBuilder.create());
45a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Ain1 = Allocation.createTyped(RS, floatBuilder.create());
46a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Out0 = Allocation.createTyped(RS, floatBuilder.create());
47a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        return;
48a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    }
49a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
50a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    public void run() {
51a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        RenderScript pRS = RenderScript.create(mCtx);
52a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        ScriptC_multi_input s = new ScriptC_multi_input(pRS);
53a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
54a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        initializeGlobals(pRS, s);
55a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
56a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        float a[] = new float[Xdim];
57a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        float b[] = new float[Xdim];
58a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
59a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
60a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        java.util.Random rand = new java.util.Random();
61a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
62a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        for (int i = 0; i < Xdim; i++) {
63a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            a[i] = rand.nextFloat();
64a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            b[i] = rand.nextFloat();
65a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        }
66a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
67a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Ain0.copyFrom(a);
68a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Ain1.copyFrom(b);
69a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
70a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        s.forEach_sum2(Ain0, Ain1, Out0);
71a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
72a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        float out0[] = new float[Xdim];
73a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        float ain0[] = new float[Xdim];
74a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        float ain1[] = new float[Xdim];
75a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Ain0.copyTo(ain0);
76a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Ain1.copyTo(ain1);
77a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        Out0.copyTo(out0);
78a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
79a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        pRS.finish();
80a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        pRS.destroy();
81a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
82a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        boolean failed = false;
83a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        for (int i = 0; i < Xdim; i++) {
84a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            if (ain0[i] != a[i]) {
85a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                Log.e(name, "Ain0 was " + a[i] + " but changed to " + ain0[i]);
86a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                failed = true;
87a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                break;
88a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            }
89a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            if (ain1[i] != b[i]) {
90a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                Log.e(name, "Ain1 was " + b[i] + " but changed to " + ain1[i]);
91a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                failed = true;
92a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                break;
93a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            }
94a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            if ((a[i] + b[i] - out0[i]) > tolerance) {
95a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                float expected = a[i]+b[i];
96a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                Log.e(name, "expects " + expected + " got " + out0[i]);
97a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                failed = true;
98a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung                break;
99a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            }
100a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        }
101a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung
102a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        if (failed) {
103a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            failTest();
104a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        } else {
105a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung            passTest();
106a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung        }
107a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung    }
108a075ad49dd56f25efebb5402bcc263766421b0c8I-Jui (Ray) Sung}
109