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