Java |检查矩阵的界限

时间:2017-01-23 17:35:10

标签: java matrix

我尝试进行简单的检查,以确保客户端提供的行和列位于矩阵数组的边界内。但是我一直在

失败:junit.framework.AssertionFailedError

有关为何会发生这种情况的任何想法?

测试仪:

  public void testBoundsForGet() {  // m is a 4x3 matrix
        try {
            m.get(-1, 2);
            fail("get should not have succeeded");
        }
        catch(MatrixException ex) {
            assertTrue(ex.getMessage().equals("Row index (-1) out of bounds"));
        }

检查边界方法:

 protected void checkBounds(int row, int column) {
         if((this.numRows < row) || (row < 0)){
             throw new MatrixException(String.format("The row (%s) is out of range", row));
         }

         if((this.numColumns < column) || (column < 0)){
             throw new MatrixException(String.format("The column (%s) is out of range", column));
         }

    }

1 个答案:

答案 0 :(得分:-1)

我想要总共看到四个测试:每行一个,每行一个,小于最小值0,高于所讨论矩阵的最大值。我按如下方式编写这些测试:

@Test(expected = MatrixException.class)
public void testCheckBounds_NegativeRow() {  // m is a 4x3 matrix
    // You must have defined m somewhere else as a member variable.
    m.get(-1, 2);
}

@Test(expected = MatrixException.class)
public void testCheckBounds_LargerThanMaxRow() {  // m is a 4x3 matrix
    // You must have defined m somewhere else as a member variable.
    m.get(10, 2);
}

@Test(expected = MatrixException.class)
public void testCheckBounds_NegativeColumn() {  // m is a 4x3 matrix
    // You must have defined m somewhere else as a member variable.
    m.get(1, -2);
}

@Test(expected = MatrixException.class)
public void testCheckBounds_LargerThanMaxColumn() {  // m is a 4x3 matrix
    // You must have defined m somewhere else as a member variable.
    m.get(1, 20);
}

@Test
public void testCheckBounds_Success() {
    // what should you asset here?  It's not a useful method.  I'd return boolean
    m.get(1,1);
}