1/*
2 * Copyright (C) 2016 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_invert extends UnitTest {
27    protected UT_invert(RSoVTestCore rstc, Context ctx) {
28        super(rstc, "invert", ctx);
29    }
30
31    private boolean Test(int width, int height, int depth) {
32        RenderScript pRS = RenderScript.create(mCtx);
33        ScriptC_invert s = new ScriptC_invert(pRS);
34
35        Type.Builder typeBuilder = new Type.Builder(pRS, Element.F32_4(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 A = Allocation.createTyped(pRS, typeBuilder.create());
53        Allocation B = Allocation.createTyped(pRS, typeBuilder.create());
54
55        float a[] = new float[width * height * depth * 4];
56        float b[] = new float[width * height * depth * 4];
57
58        java.util.Random rand = new java.util.Random();
59
60        for (int i = 0; i < width * height * depth * 4; i++) {
61            a[i] = rand.nextFloat();
62        }
63
64        A.copyFrom(a);
65
66        s.forEach_invert(A, B);
67
68        B.copyTo(b);
69
70        B.destroy();
71        A.destroy();
72
73        pRS.finish();
74        pRS.destroy();
75
76        boolean failed = false;
77        for (int i = 0; i < width * height * depth * 4; i++) {
78            if (b[i] != 1.0f - a[i]) {
79                Log.e(name, "expects " + (1.0f - a[i]) + " for element " + i +
80                      ". got " + b[i]);
81                failed = true;
82                break;
83            }
84        }
85
86        return !failed;
87    }
88
89    public void run() {
90        final int X = 96;
91        final int Y = 64;
92        final int Z = 32;
93
94        if (Test(X, 0, 0) && Test(X, Y, 0)) {
95            passTest();
96            return;
97        }
98
99        failTest();
100    }
101}
102