Skip to content

Instantly share code, notes, and snippets.

@benchristel
Last active January 7, 2017 01:59
Show Gist options
  • Save benchristel/cbf8bb306684a8f1bb295e153628d643 to your computer and use it in GitHub Desktop.
Save benchristel/cbf8bb306684a8f1bb295e153628d643 to your computer and use it in GitHub Desktop.
Unit tests in C
#include <stdio.h>
#include "hello.h"
#include "log.h"
void hello() {
for (int i = 0; i < 10; i++) {
log_printf("Hello, world!\n");
}
}
#ifndef _HELLO_H
#define _HELLO_H
void hello();
#endif
#include <stdio.h>
void log_printf(char* message) {
printf("%s", message);
}
#ifndef _LOG_H
#define _LOG_H
void log_printf(char* message);
#endif
#include "hello.h"
int main() {
hello();
return 0;
}
gcc main.c hello.c log.c -o main.o && ./main.o
#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;
}
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