From: kostasch Date: Mon, 17 Aug 2020 15:02:15 +0300 Subject: [PATCH] pk_equal_val_p function and tests. 2020-08-17 Kostas Chasialis * libpoke/libpoke.h (pk_val_equal_p): Prototype. (pk_type_equal): Prototype. * libpoke/pk-val.c (pk_val_equal_p): Define. (pk_type_equal): Define. * libpoke/pvm-val.h: Fixd typo. * 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. --- ChangeLog | 28 +++ libpoke/libpoke.h | 6 + libpoke/pk-val.c | 5 + libpoke/pvm-val.c | 206 +++++++++++++++++++ libpoke/pvm-val.h | 2 +- libpoke/pvm.h | 8 + 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 ++++++++++++++++++++- 20 files changed, 522 insertions(+), 5 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 f1be4915..3ece00d1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,31 @@ +2020-08-17 Kostas Chasialis + + * libpoke/libpoke.h (pk_val_equal_p): Prototype. + (pk_type_equal): Prototype. + * libpoke/pk-val.c (pk_val_equal_p): Define. + (pk_type_equal): Define. + * libpoke/pvm-val.h: Fixd typo. + * 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. + 2020-08-07 Kostas Chasialis * libpoke/pvm-val.c (pvm_type_equal): Bugfixes. diff --git a/libpoke/libpoke.h b/libpoke/libpoke.h index 25d9c239..af184cae 100644 --- a/libpoke/libpoke.h +++ b/libpoke/libpoke.h @@ -812,6 +812,12 @@ pk_val pk_val_offset (pk_val val); /* Other operations on values. */ +/* Compare two Poke values. + + Returns 1 if they match, 0 otherwise. */ + +int pk_val_equal_p (pk_val val1, pk_val val2); + /* Return the type of the given value. */ pk_val pk_typeof (pk_val val); diff --git a/libpoke/pk-val.c b/libpoke/pk-val.c index 035d9fa5..5102d2da 100644 --- a/libpoke/pk-val.c +++ b/libpoke/pk-val.c @@ -373,6 +373,11 @@ pk_array_type_bound (pk_val type) return PVM_VAL_TYP_A_BOUND (type); } +int pk_val_equal_p (pk_val val1, pk_val val2) +{ + return pvm_val_equal_p (val1, val2); +} + pk_val pk_typeof (pk_val val) { diff --git a/libpoke/pvm-val.c b/libpoke/pvm-val.c index 2042cfa8..106f5739 100644 --- a/libpoke/pvm-val.c +++ b/libpoke/pvm-val.c @@ -348,6 +348,211 @@ 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; + + if (val1 == PVM_NULL || val2 == PVM_NULL) + return 0; + + if (!PVM_IS_TYP (val1) && !PVM_IS_TYP (val2)) + { + if (!(pvm_type_equal (pvm_typeof (val1), pvm_typeof (val2)))) + return 0; + } + else if (PVM_IS_TYP (val1) && PVM_IS_TYP (val2)) + return pvm_type_equal (val1, val2); + else + return 0; + + if (PVM_IS_INTEGRAL (val1)) + return PVM_VAL_INTEGRAL (val1) == PVM_VAL_INTEGRAL (val2); + else if (PVM_IS_STR (val1)) + return STREQ (PVM_VAL_STR (val1), PVM_VAL_STR (val2)); + else if (PVM_IS_OFF (val1)) + { + 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; + } + else if (PVM_IS_SCT (val1)) + { + 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_type_equal (pvm_sct1_type, pvm_sct2_type))) + return 0; + + pvm_sct1_off = PVM_VAL_SCT_OFFSET (val1); + pvm_sct2_off = PVM_VAL_SCT_OFFSET (val2); + + 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)) + 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_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_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_type = PVM_VAL_ARR_TYPE (val1); + pvm_arr2_type = PVM_VAL_ARR_TYPE (val2); + + if (!pvm_type_equal (pvm_arr1_type, pvm_arr2_type)) + return 0; + + pvm_arr1_nelems = PVM_VAL_INTEGRAL (PVM_VAL_ARR_NELEM (val1)); + pvm_arr2_nelems = PVM_VAL_INTEGRAL (PVM_VAL_ARR_NELEM (val2)); + + 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; + } + + return 1; + } + else + return 0; +} + void pvm_allocate_struct_attrs (pvm_val nfields, pvm_val **fnames, pvm_val **ftypes) @@ -1144,6 +1349,7 @@ pvm_type_equal (pvm_val type1, pvm_val type2) return 1; } + break; 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 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 +#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