Browse Source

Unittest: test_1.c

main
Pascal Gutermuth 2 years ago
parent
commit
4509cf9c13
  1. 3
      .gitignore
  2. 5
      .vscode/settings.json
  3. 7
      build-project-windows-mingw.bat
  4. 7
      build-project.sh
  5. BIN
      quiz-game.zip
  6. 18
      src/functions.c
  7. 11
      src/functions.h
  8. 109
      src/main.c
  9. 20
      src/structs.h
  10. 7
      unittests/test_1.c
  11. 8
      unittests/test_main.c
  12. 8
      unittests/tests.h

3
.gitignore

@ -1 +1,2 @@
Quiz-Game.exe
quiz-game.exe
unittests-exec.exe

5
.vscode/settings.json

@ -0,0 +1,5 @@
{
"files.associations": {
"functions.h": "c"
}
}

7
build-project-windows-mingw.bat

@ -1,2 +1,5 @@
gcc src/main.c -o Quiz-Game
Quiz-Game.exe
gcc src/*.c -o quiz-game
gcc unittests/*.c src/functions.c -I src -o unittests-exec
unittests-exec.exe
quiz-game.exe

7
build-project.sh

@ -1,2 +1,5 @@
gcc src/main.c -o Quiz-Game
./Quiz-Game
gcc src/*.c -o quiz-game
gcc unittests/*.c src/functions.c -I src -o unittests-exec
./unittests-exec
./quiz-game

BIN
quiz-game.zip

18
src/functions.c

@ -0,0 +1,18 @@
#include "functions.h"
int resetInput(char* input)
{
memset(&input[0], 0, sizeof(input));
return 0;
}
int printEndMsg(int score, int questCount)
{
printf("Sie haben %d von %d Fragen richtig beantwortet.\n", score, questCount);
return 0;
}
int compareStrings(const char* input, const char* buf)
{
return strcmp(input, buf);
}

11
src/functions.h

@ -0,0 +1,11 @@
#pragma once
#include <stdio.h>
#include <string.h>
#include "structs.h"
int resetInput(char* input);
int printEndMsg(int score, int questCount);
int compareStrings(const char* input, const char* buf);
void startGame(int catCount, char* input, Category* categories);

109
src/main.c

@ -1,32 +1,56 @@
#include <stdio.h>
#include <string.h>
#include "structs.h"
#include "functions.h"
typedef struct
void startGame(int catCount, char* input, Category* categories)
{
const char* question;
char answer[256];
} Question;
int questCount = 0;
int curCatNum = 0;
typedef struct
{
int size;
Question questions[12];
} Level;
int level = 0;
int score = 0;
typedef struct
{
int size;
const char* name;
for (int i = 0; i < catCount; i++)
{
Level lvlQuestions[10];
} Category;
memcpy(lvlQuestions, categories[i].lvlQuestions, sizeof(categories[i].lvlQuestions));
int main()
{
int score = 0;
int level = 0;
printf("Kategorie %s\n", categories[i].name);
char input[256];
while (level < sizeof(lvlQuestions) / sizeof(Level))
{
for (int i = 0; i < lvlQuestions[level].size; i++)
{
printf(lvlQuestions[level].questions[i].question);
printf(" ");
fgets(input, 256, stdin);
input[strlen(input) - 1] = '\0';
const char* buf = lvlQuestions[level].questions[i].answer;
if (compareStrings(input, buf))
{
printf("Falsch. Die richtige Antwort ist: %s.\n", lvlQuestions[level].questions[i].answer);
}
else
{
printf("Richtig!\n");
score++;
}
resetInput(input);
questCount++;
}
printf("Level %d abgeschlossen!\n", level + 1);
level++;
}
level = 0;
}
// Ergebnis
printEndMsg(score, questCount);
}
int main()
{
Question fussQst1 = {.question = "Welche Fussballmannschaft wurde 1930 Weltmeister?", .answer = "Uruguay"};
Question fussQst2 = {.question = "Welche Fussballmannschaft wurde 1934 Weltmeister?", .answer = "Italien"};
Question fussQst3 = {.question = "Welche Fussballmannschaft wurde 1938 Weltmeister?", .answer = "Italien"};
@ -262,49 +286,10 @@ int main()
printf("Willkommen beim Quiz-Spiel! Beantworten Sie die folgenden Fragen:\n");
int questCount = 0;
int curCatNum = 0;
int catCount = 1; // anzahl an kategorien
char input[256];
for (int i = 0; i < catCount; i++)
{
Level lvlQuestions[10];
memcpy(lvlQuestions, categories[i].lvlQuestions, sizeof(categories[i].lvlQuestions));
printf("Kategorie %s\n", categories[i].name);
while (level < sizeof(lvlQuestions) / sizeof(Level))
{
for (int i = 0; i < lvlQuestions[level].size; i++)
{
printf(lvlQuestions[level].questions[i].question);
printf(" ");
fgets(input, 256, stdin);
input[strlen(input) - 1] = '\0';
const char* buf = lvlQuestions[level].questions[i].answer;
if (strcmp(input, buf))
{
printf("Falsch. Die richtige Antwort ist: %s.\n", lvlQuestions[level].questions[i].answer);
}
else
{
printf("Richtig!\n");
score++;
}
memset(&input[0], 0, sizeof(input));
questCount++;
}
printf("Level %d abgeschlossen!\n", level + 1);
level++;
}
level = 0;
}
// Ergebnis
printf("Sie haben %d von %d Fragen richtig beantwortet.\n", score, questCount);
startGame(catCount, input, categories);
return 0;
}

20
src/structs.h

@ -0,0 +1,20 @@
#pragma once
typedef struct
{
const char* question;
char answer[256];
} Question;
typedef struct
{
int size;
Question questions[12];
} Level;
typedef struct
{
int size;
const char* name;
Level lvlQuestions[10];
} Category;

7
unittests/test_1.c

@ -0,0 +1,7 @@
#include "tests.h"
void test_1()
{
assert(!printEndMsg(220, 1110));
printf("TEST 1 ERFOLGREICH\n");
}

8
unittests/test_main.c

@ -0,0 +1,8 @@
#include <stdio.h>
#include "tests.h"
int main()
{
test_1();
}

8
unittests/tests.h

@ -0,0 +1,8 @@
#pragma once
#include <assert.h>
#include <stdio.h>
#include "functions.h"
void test_1();
Loading…
Cancel
Save