Browse Source

10. Unittest

remotes/origin/Pascal
Pascal Schneider 11 months ago
parent
commit
272c976ce0
  1. 30
      test/test_timequiz.c

30
test/test_timequiz.c

@ -3,24 +3,16 @@
#include "unity.h" #include "unity.h"
#include "timequiz.h" #include "timequiz.h"
// Test setup function
void setUp(void) { void setUp(void) {
// This function will be called before each test
} }
// Test teardown function
void tearDown(void) { void tearDown(void) {
// This function will be called after each test
} }
void test_getRandomQuestionIndex(void) { void test_getRandomQuestionIndex(void) {
int askedQuestions[10] = {0}; int askedQuestions[10] = {0};
int totalQuestions = 10; int totalQuestions = 10;
// Call the function to get a random question index
int index = getRandomQuestionIndex(askedQuestions, totalQuestions); int index = getRandomQuestionIndex(askedQuestions, totalQuestions);
// Assert that the index is within the valid range
TEST_ASSERT_TRUE(index >= 0 && index < totalQuestions); TEST_ASSERT_TRUE(index >= 0 && index < totalQuestions);
} }
@ -45,10 +37,7 @@ void test_processUserAnswer_correct(void) {
int correctIndex = 0; int correctIndex = 0;
int userAnswer = 1; int userAnswer = 1;
// Call the function to process user answer
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers); processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
// Assert that the score and total correct answers are updated correctly
TEST_ASSERT_EQUAL_INT(1, score); TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers); TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
} }
@ -59,11 +48,7 @@ void test_processUserAnswer_wrong(void) {
char* answers[] = {"Paris", "London", "Berlin", "Madrid"}; char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 0; int correctIndex = 0;
int userAnswer = 2; // Assuming user selects the second option int userAnswer = 2; // Assuming user selects the second option
// Call the function to process user answer
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers); processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
// Assert that the score remains unchanged and total correct answers is not incremented
TEST_ASSERT_EQUAL_INT(0, score); TEST_ASSERT_EQUAL_INT(0, score);
TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers); TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
} }
@ -114,13 +99,20 @@ void test_processUserAnswer_correctAnswer_index2(void) {
char* answers[] = {"Paris", "London", "Berlin", "Madrid"}; char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 2; int correctIndex = 2;
int userAnswer = 3; // Assuming user selects the third option int userAnswer = 3; // Assuming user selects the third option
// Call the function to process user answer
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers); processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
// Assert that the score and total correct answers are updated correctly
TEST_ASSERT_EQUAL_INT(1, score); TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers); TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
} }
void test_processUserAnswer_wrongAnswer_index1(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 2;
int userAnswer = 2; // Assuming user selects the second option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(0, score);
TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
}
#endif //TEST #endif //TEST
Loading…
Cancel
Save