// 3rd_party_lib:matrix4cj/target/matrix4cj
// 3rd_party_lib_ohos:matrix4cj/target/aarch64-linux-ohos/matrix4cj

package matrix4cj.tests.HLT.LEVEL2

import matrix4cj.*
import std.math.*
import std.unittest.*
import std.unittest.testmacro.*

@Test
public class TestMatrix3 {
    @TestCase
    func L2_Test_Matrix_minus_02(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        var matrix02 = Matrix([[1.80, 2.88], [-1.11, -0.66]])
        try {
            matrix01 - (matrix02)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            if (e.toString().contains("Matrix dimensions must agree.")) {
                @Assert(true)
            } else {
                @Assert(false)
            }
        }
    }
    @TestCase
    func L2_Test_Matrix_minusEquals_02(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        var matrix02 = Matrix([[1.80, 2.88], [-1.11, -0.66]])
        try {
            matrix01 -= (matrix02)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            if (e.toString().contains("Matrix dimensions must agree.")) {
                @Assert(true)
            } else {
                @Assert(false)
            }
        }
    }
    @TestCase
    func L2_Test_Matrix_plus_02(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        var matrix02 = Matrix([[1.80, 2.88], [-1.11, -0.66]])
        try {
            matrix01 + (matrix02)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            if (e.toString().contains("Matrix dimensions must agree.")) {
                @Assert(true)
            } else {
                @Assert(false)
            }
        }
    }
    @TestCase
    func L2_Test_Matrix_plusEquals_02(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        var matrix02 = Matrix([[1.80, 2.88], [-1.11, -0.66]])
        try {
            matrix01 += (matrix02)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            if (e.toString().contains("Matrix dimensions must agree.")) {
                @Assert(true)
            } else {
                @Assert(false)
            }
        }
    }
    @TestCase
    func L2_Test_Matrix_random_03(): Unit {
        var rows = -1
        var cols = 3
        try {
            Matrix.Random(rows, cols)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            @Assert(true)
        }
    }
    @TestCase
    func L2_Test_Matrix_random_04(): Unit {
        var rows = 1
        var cols = -1
        try {
            Matrix.Random(rows, cols)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            @Assert(true)
        }
    }
    @TestCase
    func L2_Test_Matrix_set_02(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        try {
            matrix01.set(-1, 2, 66.66)
            @Assert(false)
        } catch (e: IndexOutOfBoundsException) {
            @Assert(true)
        }
    }
    @TestCase
    func L2_Test_Matrix_set_03(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        try {
            matrix01.set(1, 4, 66.66)
            @Assert(false)
        } catch (e: IndexOutOfBoundsException) {
            @Assert(true)
        }
    }
    @TestCase
    func L2_Test_Matrix_setMatrix02_03(): Unit {
        var matrix02 = Matrix([[8.80, 8.88], [8.25, -8.95]])
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        try {
            matrix01[[0, 1], [2, 4]] = matrix02
            @Assert(false)
        } catch (e: IndexOutOfBoundsException) {
            @Assert(true)
        }
    }
    @TestCase
    func L2_Test_Matrix_setMatrix04_01(): Unit {
        var matrix02 = Matrix([[8.80, 8.88], [8.25, -8.95]])
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        matrix01[0..2, [2, 0]] = matrix02
        var expected_matrix = Matrix(
        [
            [8.88, 2.88, 8.80, -0.89],
            [-8.95, -2.95, 8.25, -3.80],
            [1.58, -2.69, -2.90, -1.04],
            [-1.11, -0.66, -0.59, 0.80]
        ])
        @Assert(matrix01.rowNum,expected_matrix.rowNum)
        @Assert(matrix01.colNum,expected_matrix.colNum)
        var rows = matrix01.rowNum
        var cols = matrix01.colNum
        for (i in 0..rows) {
            for (j in 0..cols) {
                @Assert(abs(expected_matrix.get(i,j)-matrix01.get(i,j))<1e-6,true)
            }
        }
    }
    @TestCase
    func L2_Test_Matrix_solve_01(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        var matrix02 = Matrix([[1.80, 2.88], [-1.11, -0.66]])
        try {
            matrix01.solve(matrix02)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            if (e.toString().contains("Matrix row dimensions must agree")) {
                @Assert(true)
            } else {
                @Assert(false)
            }
        }
    }
    @TestCase
    func L2_Test_Matrix_solveTranspose_01(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        var matrix02 = Matrix([[1.80, 2.88], [-1.11, -0.66]])
        try {
            matrix01.solveTranspose(matrix02)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            if (e.toString().contains("Matrix row dimensions must agree")) {
                @Assert(true)
            } else {
                @Assert(false)
            }
        }
    }
    @TestCase
    func L2_Test_Matrix_times02_02(): Unit {
        var matrix01 = Matrix(
            [
                [1.80, 2.88, 2.05, -0.89],
                [5.25, -2.95, -0.95, -3.80],
                [1.58, -2.69, -2.90, -1.04],
                [-1.11, -0.66, -0.59, 0.80]
            ]
        )
        var matrix02 = Matrix([[1.80, 2.88], [-1.11, -0.66]])
        try {
            matrix01 * (matrix02)
            @Assert(false)
        } catch (e: IllegalArgumentException) {
            if (e.toString().contains("Matrix inner dimensions must agree")) {
                @Assert(true)
            } else {
                @Assert(false)
            }
        }
    }
}