1 /* 2 * Copyright (C) 2013 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 17 package com.android.inputmethod.keyboard.internal; 18 19 import com.android.inputmethod.keyboard.internal.MatrixUtils.MatrixOperationFailedException; 20 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 @SmallTest 25 public class MatrixUtilsTests extends AndroidTestCase { 26 // "run tests" -c com.android.inputmethod.keyboard.internal.MatrixUtilsTests 27 private static final boolean DEBUG = false; 28 private static final float EPSILON = 0.00001f; 29 assertEqualsFloat(float f0, float f1)30 private static void assertEqualsFloat(float f0, float f1) { 31 assertEqualsFloat(f0, f1, EPSILON); 32 } 33 assertEqualsFloat(float f0, float f1, float error)34 /* package */ static void assertEqualsFloat(float f0, float f1, float error) { 35 assertTrue(Math.abs(f0 - f1) < error); 36 } 37 38 public void testMulti() { 39 final float[][] matrixA = {{1, 2}, {3, 4}}; 40 final float[][] matrixB = {{5, 6}, {7, 8}}; 41 final float[][] retval = new float[2][2]; 42 try { 43 MatrixUtils.multiply(matrixA, matrixB, retval); 44 } catch (MatrixOperationFailedException e) { 45 assertTrue(false); 46 } 47 if (DEBUG) { 48 MatrixUtils.dump("multi", retval); 49 } 50 assertEqualsFloat(retval[0][0], 19); 51 assertEqualsFloat(retval[0][1], 22); 52 assertEqualsFloat(retval[1][0], 43); 53 assertEqualsFloat(retval[1][1], 50); 54 } 55 56 public void testInverse() { 57 final int N = 4; 58 final float[][] matrix = 59 {{1, 2, 3, 4}, {4, 0, 5, 6}, {6, 4, 2, 0}, {6, 4, 2, 1}}; 60 final float[][] inverse = new float[N][N]; 61 final float[][] tempMatrix = new float[N][N]; 62 for (int i = 0; i < N; ++i) { 63 for (int j = 0; j < N; ++j) { 64 tempMatrix[i][j] = matrix[i][j]; 65 } 66 } 67 final float[][] retval = new float[N][N]; 68 try { 69 MatrixUtils.inverse(tempMatrix, inverse); 70 } catch (MatrixOperationFailedException e) { 71 assertTrue(false); 72 } 73 try { 74 MatrixUtils.multiply(matrix, inverse, retval); 75 } catch (MatrixOperationFailedException e) { 76 assertTrue(false); 77 } 78 for (int i = 0; i < N; ++i) { 79 for (int j = 0; j < N; ++j) { 80 assertEqualsFloat(((i == j) ? 1.0f : 0.0f), retval[i][j]); 81 } 82 } 83 } 84 } 85