Skip to content

Instantly share code, notes, and snippets.

@katipogluMustafa
Created June 2, 2021 13:14
Show Gist options
  • Save katipogluMustafa/bef7b90330147096dca99d6f0cb31030 to your computer and use it in GitHub Desktop.
Save katipogluMustafa/bef7b90330147096dca99d6f0cb31030 to your computer and use it in GitHub Desktop.
Mocking in c
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
// gcc mocking.c -Wl,--wrap=rand -o mocking.exe
int __wrap_rand()
{
return 5;
}
int get_random_dice()
{
return (rand() % 6) + 1;
}
int enhance()
{
int item_enhancement = 1;
int dice = get_random_dice();
if(dice > 3)
item_enhancement = dice / 2;
return item_enhancement;
}
void test_enhance()
{
assert(enhance() == 3);
}
int main()
{
srand(time(NULL));
test_enhance();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment