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