diff options
author | Stephen Hines <srhines@google.com> | 2012-05-30 13:44:53 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-05-30 13:44:53 -0700 |
commit | 8547d55861576de4a2287637458c12521e39a5f0 (patch) | |
tree | 4673e0b233a0fb194d1be339f02a770d8901f7f3 /tests | |
parent | e09e9b7092fb9a304f19f6be3986e3e54ec6a9ab (diff) | |
parent | 325ca45471862654a70948e09415277d8646c68b (diff) | |
download | frameworks_base-8547d55861576de4a2287637458c12521e39a5f0.zip frameworks_base-8547d55861576de4a2287637458c12521e39a5f0.tar.gz frameworks_base-8547d55861576de4a2287637458c12521e39a5f0.tar.bz2 |
Merge "Add math agreement test. float_distance and float_almost_agree added to header"
Diffstat (limited to 'tests')
4 files changed, 146 insertions, 0 deletions
diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java b/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java index d0f3e62..36f13b1 100644 --- a/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java +++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/RSTestCore.java @@ -84,6 +84,7 @@ public class RSTestCore { unitTests.add(new UT_struct(this, mRes, mCtx)); unitTests.add(new UT_math(this, mRes, mCtx)); unitTests.add(new UT_math_conformance(this, mRes, mCtx)); + unitTests.add(new UT_math_agree(this, mRes, mCtx)); unitTests.add(new UT_element(this, mRes, mCtx)); unitTests.add(new UT_sampler(this, mRes, mCtx)); unitTests.add(new UT_program_store(this, mRes, mCtx)); diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_math_agree.java b/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_math_agree.java new file mode 100644 index 0000000..32a4bd7 --- /dev/null +++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/UT_math_agree.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.rs.test; + +import android.content.Context; +import android.content.res.Resources; +import android.renderscript.*; +import java.util.Random; + +public class UT_math_agree extends UnitTest { + private Resources mRes; + + protected UT_math_agree(RSTestCore rstc, Resources res, Context ctx) { + super(rstc, "Math Agreement", ctx); + mRes = res; + } + + private void initializeValues(ScriptC_math_agree s) { + Random rand = new Random(); + + float x = rand.nextFloat(); + float y = rand.nextFloat(); + + s.set_x(x); + s.set_y(y); + s.set_result_add(x + y); + s.set_result_sub(x - y); + s.set_result_mul(x * y); + s.set_result_div(x / y); + } + + public void run() { + RenderScript pRS = RenderScript.create(mCtx); + ScriptC_math_agree s = new ScriptC_math_agree(pRS, mRes, + R.raw.math_agree); + pRS.setMessageHandler(mRsMessage); + initializeValues(s); + s.invoke_math_agree_test(); + pRS.finish(); + waitForMessage(); + pRS.destroy(); + } +} diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/math_agree.rs b/tests/RenderScriptTests/tests/src/com/android/rs/test/math_agree.rs new file mode 100644 index 0000000..953b9de --- /dev/null +++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/math_agree.rs @@ -0,0 +1,54 @@ +#include "shared.rsh" +//#pragma rs_fp_relaxed + +float x = 0.0f; +float y = 0.0f; +float result_add = 0.0f; +float result_sub = 0.0f; +float result_mul = 0.0f; +float result_div = 0.0f; + +#define TEST_OP(op, opName) \ +result = x op y; \ +if (! float_almost_equal(result, result_##opName)) { \ + rsDebug(#opName " did not match!", 0); \ + rsDebug("x = ", x); \ + rsDebug("y = ", y); \ + rsDebug("Result = ", result); \ + rsDebug("Expected = ", result_##opName); \ + rsDebug("Difference = ", result - result_##opName); \ + rsDebug("ULP Difference =", float_dist(result, result_##opName)); \ + failed = true; \ +} + +static bool test_math_agree() { + bool failed = false; + float result = 0.0; + + TEST_OP(+, add); + TEST_OP(-, sub); + TEST_OP(*, mul); + TEST_OP(/, div); + + if (failed) { + rsDebug("test_math_agree FAILED", 0); + } + else { + rsDebug("test_math_agree PASSED", 0); + } + + return failed; +} + +void math_agree_test() { + bool failed = false; + failed |= test_math_agree(); + + if (failed) { + rsSendToClientBlocking(RS_MSG_TEST_FAILED); + } + else { + rsSendToClientBlocking(RS_MSG_TEST_PASSED); + } +} + diff --git a/tests/RenderScriptTests/tests/src/com/android/rs/test/shared.rsh b/tests/RenderScriptTests/tests/src/com/android/rs/test/shared.rsh index 8cdf0d8..3adc999 100644 --- a/tests/RenderScriptTests/tests/src/com/android/rs/test/shared.rsh +++ b/tests/RenderScriptTests/tests/src/com/android/rs/test/shared.rsh @@ -74,6 +74,40 @@ static bool iszero(float f) { return isposzero(f) || isnegzero(f); } +/* Absolute epsilon used for floats. Value is similar to float.h. */ +#ifndef FLT_EPSILON +#define FLT_EPSILON 1.19e7f +#endif +/* Max ULPs while still being considered "equal". Only used when this number + of ULPs is of a greater size than FLT_EPSILON. */ +#define FLT_MAX_ULP 1 + +/* Calculate the difference in ULPs between the two values. (Return zero on + perfect equality.) */ +static int float_dist(float f1, float f2) { + return *((int *)(&f1)) - *((int *)(&f2)); +} + +/* Check if two floats are essentially equal. Will fail with some values + due to design. (Validate using FLT_EPSILON or similar if necessary.) */ +static bool float_almost_equal(float f1, float f2) { + int *i1 = (int*)(&f1); + int *i2 = (int*)(&f2); + + // Check for sign equality + if ( ((*i1 >> 31) == 0) != ((*i2 >> 31) == 0) ) { + // Handle signed zeroes + if (f1 == f2) + return true; + return false; + } + + // Check with ULP distance + if (float_dist(f1, f2) > FLT_MAX_ULP) + return false; + return true; +} + /* These constants must match those in UnitTest.java */ static const int RS_MSG_TEST_PASSED = 100; static const int RS_MSG_TEST_FAILED = 101; |