[英]How to write JUnit test case for finding first non-repeating character in stream?
由于用户更多地关注较小的漏洞而不是要求,因此,我给出了需要junit测试用例的实际工作代码(替换)。
import java.util.*;
public class FirstNonRepeatingCharacterStream {
List<Character> chars = new ArrayList<>();
boolean[] repeated = new boolean[256];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
FirstNonRepeatingCharacterStream tester =
new FirstNonRepeatingCharacterStream();
while (true) {
Character ch = new Character(sc.next().charAt(0));
Character output = tester.firstNonRepeatingCharStream1(ch);
System.out.println(output);
}
}
public Character firstNonRepeatingCharStream1(Character x) {
if (x == null) {
return x;
}
Character output = null;
if (!repeated[x]) {
if (!chars.contains(x)) {
chars.add(x);
} else {
chars.remove(new Character(x));
repeated[x] = true;
}
}
if (chars.size() != 0) {
output = new Character(chars.get(0));
}
return output;
}
}
User enters one character at a time.
input a -> output a
input b -> that means input ab as it's stream -> output a
input a -> that means input aba as it's stream -> output b
input c -> that means input abac as it's stream -> output b
input b -> that means input abacb as it's stream -> output c
input a -> that means input abacba as it's stream -> output c
input d -> that means input abacbad as it's stream -> output c
请让我知道如何编写应符合主要方法的单元测试。 在junit测试用例中不需要while循环。
提前致谢。
听起来这可能主要归结为提出“平均”测试字符串以尝试击中代码中的各种边缘情况:
String testStrings[] {
null,
"", // empty string
"a", // easiest possible string with a match
"aa", // easiest possible string with no match
"aba", // slightly less easy string with a match
"aaaaa", // no match on N instances of a character
"aaaaab", // match on end of N instances of a character
"baaaaa", // match at beginning of N instances of a character
"aabaaa", // match in the middle of N instances of a character
"abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba", // harder string where the unique letter is in the middle (z)
"abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcb", // harder string where the unique character is at the front (a)
"bcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba", // harder string where the unique character is at the back
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy" // other harder string... etc.
};
然后,您将开发出类似的预期输出数组,然后在单个for(int i = 0;...)
循环中浏览所有案例。
开发测试输入的另一种方法是通过算法生成测试字符串,以使所有字符以三种策略(aabbcc,abccba,abcabc)之一成对出现,然后拨出第一个字符,然后是最后一个字符,然后是一个中间(您先检查要删除的字符,然后将其作为测试值)。
有很多不同的方法可以给这只猫蒙皮。
PS:您当前的代码将以null
或任何“整数”值大于255的字符中断。在那里出现了许多怪异的Unicode字符...我似乎还记得,听说现在有几种null
和虚构的语言都使用Unicode(古埃及语) ,JJR Tolkien的Elvish,Klingon等),更不用说CJKV范围了(中文,日文,韩文,越南文)。 超过256个的“代码点”很多。
我自己解决了。
class FirstNonRepeatingCharacterStreamTest3 {
private static FirstNonRepeatingCharacterStream tester;
@BeforeAll
static void setUpBeforeClass() throws Exception {
tester = new FirstNonRepeatingCharacterStream();
}
@DisplayName("MyTest")
@ParameterizedTest(name = "{index} => input={0}, output= {1}")
@CsvSource({
"'a', 'a'",
"'b', 'a'",
"'a', 'b'",
"'c', 'b'",
"'b', 'c'",
"'a', 'c'",
"'d', 'c'"
})
public void testFirstNonRepeatingCharStream101(Character input, Character output) {
Character actual = tester.firstNonRepeatingCharStream1(input);
assertEquals(output, actual);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.