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_global_query extends UnitTest {
27    protected UT_global_query(RSoVTestCore rstc, Context ctx) {
28        super(rstc, "global_query", ctx);
29    }
30
31    private boolean Test(int width, int height, int depth) {
32        RenderScript pRS = RenderScript.create(mCtx);
33        ScriptC_global_query s = new ScriptC_global_query(pRS);
34
35        Type.Builder typeBuilder = new Type.Builder(pRS, Element.I32(pRS));
36        typeBuilder.setX(width);
37        if (height > 0) {
38            typeBuilder.setY(height);
39            if (depth > 0) {
40                typeBuilder.setZ(depth);
41            }
42        }
43
44        if (depth < 1) {
45            depth = 1;
46        }
47
48        if (height < 1) {
49            height = 1;
50        }
51
52        Allocation G = Allocation.createTyped(pRS, typeBuilder.create());
53        Allocation dummy = Allocation.createTyped(pRS, typeBuilder.create());
54        Allocation R = Allocation.createTyped(pRS, typeBuilder.create());
55
56        int g[] = new int[width * height * depth];
57        int d[] = new int[width * height * depth];
58        int r[] = new int[width * height * depth];
59
60        java.util.Random rand = new java.util.Random();
61
62        for (int i = 0; i < width * height * depth; i++) {
63            g[i] = rand.nextInt(123456);
64            d[i] = rand.nextInt(123456);
65        }
66
67        G.copyFrom(g);
68        dummy.copyFrom(d);
69
70        s.set_g(G);
71
72        s.forEach_getDim(dummy, R);
73
74        R.copyTo(r);
75
76        R.destroy();
77        dummy.destroy();
78        G.destroy();
79
80        pRS.finish();
81        pRS.destroy();
82
83        boolean failed = false;
84        for (int i = 0; i < width * height * depth; i++) {
85            if (r[i] != width) {
86                Log.e(name, "expects " + width + " for element " + i +
87                      ". got " + r[i]);
88                failed = true;
89                break;
90            }
91        }
92
93        return !failed;
94    }
95
96    public void run() {
97        final int X = 96;
98
99        if (Test(X, 0, 0)) {
100            passTest();
101            return;
102        }
103
104        failTest();
105    }
106}
107