1// This file is automatically generated from
2// frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py
3/*
4 * Copyright (C) 2017 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package com.android.rs.unittest;
20
21import android.content.Context;
22import android.support.v8.renderscript.Allocation;
23import android.support.v8.renderscript.Element;
24import android.support.v8.renderscript.RenderScript;
25import android.support.v8.renderscript.Short4;
26import android.support.v8.renderscript.Type;
27import android.util.Log;
28import java.util.Random;
29
30import static junit.framework.Assert.assertEquals;
31
32public class UT_reflection3264 extends UnitTest {
33  private static final String TAG = "reflection3264";
34
35  public UT_reflection3264(Context ctx) {
36    super("reflection3264", ctx);
37  }
38
39  private final int xSize = 17, ySize = 23;  // arbitrary values
40
41  private final long seed = 20170609;  // arbitrary value
42
43  private static long unsigned(int v) {
44    if (v >= 0)
45      return (long)v;
46    else
47      return (long)v + ((long)1 << 32);
48  }
49
50  private static short unsigned(byte v) {
51    if (v >= 0)
52      return (short)v;
53    else
54      return (short)((short)v + (short)(1 << 8));
55  }
56
57  public void run() {
58    Random r = new Random(seed);
59
60    RenderScript pRS = createRenderScript(true);
61    ScriptC_reflection3264 s = new ScriptC_reflection3264(pRS);
62
63    Type.Builder typeBuilder = new Type.Builder(pRS, Element.U8_4(pRS));
64    typeBuilder.setX(xSize).setY(ySize);
65    Allocation inputAllocation = Allocation.createTyped(pRS, typeBuilder.create());
66    byte[] inputArray = new byte[xSize * ySize * 4];
67    r.nextBytes(inputArray);
68    inputAllocation.copyFrom(inputArray);
69
70    ScriptField_user_t.Item usrData = new ScriptField_user_t.Item();
71    usrData.ans = new Short4(
72        unsigned((byte)r.nextInt()),
73        unsigned((byte)r.nextInt()),
74        unsigned((byte)r.nextInt()),
75        unsigned((byte)r.nextInt()));
76    s.set_expect_ans(usrData.ans);
77    usrData.x = unsigned(r.nextInt());
78    s.set_expect_x(usrData.x);
79    usrData.y = unsigned(r.nextInt());
80    s.set_expect_y(usrData.y);
81
82    usrData.alloc = inputAllocation;
83
84    Allocation outputAllocation = Allocation.createTyped(pRS, typeBuilder.create());
85
86    s.set_expect_dAlloc_GetDimX(xSize);
87    s.set_expect_sAlloc_GetDimX(xSize);
88    final int dXOff = r.nextInt();
89    s.set_expect_dXOff(dXOff);
90    final int dMip = r.nextInt();
91    s.set_expect_dMip(dMip);
92    final int count = r.nextInt();
93    s.set_expect_count(count);
94    final int sXOff = r.nextInt();
95    s.set_expect_sXOff(sXOff);
96    final int sMip = r.nextInt();
97    s.set_expect_sMip(sMip);
98    s.invoke_args(outputAllocation, dXOff, dMip, count, inputAllocation, sXOff, sMip);
99
100    s.forEach_root(outputAllocation, usrData);
101    byte[] outputArray = new byte[xSize * ySize * 4];
102    outputAllocation.copyTo(outputArray);
103
104    for (int i = 0; i < xSize; ++i)
105      for (int j = 0; j < ySize; ++j) {
106        int idx = j * xSize + i;
107        assertEquals("[" + i + "][" + j + "]", inputArray[idx], outputArray[idx]);
108      }
109
110    s.invoke_check_asserts();
111    pRS.finish();
112    inputAllocation.destroy();
113    outputAllocation.destroy();
114    s.destroy();
115    pRS.destroy();
116  }
117}
118