View of xos/tests/common.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include "common.h"
 
//#include <dirent.h>
//#include <sys/stat.h>
//#include <sys/mount.h>
//#include <fcntl.h>
//#include <time.h>
//#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
//#include <limits.h>
 
int status = 1;
 
static void print_test_status(const char *file, int line, const char *expr, int test_status)
{
  if (test_status)
    printf("\e[1;32m[OK]\e[0m  ");
  else
    printf("\e[1;31m[KO]\e[0m  ");
  printf("%s:%d: \e[1m%s\e[0m", file, line, expr);
}
 
static void test_common(const char *file, int line, const char *expr, int errnum, int exp_errnum, int stop_on_error)
{
  int test_status;
  char buf_errnum[64], buf_exp_errnum[64];
 
  test_status = errnum == exp_errnum;
  if (!test_status)
    status = 0;
  print_test_status(file, line, expr, test_status);
  strncpy(buf_errnum, strerror(errnum), sizeof buf_errnum);
  strncpy(buf_exp_errnum, strerror(exp_errnum), sizeof buf_exp_errnum);
  printf("  errnum=%d (%.*s)  expected: %d (%.*s)\n", errnum, sizeof buf_errnum, buf_errnum, exp_errnum, sizeof buf_exp_errnum, buf_exp_errnum);
  if (stop_on_error)
    if (!status)
      exit(EXIT_FAILURE);
}
 
int test(const char *file, int line, const char *expr, int retval, int exp_errnum, int stop_on_error)
{
  test_common(file, line, expr, retval != -1 ? 0 : errno, exp_errnum, stop_on_error);
  return retval;
}
 
void *test_ptr(const char *file, int line, const char *expr, void *retval, int exp_errnum, int stop_on_error)
{
  test_common(file, line, expr, retval ? 0 : errno, exp_errnum, stop_on_error);
  return retval;
}
 
void _assert(const char *file, int line, const char *expr, int test_status, int stop_on_error)
{
  if (!test_status)
    status = 0;
  print_test_status(file, line, expr, test_status);
  printf("  %s\n", test_status ? "True" : "False");
  if (stop_on_error)
    if (!status)
      exit(EXIT_FAILURE);
}