From 4509cf9c13f908bd29c2b477a71cb893eebc35f8 Mon Sep 17 00:00:00 2001 From: Pascal Gutermuth Date: Tue, 31 Jan 2023 19:46:59 +0100 Subject: [PATCH] Unittest: test_1.c --- .gitignore | 3 +- .vscode/settings.json | 5 ++ build-project-windows-mingw.bat | 7 +- build-project.sh | 7 +- quiz-game.zip | Bin 4635 -> 0 bytes src/functions.c | 18 ++++++ src/functions.h | 11 ++++ src/main.c | 111 ++++++++++++++------------------ src/structs.h | 20 ++++++ unittests/test_1.c | 7 ++ unittests/test_main.c | 8 +++ unittests/tests.h | 8 +++ 12 files changed, 137 insertions(+), 68 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 quiz-game.zip create mode 100644 src/functions.c create mode 100644 src/functions.h create mode 100644 src/structs.h create mode 100644 unittests/test_1.c create mode 100644 unittests/test_main.c create mode 100644 unittests/tests.h diff --git a/.gitignore b/.gitignore index 9b3702a..258070d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -Quiz-Game.exe \ No newline at end of file +quiz-game.exe +unittests-exec.exe \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e554b2a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "functions.h": "c" + } +} \ No newline at end of file diff --git a/build-project-windows-mingw.bat b/build-project-windows-mingw.bat index c177ca1..79b0b2d 100644 --- a/build-project-windows-mingw.bat +++ b/build-project-windows-mingw.bat @@ -1,2 +1,5 @@ -gcc src/main.c -o Quiz-Game -Quiz-Game.exe \ No newline at end of file +gcc src/*.c -o quiz-game +gcc unittests/*.c src/functions.c -I src -o unittests-exec + +unittests-exec.exe +quiz-game.exe \ No newline at end of file diff --git a/build-project.sh b/build-project.sh index 52a949f..496d02c 100644 --- a/build-project.sh +++ b/build-project.sh @@ -1,2 +1,5 @@ -gcc src/main.c -o Quiz-Game -./Quiz-Game \ No newline at end of file +gcc src/*.c -o quiz-game +gcc unittests/*.c src/functions.c -I src -o unittests-exec + +./unittests-exec +./quiz-game \ No newline at end of file diff --git a/quiz-game.zip b/quiz-game.zip deleted file mode 100644 index 51b69124293ec63ef87c31dde80c782589e78192..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4635 zcmaKw2RK{ZAIGCbQKLqyh|!{UYKz!=t5H$2Z;TRRMU0|K?AjDXl^V5o&Du(BX>F=j zYqYgDt^Skt)z|Zm|IKsmJ?GqWKi}W^opYb$8y$`F_#^<_u@@%|y>G-iM~1EA3a%Gq z14qGa9G#HXx^8eUJ{6dQHOSi2S_k_O4fZww5c5I``m^%q2_Jj4fIHsAjrfHnh?jrN7I7Ty6gkmjrb4H~6pEm$Rjy_f=2k zPNBqDsGgFXqLvcK!HSi|1(y>WY);5Xl;Dmm)^NvrFiTr&*Pr3KI>W6IATpA_a`paS za)q1b=?Ef#Pm~OF5Mf)LNeTcw*8jBxePo0!Hl<@I=&V$i!3L_0444F_KUMjvsANU!YPD@5gF-2 z2;~Kv-n;2O1~Q2lTV0^fmVtZCbs<3&Xa4kix0;UDyA6p>v=%t~F{rS3W=ylVnh?Fp zV7PvVTwvBtB3t^?*kd|!nG6}RG1Z=I-C#YhTRq4ElY*_GTh;oB?sAT*8M?RA90J>S z=PIt!yvKitya$@= zbF?2Ql2c7kI18UqW#5y|uUTD`a=fsC$fI-%vG6+BaPfZ1N%B3>$Hz03k>Rs%T2-ht zlkn484~faqmsE5~a{7f(sT?}Zd!Bz>tL3~%tR)op%^jmK*mGo2^CRJ7IBP7X{eZy_Lv@_Khl{p&)VJ){_HNm?fOLxt95@nh96?x1?jJ=5>nT3}VMNL#>O3+F}wV#*Y7?jzK2LivBeZCWAd6gEJ zX8ZE>C%?lAe%j~JNz3Q^RWqhhX{)w75c!&*xl3lU(+Grr6~IVQ`8+PLMbLDaiGgGa@l3s6}+~Y*aOo_1d?9>lJ{G`rVJX*E)e&#mN>k`p%Wn9 zy{3j@j1L9@E=K!ir2Y@BU2s7NoQ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}g7B2}Kz4%vmV z>yK$Av@&w-3ki*>eY6nSJ+GsIcTVP~3;s86%!Zv1?qK#2IkZio@wxEcLW~HsXKU z?o%o*VnZDD|C{i$f?X$j`M9v<7aY8B(+JEc4(s|ojsG&>X9b)7E{L_`XB8s$r*j3c z#1T`(PX%!~J4)uiDg4uopH-m7?y}$B{4Dx1_l!9m7ImA%@hAGUeRd80Y0Xd3xHW)b z(Ks7^mg=Wy9fey1ik;vI+#19%=GWj<{-XYyoj;|rV5tFrQh)u+ah83oxS)qibDO-= z&xlX8&a(gcf}Le+k^g?(&Vns%0-A6^o`N?Br1O6TO+bi^2tb7Wd^raI&{N=A{{x>X Bz +#include + +#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); \ No newline at end of file diff --git a/src/main.c b/src/main.c index cfd172a..4484d73 100644 --- a/src/main.c +++ b/src/main.c @@ -1,32 +1,56 @@ -#include -#include +#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; - Level lvlQuestions[10]; -} Category; + for (int i = 0; i < catCount; i++) + { + Level lvlQuestions[10]; + 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; } \ No newline at end of file diff --git a/src/structs.h b/src/structs.h new file mode 100644 index 0000000..729cfd1 --- /dev/null +++ b/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; \ No newline at end of file diff --git a/unittests/test_1.c b/unittests/test_1.c new file mode 100644 index 0000000..6951562 --- /dev/null +++ b/unittests/test_1.c @@ -0,0 +1,7 @@ +#include "tests.h" + +void test_1() +{ + assert(!printEndMsg(220, 1110)); + printf("TEST 1 ERFOLGREICH\n"); +} \ No newline at end of file diff --git a/unittests/test_main.c b/unittests/test_main.c new file mode 100644 index 0000000..5048a8c --- /dev/null +++ b/unittests/test_main.c @@ -0,0 +1,8 @@ +#include + +#include "tests.h" + +int main() +{ + test_1(); +} \ No newline at end of file diff --git a/unittests/tests.h b/unittests/tests.h new file mode 100644 index 0000000..3604ea9 --- /dev/null +++ b/unittests/tests.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include + +#include "functions.h" + +void test_1(); \ No newline at end of file