Blame src/check_str.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 <stdio.h>
Packit 0b5880
#include <stdarg.h>
Packit 0b5880
Packit 0b5880
#include "check.h"
Packit 0b5880
#include "check_list.h"
Packit 0b5880
#include "check_error.h"
Packit 0b5880
#include "check_impl.h"
Packit 0b5880
#include "check_str.h"
Packit 0b5880
Packit 0b5880
static const char *tr_type_str(TestResult * tr);
Packit 0b5880
static int percent_passed(TestStats * t);
Packit 0b5880
Packit 0b5880
char *tr_str(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    const char *exact_msg;
Packit 0b5880
    char *rstr;
Packit 0b5880
Packit 0b5880
    exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";
Packit 0b5880
Packit 0b5880
    rstr = ck_strdup_printf("%s:%d:%s:%s:%s:%d: %s%s",
Packit 0b5880
                            tr->file, tr->line,
Packit 0b5880
                            tr_type_str(tr), tr->tcname, tr->tname, tr->iter,
Packit 0b5880
                            exact_msg, tr->msg);
Packit 0b5880
Packit 0b5880
    return rstr;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
char *tr_short_str(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    const char *exact_msg;
Packit 0b5880
    char *rstr;
Packit 0b5880
Packit 0b5880
    exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";
Packit 0b5880
Packit 0b5880
    rstr = ck_strdup_printf("%s:%d: %s%s",
Packit 0b5880
                            tr->file, tr->line, exact_msg, tr->msg);
Packit 0b5880
Packit 0b5880
    return rstr;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
char *sr_stat_str(SRunner * sr)
Packit 0b5880
{
Packit 0b5880
    char *str;
Packit 0b5880
    TestStats *ts;
Packit 0b5880
Packit 0b5880
    ts = sr->stats;
Packit 0b5880
Packit 0b5880
    str = ck_strdup_printf("%d%%: Checks: %d, Failures: %d, Errors: %d",
Packit 0b5880
                           percent_passed(ts), ts->n_checked, ts->n_failed,
Packit 0b5880
                           ts->n_errors);
Packit 0b5880
Packit 0b5880
    return str;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
char *ck_strdup_printf(const char *fmt, ...)
Packit 0b5880
{
Packit 0b5880
    /* Guess we need no more than 100 bytes. */
Packit 0b5880
    int n;
Packit 0b5880
    size_t size = 100;
Packit 0b5880
    char *p;
Packit 0b5880
    va_list ap;
Packit 0b5880
Packit 0b5880
    p = (char *)emalloc(size);
Packit 0b5880
Packit 0b5880
    while(1)
Packit 0b5880
    {
Packit 0b5880
        /* Try to print in the allocated space. */
Packit 0b5880
        va_start(ap, fmt);
Packit 0b5880
        n = vsnprintf(p, size, fmt, ap);
Packit 0b5880
        va_end(ap);
Packit 0b5880
        /* If that worked, return the string. */
Packit 0b5880
        if(n > -1 && n < (int)size)
Packit 0b5880
            return p;
Packit 0b5880
Packit 0b5880
        /* Else try again with more space. */
Packit 0b5880
        if(n > -1)              /* C99 conform vsnprintf() */
Packit 0b5880
            size = (size_t) n + 1;      /* precisely what is needed */
Packit 0b5880
        else                    /* glibc 2.0 */
Packit 0b5880
            size *= 2;          /* twice the old size */
Packit 0b5880
Packit 0b5880
        p = (char *)erealloc(p, size);
Packit 0b5880
    }
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static const char *tr_type_str(TestResult * tr)
Packit 0b5880
{
Packit 0b5880
    if(tr->ctx == CK_CTX_TEST)
Packit 0b5880
    {
Packit 0b5880
        if(tr->rtype == CK_PASS)
Packit 0b5880
            return "P";
Packit 0b5880
        if(tr->rtype == CK_FAILURE)
Packit 0b5880
            return "F";
Packit 0b5880
        if(tr->rtype == CK_ERROR)
Packit 0b5880
            return "E";
Packit 0b5880
        return NULL;
Packit 0b5880
    }
Packit 0b5880
    return "S";
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int percent_passed(TestStats * t)
Packit 0b5880
{
Packit 0b5880
    if(t->n_failed == 0 && t->n_errors == 0)
Packit 0b5880
        return 100;
Packit 0b5880
    if(t->n_checked == 0)
Packit 0b5880
        return 0;
Packit 0b5880
    return (int)((float)(t->n_checked - (t->n_failed + t->n_errors)) /
Packit 0b5880
                 (float)t->n_checked * 100);
Packit 0b5880
}