diff options
author | Mathias Agopian <mathias@google.com> | 2013-08-31 01:30:10 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2013-08-31 01:30:10 +0000 |
commit | 9b5534b0e5e1510f56e6a2c58ad0816167603ebd (patch) | |
tree | 8d8e55c28871493a84fb1d25f830e8665c7af938 /libs | |
parent | 87967c186995d1500875c495cba8f04f7cff76cc (diff) | |
parent | a8c386f1c36e916c1df18d41a22104d655a89817 (diff) | |
download | frameworks_native-9b5534b0e5e1510f56e6a2c58ad0816167603ebd.zip frameworks_native-9b5534b0e5e1510f56e6a2c58ad0816167603ebd.tar.gz frameworks_native-9b5534b0e5e1510f56e6a2c58ad0816167603ebd.tar.bz2 |
Merge changes I8283a989,I64add89a into klp-dev
* changes:
switch to use mat4
vector and matrix classes for graphics use
Diffstat (limited to 'libs')
-rw-r--r-- | libs/ui/tests/Android.mk | 5 | ||||
-rw-r--r-- | libs/ui/tests/mat_test.cpp | 139 | ||||
-rw-r--r-- | libs/ui/tests/vec_test.cpp | 256 |
3 files changed, 399 insertions, 1 deletions
diff --git a/libs/ui/tests/Android.mk b/libs/ui/tests/Android.mk index 8b8e1d8..6f62a55 100644 --- a/libs/ui/tests/Android.mk +++ b/libs/ui/tests/Android.mk @@ -4,9 +4,12 @@ include $(CLEAR_VARS) # Build the unit tests. test_src_files := \ - Region_test.cpp + Region_test.cpp \ + vec_test.cpp \ + mat_test.cpp shared_libraries := \ + libutils \ libui static_libraries := \ diff --git a/libs/ui/tests/mat_test.cpp b/libs/ui/tests/mat_test.cpp new file mode 100644 index 0000000..a2c63ac --- /dev/null +++ b/libs/ui/tests/mat_test.cpp @@ -0,0 +1,139 @@ +/* + * Copyright 2013 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. + */ + +#define LOG_TAG "RegionTest" + +#include <stdlib.h> +#include <ui/Region.h> +#include <ui/Rect.h> +#include <gtest/gtest.h> + +#include <ui/mat4.h> + +namespace android { + +class MatTest : public testing::Test { +protected: +}; + +TEST_F(MatTest, Basics) { + mat4 m0; + EXPECT_EQ(sizeof(mat4), sizeof(float)*16); +} + +TEST_F(MatTest, ComparisonOps) { + mat4 m0; + mat4 m1(2); + + EXPECT_TRUE(m0 == m0); + EXPECT_TRUE(m0 != m1); + EXPECT_FALSE(m0 != m0); + EXPECT_FALSE(m0 == m1); +} + +TEST_F(MatTest, Constructors) { + mat4 m0; + ASSERT_EQ(m0[0].x, 1); + ASSERT_EQ(m0[0].y, 0); + ASSERT_EQ(m0[0].z, 0); + ASSERT_EQ(m0[0].w, 0); + ASSERT_EQ(m0[1].x, 0); + ASSERT_EQ(m0[1].y, 1); + ASSERT_EQ(m0[1].z, 0); + ASSERT_EQ(m0[1].w, 0); + ASSERT_EQ(m0[2].x, 0); + ASSERT_EQ(m0[2].y, 0); + ASSERT_EQ(m0[2].z, 1); + ASSERT_EQ(m0[2].w, 0); + ASSERT_EQ(m0[3].x, 0); + ASSERT_EQ(m0[3].y, 0); + ASSERT_EQ(m0[3].z, 0); + ASSERT_EQ(m0[3].w, 1); + + mat4 m1(2); + mat4 m2(vec4(2)); + mat4 m3(m2); + + EXPECT_EQ(m1, m2); + EXPECT_EQ(m2, m3); + EXPECT_EQ(m3, m1); + + mat4 m4(vec4(1), vec4(2), vec4(3), vec4(4)); +} + +TEST_F(MatTest, ArithmeticOps) { + mat4 m0; + mat4 m1(2); + mat4 m2(vec4(2)); + + m1 += m2; + EXPECT_EQ(mat4(4), m1); + + m2 -= m1; + EXPECT_EQ(mat4(-2), m2); + + m1 *= 2; + EXPECT_EQ(mat4(8), m1); + + m1 /= 2; + EXPECT_EQ(mat4(4), m1); + + m0 = -m0; + EXPECT_EQ(mat4(-1), m0); +} + +TEST_F(MatTest, UnaryOps) { + const mat4 identity; + mat4 m0; + + ++m0; + EXPECT_EQ(mat4( vec4(2,1,1,1), vec4(1,2,1,1), vec4(1,1,2,1), vec4(1,1,1,2) ), m0); + EXPECT_EQ(mat4( -vec4(2,1,1,1), -vec4(1,2,1,1), -vec4(1,1,2,1), -vec4(1,1,1,2) ), -m0); + + --m0; + EXPECT_EQ(identity, m0); +} + +TEST_F(MatTest, MiscOps) { + const mat4 identity; + mat4 m0; + EXPECT_EQ(4, trace(m0)); + + mat4 m1(vec4(1,2,3,4), vec4(5,6,7,8), vec4(9,10,11,12), vec4(13,14,15,16)); + mat4 m2(vec4(1,5,9,13), vec4(2,6,10,14), vec4(3,7,11,15), vec4(4,8,12,16)); + EXPECT_EQ(m1, transpose(m2)); + EXPECT_EQ(m2, transpose(m1)); + EXPECT_EQ(vec4(1,6,11,16), diag(m1)); + + EXPECT_EQ(identity, inverse(identity)); + + mat4 m3(vec4(4,3,0,0), vec4(3,2,0,0), vec4(0,0,1,0), vec4(0,0,0,1)); + mat4 m3i(inverse(m3)); + EXPECT_FLOAT_EQ(-2, m3i[0][0]); + EXPECT_FLOAT_EQ( 3, m3i[0][1]); + EXPECT_FLOAT_EQ( 3, m3i[1][0]); + EXPECT_FLOAT_EQ(-4, m3i[1][1]); + + mat4 m3ii(inverse(m3i)); + EXPECT_FLOAT_EQ(m3[0][0], m3ii[0][0]); + EXPECT_FLOAT_EQ(m3[0][1], m3ii[0][1]); + EXPECT_FLOAT_EQ(m3[1][0], m3ii[1][0]); + EXPECT_FLOAT_EQ(m3[1][1], m3ii[1][1]); + + EXPECT_EQ(m1, m1*identity); +} + +}; // namespace android diff --git a/libs/ui/tests/vec_test.cpp b/libs/ui/tests/vec_test.cpp new file mode 100644 index 0000000..00f737e --- /dev/null +++ b/libs/ui/tests/vec_test.cpp @@ -0,0 +1,256 @@ +/* + * Copyright 2013 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. + */ + +#define LOG_TAG "RegionTest" + +#include <stdlib.h> +#include <ui/Region.h> +#include <ui/Rect.h> +#include <gtest/gtest.h> + +#include <ui/vec4.h> + +namespace android { + +class VecTest : public testing::Test { +protected: +}; + +TEST_F(VecTest, Basics) { + vec4 v4; + vec3& v3(v4.xyz); + + EXPECT_EQ(sizeof(vec4), sizeof(float)*4); + EXPECT_EQ(sizeof(vec3), sizeof(float)*3); + EXPECT_EQ(sizeof(vec2), sizeof(float)*2); + EXPECT_EQ((void*)&v3, (void*)&v4); +} + +TEST_F(VecTest, Constructors) { + vec4 v0; + EXPECT_EQ(v0.x, 0); + EXPECT_EQ(v0.y, 0); + EXPECT_EQ(v0.z, 0); + EXPECT_EQ(v0.w, 0); + + vec4 v1(1); + EXPECT_EQ(v1.x, 1); + EXPECT_EQ(v1.y, 1); + EXPECT_EQ(v1.z, 1); + EXPECT_EQ(v1.w, 1); + + vec4 v2(1,2,3,4); + EXPECT_EQ(v2.x, 1); + EXPECT_EQ(v2.y, 2); + EXPECT_EQ(v2.z, 3); + EXPECT_EQ(v2.w, 4); + + vec4 v3(v2); + EXPECT_EQ(v3.x, 1); + EXPECT_EQ(v3.y, 2); + EXPECT_EQ(v3.z, 3); + EXPECT_EQ(v3.w, 4); + + vec4 v4(v3.xyz, 42); + EXPECT_EQ(v4.x, 1); + EXPECT_EQ(v4.y, 2); + EXPECT_EQ(v4.z, 3); + EXPECT_EQ(v4.w, 42); + + vec4 v5(vec3(v2.xy, 42), 24); + EXPECT_EQ(v5.x, 1); + EXPECT_EQ(v5.y, 2); + EXPECT_EQ(v5.z, 42); + EXPECT_EQ(v5.w, 24); + + tvec4<double> vd(2); + EXPECT_EQ(vd.x, 2); + EXPECT_EQ(vd.y, 2); + EXPECT_EQ(vd.z, 2); + EXPECT_EQ(vd.w, 2); +} + +TEST_F(VecTest, Access) { + vec4 v0(1,2,3,4); + v0.x = 10; + v0.y = 20; + v0.z = 30; + v0.w = 40; + EXPECT_EQ(v0.x, 10); + EXPECT_EQ(v0.y, 20); + EXPECT_EQ(v0.z, 30); + EXPECT_EQ(v0.w, 40); + + v0[0] = 100; + v0[1] = 200; + v0[2] = 300; + v0[3] = 400; + EXPECT_EQ(v0.x, 100); + EXPECT_EQ(v0.y, 200); + EXPECT_EQ(v0.z, 300); + EXPECT_EQ(v0.w, 400); + + v0.xyz = vec3(1,2,3); + EXPECT_EQ(v0.x, 1); + EXPECT_EQ(v0.y, 2); + EXPECT_EQ(v0.z, 3); + EXPECT_EQ(v0.w, 400); +} + +TEST_F(VecTest, UnaryOps) { + vec4 v0(1,2,3,4); + + v0 += 1; + EXPECT_EQ(v0.x, 2); + EXPECT_EQ(v0.y, 3); + EXPECT_EQ(v0.z, 4); + EXPECT_EQ(v0.w, 5); + + v0 -= 1; + EXPECT_EQ(v0.x, 1); + EXPECT_EQ(v0.y, 2); + EXPECT_EQ(v0.z, 3); + EXPECT_EQ(v0.w, 4); + + v0 *= 2; + EXPECT_EQ(v0.x, 2); + EXPECT_EQ(v0.y, 4); + EXPECT_EQ(v0.z, 6); + EXPECT_EQ(v0.w, 8); + + v0 /= 2; + EXPECT_EQ(v0.x, 1); + EXPECT_EQ(v0.y, 2); + EXPECT_EQ(v0.z, 3); + EXPECT_EQ(v0.w, 4); + + vec4 v1(10, 20, 30, 40); + + v0 += v1; + EXPECT_EQ(v0.x, 11); + EXPECT_EQ(v0.y, 22); + EXPECT_EQ(v0.z, 33); + EXPECT_EQ(v0.w, 44); + + v0 -= v1; + EXPECT_EQ(v0.x, 1); + EXPECT_EQ(v0.y, 2); + EXPECT_EQ(v0.z, 3); + EXPECT_EQ(v0.w, 4); + + v0 *= v1; + EXPECT_EQ(v0.x, 10); + EXPECT_EQ(v0.y, 40); + EXPECT_EQ(v0.z, 90); + EXPECT_EQ(v0.w, 160); + + v0 /= v1; + EXPECT_EQ(v0.x, 1); + EXPECT_EQ(v0.y, 2); + EXPECT_EQ(v0.z, 3); + EXPECT_EQ(v0.w, 4); + + ++v0; + EXPECT_EQ(v0.x, 2); + EXPECT_EQ(v0.y, 3); + EXPECT_EQ(v0.z, 4); + EXPECT_EQ(v0.w, 5); + + ++++v0; + EXPECT_EQ(v0.x, 4); + EXPECT_EQ(v0.y, 5); + EXPECT_EQ(v0.z, 6); + EXPECT_EQ(v0.w, 7); + + --v1; + EXPECT_EQ(v1.x, 9); + EXPECT_EQ(v1.y, 19); + EXPECT_EQ(v1.z, 29); + EXPECT_EQ(v1.w, 39); + + v1 = -v1; + EXPECT_EQ(v1.x, -9); + EXPECT_EQ(v1.y, -19); + EXPECT_EQ(v1.z, -29); + EXPECT_EQ(v1.w, -39); + + tvec4<double> dv(1,2,3,4); + v1 += dv; + EXPECT_EQ(v1.x, -8); + EXPECT_EQ(v1.y, -17); + EXPECT_EQ(v1.z, -26); + EXPECT_EQ(v1.w, -35); +} + +TEST_F(VecTest, ComparisonOps) { + vec4 v0(1,2,3,4); + vec4 v1(10,20,30,40); + + EXPECT_TRUE(v0 == v0); + EXPECT_TRUE(v0 != v1); + EXPECT_FALSE(v0 != v0); + EXPECT_FALSE(v0 == v1); +} + +TEST_F(VecTest, ArithmeticOps) { + vec4 v0(1,2,3,4); + vec4 v1(10,20,30,40); + + vec4 v2(v0 + v1); + EXPECT_EQ(v2.x, 11); + EXPECT_EQ(v2.y, 22); + EXPECT_EQ(v2.z, 33); + EXPECT_EQ(v2.w, 44); + + v0 = v1 * 2; + EXPECT_EQ(v0.x, 20); + EXPECT_EQ(v0.y, 40); + EXPECT_EQ(v0.z, 60); + EXPECT_EQ(v0.w, 80); + + v0 = 2 * v1; + EXPECT_EQ(v0.x, 20); + EXPECT_EQ(v0.y, 40); + EXPECT_EQ(v0.z, 60); + EXPECT_EQ(v0.w, 80); + + tvec4<double> vd(2); + v0 = v1 * vd; + EXPECT_EQ(v0.x, 20); + EXPECT_EQ(v0.y, 40); + EXPECT_EQ(v0.z, 60); + EXPECT_EQ(v0.w, 80); +} + +TEST_F(VecTest, ArithmeticFunc) { + vec3 east(1, 0, 0); + vec3 north(0, 1, 0); + vec3 up( cross(east, north) ); + EXPECT_EQ(up, vec3(0,0,1)); + EXPECT_EQ(dot(east, north), 0); + EXPECT_EQ(length(east), 1); + EXPECT_EQ(distance(east, north), sqrtf(2)); + + vec3 v0(1,2,3); + vec3 vn(normalize(v0)); + EXPECT_FLOAT_EQ(1, length(vn)); + EXPECT_FLOAT_EQ(length(v0), dot(v0, vn)); + + tvec3<double> vd(east); + EXPECT_EQ(length(vd), 1); +} + +}; // namespace android |