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.

112 lines
3.5 KiB

11 months ago
11 months ago
11 months ago
11 months ago
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. void test_processUserAnswer_correctAnswer_index0(void) {
  66. int score = 0;
  67. int totalCorrectAnswers = 0;
  68. char* answers[] = {"A", "B", "C", "D"};
  69. processUserAnswer(1, 0, &score, &totalCorrectAnswers, answers);
  70. TEST_ASSERT_EQUAL_INT(1, score);
  71. TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
  72. }
  73. void test_processUserAnswer_wrongAnswer_index3(void) {
  74. int score = 0;
  75. int totalCorrectAnswers = 0;
  76. char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
  77. int correctIndex = 0;
  78. int userAnswer = 4; // Assuming user selects the fourth option
  79. processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
  80. TEST_ASSERT_EQUAL_INT(0, score);
  81. TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
  82. }
  83. void test_processUserAnswer_correctAnswer_index1(void) {
  84. int score = 0;
  85. int totalCorrectAnswers = 0;
  86. char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
  87. int correctIndex = 1;
  88. int userAnswer = 2; // Assuming user selects the second option
  89. processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
  90. TEST_ASSERT_EQUAL_INT(1, score);
  91. TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
  92. }
  93. #endif //TEST