summaryrefslogtreecommitdiff
path: root/libs/gst/check/libcheck/check_str.c
blob: 62b67233a95e574565259f9b09d52c2f14a36e6c (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Check: a unit test framework for C
 * Copyright (C) 2001, 2002 Arien Malec
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "libcompat.h"

#include <stdio.h>
#include <stdarg.h>

#include "check.h"
#include "check_list.h"
#include "check_error.h"
#include "check_impl.h"
#include "check_str.h"

static const char *tr_type_str (TestResult * tr);
static int percent_passed (TestStats * t);

char *
tr_str (TestResult * tr)
{
  const char *exact_msg;
  char *rstr;

  exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";

  rstr = ck_strdup_printf ("%s:%d:%s:%s:%s:%d: %s%s",
      tr->file, tr->line,
      tr_type_str (tr), tr->tcname, tr->tname, tr->iter, exact_msg, tr->msg);

  return rstr;
}

char *
tr_short_str (TestResult * tr)
{
  const char *exact_msg;
  char *rstr;

  exact_msg = (tr->rtype == CK_ERROR) ? "(after this point) " : "";

  rstr = ck_strdup_printf ("%s:%d: %s%s",
      tr->file, tr->line, exact_msg, tr->msg);

  return rstr;
}

char *
sr_stat_str (SRunner * sr)
{
  char *str;
  TestStats *ts;

  ts = sr->stats;

  str = ck_strdup_printf ("%d%%: Checks: %d, Failures: %d, Errors: %d",
      percent_passed (ts), ts->n_checked, ts->n_failed, ts->n_errors);

  return str;
}

char *
ck_strdup_printf (const char *fmt, ...)
{
  /* Guess we need no more than 100 bytes. */
  int n;
  size_t size = 100;
  char *p;
  va_list ap;

  p = (char *) emalloc (size);

  while (1) {
    /* Try to print in the allocated space. */
    va_start (ap, fmt);
    n = vsnprintf (p, size, fmt, ap);
    va_end (ap);
    /* If that worked, return the string. */
    if (n > -1 && n < (int) size)
      return p;

    /* Else try again with more space. */
    if (n > -1)                 /* C99 conform vsnprintf() */
      size = (size_t) n + 1;    /* precisely what is needed */
    else                        /* glibc 2.0 */
      size *= 2;                /* twice the old size */

    p = (char *) erealloc (p, size);
  }
}

static const char *
tr_type_str (TestResult * tr)
{
  const char *str = NULL;

  if (tr->ctx == CK_CTX_TEST) {
    if (tr->rtype == CK_PASS)
      str = "P";
    else if (tr->rtype == CK_FAILURE)
      str = "F";
    else if (tr->rtype == CK_ERROR)
      str = "E";
  } else
    str = "S";

  return str;
}

static int
percent_passed (TestStats * t)
{
  if (t->n_failed == 0 && t->n_errors == 0)
    return 100;
  else if (t->n_checked == 0)
    return 0;
  else
    return (int) ((float) (t->n_checked - (t->n_failed + t->n_errors)) /
        (float) t->n_checked * 100);
}