条件判定覆盖测试用例设计 - C语言单词猜谜游戏
条件判定覆盖测试用例设计 - C语言单词猜谜游戏
修正条件判定覆盖要求每个条件的真假至少被覆盖一次,且所有条件组合的结果至少被覆盖一次。对于以下C语言单词猜谜游戏函数 Word(int n, char *word),我们可以识别出以下条件:
number != 1number == 1word[a] == alphabetword[a] != alphabet && a == n - 1strcmp(initialword, word) == 0strcmp(initialword, word) != 0
void Word(int n, char *word) {
void *cat;
cat = malloc((sizeof(char) * n) + 1);
char *initialword = cat;
startWord(initialword, n);
int number=8,a=0, num=0;
char alphabet;
while (number!=0&&strcmp(initialword,word)!=0) {
printf("The word now looks like this: %s\n", initialword);
if (number!= 1) {
printf("You have %d guesses left.\n", number);
} else {
printf("You have only one guess left.\n");
}
alphabet = LetterFromUser();
for (a= 0;word[a]!= '\0'; a++) {
if (word[a] == alphabet) {
initialword[a] = alphabet;
num = a;
number++;
} else if (a == n - 1) {
number--;
}
}
if (word[num]== alphabet) {
printf("That guess is correct.\n");
} else {
printf("That are no %c's in the word.\n", alphabet);
}
}
if (strcmp(initialword, word)== 0) {
printf("You guessed the word: %s\n", word);
printf("You win.\n");
} else {
printf("You lose.\n");
}
}
根据上述条件,我们可以设计以下测试用例:
| n | word | 输入字母 | 预期输出 | | ---- | ------------ | -------- | ------------------------------------------------------------ | | 5 | 'hello' | 'h' | "That guess is correct." | | 5 | 'hello' | 'a' | "That are no a's in the word." | | 5 | 'hello' | 'o' | "The word now looks like this: _ _ _ _ o" | | 5 | 'hello' | 'l' | "The word now looks like this: _ _ l l _" | | 5 | 'hello' | 'x' | "You have 7 guesses left.\nThat are no x's in the word." | | 5 | 'hello' | 'o' | "That guess is correct.\nThe word now looks like this: _ _ _ _ o" | | 5 | 'hello' | 'h' | "You have 6 guesses left.\nThat are no h's in the word." | | 5 | 'hello' | 'e' | "The word now looks like this: h e _ _ _" | | 5 | 'hello' | 'l' | "The word now looks like this: h e l l _" | | 5 | 'hello' | 'o' | "The word now looks like this: h e l l o\nYou guessed the word: hello\nYou win." | | 5 | 'world' | 'a' | "You have 8 guesses left.\nThat are no a's in the word." | | 5 | 'world' | 'o' | "The word now looks like this: _ o _ _ _" | | 5 | 'world' | 'r' | "The word now looks like this: _ o r _ _" | | 5 | 'world' | 'd' | "The word now looks like this: _ o r d _" | | 5 | 'world' | 'w' | "The word now looks like this: w o r _ _" | | 5 | 'world' | 'l' | "You have 7 guesses left.\nThat are no l's in the word." | | 5 | 'world' | 'o' | "You have 6 guesses left.\nThat are no o's in the word." | | 5 | 'world' | 'r' | "You have 5 guesses left.\nThat guess is correct.\nThe word now looks like this: _ o r _ _" | | 5 | 'world' | 'l' | "You have 4 guesses left.\nThat are no l's in the word." | | 5 | 'world' | 'd' | "You have 3 guesses left.\nThat guess is correct.\nThe word now looks like this: _ o r d _" | | 5 | 'world' | 'w' | "You have 2 guesses left.\nThat guess is correct.\nThe word now looks like this: w o r _ _" | | 5 | 'world' | 'z' | "You have 1 guess left.\nThat are no z's in the word." | | 5 | 'world' | 'x' | "You have 0 guesses left.\nYou lose." | | 5 | 'hockey' | 'h' | "The word now looks like this: h _ _ _ _ _" | | 5 | 'hockey' | 'o' | "The word now looks like this: h o _ _ _ _" | | 5 | 'hockey' | 'k' | "The word now looks like this: h o _ k _ _" | | 5 | 'hockey' | 'e' | "You have 7 guesses left.\nThat are no e's in the word." | | 5 | 'hockey' | 'y' | "The word now looks like this: h o _ k e y\nYou guessed the word: hockey\nYou win." | | 10 | 'hello world'| 'h' | "That guess is correct.\nThe word now looks like this: h _ _ _ _ _ _ _ _ _" | | 10 | 'hello world'| 'a' | "You have 9 guesses left.\nThat are no a's in the word." | | 10 | 'hello world'| 'o' | "The word now looks like this: h _ _ _ o _ _ _ _ _" | | 10 | 'hello world'| 'l' | "The word now looks like this: h _ l l o _ _ _ _ _" | | 10 | 'hello world'| 'x' | "You have 7 guesses left.\nThat are no x's in the word." | | 10 | 'hello world'| 'o' | "That guess is correct.\nThe word now looks like this: h _ l l o _ _ _ o _" | | 10 | 'hello world'| 'h' | "You have 6 guesses left.\nThat are no h's in the word." | | 10 | 'hello world'| 'e' | "The word now looks like this: h e l l o _ _ _ o _" | | 10 | 'hello world'| 'w' | "You have 5 guesses left.\nThat are no w's in the word." | | 10 | 'hello world'| 'r' | "You have 4 guesses left.\nThat are no r's in the word." | | 10 | 'hello world'| 'd' | "You have 3 guesses left.\nThat are no d's in the word." | | 10 | 'hello world'| 'y' | "The word now looks like this: h e l l o _ _ _ o _ y\nYou guessed the word: hello world\nYou win." |
每个测试用例都覆盖了至少一个条件组合。通过这些测试用例,我们可以确保函数的代码覆盖率,并提高代码质量。
原文地址: https://www.cveoy.top/t/topic/owFI 著作权归作者所有。请勿转载和采集!