Blame src/check.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "../lib/libcompat.h"
Packit 0b5880
Packit 0b5880
#include <string.h>
Packit 0b5880
#include <stdio.h>
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
#include <stdarg.h>
Packit 0b5880
#include <math.h>
Packit 0b5880
Packit 0b5880
#include "check.h"
Packit 0b5880
#include "check_error.h"
Packit 0b5880
#include "check_list.h"
Packit 0b5880
#include "check_impl.h"
Packit 0b5880
#include "check_msg.h"
Packit 0b5880
Packit 0b5880
#ifndef DEFAULT_TIMEOUT
Packit 0b5880
#define DEFAULT_TIMEOUT 4
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
/*
Packit 0b5880
 * When a process exits either normally, with exit(), or
Packit 0b5880
 * by an uncaught signal, The lower 0x377 bits are passed
Packit 0b5880
 * to the parent. Of those, only the lower 8 bits are
Packit 0b5880
 * returned by the WEXITSTATUS() macro.
Packit 0b5880
 */
Packit 0b5880
#define WEXITSTATUS_MASK 0xFF
Packit 0b5880
Packit 0b5880
int check_major_version = CHECK_MAJOR_VERSION;
Packit 0b5880
int check_minor_version = CHECK_MINOR_VERSION;
Packit 0b5880
int check_micro_version = CHECK_MICRO_VERSION;
Packit 0b5880
Packit 0b5880
const char* current_test_name = NULL;
Packit 0b5880
Packit 0b5880
static int non_pass(int val);
Packit 0b5880
static Fixture *fixture_create(SFun fun, int ischecked);
Packit 0b5880
static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
Packit 0b5880
                              int ischecked);
Packit 0b5880
static void tr_init(TestResult * tr);
Packit 0b5880
static void suite_free(Suite * s);
Packit 0b5880
static void tcase_free(TCase * tc);
Packit 0b5880
Packit 0b5880
Suite *suite_create(const char *name)
Packit 0b5880
{
Packit 0b5880
    Suite *s;
Packit 0b5880
Packit 0b5880
    s = (Suite *)emalloc(sizeof(Suite)); /* freed in suite_free */
Packit 0b5880
    if(name == NULL)
Packit 0b5880
        s->name = "";
Packit 0b5880
    else
Packit 0b5880
        s->name = name;
Packit 0b5880
    s->tclst = check_list_create();
Packit 0b5880
    return s;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
int suite_tcase(Suite * s, const char *tcname)
Packit 0b5880
{
Packit 0b5880
    List *l;
Packit 0b5880
    TCase *tc;
Packit 0b5880
Packit 0b5880
    if(s == NULL)
Packit 0b5880
        return 0;
Packit 0b5880
Packit 0b5880
    l = s->tclst;
Packit 0b5880
    for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
Packit 0b5880
    {
Packit 0b5880
        tc = (TCase *)check_list_val(l);
Packit 0b5880
        if(strcmp(tcname, tc->name) == 0)
Packit 0b5880
            return 1;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    return 0;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void suite_free(Suite * s)
Packit 0b5880
{
Packit 0b5880
    List *l;
Packit 0b5880
Packit 0b5880
    if(s == NULL)
Packit 0b5880
        return;
Packit 0b5880
    l = s->tclst;
Packit 0b5880
    for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
Packit 0b5880
    {
Packit 0b5880
        tcase_free((TCase *)check_list_val(l));
Packit 0b5880
    }
Packit 0b5880
    check_list_free(s->tclst);
Packit 0b5880
    free(s);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
Packit 0b5880
TCase *tcase_create(const char *name)
Packit 0b5880
{
Packit 0b5880
    char *env;
Packit 0b5880
    double timeout_sec = DEFAULT_TIMEOUT;
Packit 0b5880
Packit 0b5880
    TCase *tc = (TCase *)emalloc(sizeof(TCase)); /*freed in tcase_free */
Packit 0b5880
Packit 0b5880
    if(name == NULL)
Packit 0b5880
        tc->name = "";
Packit 0b5880
    else
Packit 0b5880
        tc->name = name;
Packit 0b5880
Packit 0b5880
    env = getenv("CK_DEFAULT_TIMEOUT");
Packit 0b5880
    if(env != NULL)
Packit 0b5880
    {
Packit 0b5880
        char *endptr = NULL;
Packit 0b5880
        double tmp = strtod(env, &endptr);
Packit 0b5880
Packit 0b5880
        if(tmp >= 0 && endptr != env && (*endptr) == '\0')
Packit 0b5880
        {
Packit 0b5880
            timeout_sec = tmp;
Packit 0b5880
        }
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    env = getenv("CK_TIMEOUT_MULTIPLIER");
Packit 0b5880
    if(env != NULL)
Packit 0b5880
    {
Packit 0b5880
        char *endptr = NULL;
Packit 0b5880
        double tmp = strtod(env, &endptr);
Packit 0b5880
Packit 0b5880
        if(tmp >= 0 && endptr != env && (*endptr) == '\0')
Packit 0b5880
        {
Packit 0b5880
            timeout_sec = timeout_sec * tmp;
Packit 0b5880
        }
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    tc->timeout.tv_sec = (time_t) floor(timeout_sec);
Packit 0b5880
    tc->timeout.tv_nsec =
Packit 0b5880
        (long)((timeout_sec -
Packit 0b5880
                floor(timeout_sec)) * (double)NANOS_PER_SECONDS);
Packit 0b5880
Packit 0b5880
    tc->tflst = check_list_create();
Packit 0b5880
    tc->unch_sflst = check_list_create();
Packit 0b5880
    tc->ch_sflst = check_list_create();
Packit 0b5880
    tc->unch_tflst = check_list_create();
Packit 0b5880
    tc->ch_tflst = check_list_create();
Packit 0b5880
    tc->tags = check_list_create();
Packit 0b5880
Packit 0b5880
    return tc;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
/*
Packit 0b5880
 * Helper function to create a list of tags from
Packit 0b5880
 * a space separated string.
Packit 0b5880
 */
Packit 0b5880
List *tag_string_to_list(const char *tags_string)
Packit 0b5880
{
Packit 0b5880
    List *list;
Packit 0b5880
    char *tags;
Packit 0b5880
    char *tag;
Packit 0b5880
Packit 0b5880
    list = check_list_create();
Packit 0b5880
Packit 0b5880
    if (NULL == tags_string)
Packit 0b5880
    {
Packit 0b5880
	return list;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    tags = strdup(tags_string);
Packit 0b5880
    tag = strtok(tags, " ");
Packit 0b5880
    while (tag)
Packit 0b5880
    {
Packit 0b5880
	check_list_add_end(list, strdup(tag));
Packit 0b5880
	tag = strtok(NULL, " ");
Packit 0b5880
    }
Packit 0b5880
    free(tags);
Packit 0b5880
    return list;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void tcase_set_tags(TCase * tc, const char *tags_orig)
Packit 0b5880
{
Packit 0b5880
    /* replace any pre-existing list */
Packit 0b5880
    if (tc->tags)
Packit 0b5880
    {
Packit 0b5880
	check_list_apply(tc->tags, free);
Packit 0b5880
	check_list_free(tc->tags);
Packit 0b5880
    }
Packit 0b5880
    tc->tags = tag_string_to_list(tags_orig);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void tcase_free(TCase * tc)
Packit 0b5880
{
Packit 0b5880
    check_list_apply(tc->tflst, free);
Packit 0b5880
    check_list_apply(tc->unch_sflst, free);
Packit 0b5880
    check_list_apply(tc->ch_sflst, free);
Packit 0b5880
    check_list_apply(tc->unch_tflst, free);
Packit 0b5880
    check_list_apply(tc->ch_tflst, free);
Packit 0b5880
    check_list_apply(tc->tags, free);
Packit 0b5880
    check_list_free(tc->tflst);
Packit 0b5880
    check_list_free(tc->unch_sflst);
Packit 0b5880
    check_list_free(tc->ch_sflst);
Packit 0b5880
    check_list_free(tc->unch_tflst);
Packit 0b5880
    check_list_free(tc->ch_tflst);
Packit 0b5880
    check_list_free(tc->tags);
Packit 0b5880
    free(tc);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
unsigned int tcase_matching_tag(TCase *tc, List *check_for)
Packit 0b5880
{
Packit 0b5880
Packit 0b5880
    if (NULL == check_for)
Packit 0b5880
    {
Packit 0b5880
	return 0;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    for(check_list_front(check_for); !check_list_at_end(check_for);
Packit 0b5880
        check_list_advance(check_for))
Packit 0b5880
    {
Packit 0b5880
	for(check_list_front(tc->tags); !check_list_at_end(tc->tags);
Packit 0b5880
	    check_list_advance(tc->tags))
Packit 0b5880
	{
Packit 0b5880
	    if (0 == strcmp((const char *)check_list_val(tc->tags),
Packit 0b5880
			    (const char *)check_list_val(check_for)))
Packit 0b5880
	    {
Packit 0b5880
		return 1;
Packit 0b5880
	    }
Packit 0b5880
	}
Packit 0b5880
    }
Packit 0b5880
    return 0;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void suite_add_tcase(Suite * s, TCase * tc)
Packit 0b5880
{
Packit 0b5880
    if(s == NULL || tc == NULL || check_list_contains(s->tclst, tc))
Packit 0b5880
    {
Packit 0b5880
        return;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    check_list_add_end(s->tclst, tc);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void _tcase_add_test(TCase * tc, TFun fn, const char *name, int _signal,
Packit 0b5880
                     int allowed_exit_value, int start, int end)
Packit 0b5880
{
Packit 0b5880
    TF *tf;
Packit 0b5880
Packit 0b5880
    if(tc == NULL || fn == NULL || name == NULL)
Packit 0b5880
        return;
Packit 0b5880
    tf = (TF *)emalloc(sizeof(TF));   /* freed in tcase_free */
Packit 0b5880
    tf->fn = fn;
Packit 0b5880
    tf->loop_start = start;
Packit 0b5880
    tf->loop_end = end;
Packit 0b5880
    tf->signal = _signal;       /* 0 means no signal expected */
Packit 0b5880
    tf->allowed_exit_value =
Packit 0b5880
      (WEXITSTATUS_MASK & allowed_exit_value);   /* 0 is default successful exit */
Packit 0b5880
    tf->name = name;
Packit 0b5880
    check_list_add_end(tc->tflst, tf);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static Fixture *fixture_create(SFun fun, int ischecked)
Packit 0b5880
{
Packit 0b5880
    Fixture *f;
Packit 0b5880
Packit 0b5880
    f = (Fixture *)emalloc(sizeof(Fixture));
Packit 0b5880
    f->fun = fun;
Packit 0b5880
    f->ischecked = ischecked;
Packit 0b5880
Packit 0b5880
    return f;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void tcase_add_unchecked_fixture(TCase * tc, SFun setup, SFun teardown)
Packit 0b5880
{
Packit 0b5880
    tcase_add_fixture(tc, setup, teardown, 0);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void tcase_add_checked_fixture(TCase * tc, SFun setup, SFun teardown)
Packit 0b5880
{
Packit 0b5880
    tcase_add_fixture(tc, setup, teardown, 1);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void tcase_add_fixture(TCase * tc, SFun setup, SFun teardown,
Packit 0b5880
                              int ischecked)
Packit 0b5880
{
Packit 0b5880
    if(setup)
Packit 0b5880
    {
Packit 0b5880
        if(ischecked)
Packit 0b5880
            check_list_add_end(tc->ch_sflst,
Packit 0b5880
                               fixture_create(setup, ischecked));
Packit 0b5880
        else
Packit 0b5880
            check_list_add_end(tc->unch_sflst,
Packit 0b5880
                               fixture_create(setup, ischecked));
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    /* Add teardowns at front so they are run in reverse order. */
Packit 0b5880
    if(teardown)
Packit 0b5880
    {
Packit 0b5880
        if(ischecked)
Packit 0b5880
            check_list_add_front(tc->ch_tflst,
Packit 0b5880
                                 fixture_create(teardown, ischecked));
Packit 0b5880
        else
Packit 0b5880
            check_list_add_front(tc->unch_tflst,
Packit 0b5880
                                 fixture_create(teardown, ischecked));
Packit 0b5880
    }
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void tcase_set_timeout(TCase * tc, double timeout)
Packit 0b5880
{
Packit 0b5880
#if defined(HAVE_FORK)
Packit 0b5880
    if(timeout >= 0)
Packit 0b5880
    {
Packit 0b5880
        char *env = getenv("CK_TIMEOUT_MULTIPLIER");
Packit 0b5880
Packit 0b5880
        if(env != NULL)
Packit 0b5880
        {
Packit 0b5880
            char *endptr = NULL;
Packit 0b5880
            double tmp = strtod(env, &endptr);
Packit 0b5880
Packit 0b5880
            if(tmp >= 0 && endptr != env && (*endptr) == '\0')
Packit 0b5880
            {
Packit 0b5880
                timeout = timeout * tmp;
Packit 0b5880
            }
Packit 0b5880
        }
Packit 0b5880
Packit 0b5880
        tc->timeout.tv_sec = (time_t) floor(timeout);
Packit 0b5880
        tc->timeout.tv_nsec =
Packit 0b5880
            (long)((timeout - floor(timeout)) * (double)NANOS_PER_SECONDS);
Packit 0b5880
    }
Packit 0b5880
#else
Packit 0b5880
    (void)tc;
Packit 0b5880
    (void)timeout;
Packit 0b5880
    /* Ignoring, as Check is not compiled with fork support. */
Packit 0b5880
#endif /* HAVE_FORK */
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void tcase_fn_start(const char *fname, const char *file,
Packit 0b5880
                    int line)
Packit 0b5880
{
Packit 0b5880
    send_ctx_info(CK_CTX_TEST);
Packit 0b5880
    send_loc_info(file, line);
Packit 0b5880
Packit 0b5880
    current_test_name = fname;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
const char* tcase_name(void)
Packit 0b5880
{
Packit 0b5880
	return current_test_name;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void _mark_point(const char *file, int line)
Packit 0b5880
{
Packit 0b5880
    send_loc_info(file, line);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void _ck_assert_failed(const char *file, int line, const char *expr, ...)
Packit 0b5880
{
Packit 0b5880
    const char *msg;
Packit 0b5880
    va_list ap;
Packit 0b5880
    char buf[BUFSIZ];
Packit 0b5880
    const char *to_send;
Packit 0b5880
Packit 0b5880
    send_loc_info(file, line);
Packit 0b5880
Packit 0b5880
    va_start(ap, expr);
Packit 0b5880
    msg = (const char *)va_arg(ap, char *);
Packit 0b5880
Packit 0b5880
    /*
Packit 0b5880
     * If a message was passed, format it with vsnprintf.
Packit 0b5880
     * Otherwise, print the expression as is.
Packit 0b5880
     */
Packit 0b5880
    if(msg != NULL)
Packit 0b5880
    {
Packit 0b5880
        vsnprintf(buf, BUFSIZ, msg, ap);
Packit 0b5880
        to_send = buf;
Packit 0b5880
    }
Packit 0b5880
    else
Packit 0b5880
    {
Packit 0b5880
        to_send = expr;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    va_end(ap);
Packit 0b5880
    send_failure_info(to_send);
Packit 0b5880
    if(cur_fork_status() == CK_FORK)
Packit 0b5880
    {
Packit 0b5880
#if defined(HAVE_FORK) && HAVE_FORK==1
Packit 0b5880
        _exit(1);
Packit 0b5880
#endif /* HAVE_FORK */
Packit 0b5880
    }
Packit 0b5880
    else
Packit 0b5880
    {
Packit 0b5880
        longjmp(error_jmp_buffer, 1);
Packit 0b5880
    }
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
SRunner *srunner_create(Suite * s)
Packit 0b5880
{
Packit 0b5880
    SRunner *sr = (SRunner *)emalloc(sizeof(SRunner));     /* freed in srunner_free */
Packit 0b5880
Packit 0b5880
    sr->slst = check_list_create();
Packit 0b5880
    if(s != NULL)
Packit 0b5880
        check_list_add_end(sr->slst, s);
Packit 0b5880
    sr->stats = (TestStats *)emalloc(sizeof(TestStats));     /* freed in srunner_free */
Packit 0b5880
    sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0;
Packit 0b5880
    sr->resultlst = check_list_create();
Packit 0b5880
    sr->log_fname = NULL;
Packit 0b5880
    sr->xml_fname = NULL;
Packit 0b5880
    sr->tap_fname = NULL;
Packit 0b5880
    sr->loglst = NULL;
Packit 0b5880
Packit 0b5880
#if defined(HAVE_FORK)
Packit 0b5880
    sr->fstat = CK_FORK_GETENV;
Packit 0b5880
#else
Packit 0b5880
    /*
Packit 0b5880
     * Overriding the default of running tests in fork mode,
Packit 0b5880
     * as this system does not have fork()
Packit 0b5880
     */
Packit 0b5880
    sr->fstat = CK_NOFORK;
Packit 0b5880
#endif /* HAVE_FORK */
Packit 0b5880
Packit 0b5880
    return sr;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void srunner_add_suite(SRunner * sr, Suite * s)
Packit 0b5880
{
Packit 0b5880
    if(s == NULL)
Packit 0b5880
        return;
Packit 0b5880
Packit 0b5880
    check_list_add_end(sr->slst, s);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void srunner_free(SRunner * sr)
Packit 0b5880
{
Packit 0b5880
    List *l;
Packit 0b5880
    TestResult *tr;
Packit 0b5880
Packit 0b5880
    if(sr == NULL)
Packit 0b5880
        return;
Packit 0b5880
Packit 0b5880
    free(sr->stats);
Packit 0b5880
    l = sr->slst;
Packit 0b5880
    for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
Packit 0b5880
    {
Packit 0b5880
        suite_free((Suite *)check_list_val(l));
Packit 0b5880
    }
Packit 0b5880
    check_list_free(sr->slst);
Packit 0b5880
Packit 0b5880
    l = sr->resultlst;
Packit 0b5880
    for(check_list_front(l); !check_list_at_end(l); check_list_advance(l))
Packit 0b5880
    {
Packit 0b5880
        tr = (TestResult *)check_list_val(l);
Packit 0b5880
        tr_free(tr);
Packit 0b5880
    }
Packit 0b5880
    check_list_free(sr->resultlst);
Packit 0b5880
Packit 0b5880
    free(sr);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
int srunner_ntests_failed(SRunner * sr)
Packit 0b5880
{
Packit 0b5880
    return sr->stats->n_failed + sr->stats->n_errors;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
int srunner_ntests_run(SRunner * sr)
Packit 0b5880
{
Packit 0b5880
    return sr->stats->n_checked;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
TestResult **srunner_failures(SRunner * sr)
Packit 0b5880
{
Packit 0b5880
    int i = 0;
Packit 0b5880
    TestResult **trarray;
Packit 0b5880
    List *rlst;
Packit 0b5880
Packit 0b5880
    trarray = (TestResult **)emalloc(sizeof(trarray[0]) * srunner_ntests_failed(sr));
Packit 0b5880
Packit 0b5880
    rlst = sr->resultlst;
Packit 0b5880
    for(check_list_front(rlst); !check_list_at_end(rlst);
Packit 0b5880
        check_list_advance(rlst))
Packit 0b5880
    {
Packit 0b5880
        TestResult *tr = (TestResult *)check_list_val(rlst);
Packit 0b5880
Packit 0b5880
        if(non_pass(tr->rtype))
Packit 0b5880
            trarray[i++] = tr;
Packit 0b5880
Packit 0b5880
    }
Packit 0b5880
    return trarray;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
TestResult **srunner_results(SRunner * sr)
Packit 0b5880
{
Packit 0b5880
    int i = 0;
Packit 0b5880
    TestResult **trarray;
Packit 0b5880
    List *rlst;
Packit 0b5880
Packit 0b5880
    trarray =(TestResult **) emalloc(sizeof(trarray[0]) * srunner_ntests_run(sr));
Packit 0b5880
Packit 0b5880
    rlst = sr->resultlst;
Packit 0b5880
    for(check_list_front(rlst); !check_list_at_end(rlst);
Packit 0b5880
        check_list_advance(rlst))
Packit 0b5880
    {
Packit 0b5880
        trarray[i++] = (TestResult *)check_list_val(rlst);
Packit 0b5880
    }
Packit 0b5880
    return trarray;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int non_pass(int val)
Packit 0b5880
{
Packit 0b5880
    return val != CK_PASS;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
TestResult *tr_create(void)
Packit 0b5880
{
Packit 0b5880
    TestResult *tr;
Packit 0b5880
Packit 0b5880
    tr = (TestResult *)emalloc(sizeof(TestResult));
Packit 0b5880
    tr_init(tr);
Packit 0b5880
    return tr;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void tr_init(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    tr->ctx = CK_CTX_INVALID;
Packit 0b5880
    tr->line = -1;
Packit 0b5880
    tr->rtype = CK_TEST_RESULT_INVALID;
Packit 0b5880
    tr->msg = NULL;
Packit 0b5880
    tr->file = NULL;
Packit 0b5880
    tr->tcname = NULL;
Packit 0b5880
    tr->tname = NULL;
Packit 0b5880
    tr->duration = -1;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void tr_free(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    free(tr->file);
Packit 0b5880
    free(tr->msg);
Packit 0b5880
    free(tr);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
Packit 0b5880
const char *tr_msg(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    return tr->msg;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
int tr_lno(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    return tr->line;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
const char *tr_lfile(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    return tr->file;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
int tr_rtype(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    return tr->rtype;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
enum ck_result_ctx tr_ctx(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    return tr->ctx;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
const char *tr_tcname(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    return tr->tcname;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static enum fork_status _fstat = CK_FORK;
Packit 0b5880
Packit 0b5880
void set_fork_status(enum fork_status fstat)
Packit 0b5880
{
Packit 0b5880
    if(fstat == CK_FORK || fstat == CK_NOFORK || fstat == CK_FORK_GETENV)
Packit 0b5880
        _fstat = fstat;
Packit 0b5880
    else
Packit 0b5880
        eprintf("Bad status in set_fork_status", __FILE__, __LINE__);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
enum fork_status cur_fork_status(void)
Packit 0b5880
{
Packit 0b5880
    return _fstat;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
/**
Packit 0b5880
 * Not all systems support the same clockid_t's. This call checks
Packit 0b5880
 * if the CLOCK_MONOTONIC clockid_t is valid. If so, that is returned,
Packit 0b5880
 * otherwise, CLOCK_REALTIME is returned.
Packit 0b5880
 *
Packit 0b5880
 * The clockid_t that was found to work on the first call is
Packit 0b5880
 * cached for subsequent calls.
Packit 0b5880
 */
Packit 0b5880
clockid_t check_get_clockid()
Packit 0b5880
{
Packit 0b5880
    static clockid_t clockid = -1;
Packit 0b5880
Packit 0b5880
    if(clockid == -1)
Packit 0b5880
    {
Packit 0b5880
/*
Packit 0b5880
 * Only check if we have librt available. Otherwise, the clockid
Packit 0b5880
 * will be ignored anyway, as the clock_gettime() and
Packit 0b5880
 * timer_create() functions will be re-implemented in libcompat.
Packit 0b5880
 * Worse, if librt and alarm() are unavailable, this check
Packit 0b5880
 * will result in an assert(0).
Packit 0b5880
 */
Packit 0b5880
#ifdef HAVE_LIBRT
Packit 0b5880
        timer_t timerid;
Packit 0b5880
Packit 0b5880
        if(timer_create(CLOCK_MONOTONIC, NULL, &timerid) == 0)
Packit 0b5880
        {
Packit 0b5880
            timer_delete(timerid);
Packit 0b5880
            clockid = CLOCK_MONOTONIC;
Packit 0b5880
        }
Packit 0b5880
        else
Packit 0b5880
        {
Packit 0b5880
            clockid = CLOCK_REALTIME;
Packit 0b5880
        }
Packit 0b5880
#else
Packit 0b5880
        clockid = CLOCK_MONOTONIC;
Packit 0b5880
#endif
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    return clockid;
Packit 0b5880
}