From e26d3268531405e83a2a4ba023c371e72983a457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos?= Date: Fri, 20 Jan 2023 12:06:53 +0100 Subject: [PATCH] Function to get random nums in range --- src/c/encounter.c | 7 +++++++ src/c/encounter.h | 2 ++ src/c/helper.c | 2 +- src/c/helper.h | 3 ++- test/c/test_encounter.c | 13 +++++++++++++ test/c/test_helper.c | 23 +++++++++++++++++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/c/test_helper.c diff --git a/src/c/encounter.c b/src/c/encounter.c index 4937d79..c8d441a 100644 --- a/src/c/encounter.c +++ b/src/c/encounter.c @@ -5,6 +5,7 @@ #include "encounter.h" #include "playerinput.h" +#include "helper.h" /*Gegner mit AC, damagedealt = damage-AC, kann nicht kleiner 1 sein evtl. lair bonus der dem gegner ein wenig mehr/weniger damage erlaubt @@ -93,6 +94,12 @@ int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enem } } +int randomIntRange(int min, int max) +{ + int value = randomInt(); + return (value % (max - min + 1)) + min; +} + // Getter/Setter Funktionen void setEnemyHealth(enemy* enemy, int newhealth) diff --git a/src/c/encounter.h b/src/c/encounter.h index 154a91c..c92a649 100644 --- a/src/c/encounter.h +++ b/src/c/encounter.h @@ -18,6 +18,8 @@ int switchTurns(int currentTurn); int fight(int playerH, int playerDamage, int playerArmor, int playerAttack, enemy* enemy); + +int randomIntRange(int min, int max); //Getter/Setter Funktionen //setEnemyHealth(&enemy, health); diff --git a/src/c/helper.c b/src/c/helper.c index 7f94482..7b3381d 100644 --- a/src/c/helper.c +++ b/src/c/helper.c @@ -3,4 +3,4 @@ int randomInt() { return rand(); -} +} \ No newline at end of file diff --git a/src/c/helper.h b/src/c/helper.h index d7ceb04..c1a5799 100644 --- a/src/c/helper.h +++ b/src/c/helper.h @@ -2,6 +2,7 @@ #define HELPER_H //erlaubt es z.b. rand() zu mocken -int randomInt; +int randomInt(); + #endif \ No newline at end of file diff --git a/test/c/test_encounter.c b/test/c/test_encounter.c index 34c5412..2595ad6 100644 --- a/test/c/test_encounter.c +++ b/test/c/test_encounter.c @@ -4,9 +4,13 @@ #include "encounter.h" #include "playerinput.h" #include "mock_playerinput.h" +#include "helper.h" +#include "mock_helper.h" + void setUp(void) { + } void teardown(void) @@ -251,4 +255,13 @@ void test_FightPlayerFlees(void) TEST_ASSERT_EQUAL(2, result); } +void test_randomIntRange(void) +{ + int expected = 4; + randomInt_ExpectAndReturn(15); + int result = randomIntRange(1, 6); + TEST_ASSERT_EQUAL_INT(expected, result); +} + + #endif // TEST diff --git a/test/c/test_helper.c b/test/c/test_helper.c new file mode 100644 index 0000000..732f30c --- /dev/null +++ b/test/c/test_helper.c @@ -0,0 +1,23 @@ +#ifdef TEST +#include "unity.h" +#include "helper.h" +#include "mock_helper.h" + +void setUp(void) +{ +} + +void teardown(void) +{ +} + +//Just to test the mock +void test_randomInt(void) +{ + int expected = 42; + randomInt_ExpectAndReturn(expected); + TEST_ASSERT_EQUAL_INT(expected, randomInt()); +} + + +#endif//Test \ No newline at end of file