poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] pk_equal_val_p function and tests.


From: Jose E. Marchesi
Subject: Re: [PATCH] pk_equal_val_p function and tests.
Date: Wed, 19 Aug 2020 20:53:33 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> +int
> +pvm_val_equal_p (pvm_val val1, pvm_val val2)
> +{
> +  if (val1 == PVM_NULL && val2 == PVM_NULL)
> +    return 1;
> +  else if (PVM_IS_INT (val1) && PVM_IS_INT (val2))
> +    return (PVM_VAL_INT_SIZE (val1) == PVM_VAL_INT_SIZE (val2))
> +           && (PVM_VAL_INT (val1) == PVM_VAL_INT (val2));
> +  else if (PVM_IS_UINT (val1) && PVM_IS_UINT (val2))
> +    return (PVM_VAL_UINT_SIZE (val1) == PVM_VAL_UINT_SIZE (val2))
> +           && (PVM_VAL_UINT (val1) == PVM_VAL_UINT (val2));
> +  else if (PVM_IS_LONG (val1) && PVM_IS_LONG (val2))
> +    return (PVM_VAL_LONG_SIZE (val1) && PVM_VAL_LONG_SIZE (val2))
> +           && (PVM_VAL_LONG (val1) == PVM_VAL_LONG (val2));
> +  else if (PVM_IS_ULONG (val1) && PVM_IS_ULONG (val2))
> +    return (PVM_VAL_ULONG_SIZE (val1) == PVM_VAL_ULONG_SIZE (val2))
> +           && (PVM_VAL_ULONG (val1) == PVM_VAL_ULONG (val2));
> +  else if (PVM_IS_STR (val1) && PVM_IS_STR (val2))
> +    return STREQ (PVM_VAL_STR (val1), PVM_VAL_STR (val2));
> +  else if (PVM_IS_OFF (val1) && PVM_IS_OFF (val2))
> +    {
> +      pvm_val pvm_off1_magnitude, pvm_off2_magnitude;
> +      pvm_val pvm_off1_unit, pvm_off2_unit;
> +      pvm_val pvm_off1_base_type, pvm_off2_base_type;
> +      int pvm_off_mag_equal, pvm_off_unit_equal, pvm_off_base_type_equal;
> +
> +      pvm_off1_magnitude = PVM_VAL_OFF_MAGNITUDE (val1);
> +      pvm_off2_magnitude = PVM_VAL_OFF_MAGNITUDE (val2);
> +
> +      pvm_off1_unit = PVM_VAL_OFF_UNIT (val1);
> +      pvm_off2_unit = PVM_VAL_OFF_UNIT (val2);
> +
> +      pvm_off1_base_type = PVM_VAL_OFF_BASE_TYPE (val1);
> +      pvm_off2_base_type = PVM_VAL_OFF_BASE_TYPE (val2);
> +
> +      pvm_off_mag_equal = pvm_val_equal_p (pvm_off1_magnitude, 
> pvm_off2_magnitude);
> +      pvm_off_unit_equal = pvm_val_equal_p (pvm_off1_unit, pvm_off2_unit);
> +      pvm_off_base_type_equal = pvm_val_equal_p (pvm_off1_base_type,
> +                                                pvm_off2_base_type);
> +
> +      return pvm_off_mag_equal && pvm_off_unit_equal && 
> pvm_off_base_type_equal;

The magnitude of an offset is always guaranteed to be an integral, so I
think the checks for base_type can be omitted there.

> +    }
> +  else if (PVM_IS_SCT (val1) && PVM_IS_SCT (val2))
> +    {
> +      pvm_val pvm_sct1_ios, pvm_sct2_ios;
> +      pvm_val pvm_sct1_type, pvm_sct2_type;
> +      pvm_val pvm_sct1_off, pvm_sct2_off;
> +      uint64_t pvm_sct1_nfields, pvm_sct2_nfields;
> +      uint64_t pvm_sct1_nmethods, pvm_sct2_nmethods;
> +
> +      pvm_sct1_nfields = PVM_VAL_INTEGRAL (PVM_VAL_SCT_NFIELDS (val1));
> +      pvm_sct2_nfields = PVM_VAL_INTEGRAL (PVM_VAL_SCT_NFIELDS (val2));
> +
> +      pvm_sct1_nmethods = PVM_VAL_INTEGRAL (PVM_VAL_SCT_NMETHODS (val1));
> +      pvm_sct2_nmethods = PVM_VAL_INTEGRAL (PVM_VAL_SCT_NMETHODS (val2));
> +
> +      if (pvm_sct1_nfields != pvm_sct2_nfields
> +          || pvm_sct1_nmethods != pvm_sct2_nmethods)
> +        return 0;
> +
> +      pvm_sct1_ios = PVM_VAL_SCT_IOS (val1);
> +      pvm_sct2_ios = PVM_VAL_SCT_IOS (val2);
> +
> +      if (!(pvm_val_equal_p (pvm_sct1_ios, pvm_sct2_ios)))
> +        return 0;
> +
> +      pvm_sct1_type = PVM_VAL_SCT_TYPE (val1);
> +      pvm_sct2_type = PVM_VAL_SCT_TYPE (val2);
> +
> +      if (!(pvm_val_equal_p (pvm_sct1_type, pvm_sct2_type)))
> +        return 0;
> +
> +      pvm_sct1_off = PVM_VAL_SCT_OFFSET (val1);
> +      pvm_sct2_off = PVM_VAL_SCT_OFFSET (val2);
> +

You can make the code much more compact and easy to read by not using
temporaries for these values, such as:

if (PVM_VAL_INTEGRAL (PVM_VAL_SCT_NFIELDS (val1))
    != PVM_VAL_INTEGRAL (PVAM_VAL_SCT_NFIELDS (val2)))
  return 0;


> +      if (!(pvm_val_equal_p (pvm_sct1_off, pvm_sct2_off)))
> +        return 0;
> +
> +      for (size_t i = 0 ; i < pvm_sct1_nfields ; i++)
> +        {
> +          if (PVM_VAL_SCT_FIELD_ABSENT_P (val1, i)
> +              != PVM_VAL_SCT_FIELD_ABSENT_P (val2, i))
> +              return 0;
> +
> +          if (!PVM_VAL_SCT_FIELD_ABSENT_P (val1, i))
> +            {
> +              pvm_val pvm_sct1_field_boffset, pvm_sct2_field_boffset;
> +              pvm_val pvm_sct1_field_value, pvm_sct2_field_value;
> +              pvm_val pvm_sct1_field_name, pvm_sct2_field_name;
> +
> +              pvm_sct1_field_name = PVM_VAL_SCT_FIELD_NAME (val1, i);
> +              pvm_sct2_field_name = PVM_VAL_SCT_FIELD_NAME (val2, i);
> +
> +              pvm_sct1_field_value = PVM_VAL_SCT_FIELD_VALUE (val1, i);
> +              pvm_sct2_field_value = PVM_VAL_SCT_FIELD_VALUE (val2, i);
> +
> +              pvm_sct1_field_boffset = PVM_VAL_SCT_FIELD_OFFSET (val1, i);
> +              pvm_sct2_field_boffset = PVM_VAL_SCT_FIELD_OFFSET (val2, i);
> +
> +              if (!pvm_val_equal_p (pvm_sct1_field_name, pvm_sct2_field_name)
> +                  || !pvm_val_equal_p (pvm_sct1_field_value, 
> pvm_sct2_field_value)
> +                  || !pvm_val_equal_p (pvm_sct1_field_boffset,
> +                                      pvm_sct2_field_boffset))

Ditto.

> +                return 0;
> +            }
> +        }
> +
> +      for (size_t i = 0 ; i < pvm_sct1_nmethods ; i++)
> +        {
> +          pvm_val pvm_sct1_method_name, pvm_sct2_method_name;
> +
> +          pvm_sct1_method_name = PVM_VAL_SCT_METHOD_NAME (val1, i);
> +          pvm_sct2_method_name = PVM_VAL_SCT_METHOD_NAME (val2, i);
> +
> +          if (!pvm_val_equal_p (pvm_sct1_method_name, pvm_sct2_method_name))
> +            return 0;
> +        }
> +
> +      return 1;
> +    }
> +  else if (PVM_IS_ARR (val1) && PVM_IS_ARR (val2))
> +    {
> +      pvm_val pvm_arr1_ios, pvm_arr2_ios;
> +      pvm_val pvm_arr1_off, pvm_arr2_off;
> +      pvm_val pvm_arr1_elems_bound, pvm_arr2_elems_bound;
> +      pvm_val pvm_arr1_size_bound, pvm_arr2_size_bound;
> +      pvm_val pvm_arr1_mapper, pvm_arr2_mapper;
> +      pvm_val pvm_arr1_writer, pvm_arr2_writer;
> +      pvm_val pvm_arr1_type, pvm_arr2_type;
> +      uint64_t pvm_arr1_nelems, pvm_arr2_nelems;
> +
> +      pvm_arr1_type = PVM_VAL_ARR_TYPE (val1);
> +      pvm_arr2_type = PVM_VAL_ARR_TYPE (val2);
> +
> +      if (!pvm_val_equal_p (pvm_arr1_type, pvm_arr2_type))
> +        return 0;
> +
> +      pvm_arr1_ios = PVM_VAL_ARR_IOS (val1);
> +      pvm_arr2_ios = PVM_VAL_ARR_IOS (val2);
> +
> +      if (!pvm_val_equal_p (pvm_arr1_ios, pvm_arr2_ios))
> +        return 0;
> +
> +      pvm_arr1_off = PVM_VAL_ARR_OFFSET (val1);
> +      pvm_arr2_off = PVM_VAL_ARR_OFFSET (val2);
> +
> +      if (!pvm_val_equal_p (pvm_arr1_off, pvm_arr2_off))
> +        return 0;
> +
> +      pvm_arr1_elems_bound = PVM_VAL_ARR_ELEMS_BOUND (val1);
> +      pvm_arr2_elems_bound = PVM_VAL_ARR_ELEMS_BOUND (val2);
> +
> +      if (!pvm_val_equal_p (pvm_arr1_elems_bound, pvm_arr2_elems_bound))
> +        return 0;
> +
> +      pvm_arr1_size_bound = PVM_VAL_ARR_SIZE_BOUND (val1);
> +      pvm_arr2_size_bound = PVM_VAL_ARR_SIZE_BOUND (val2);
> +
> +      if (!pvm_val_equal_p (pvm_arr1_size_bound, pvm_arr2_size_bound))
> +        return 0;
> +
> +      pvm_arr1_mapper = PVM_VAL_ARR_MAPPER (val1);
> +      pvm_arr2_mapper = PVM_VAL_ARR_MAPPER (val2);
> +
> +      if (!pvm_val_equal_p (pvm_arr1_mapper, pvm_arr2_mapper))
> +        return 0;
> +
> +      pvm_arr1_writer = PVM_VAL_ARR_WRITER (val1);
> +      pvm_arr2_writer = PVM_VAL_ARR_WRITER (val2);
> +
> +      if (!pvm_val_equal_p (pvm_arr1_writer, pvm_arr2_writer))
> +        return 0;
> +
> +      pvm_arr1_nelems = PVM_VAL_INTEGRAL (PVM_VAL_ARR_NELEM (val1));
> +      pvm_arr2_nelems = PVM_VAL_INTEGRAL (PVM_VAL_ARR_NELEM (val2));
> +

Ditto.

> +      if (pvm_arr1_nelems != pvm_arr2_nelems)
> +        return 0;
> +
> +      for (size_t i = 0 ; i < pvm_arr1_nelems ; i++)
> +        {
> +          pvm_val pvm_arr1_elem_value, pvm_arr2_elem_value;
> +          pvm_val pvm_arr1_elem_boffset, pvm_arr2_elem_boffset;
> +
> +          pvm_arr1_elem_value = PVM_VAL_ARR_ELEM_VALUE (val1, i);
> +          pvm_arr2_elem_value = PVM_VAL_ARR_ELEM_VALUE (val2, i);
> +
> +          pvm_arr1_elem_boffset = PVM_VAL_ARR_ELEM_OFFSET (val1, i);
> +          pvm_arr2_elem_boffset = PVM_VAL_ARR_ELEM_OFFSET (val2, i);
> +
> +          if (!pvm_val_equal_p (pvm_arr1_elem_value, pvm_arr2_elem_value)
> +              || !pvm_val_equal_p (pvm_arr1_elem_boffset, 
> pvm_arr2_elem_boffset))
> +            return 0;

Ditto.

> +        }
> +
> +      return 1;
> +    }
> +  else if (PVM_IS_TYP (val1) && PVM_IS_TYP (val2))
> +    return pvm_type_equal (val1, val2);
> +  else
> +    return 0;
> +}
> +
>  void
>  pvm_allocate_struct_attrs (pvm_val nfields,
>                             pvm_val **fnames, pvm_val **ftypes)
> @@ -1144,6 +1347,7 @@ pvm_type_equal (pvm_val type1, pvm_val type2)
>  
>          return 1;
>        }
> +      break;

This belongs to a different patch.

>      default:
>        assert (0);
>      }
> diff --git a/libpoke/pvm-val.h b/libpoke/pvm-val.h
> index a2bb8cdd..4018be64 100644
> --- a/libpoke/pvm-val.h
> +++ b/libpoke/pvm-val.h
> @@ -238,7 +238,7 @@ struct pvm_array_elem
>     called structure "elements".  They can be mapped in IO, or
>     unmapped.
>  
> -   IO is an int<32> value that identifies the IO space where the value
> +   IOS is an int<32> value that identifies the IO space where the value
>     is mapped.  If the structure is not mapped then this is PVM_NULL.
>  
>     OFFSET is an ulong<64> value holding the bit offset of in the IO

This also belongs to a different patch.


> diff --git a/libpoke/pvm.h b/libpoke/pvm.h
> index f023d123..02da8227 100644
> --- a/libpoke/pvm.h
> +++ b/libpoke/pvm.h
> @@ -274,6 +274,13 @@ pvm_val pvm_make_struct (pvm_val nfields, pvm_val 
> nmethods, pvm_val type)
>  pvm_val pvm_make_cls (pvm_program program)
>    __attribute__ ((visibility ("hidden")));
>  
> +/* Compare two PVM values.
> +
> +   Returns 1 if they match, 0 otherwise.  */
> +
> +int pvm_val_equal_p (pvm_val val1, pvm_val val2)
> +  __attribute__ ((visibility ("hidden")));
> +
>  /*** PVM values.  ***/
>  
>  void pvm_print_string (pvm_val string)
> @@ -293,6 +300,7 @@ pvm_val pvm_make_integral_type (pvm_val size, pvm_val 
> signed_p)
>  
>  pvm_val pvm_make_string_type (void)
>    __attribute__ ((visibility ("hidden")));
> +
>  pvm_val pvm_make_any_type (void)
>    __attribute__ ((visibility ("hidden")));
>  
> diff --git a/testsuite/poke.libpoke/Makefile.am 
> b/testsuite/poke.libpoke/Makefile.am
> index 36ebf2ea..3d47507d 100644
> --- a/testsuite/poke.libpoke/Makefile.am
> +++ b/testsuite/poke.libpoke/Makefile.am
> @@ -23,6 +23,7 @@ values_SOURCES = $(COMMON) values.c
>  
>  values_CPPFLAGS = -I$(top_builddir)/gl -I$(top_srcdir)/gl \
>                    -I$(top_srcdir)/common \
> +                  -DPKGDATADIR=\"$(pkgdatadir)\" \
>                    -I$(top_srcdir)/libpoke -I$(top_builddir)/libpoke
>  
>  values_LDADD = $(top_builddir)/gl/libgnu.la \
> diff --git a/testsuite/poke.libpoke/pk_equal_arr.test 
> b/testsuite/poke.libpoke/pk_equal_arr.test
> new file mode 100644
> index 00000000..e6616c31
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_equal_arr.test
> @@ -0,0 +1,4 @@
> +##
> +[1L, 2L, 3L, 15L, 25L, -35L, 64L, 9223372036854775807L]
> +##
> +[1L, 2L, 3L, 15L, 25L, -35L, 64L, 9223372036854775807L]
> diff --git a/testsuite/poke.libpoke/pk_equal_int.test 
> b/testsuite/poke.libpoke/pk_equal_int.test
> new file mode 100644
> index 00000000..6235b2f2
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_equal_int.test
> @@ -0,0 +1,4 @@
> +##
> +-3500 as int<32>
> +##
> +-3500 as int<32>
> diff --git a/testsuite/poke.libpoke/pk_equal_off.test 
> b/testsuite/poke.libpoke/pk_equal_off.test
> new file mode 100644
> index 00000000..63b75d31
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_equal_off.test
> @@ -0,0 +1,4 @@
> +##
> +25#B
> +##
> +25#B
> diff --git a/testsuite/poke.libpoke/pk_equal_sct.test 
> b/testsuite/poke.libpoke/pk_equal_sct.test
> new file mode 100644
> index 00000000..8647da8c
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_equal_sct.test
> @@ -0,0 +1,5 @@
> +deftype Packet = struct { int i; long j; };
> +##
> +Packet { i = 1, j = 2 }
> +##
> +Packet { i = 1, j = 2 }
> diff --git a/testsuite/poke.libpoke/pk_equal_str.test 
> b/testsuite/poke.libpoke/pk_equal_str.test
> new file mode 100644
> index 00000000..c626397e
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_equal_str.test
> @@ -0,0 +1,4 @@
> +##
> +"foo"
> +##
> +"foo"
> diff --git a/testsuite/poke.libpoke/pk_equal_uint.test 
> b/testsuite/poke.libpoke/pk_equal_uint.test
> new file mode 100644
> index 00000000..e9a97b21
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_equal_uint.test
> @@ -0,0 +1,4 @@
> +##
> +18446744073709551615 as uint<64>
> +##
> +18446744073709551615 as uint<64>
> diff --git a/testsuite/poke.libpoke/pk_nequal_arr.test 
> b/testsuite/poke.libpoke/pk_nequal_arr.test
> new file mode 100644
> index 00000000..1abca786
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_nequal_arr.test
> @@ -0,0 +1,4 @@
> +##
> +[1L, 2L, 3L, 15L, 25L, -35L, 64L, 9223372036854775807L]
> +##
> +[1L, 2L, 4L, 15L, -25L, -35L, 64L, 9223372036854775807L]
> diff --git a/testsuite/poke.libpoke/pk_nequal_int.test 
> b/testsuite/poke.libpoke/pk_nequal_int.test
> new file mode 100644
> index 00000000..b142a4c8
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_nequal_int.test
> @@ -0,0 +1,4 @@
> +##
> +-3500 as int<32>
> +##
> +-3500 as int<64>
> diff --git a/testsuite/poke.libpoke/pk_nequal_off.test 
> b/testsuite/poke.libpoke/pk_nequal_off.test
> new file mode 100644
> index 00000000..9719e495
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_nequal_off.test
> @@ -0,0 +1,4 @@
> +##
> +25#B
> +##
> +22#b
> diff --git a/testsuite/poke.libpoke/pk_nequal_sct.test 
> b/testsuite/poke.libpoke/pk_nequal_sct.test
> new file mode 100644
> index 00000000..cb7489c6
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_nequal_sct.test
> @@ -0,0 +1,5 @@
> +deftype Packet = struct { int i; long j; };
> +##
> +Packet { i = 1, j = 2 }
> +##
> +Packet { i = 2, j = 2 }
> diff --git a/testsuite/poke.libpoke/pk_nequal_str.test 
> b/testsuite/poke.libpoke/pk_nequal_str.test
> new file mode 100644
> index 00000000..fa7760db
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_nequal_str.test
> @@ -0,0 +1,4 @@
> +##
> +"foo"
> +##
> +"bar"
> diff --git a/testsuite/poke.libpoke/pk_nequal_uint.test 
> b/testsuite/poke.libpoke/pk_nequal_uint.test
> new file mode 100644
> index 00000000..3ca5ceb0
> --- /dev/null
> +++ b/testsuite/poke.libpoke/pk_nequal_uint.test
> @@ -0,0 +1,4 @@
> +##
> +18446744073709551615 as uint<64>
> +##
> +12312 as uint<64>
> diff --git a/testsuite/poke.libpoke/values.c b/testsuite/poke.libpoke/values.c
> index 17d3378f..495f423f 100644
> --- a/testsuite/poke.libpoke/values.c
> +++ b/testsuite/poke.libpoke/values.c
> @@ -18,7 +18,11 @@
>  
>  #include <config.h>
>  
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
>  #include <dejagnu.h>
> +#include <dirent.h>
>  #include "libpoke.h"
>  
>  #include "term-if.h"
> @@ -61,13 +65,222 @@ test_simple_values ()
>  }
>  
>  int
> -main (int argc, char *argv[])
> +compile_initial_poke_code (FILE *ifp, pk_compiler pkc)
>  {
> -  pk_compiler pk_compiler = pk_compiler_new (".", &poke_term_if);
> +  ssize_t nread, s_nread = 0;
> +  char *line = NULL, *poke_code = NULL;
> +  size_t len = 0;
> +  int error = 1;
>  
> -  test_simple_values ();
> +  while (1)
> +    {
> +      nread = getline (&line, &len, ifp);
> +      if (nread == -1)
> +        return 0;
> +
> +      /* If we reached the next section of the file, break.  */
> +      if (nread == 3 && line[0] == '#' && line[1] == '#')
> +        break;
> +
> +      line[nread - 1] = '\0';
> +      s_nread += nread - 1;
> +
> +      if (poke_code == NULL)
> +        {
> +          poke_code = (char *) malloc (nread);
> +          memcpy (poke_code, line, nread);
> +        }
> +      else
> +        {
> +          poke_code = (char *) realloc (poke_code, s_nread + 1);
> +          strncat (poke_code, line, nread + 1);
> +        }
> +    }
> +
> +  if (ferror (ifp))
> +    return 0;
> +
> +  if (poke_code)
> +    {
> +      error = pk_compile_buffer (pkc, poke_code, NULL);
> +      free (poke_code);
> +    }
> +
> +  free (line);
> +  return error;
> +}
> +
> +static int
> +copy_line_to_expression (char **expr, char *line, size_t nbytes)
> +{
> +  if (*expr == NULL)
> +    {
> +      *expr = (char *) malloc (nbytes);
> +      if (*expr == NULL)
> +        goto error;
> +
> +      memcpy (*expr, line, nbytes);
> +    }
> +  else
> +    {
> +      *expr = (char *) realloc (*expr, nbytes);
> +      if (*expr == NULL)
> +        goto error;
> +
> +      strncat (*expr, line, strlen (line) + 1);
> +    }
> +
> +  return 0;
> +
> +  error:
> +    return -1;
> +}
> +
> +/* Returns a C array containing the Poke values that were compiled.
> +   Returns NULL on error.  */
> +int
> +compile_poke_expressions (FILE *ifp, pk_compiler pkc, pk_val *val1,
> +                          pk_val *val2)
> +{
> +  ssize_t nread;
> +  char *expr1 = NULL, *expr2 = NULL, *line = NULL;
> +  size_t s_read = 0, len = 0;
> +
> +  /* Read the first expression of the file.  */
> +  while (1)
> +    {
> +      nread = getline (&line, &len, ifp);
> +      if (nread == -1)
> +          goto error;
> +
> +      line[nread - 1] = '\0';
> +      s_read += nread;
> +
> +      if (nread == 3 && line[0] == '#' && line[1] == '#')
> +        break;
> +
> +      copy_line_to_expression (&expr1, line, s_read);
> +    }
> +
> +  /* Read the second expression of the file.  */
> +  s_read = 0;
> +  while ((nread = getline (&line, &len, ifp)) != -1)
> +    {
> +      if (line[nread - 1] == '\n')
> +        {
> +          line[nread - 1] = '\0';
> +          s_read += nread;
> +        }
> +      else
> +        {
> +          s_read += nread + 1;
> +        }
> +
> +      copy_line_to_expression (&expr2, line, s_read);
> +    }
> +
> +  if (pk_compile_expression (pkc, (const char *) expr1, NULL, val1) == 0
> +      || pk_compile_expression (pkc, (const char *) expr2, NULL, val2) == 0)
> +    goto error;
> +
> +  free (expr1);
> +  free (expr2);
> +  free (line);
> +  return 1;
>  
> -  pk_compiler_free (pk_compiler);
> +  error:
> +    free (expr1);
> +    free (expr2);
> +    free (line);
> +    return 0;
> +}
> +
> +#define STARTS_WITH(PREFIX, STR) (strncmp (PREFIX, STR, strlen (PREFIX)) == 
> 0)
> +
> +void
> +test_pk_equal_file (const char *filename, FILE *ifp)
> +{
> +  pk_val val1, val2;
> +  pk_compiler pkc;
> +  int equal;
> +  char *poke_datadir;
> +
> +  poke_datadir = getenv ("POKEDATADIR");
> +  if (poke_datadir == NULL)
> +    poke_datadir = PKGDATADIR;
> +
> +  pkc = pk_compiler_new (poke_datadir, &poke_term_if);
> +
> +  if (!pkc)
> +    goto error;
> +
> +  if (compile_initial_poke_code (ifp, pkc) == 0)
> +    goto error;
> +
> +  if (compile_poke_expressions (ifp, pkc, &val1, &val2) == 0)
> +    goto error;
> +
> +  /* (NOTE: kostas) We should have a way to discriminate if we should check
> +      if 2 values should match or not.
> +
> +      Currently, this decision is taken based on the name of the file.
> +
> +      If file begins with pk_equal we should mark the test as "passed"
> +      if the 2 values are equal.
> +
> +      If file begins with pk_nequal we should mark the test as "passed"
> +      if the 2 values are non equal.  */
> +
> +  equal = pk_val_equal_p (val1, val2);
> +  if (STARTS_WITH ("pk_equal", filename) && equal)
> +    pass (filename);
> +  else if (STARTS_WITH ("pk_nequal", filename) && !equal)
> +    pass (filename);
> +  else
> +    fail (filename);
> +
> +  pk_compiler_free (pkc);
> +  return;
> +
> +  error:
> +    fail (filename);
> +}
> +
> +void
> +test_pk_val_equal_p ()
> +{
> +  FILE *ifp;
> +  DIR *directory;
> +  struct dirent *dir;
> +  const char *extension;
> +
> +  directory = opendir (".");
> +  if (directory)
> +    {
> +      while ((dir = readdir (directory)) != NULL)
> +        {
> +          /* If this file a .json file, proccess it.  */
> +          extension = strrchr (dir->d_name, '.');
> +          if (extension)
> +            {
> +              if (!strncmp (extension + 1, "test", 4))
> +                {
> +                  ifp = fopen (dir->d_name, "r");
> +                  if (ifp)
> +                    test_pk_equal_file (dir->d_name, ifp);
> +                  fclose (ifp);
> +                }
> +            }
> +        }
> +      closedir (directory);
> +    }
> +}
> +
> +int
> +main (int argc, char *argv[])
> +{
> +  test_simple_values ();
> +  test_pk_val_equal_p ();
>  
>    totals ();
>    return 0;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]