/**
 * utest.c
 *   Fun with unit testing!
 */

// +---------+--------------------------------------------------------
// | Headers |
// +---------+

#include <stdio.h>
#include <string.h>
#include "utest.h"


// +---------+--------------------------------------------------------
// | Globals |
// +---------+

int errors = 0;


// +-----------------+------------------------------------------------
// | Local Functions |
// +-----------------+

static int
string_equal (char *str1, char *str2)
{
  if (str1 == str2)
    return 1;
  else if ((str1 == NULL) || (str2 == NULL))
    return 0;
  else 
    return (strcmp (str1, str2) == 0);
} // string_equal


// +-----------+------------------------------------------------------
// | Functions |
// +-----------+

void 
check_int (char *command, int result, int expected)
{
  if (result != expected)
    {
      ++errors;
      fprintf (stderr, "ERROR\n");
      fprintf (stderr, "  Command:  %s\n", command);
      fprintf (stderr, "  Result:   %d\n", result);
      fprintf (stderr, "  Expected: %d\n", expected);
    }
} // check_int

void 
check_ptr (char *command, void *result, void *expected)
{
  if (result != expected)
    {
      ++errors;
      fprintf (stderr, "ERROR\n");
      fprintf (stderr,"  Command:  %s\n", command);
    }
} // check_ptr

void 
check_str (char *command, char *result, char *expected)
{
  if (! string_equal (result, expected))
    {
      ++errors;
      fprintf (stderr, "ERROR\n");
      fprintf (stderr, "  Command:  %s\n", command);
      if (result != NULL)
        fprintf (stderr, "  Result:   \"%s\"\n", result);
      else
        fprintf (stderr, "  Result:   NULL\n");
      if (expected != NULL)
        fprintf (stderr, "  Expected: \"%s\"\n", expected);
      else
        fprintf (stderr, "  Expected: NULL\n");
    }
} // check_str

