1/*
2 * Copyright (C) 2011 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.renderscript.Element.*;
23import android.renderscript.Element.DataKind.*;
24import android.renderscript.Element.DataType.*;
25
26public class UT_element extends UnitTest {
27    private Resources mRes;
28
29    Element simpleElem;
30    Element complexElem;
31
32    final String subElemNames[] = {
33        "subElem0",
34        "subElem1",
35        "subElem2",
36        "arrayElem0",
37        "arrayElem1",
38        "subElem3",
39        "subElem4",
40        "subElem5",
41        "subElem6",
42        "subElem_7",
43    };
44
45    final int subElemArraySizes[] = {
46        1,
47        1,
48        1,
49        2,
50        5,
51        1,
52        1,
53        1,
54        1,
55        1,
56    };
57
58    final int subElemOffsets[] = {
59        0,
60        4,
61        8,
62        12,
63        20,
64        40,
65        44,
66        48,
67        64,
68        80,
69    };
70
71    protected UT_element(RSTestCore rstc, Resources res, Context ctx) {
72        super(rstc, "Element", ctx);
73        mRes = res;
74    }
75
76    private void initializeGlobals(RenderScript RS, ScriptC_element s) {
77        simpleElem = Element.F32_3(RS);
78        complexElem = ScriptField_ComplexStruct.createElement(RS);
79        s.set_simpleElem(simpleElem);
80        s.set_complexElem(complexElem);
81
82        ScriptField_ComplexStruct data = new ScriptField_ComplexStruct(RS, 1);
83        s.bind_complexStruct(data);
84    }
85
86    private void testScriptSide(RenderScript pRS) {
87        ScriptC_element s = new ScriptC_element(pRS);
88        pRS.setMessageHandler(mRsMessage);
89        initializeGlobals(pRS, s);
90        s.invoke_element_test();
91        pRS.finish();
92        waitForMessage();
93    }
94
95    private void testJavaSide(RenderScript RS) {
96
97        int subElemCount = simpleElem.getSubElementCount();
98        _RS_ASSERT("subElemCount == 0", subElemCount == 0);
99        _RS_ASSERT("simpleElem.getDataKind() == USER",
100                   simpleElem.getDataKind() == DataKind.USER);
101        _RS_ASSERT("simpleElem.getDataType() == FLOAT_32",
102                   simpleElem.getDataType() == DataType.FLOAT_32);
103
104        subElemCount = complexElem.getSubElementCount();
105        _RS_ASSERT("subElemCount == 10", subElemCount == 10);
106        _RS_ASSERT("complexElem.getDataKind() == USER",
107                   complexElem.getDataKind() == DataKind.USER);
108        _RS_ASSERT("complexElemsimpleElem.getDataType() == NONE",
109                   complexElem.getDataType() == DataType.NONE);
110        _RS_ASSERT("complexElem.getSizeBytes() == ScriptField_ComplexStruct.Item.sizeof",
111                   complexElem.getBytesSize() == ScriptField_ComplexStruct.Item.sizeof);
112
113        for (int i = 0; i < subElemCount; i ++) {
114            _RS_ASSERT("complexElem.getSubElement(i) != null",
115                       complexElem.getSubElement(i) != null);
116            _RS_ASSERT("complexElem.getSubElementName(i).equals(subElemNames[i])",
117                       complexElem.getSubElementName(i).equals(subElemNames[i]));
118            _RS_ASSERT("complexElem.getSubElementArraySize(i) == subElemArraySizes[i]",
119                       complexElem.getSubElementArraySize(i) == subElemArraySizes[i]);
120            _RS_ASSERT("complexElem.getSubElementOffsetBytes(i) == subElemOffsets[i]",
121                       complexElem.getSubElementOffsetBytes(i) == subElemOffsets[i]);
122        }
123    }
124
125    public void run() {
126        RenderScript pRS = RenderScript.create(mCtx);
127        testScriptSide(pRS);
128        testJavaSide(pRS);
129        passTest();
130        pRS.destroy();
131    }
132}
133