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.unittest;
18
19import android.content.Context;
20import android.renderscript.Allocation;
21import android.renderscript.Element;
22import android.renderscript.RenderScript;
23import android.util.Log;
24
25public class UT_copy_test extends UnitTest {
26    boolean pass = true;
27
28    public UT_copy_test(Context ctx) {
29        super("Copy", ctx);
30    }
31
32    void testFloat2(RenderScript rs, ScriptC_copy_test s) {
33        Allocation a1 = Allocation.createSized(rs, Element.F32_2(rs), 1024);
34        Allocation a2 = Allocation.createSized(rs, Element.F32_2(rs), 1024);
35
36        float[] f1 = new float[1024 * 2];
37        float[] f2 = new float[1024 * 2];
38        for (int ct = 0; ct < f1.length; ct++) {
39            f1[ct] = (float) ct;
40        }
41        a1.copyFrom(f1);
42
43        s.forEach_copyFloat2(a1, a2);
44
45        a2.copyTo(f2);
46        for (int ct = 0; ct < f1.length; ct++) {
47            if (f1[ct] != f2[ct]) {
48                failTest();
49                Log.v("RS Test", "Compare failed at " + ct + ", " + f1[ct] + ", " + f2[ct]);
50            }
51        }
52        a1.destroy();
53        a2.destroy();
54    }
55
56    void testFloat3(RenderScript rs, ScriptC_copy_test s) {
57        Allocation a1 = Allocation.createSized(rs, Element.F32_3(rs), 1024);
58        Allocation a2 = Allocation.createSized(rs, Element.F32_3(rs), 1024);
59
60        float[] f1 = new float[1024 * 4];
61        float[] f2 = new float[1024 * 4];
62        for (int ct = 0; ct < f1.length; ct++) {
63            f1[ct] = (float) ct;
64        }
65        a1.copyFrom(f1);
66
67        s.forEach_copyFloat3(a1, a2);
68
69        a2.copyTo(f2);
70        for (int ct = 0; ct < f1.length; ct++) {
71            if ((f1[ct] != f2[ct]) && ((ct & 3) != 3)) {
72                failTest();
73                Log.v("RS Test", "Compare failed at " + ct + ", " + f1[ct] + ", " + f2[ct]);
74            }
75        }
76        a1.destroy();
77        a2.destroy();
78    }
79
80    void testFloat4(RenderScript rs, ScriptC_copy_test s) {
81        Allocation a1 = Allocation.createSized(rs, Element.F32_4(rs), 1024);
82        Allocation a2 = Allocation.createSized(rs, Element.F32_4(rs), 1024);
83
84        float[] f1 = new float[1024 * 4];
85        float[] f2 = new float[1024 * 4];
86        for (int ct = 0; ct < f1.length; ct++) {
87            f1[ct] = (float) ct;
88        }
89        a1.copyFrom(f1);
90
91        s.forEach_copyFloat4(a1, a2);
92
93        a2.copyTo(f2);
94        for (int ct = 0; ct < f1.length; ct++) {
95            if (f1[ct] != f2[ct]) {
96                failTest();
97                Log.v("RS Test", "Compare failed at " + ct + ", " + f1[ct] + ", " + f2[ct]);
98            }
99        }
100        a1.destroy();
101        a2.destroy();
102    }
103
104    public void run() {
105        RenderScript pRS = createRenderScript(true);
106        ScriptC_copy_test s = new ScriptC_copy_test(pRS);
107
108        testFloat2(pRS, s);
109        testFloat3(pRS, s);
110        testFloat4(pRS, s);
111        s.invoke_sendResult(true);
112
113        pRS.finish();
114        s.destroy();
115        pRS.destroy();
116    }
117}
118
119