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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import androidx.test.filters.SmallTest;
23 import androidx.test.runner.AndroidJUnit4;
24 
25 import com.android.inputmethod.keyboard.internal.MatrixUtils.MatrixOperationFailedException;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 @SmallTest
31 @RunWith(AndroidJUnit4.class)
32 public class MatrixUtilsTests {
33     // "run tests" -c com.android.inputmethod.keyboard.internal.MatrixUtilsTests
34     private static final boolean DEBUG = false;
35     private static final float EPSILON = 0.00001f;
36 
37     @Test
testMulti()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         assertEquals(retval[0][0], 19, EPSILON);
51         assertEquals(retval[0][1], 22, EPSILON);
52         assertEquals(retval[1][0], 43, EPSILON);
53         assertEquals(retval[1][1], 50, EPSILON);
54     }
55 
56     @Test
testInverse()57     public void testInverse() {
58         final int N = 4;
59         final float[][] matrix =
60                 {{1, 2, 3, 4}, {4, 0, 5, 6}, {6, 4, 2, 0}, {6, 4, 2, 1}};
61         final float[][] inverse = new float[N][N];
62         final float[][] tempMatrix = new float[N][N];
63         for (int i = 0; i < N; ++i) {
64             for (int j = 0; j < N; ++j) {
65                 tempMatrix[i][j] = matrix[i][j];
66             }
67         }
68         final float[][] retval = new float[N][N];
69         try {
70             MatrixUtils.inverse(tempMatrix, inverse);
71         } catch (MatrixOperationFailedException e) {
72             assertTrue(false);
73         }
74         try {
75             MatrixUtils.multiply(matrix, inverse, retval);
76         } catch (MatrixOperationFailedException e) {
77             assertTrue(false);
78         }
79         for (int i = 0; i < N; ++i) {
80             for (int j = 0; j < N; ++j) {
81                 assertEquals(((i == j) ? 1.0f : 0.0f), retval[i][j], EPSILON);
82             }
83         }
84     }
85 }
86