From 326be07b4afd32f4c945afc59be91486a0b614e2 Mon Sep 17 00:00:00 2001 From: "Ryan \"Lofenyy\" Medeiros" Date: Sun, 2 Jul 2023 05:55:47 -0600 Subject: [PATCH] Added code tests. --- Makefile | 3 ++ Source/editMaps.c | 4 +-- Source/engineCore.c | 2 ++ Source/engineMaps.c | 50 ++++++++++++++-------------- Tests/main.c | 79 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 0a2c531..4e70791 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,9 @@ editMaps: editMaps-debug: cd Build/;gcc ../Source/editMaps.c -lncurses -g3 -Og -Wall -fanalyzer -o librewands_editmaps +test: + cd Build/;gcc ../Tests/main.c -lncurses -g3 -Og -Wall -fanalyzer -o tests;./tests + format: clang-format -i Source/* clang-format -i Tests/* diff --git a/Source/editMaps.c b/Source/editMaps.c index eaf6c5e..430b943 100644 --- a/Source/editMaps.c +++ b/Source/editMaps.c @@ -342,7 +342,7 @@ int main() if (k == KEY_F(5)) { echo(); - //Get our save name + // Get our save name mvprintw(4, 0, "Save name: "); getnstr(buffer1, 8); @@ -351,7 +351,7 @@ int main() if (k == KEY_F(6)) { echo(); - //Get our load name + // Get our load name mvprintw(4, 0, "Load name: "); getnstr(buffer1, 8); diff --git a/Source/engineCore.c b/Source/engineCore.c index 50cb734..d32d7b6 100644 --- a/Source/engineCore.c +++ b/Source/engineCore.c @@ -14,6 +14,8 @@ // --- #include "engineMaps.h" +#include +#include // To run at the beginning of the game. void initEngine() diff --git a/Source/engineMaps.c b/Source/engineMaps.c index 565a440..4fdd61d 100644 --- a/Source/engineMaps.c +++ b/Source/engineMaps.c @@ -11,6 +11,31 @@ // You should have received a copy of the GNU General Public License along with LibreWands. If // not, see https://www.gnu.org/licenses/. +// Allocate memory for our internal map +int newMaps() + { + char buffer[1] = ""; + + // Allocate the needed memory + map = malloc(255 * sizeof(struct maps)); + + // On failure, report error. + if (map == NULL) + { + mvprintw(2, 0, "Unable to allocate memory."); + getnstr(buffer, 1); + return 1; + } + else + { + // Or else, record our new number of maps. + nMap = 255; + } + + // Exit gracefully + return 0; + } + // Add a new Base tile uint_fast8_t addBase(uint_fast16_t x, uint_fast16_t y, uint_fast8_t z, uint_fast8_t xmax, uint_fast8_t ymax, uint_fast8_t t, uint_fast8_t c, uint_fast8_t walk) @@ -207,31 +232,6 @@ int delTile(uint_fast16_t x, uint_fast16_t y, uint_fast8_t z, uint_fast8_t xmax, return 0; } -// Allocate memory for our internal map -int newMaps() - { - char buffer[1] = ""; - - // Allocate the needed memory - map = malloc(255 * sizeof(struct maps)); - - // On failure, report error. - if (map == NULL) - { - mvprintw(2, 0, "Unable to allocate memory."); - getnstr(buffer, 1); - return 1; - } - else - { - // Or else, record our new number of maps. - nMap = 255; - } - - // Exit gracefully - return 0; - } - // Save our internal world map as an XML document. int saveMaps(char filename[256]) { diff --git a/Tests/main.c b/Tests/main.c index 71ef684..4fcdcc8 100644 --- a/Tests/main.c +++ b/Tests/main.c @@ -10,3 +10,82 @@ // You should have received a copy of the GNU General Public License along with LibreWands. If // not, see https://www.gnu.org/licenses/. + +#include "../Source/engineCore.c" +#include +#include + +struct mapmonster mon[5]; + +// The test function +int test(int input, int value) + { + // If our input doesn't match the expected value + if (input != value) + { + // The test failed. + printf("Test failed.\n"); + exit(1); + } + else + { + // Unless it passed. + printf("Passed. \n"); + return 0; + } + } + +// Test our source code for unexpected output. +int main() + { + // Test allocating our map memory. + printf("newMaps(): "); + test(newMaps(), 0); + + // Test creating base tiles. + for (int C = 0; C < 512; C++) + { + printf("addBase(): "); + test(addBase(1, 1, 1, 1, 1, 1, 1, 1), 0); + } + + // Test creating info tiles. + for (int C = 0; C < 512; C++) + { + printf("addInfo(): "); + test(addInfo(1, 1, 1, 1, 1, 1, 1, "1"), 0); + } + + // Test creating item tiles. + for (int C = 0; C < 512; C++) + { + printf("addItem(): "); + test(addItem(1, 1, 1, 1, 1, 1, 1, 1), 0); + } + + // Test creating door tiles. + for (int C = 0; C < 512; C++) + { + printf("addDoor(): "); + test(addDoor(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 0); + } + + // Test creating char tiles. + for (int C = 0; C < 512; C++) + { + printf("addChar(): "); + test(addChar(1, 1, 1, 1, 1, 1, 1, 1, 1, "1", "1", "1", mon), 0); + } + + // Test deleting tiles. + printf("delTile(): "); + test(delTile(1, 1, 1, 1, 1), 0); + + // Test saving maps + printf("saveMaps(): "); + test(saveMaps("testfile"), 0); + + // Test saving maps + printf("loadMaps(): "); + test(loadMaps("testfile"), 0); + }