Skip to content

Instantly share code, notes, and snippets.

@zaphod42
Created December 12, 2016 20:43
Show Gist options
  • Select an option

  • Save zaphod42/128faeb72fcf779efa70d806f4caebcc to your computer and use it in GitHub Desktop.

Select an option

Save zaphod42/128faeb72fcf779efa70d806f4caebcc to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <setjmp.h>
#define TEST(name, e) printf("%s\n", name); if((result = setjmp(j)) == 0) { e; longjmp(j, 2); } else { printf("%d\n", result); };
#define ASSERT(exp, act, msg)\
if(exp != act) { printf("Failure! %s\nAt line %d\n", msg, __LINE__); longjmp(j, 1); }
#define SUITE(setup, teardown, e)\
int main() { jmp_buf j; int result; setup; e; teardown; printf("!!!! All tests pass !!!!\n"); return 0; }
#define FALSE 0
#define TRUE 1
char get_position(char* game, int position) {
return game[position - 1];
}
int move(char* game, char player, int position) {
if(get_position(game, position) != 'e') {
return FALSE;
}
game[position - 1] = player;
return TRUE;
}
SUITE(
char *game = malloc(10 * sizeof(char));
memset(game, 0, 10);
memset(game, (int)'e', 9);,
free(game),
TEST("A game board is 9 slots", ASSERT(strlen(game), 9, "Board not initialised"))
TEST("Position 1 is empty", ASSERT(get_position(game, 1), 'e', "Position not empty"))
TEST("Position 5 can be set",
ASSERT(move(game, 'x', 5), TRUE, "Move was not allowed")
ASSERT(get_position(game, 5), 'x', "Position not set"))
TEST("An already used position cannot be reused",
move(game, 'x', 4);
ASSERT(move(game, 'x', 4), FALSE, "Move was allowed")
ASSERT(get_position(game, 4), 'x', "Move changed game")))
@jamescooke

Copy link
Copy Markdown

Thanks Andy - grabbed it. Good pairing with you on this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment