From: kostasch Date: Tue, 1 Sep 2020 17:36:03 +0300 Subject: [PATCH] pk_equal_val_p function, testing and new modules. 2020-09-01 Kostas Chasialis * libpoke/libpoke.h (pk_val_equal_p): Prototype. * libpoke/pk-val.c (pk_val_equal_p): Define. * libpoke/pvm.h (pvm_val_equal_p): Prototype. * libpoke/pvm-val.c (pvm_val_equal_p): Define. * testsuite/poke.libpoke/values.c (test_pk_val_equal_p): New function. (test_pk_equal_file): Likewise. (copy_line_to_expression): Likewise. (compile_poke_expressions): Likewise. (compile_initial_poke_code): Likewise. * testsuite/poke.libpoke/pk_equal_int.test: New test. * testsuite/poke.libpoke/pk_equal_uint.test: New test. * testsuite/poke.libpoke/pk_equal_str.test: New test. * testsuite/poke.libpoke/pk_equal_off.test: New test. * testsuite/poke.libpoke/pk_equal_sct.test: New test. * testsuite/poke.libpoke/pk_equal_arr.test: New test. * testsuite/poke.libpoke/pk_nequal_int.test: New test. * testsuite/poke.libpoke/pk_nequal_uint.test: New test. * testsuite/poke.libpoke/pk_nequal_str.test: New test. * testsuite/poke.libpoke/pk_nequal_off.test: New test. * testsuite/poke.libpoke/pk_nequal_sct.test: New test. * testsuite/poke.libpoke/pk_nequal_arr.test: New test. * testsuite/poke.libpoke/Makefile.am: Added PKGDATADIR. * testsuite/Makefile.am (EXTRA_DIST): Updated with new tests. * bootstrap.conf (gnulib_modules): Added new modules. --- ChangeLog | 27 +++ bootstrap.conf | 6 +- libpoke/libpoke.h | 6 + libpoke/pk-val.c | 6 + libpoke/pvm-val.c | 135 +++++++++++++ libpoke/pvm.h | 8 + testsuite/Makefile.am | 14 +- testsuite/poke.libpoke/Makefile.am | 1 + testsuite/poke.libpoke/pk_equal_arr.test | 4 + testsuite/poke.libpoke/pk_equal_int.test | 4 + testsuite/poke.libpoke/pk_equal_off.test | 4 + testsuite/poke.libpoke/pk_equal_sct.test | 5 + testsuite/poke.libpoke/pk_equal_str.test | 4 + testsuite/poke.libpoke/pk_equal_uint.test | 4 + testsuite/poke.libpoke/pk_nequal_arr.test | 4 + testsuite/poke.libpoke/pk_nequal_int.test | 4 + testsuite/poke.libpoke/pk_nequal_off.test | 4 + testsuite/poke.libpoke/pk_nequal_sct.test | 5 + testsuite/poke.libpoke/pk_nequal_str.test | 4 + testsuite/poke.libpoke/pk_nequal_uint.test | 4 + testsuite/poke.libpoke/values.c | 221 ++++++++++++++++++++- 21 files changed, 468 insertions(+), 6 deletions(-) create mode 100644 testsuite/poke.libpoke/pk_equal_arr.test create mode 100644 testsuite/poke.libpoke/pk_equal_int.test create mode 100644 testsuite/poke.libpoke/pk_equal_off.test create mode 100644 testsuite/poke.libpoke/pk_equal_sct.test create mode 100644 testsuite/poke.libpoke/pk_equal_str.test create mode 100644 testsuite/poke.libpoke/pk_equal_uint.test create mode 100644 testsuite/poke.libpoke/pk_nequal_arr.test create mode 100644 testsuite/poke.libpoke/pk_nequal_int.test create mode 100644 testsuite/poke.libpoke/pk_nequal_off.test create mode 100644 testsuite/poke.libpoke/pk_nequal_sct.test create mode 100644 testsuite/poke.libpoke/pk_nequal_str.test create mode 100644 testsuite/poke.libpoke/pk_nequal_uint.test diff --git a/ChangeLog b/ChangeLog index 74fd7889..6fb264c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,30 @@ +2020-09-01 Kostas Chasialis + + * libpoke/libpoke.h (pk_val_equal_p): Prototype. + * libpoke/pk-val.c (pk_val_equal_p): Define. + * libpoke/pvm.h (pvm_val_equal_p): Prototype. + * libpoke/pvm-val.c (pvm_val_equal_p): Define. + * testsuite/poke.libpoke/values.c (test_pk_val_equal_p): New function. + (test_pk_equal_file): Likewise. + (copy_line_to_expression): Likewise. + (compile_poke_expressions): Likewise. + (compile_initial_poke_code): Likewise. + * testsuite/poke.libpoke/pk_equal_int.test: New test. + * testsuite/poke.libpoke/pk_equal_uint.test: New test. + * testsuite/poke.libpoke/pk_equal_str.test: New test. + * testsuite/poke.libpoke/pk_equal_off.test: New test. + * testsuite/poke.libpoke/pk_equal_sct.test: New test. + * testsuite/poke.libpoke/pk_equal_arr.test: New test. + * testsuite/poke.libpoke/pk_nequal_int.test: New test. + * testsuite/poke.libpoke/pk_nequal_uint.test: New test. + * testsuite/poke.libpoke/pk_nequal_str.test: New test. + * testsuite/poke.libpoke/pk_nequal_off.test: New test. + * testsuite/poke.libpoke/pk_nequal_sct.test: New test. + * testsuite/poke.libpoke/pk_nequal_arr.test: New test. + * testsuite/poke.libpoke/Makefile.am: Added PKGDATADIR. + * testsuite/Makefile.am (EXTRA_DIST): Updated with new tests. + * bootstrap.conf (gnulib_modules): Added new modules. + 2020-09-01 Jose E. Marchesi * libpoke/pkl-gen.c (pkl_gen_pr_type): Update comment. diff --git a/bootstrap.conf b/bootstrap.conf index 1d9dc390..3c263fce 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -51,6 +51,10 @@ gnulib_modules=" xalloc xstrndup strstr + opendir + closedir + readdir + memcpy " # gnulib modules used in libpoke/. @@ -145,7 +149,7 @@ buildreq="\ autoconf 2.62 automake 1.16 flex 2.5.37 -bison 3.6 +bison 3.6.0 gettext 0.18.2 pkg-config - makeinfo 6.0 diff --git a/libpoke/libpoke.h b/libpoke/libpoke.h index 25d9c239..4aa550d3 100644 --- a/libpoke/libpoke.h +++ b/libpoke/libpoke.h @@ -830,6 +830,12 @@ pk_val pk_typeof (pk_val val); int pk_type_code (pk_val val); +/* Compare two Poke values. + + Returns 1 if they match, 0 otherwise. */ + +int pk_val_equal_p (pk_val val1, pk_val val2); + /* Print the given value. */ void pk_print_val (pk_compiler pkc, pk_val val); diff --git a/libpoke/pk-val.c b/libpoke/pk-val.c index 1b57f21e..808cb509 100644 --- a/libpoke/pk-val.c +++ b/libpoke/pk-val.c @@ -185,6 +185,12 @@ pk_type_code (pk_val val) } } +int +pk_val_equal_p (pk_val val1, pk_val val2) +{ + return pvm_val_equal_p (val1, val2); +} + pk_val pk_make_struct (pk_val nfields, pk_val type) { diff --git a/libpoke/pvm-val.c b/libpoke/pvm-val.c index 2042cfa8..1684a59e 100644 --- a/libpoke/pvm-val.c +++ b/libpoke/pvm-val.c @@ -348,6 +348,141 @@ pvm_make_offset (pvm_val magnitude, pvm_val unit) return PVM_BOX (box); } +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)) + { + int pvm_off_mag_equal, pvm_off_unit_equal; + + pvm_off_mag_equal = pvm_val_equal_p (PVM_VAL_OFF_MAGNITUDE (val1), + PVM_VAL_OFF_MAGNITUDE (val2)); + pvm_off_unit_equal = pvm_val_equal_p (PVM_VAL_OFF_UNIT (val1), + PVM_VAL_OFF_UNIT (val2)); + + return pvm_off_mag_equal && pvm_off_unit_equal; + } + else if (PVM_IS_SCT (val1) && PVM_IS_SCT (val2)) + { + size_t pvm_sct1_nfields = PVM_VAL_ULONG (PVM_VAL_SCT_NFIELDS (val1)); + size_t pvm_sct2_nfields = PVM_VAL_ULONG (PVM_VAL_SCT_NFIELDS (val2)); + size_t pvm_sct1_nmethods = PVM_VAL_ULONG (PVM_VAL_SCT_NMETHODS (val1)); + size_t pvm_sct2_nmethods = PVM_VAL_ULONG (PVM_VAL_SCT_NMETHODS (val2)); + + if ((pvm_sct1_nfields != pvm_sct2_nfields) + || (pvm_sct1_nmethods != pvm_sct2_nmethods)) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_SCT_IOS (val1), PVM_VAL_SCT_IOS (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_SCT_TYPE (val1), PVM_VAL_SCT_TYPE (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_SCT_OFFSET (val1), + PVM_VAL_SCT_OFFSET (val2))) + 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)) + { + if (!pvm_val_equal_p (PVM_VAL_SCT_FIELD_NAME (val1, i), + PVM_VAL_SCT_FIELD_NAME (val2, i))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_SCT_FIELD_VALUE (val1, i), + PVM_VAL_SCT_FIELD_VALUE (val2, i))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_SCT_FIELD_OFFSET (val1, i), + PVM_VAL_SCT_FIELD_OFFSET (val2, i))) + return 0; + } + } + + for (size_t i = 0 ; i < pvm_sct1_nmethods ; i++) + { + if (!pvm_val_equal_p (PVM_VAL_SCT_METHOD_NAME (val1, i), + PVM_VAL_SCT_METHOD_NAME (val2, i))) + return 0; + } + + return 1; + } + else if (PVM_IS_ARR (val1) && PVM_IS_ARR (val2)) + { + size_t pvm_arr1_nelems = PVM_VAL_ULONG (PVM_VAL_ARR_NELEM (val1)); + size_t pvm_arr2_nelems = PVM_VAL_ULONG (PVM_VAL_ARR_NELEM (val2)); + + if (pvm_arr1_nelems != pvm_arr2_nelems) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_TYPE (val1), PVM_VAL_ARR_TYPE (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_IOS (val1), PVM_VAL_ARR_IOS (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_OFFSET (val1), + PVM_VAL_ARR_OFFSET (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_ELEMS_BOUND (val1), + PVM_VAL_ARR_ELEMS_BOUND (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_SIZE_BOUND (val1), + PVM_VAL_ARR_SIZE_BOUND (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_MAPPER (val1), + PVM_VAL_ARR_MAPPER (val2))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_WRITER (val1), + PVM_VAL_ARR_WRITER (val2))) + return 0; + + for (size_t i = 0 ; i < pvm_arr1_nelems ; i++) + { + if (!pvm_val_equal_p (PVM_VAL_ARR_ELEM_VALUE (val1, i), + PVM_VAL_ARR_ELEM_VALUE (val2, i))) + return 0; + + if (!pvm_val_equal_p (PVM_VAL_ARR_ELEM_OFFSET (val1, i), + PVM_VAL_ARR_ELEM_OFFSET (val2, i))) + return 0; + } + + 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) 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/Makefile.am b/testsuite/Makefile.am index 74743193..1aa239e1 100644 --- a/testsuite/Makefile.am +++ b/testsuite/Makefile.am @@ -1572,4 +1572,16 @@ EXTRA_DIST = \ poke.std/strchr-1.pk \ poke.std/strchr-2.pk \ poke.std/strchr-3.pk \ - poke.std/time32.pk + poke.std/time32.pk \ + poke.libpoke/pk_equal_int.test \ + poke.libpoke/pk_equal_uint.test \ + poke.libpoke/pk_equal_str.test \ + poke.libpoke/pk_equal_off.test \ + poke.libpoke/pk_equal_sct.test \ + poke.libpoke/pk_equal_arr.test \ + poke.libpoke/pk_nequal_int.test \ + poke.libpoke/pk_nequal_uint.test \ + poke.libpoke/pk_nequal_str.test \ + poke.libpoke/pk_nequal_off.test \ + poke.libpoke/pk_nequal_sct.test \ + poke.libpoke/pk_nequal_arr.test 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 +#include +#include +#include #include +#include #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; -- 2.17.1