summaryrefslogtreecommitdiff
path: root/test-lib.c
blob: d9a3177482e4355732b3648d2b30fa9d7f24ff41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "test-lib.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

static const char *GREEN = "\033[0;32m";
static const char *RED = "\033[0;31m";
static const char *ESCAPE = "\e[0m";

static void printPass() { printf("%s\tPassed\t%s\n", GREEN, ESCAPE); }

static void printFail(char *msg) {
  printf("%s\tFailed\n\t%s%s\n", RED, msg, ESCAPE);
}

int syncTest(char *testName, char *errMsg, syncTestHandler_t callback) {
  int status;
  pid_t pid;
  printf("Running %s...\n", testName);
  pid = fork();
  if(pid == 0) {
    return callback();
  }
  else {
    waitpid(pid, &status, 0);
    if(status == 0) {
      printPass();
    }
    else {
      printFail(errMsg);
    }
    return status;
  }
}