test-eevl.c 5.53 KB
Newer Older
1 2 3 4 5 6 7
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * test-eevl.c
 * Copyright (C) 2008 Fredrik Alstromer <roe@excu.se>
 * Copyright (C) 2008 Martin Nordholts <martinn@svn.gnome.org>
 *
8
 * This library is free software: you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or (at your option) any later version.
12 13 14 15 16 17 18
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see
20
 * <https://www.gnu.org/licenses/>.
21 22 23 24 25 26 27 28 29 30
 */

/* A small regression test case for the evaluator */

#include "config.h"

#include <string.h>

#include <glib-object.h>

31 32
#include "libgimpmath/gimpmath.h"

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
#include "gimpeevl.h"


typedef struct
{
  const gchar      *string;
  GimpEevlQuantity  result;
  gboolean          should_succeed;
} TestCase;

static gboolean
test_units (const gchar      *ident,
            GimpEevlQuantity *result,
            gpointer          data)
{
  gboolean resolved     = FALSE;
  gboolean default_unit = (ident == NULL);

  if (default_unit ||
      (ident && strcmp ("in", ident) == 0))
    {
      result->dimension = 1;
      result->value     = 1.;

      resolved          = TRUE;
    }
  else if (ident && strcmp ("mm", ident) == 0)
    {
      result->dimension = 1;
      result->value     = 25.4;

      resolved          = TRUE;
    }

  return resolved;
}


int
main(void)
{
  gint i;
  gint failed    = 0;
  gint succeeded = 0;

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
  TestCase cases[] =
  {
    /* "Default" test case */
    { "2in + 3in",                 { 2 + 3, 1},               TRUE },

    /* Whitespace variations */
    { "2in+3in",                   { 2 + 3, 1},               TRUE },
    { "   2in + 3in",              { 2 + 3, 1},               TRUE },
    { "2in + 3in   ",              { 2 + 3, 1},               TRUE },
    { "2 in + 3 in",               { 2 + 3, 1},               TRUE },
    { "   2   in   +   3   in   ", { 2 + 3, 1},               TRUE },

    /* Make sure the default unit is applied as it should */
    { "2 + 3in",                   { 2 + 3, 1 },              TRUE },
    { "3",                         { 3, 1 },                  TRUE },

    /* Somewhat complicated input */
    { "(2 + 3)in",                 { 2 + 3, 1},               TRUE },
  //  { "2 / 3 in",                  { 2 / 3., 1},              TRUE },
    { "(2 + 2/3)in",               { 2 + 2 / 3., 1},          TRUE },
    { "1/2 + 1/2",                 { 1, 1},                   TRUE },
    { "2 ^ 3 ^ 4",                 { pow (2, pow (3, 4)), 1}, TRUE },

    /* Mixing of units */
    { "2mm + 3in",                 { 2 / 25.4 + 3, 1},        TRUE },

    /* 'odd' behavior */
    { "2 ++ 1",                    { 3, 1},                   TRUE },
    { "2 +- 1",                    { 1, 1},                   TRUE },
    { "2 -- 1",                    { 3, 1},                   TRUE },

    /* End of test cases */
    { NULL, { 0, 0 }, TRUE }
  };

113 114 115 116
  g_print ("Testing Eevl Eva, the Evaluator\n\n");

  for (i = 0; cases[i].string; i++)
    {
117 118 119 120 121 122 123 124 125
      const gchar      *test           = cases[i].string;
      GimpEevlOptions   options        = GIMP_EEVL_OPTIONS_INIT;
      GimpEevlQuantity  should         = cases[i].result;
      GimpEevlQuantity  result         = { 0, -1 };
      gboolean          should_succeed = cases[i].should_succeed;
      GError           *error          = NULL;
      const gchar      *error_pos      = 0;

      options.unit_resolver_proc = test_units;
126

127
      gimp_eevl_evaluate (test,
128
                          &options,
129 130 131
                          &result,
                          &error_pos,
                          &error);
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

      g_print ("%s = %lg (%d): ", test, result.value, result.dimension);
      if (error || error_pos)
        {
          if (should_succeed)
            {
              failed++;
              g_print ("evaluation failed ");
              if (error)
                {
                  g_print ("with: %s, ", error->message);
                }
              else
                {
                  g_print ("without reason, ");
                }
              if (error_pos)
                {
                  if (*error_pos) g_print ("'%s'.", error_pos);
                  else g_print ("at end of input.");
                }
              else
                {
                  g_print ("but didn't say where.");
                }
              g_print ("\n");
            }
          else
            {
              g_print ("OK (failure test case)\n");
              succeeded++;
            }
        }
      else if (!should_succeed)
        {
          g_print ("evaluation should've failed, but didn't.\n");
          failed++;
        }
      else if (should.value != result.value || should.dimension != result.dimension)
        {
          g_print ("results don't match, should be: %lg (%d)\n",
                    should.value, should.dimension);
          failed++;
        }
      else
        {
          g_print ("OK\n");
          succeeded++;
        }
    }

  g_print ("\n");
  if (!failed)
    g_print ("All OK. ");
  else
    g_print ("Test failed! ");

  g_print ("(%d/%d) %lg%%\n\n", succeeded, succeeded+failed,
            100*succeeded/(gdouble)(succeeded+failed));

  return failed;
}