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.renderscript.Short4;
24import android.renderscript.Type;
25import android.util.Log;
26import java.util.Random;
27
28import static junit.framework.Assert.assertEquals;
29
30public class UT_reflection3264 extends UnitTest {
31  private static final String TAG = "reflection3264";
32
33  public UT_reflection3264(Context ctx) {
34    super("reflection3264", ctx);
35  }
36
37  private final int xSize = 17, ySize = 23;  // arbitrary values
38
39  private final long seed = 20170609;  // arbitrary value
40
41  private static long unsigned(int v) {
42    if (v >= 0)
43      return (long)v;
44    else
45      return (long)v + ((long)1 << 32);
46  }
47
48  private static short unsigned(byte v) {
49    if (v >= 0)
50      return (short)v;
51    else
52      return (short)((short)v + (short)(1 << 8));
53  }
54
55  public void run() {
56    Random r = new Random(seed);
57
58    RenderScript pRS = createRenderScript(true);
59    ScriptC_reflection3264 s = new ScriptC_reflection3264(pRS);
60
61    Type.Builder typeBuilder = new Type.Builder(pRS, Element.U8_4(pRS));
62    typeBuilder.setX(xSize).setY(ySize);
63    Allocation inputAllocation = Allocation.createTyped(pRS, typeBuilder.create());
64    byte[] inputArray = new byte[xSize * ySize * 4];
65    r.nextBytes(inputArray);
66    inputAllocation.copyFrom(inputArray);
67
68    ScriptField_user_t.Item usrData = new ScriptField_user_t.Item();
69    usrData.ans = new Short4(
70        unsigned((byte)r.nextInt()),
71        unsigned((byte)r.nextInt()),
72        unsigned((byte)r.nextInt()),
73        unsigned((byte)r.nextInt()));
74    s.set_expect_ans(usrData.ans);
75    usrData.x = unsigned(r.nextInt());
76    s.set_expect_x(usrData.x);
77    usrData.y = unsigned(r.nextInt());
78    s.set_expect_y(usrData.y);
79
80    usrData.alloc = inputAllocation;
81
82    Allocation outputAllocation = Allocation.createTyped(pRS, typeBuilder.create());
83
84    s.set_expect_dAlloc_GetDimX(xSize);
85    s.set_expect_sAlloc_GetDimX(xSize);
86    final int dXOff = r.nextInt();
87    s.set_expect_dXOff(dXOff);
88    final int dMip = r.nextInt();
89    s.set_expect_dMip(dMip);
90    final int count = r.nextInt();
91    s.set_expect_count(count);
92    final int sXOff = r.nextInt();
93    s.set_expect_sXOff(sXOff);
94    final int sMip = r.nextInt();
95    s.set_expect_sMip(sMip);
96    s.invoke_args(outputAllocation, dXOff, dMip, count, inputAllocation, sXOff, sMip);
97
98    s.forEach_root(outputAllocation, usrData);
99    byte[] outputArray = new byte[xSize * ySize * 4];
100    outputAllocation.copyTo(outputArray);
101
102    for (int i = 0; i < xSize; ++i)
103      for (int j = 0; j < ySize; ++j) {
104        int idx = j * xSize + i;
105        assertEquals("[" + i + "][" + j + "]", inputArray[idx], outputArray[idx]);
106      }
107
108    s.invoke_check_asserts();
109    pRS.finish();
110    inputAllocation.destroy();
111    outputAllocation.destroy();
112    s.destroy();
113    pRS.destroy();
114  }
115}
116