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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <glib.h>
#include <gio/gio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "../src/ctpl.h"
#include "ctpl-test-lib.h"
/*
* FIXME: support exponents in input/output values
*/
/*#define LOGFILE stdout*/
/* creates a string representing a "float" from two longs: m.n */
static gchar *
float_string (glong m,
glong d)
{
gchar *f;
gchar *p;
g_return_val_if_fail (d >= 0 || m == 0, NULL);
if (m == 0) {
if (d >= 0) {
f = g_strdup_printf ("0.%ld", d);
} else {
f = g_strdup_printf ("-0.%ld", -d);
}
} else {
f = g_strdup_printf ("%ld.%ld", m, d);
}
/* now strip extra 0's */
for (p = strchr (f, 0) - 1; p[0] == '0' && p[-1] != '.'; p--) {
p[0] = 0;
}
return f;
}
/* strips @sfx from the end of @s */
static void
rstripstr (gchar *s,
const gchar *sfx)
{
gsize s_len = strlen (s);
gsize sfx_len = strlen (sfx);
if (s_len > sfx_len) {
if (strcmp (&s[s_len - sfx_len], sfx) == 0) {
s[s_len - sfx_len] = 0;
}
}
}
/* actual test that check if the float represented by float_string(m, d) is
* output as the same string by CTPL */
static gboolean
test_float (glong m,
glong d)
{
gchar *ctpl_f;
gchar *real_f;
gboolean ret = FALSE;
gchar *env;
GError *err = NULL;
real_f = float_string (m, d);
env = g_strconcat ("float = ", real_f, ";", NULL);
ctpl_f = ctpltest_parse_string ("{float}", env, &err);
if (! ctpl_f) {
g_warning ("Failed to parse test template: %s", err->message);
g_error_free (err);
} else {
/* strip extra .0 if present */
rstripstr (real_f, ".0");
ret = strcmp (real_f, ctpl_f) == 0;
if (! ret) {
fprintf (stderr, "** %s expected, got %s\n", real_f, ctpl_f);
}
#ifdef LOGFILE
{
static glong n = 0;
if (n == 100000) {
fprintf (LOGFILE, "%s =\n%s\n", real_f, ctpl_f);
n = 0;
} else {
n++;
}
}
#endif
}
g_free (env);
g_free (ctpl_f);
g_free (real_f);
return ret;
}
/* test cases.
* try not to make checks take too much time if they get activated by default,
* so anyone can run this safely. */
#define PERCENT(max, n) \
(((max) == 0) ? (0.0) : (100.0 * (n) / (max)))
/* test many really small (-1.0, +1.0) values */
static int
test_1 (void)
{
glong m;
glong d;
int ret = 0;
#define M_MIN -1
#define M_MAX +1
#define M_STEP 1
#define D_MIN 0
#define D_MAX +10000000
#define D_STEP 1111
for (m = M_MIN; m < M_MAX; m += M_STEP) {
for (d = D_MIN; d < D_MAX; d += D_STEP) {
if (! test_float (m, d)) {
ret = 1;
}
}
}
return ret;
}
/* test on random values (but relatively small) */
static int
test_2 (void)
{
guint i;
guint n_success = 0;
for (i = 0; i < 100000; i++) {
if (test_float (g_random_int_range (-100000, 100000),
g_random_int_range (0, 999999999))) {
n_success ++;
}
}
g_debug ("%d/%d tests suceeded (%.2f%%)",
n_success, i, PERCENT (i, n_success));
return n_success == i ? 0 : 1;
}
int
main (void)
{
g_type_init ();
return (test_1 () +
test_2 ());
}
|