Last active
January 7, 2017 01:59
-
-
Save benchristel/cbf8bb306684a8f1bb295e153628d643 to your computer and use it in GitHub Desktop.
Unit tests in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "hello.h" | |
#include "log.h" | |
void hello() { | |
for (int i = 0; i < 10; i++) { | |
log_printf("Hello, world!\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _HELLO_H | |
#define _HELLO_H | |
void hello(); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void log_printf(char* message) { | |
printf("%s", message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _LOG_H | |
#define _LOG_H | |
void log_printf(char* message); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "hello.h" | |
int main() { | |
hello(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc main.c hello.c log.c -o main.o && ./main.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <strings.h> | |
#include "hello.h" | |
#include "log.h" | |
static char* last_message = 0; | |
static int calls = 0; | |
void log_printf(char* message) { | |
calls++; | |
last_message = message; | |
} | |
int main() { | |
hello(); | |
if (strcmp(last_message, "Hello, world!\n") != 0) { | |
return 1; | |
} | |
if (calls != 10) { | |
return 2; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcc test.c hello.c -o test.o && ./test.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment