You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.4 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. #ifdef TEST
  2. #include "unity.h"
  3. #include "timequiz.h"
  4. // Test setup function
  5. void setUp(void) {
  6. // This function will be called before each test
  7. }
  8. // Test teardown function
  9. void tearDown(void) {
  10. // This function will be called after each test
  11. }
  12. void test_getRandomQuestionIndex(void) {
  13. int askedQuestions[10] = {0};
  14. int totalQuestions = 10;
  15. // Call the function to get a random question index
  16. int index = getRandomQuestionIndex(askedQuestions, totalQuestions);
  17. // Assert that the index is within the valid range
  18. TEST_ASSERT_TRUE(index >= 0 && index < totalQuestions);
  19. }
  20. void test_displayQuestion(void) {
  21. char* question = "Test Question";
  22. char* answers[] = {"A", "B", "C", "D"};
  23. int correctIndex = 0;
  24. printf("\nExpected Output:\n");
  25. printf("Question: %s\n", question);
  26. for (int i = 0; i < 4; i++) {
  27. printf("%d. %s\n", i + 1, answers[i]);
  28. }
  29. printf("\n");
  30. printf("Actual Output:\n");
  31. displayQuestion(question, answers, correctIndex);
  32. }
  33. void test_processUserAnswer_correct(void) {
  34. int score = 0;
  35. int totalCorrectAnswers = 0;
  36. char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
  37. int correctIndex = 0;
  38. int userAnswer = 1;
  39. // Call the function to process user answer
  40. processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
  41. // Assert that the score and total correct answers are updated correctly
  42. TEST_ASSERT_EQUAL_INT(1, score);
  43. TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
  44. }
  45. void test_processUserAnswer_wrong(void) {
  46. int score = 0;
  47. int totalCorrectAnswers = 0;
  48. char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
  49. int correctIndex = 0;
  50. int userAnswer = 2; // Assuming user selects the second option
  51. // Call the function to process user answer
  52. processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
  53. // Assert that the score remains unchanged and total correct answers is not incremented
  54. TEST_ASSERT_EQUAL_INT(0, score);
  55. TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
  56. }
  57. void test_processUserAnswer_correctAnswer_index3(void) {
  58. int score = 0;
  59. int totalCorrectAnswers = 0;
  60. char* answers[] = {"A", "B", "C", "D"};
  61. processUserAnswer(4, 3, &score, &totalCorrectAnswers, answers);
  62. TEST_ASSERT_EQUAL_INT(1, score);
  63. TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
  64. }
  65. #endif //TEST