简体   繁体   English

Junit测试是否还可以?

[英]Does this Junit test even work?

The Task is to shift Elements in an array starting with an index equal or bigger than int i, one step to right. 任务是从索引等于或大于int i的索引开始向右移动一步,将元素移动到数组中。 Sorry, normally this is easy to google but I have a Junit test which condition I need to meet. 抱歉,通常这很容易用Google进行搜索,但是我有一个Junit测试需要满足哪些条件。

It should meet the requirement of this JUnittest: 它应该满足此JUnittest的要求:

@Test
public void testShiftElements() {
    String[] a = { "a", "b", "c", "d", "e" };
    String[] b = a.clone();
    int i = 1;
    Arrays.shiftElements(a, i);
    for (int j = 0; j < i; j++) {
        assertEquals(b[j], a[j]);
    }
    for (int j = i; j < a.length - 1; j++) {
        assertEquals(b[j], a[j + 1]);
    }
}

Does the JUnistest even work, because it seems the test requires that eg "a" should equal "b", "b" should equal "c" . JUnistest甚至不起作用,因为似乎测试要求例如“ a”应该等于“ b”,“ b”应该等于“ c”。 Which is impossible. 这是不可能的。 Here is my code, which shifts. 这是我的代码,该代码会发生变化。

public static String[] shiftElements(String a[], int i) {

    String[] b = new String[a.length + i];

    for (int j = 0; j < a.length; j++) {
        b[j] = a[j];
    }

    for (int d = i; d < b.length - 1; d++) {
        for (int c = i; c < b.length - 1; c++) {
            String temp = b[c + 1];
            b[c + 1] = b[c];
            b[c] = temp;
        }
    }
    return b;
}

The test was correct had to do it like this. 测试是正确的,必须这样做。 Just without a return, but with void. 只是没有回报,而是虚无。

public static void shiftElements(String[] a, int i) {
    for (int j = a.length -1; j > i; j--) {
        if (j >= i) {
            a[j] = a[j - i];
        }

    }

}

or like this, if I dont want to delete the last String. 或类似的,如果我不想删除最后一个字符串。

public static void shiftElements(String a[], int i) {
    for (int d = i; d < a.length - 1; d++) {
        for (int c = i; c < a.length - 1; c++) {
            String temp = a[c + 1];
            a[c + 1] = a[c];
            a[c] = temp;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM