JUnit测试用于制作Anagram的程序?

huangapple go评论77阅读模式
英文:

JUnit test for Anagram making program?

问题

public String reverseWords(String input) {
    String oneOrMoreSpaces = "\\s+";
    String[] words = input.split(oneOrMoreSpaces);
    String completeAnagram = "";

    for (int i = 0; i < words.length; i++) {
        char[] symbol = words[i].toCharArray();
        char newSymbolSequence;
        int j = symbol.length - 1, k = 0;

        while (k < j) {
            if (!Character.isAlphabetic(symbol[k]))
                k++;
            else if (!Character.isAlphabetic(symbol[j]))
                j--;
            else {
                newSymbolSequence = symbol[k];
                symbol[k] = symbol[j];
                symbol[j] = newSymbolSequence;
                k++;
                j--;
            }
        }
        completeAnagram = new String(symbol);
    }
    return completeAnagram;
}
private Anagram anagram = new Anagram();

@Test(expected = NullPointerException.class)
public void checkExpected_NullPointerException() {
    anagram.reverseWords(null);
}

@Test
public void reverseWords_shouldMakeAnagram_ofInputText() {
    String actual = anagram.reverseWords("qwe123rty");
    String expected = "ytr123ewq";
    assertEquals(expected, actual);
}

@Test
public void throwingTest() {
    String actual = anagram.reverseWords("qwe123rty");
    String expected = "qwe123rty";
    assertNotSame(expected, actual);
}

If you need additional @Test methods, you could consider adding tests for some edge cases and special scenarios:

@Test
public void reverseWords_emptyInput() {
    String actual = anagram.reverseWords("");
    String expected = "";
    assertEquals(expected, actual);
}

@Test
public void reverseWords_singleWord() {
    String actual = anagram.reverseWords("hello");
    String expected = "olleh";
    assertEquals(expected, actual);
}

@Test
public void reverseWords_multipleSpaces() {
    String actual = anagram.reverseWords("   multiple     spaces   ");
    String expected = "   elpitonum     secaps   ";
    assertEquals(expected, actual);
}

@Test
public void reverseWords_specialCharacters() {
    String actual = anagram.reverseWords("hello@world!");
    String expected = "dlrow@olleh!";
    assertEquals(expected, actual);
}

These additional test cases cover various scenarios and help ensure the robustness of your reverseWords method.

英文:

I have to write junit tests for this part of code: but I have to tests everything, means not only assertEquals(expected, actual), but also all exceptions which could be, all functionality.

public String reverseWords(String input) {
    String oneOrMoreSpaces = &quot;\\s+&quot;;
    String[] words = input.split(oneOrMoreSpaces);
    String completeAnagram = &quot;&quot;;

    for (int i = 0; i &lt; words.length; i++) {
        char[] symbol = words[i].toCharArray();
        char newSymbolSequence;
        int j = symbol.length - 1, k = 0;

        while (k &lt; j) {
            if (!Character.isAlphabetic(symbol[k]))
                k++;
            else if (!Character.isAlphabetic(symbol[j]))
                j--;
            else {
                newSymbolSequence = symbol[k];
                symbol[k] = symbol[j];
                symbol[j] = newSymbolSequence;
                k++;
                j--;
            }
        }
        completeAnagram = new String(symbol);
    }
    return completeAnagram;
}

This is what I've already made

private Anagram anagram = new Anagram();

@Test(expected = NullPointerException.class)
public void checkExpected_NullPointerException(){
    anagram.reverseWords(null);
}
@Test
public void reverseWords_shouldMakeAnagram_ofInputText(){
    String actual = anagram.reverseWords(&quot;qwe123rty&quot;);
    String expected = &quot;ytr123ewq&quot;;
    assertEquals(expected, actual);
}
@Test
public void throwingTest(){
    String actual = anagram.reverseWords(&quot;qwe123rty&quot;);
    String expected = &quot;qwe123rty&quot;;
    assertNotSame(expected, actual);
}

Can anyone help me with good @Test methods which really should be there. I have no idea what I can @Test else.

答案1

得分: 2

测试一些极限值:

    String actual = anagram.reverseWords("");
    String actual = anagram.reverseWords(" ");
    String actual = anagram.reverseWords("q");

你正在对单词进行一些逻辑操作,所以也要测试这个:

    String actual = anagram.reverseWords("more than a feeling");
    String actual = anagram.reverseWords("azerty querty");
    String actual = anagram.reverseWords("azerty ");
    String actual = anagram.reverseWords(" azerty ");

提示:我认为你会发现一些错误,因为`completeAnagram`每次被覆盖,只包含最后一个单词。
英文:

Test some limit values:

String actual = anagram.reverseWords(&quot;&quot;);
String actual = anagram.reverseWords(&quot; &quot;);
String actual = anagram.reverseWords(&quot;q&quot;);

You're doing some logic with words so test this too:

String actual = anagram.reverseWords(&quot;more than a feeling&quot;);
String actual = anagram.reverseWords(&quot;azerty querty&quot;);
String actual = anagram.reverseWords(&quot;azerty &quot;);
String actual = anagram.reverseWords(&quot; azerty &quot;);

spoiler: I think you will discover some bugs as completeAnagram is each time overwritten and only contains the last word.

huangapple
  • 本文由 发表于 2020年8月25日 23:23:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63582112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定