From 72db474c846a1359fc4030adfb41d0c74741b98a Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 8 Aug 2014 11:03:08 +0100 Subject: [PATCH] Added new script for number comparison --- tests/Makefile.am | 5 ++- tests/bufrdc_ref.sh | 3 +- tests/number_compare.pl | 84 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) create mode 100755 tests/number_compare.pl diff --git a/tests/Makefile.am b/tests/Makefile.am index 6e8a9eed1..d2fba1285 100755 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -9,7 +9,8 @@ TESTS = definitions.sh \ bitmap.sh list.sh second_order.sh \ multi_from_message.sh change_scanning.sh \ julian.sh statistics.sh tigge.sh tigge_conversions.sh \ - read_any.sh padding.sh lamb_az_eq_area.sh grib_to_netcdf.sh bufrdc_ref.sh bufr_dump.sh debug.sh $(JPEG_TEST) + read_any.sh padding.sh lamb_az_eq_area.sh grib_to_netcdf.sh \ + bufrdc_ref.sh bufr_dump.sh debug.sh $(JPEG_TEST) noinst_PROGRAMS = packing_check gauss_sub read_any double_cmp packing pack_unpack \ multi_from_message julian read_index index gribex_perf\ @@ -37,5 +38,5 @@ LDADD = $(top_builddir)/src/libgrib_api.la $(EMOS_LIB) INCLUDES = -I$(top_builddir)/src EXTRA_DIST = $(TESTS) mf.rules filter_rules include.sh include.ctest.sh.in \ - lamb_az_eq_area.ref CMakeLists.txt + lamb_az_eq_area.ref CMakeLists.txt number_compare.pl diff --git a/tests/bufrdc_ref.sh b/tests/bufrdc_ref.sh index e70fee8e6..0ffd71567 100755 --- a/tests/bufrdc_ref.sh +++ b/tests/bufrdc_ref.sh @@ -30,7 +30,8 @@ do ${tools_dir}bufr_filter bufrdc_ref.filter $file 2> $REDIRECT > $res_num # Cannot use plain diff. We need to compare FLOAT NUMBERS with a tolerance - numdiff $ref_num $res_num >$REDIRECT + #numdiff $ref_num $res_num >$REDIRECT + perl number_compare.pl $ref_num $res_num >$REDIRECT 2> $REDIRECT rm -f $res_num diff --git a/tests/number_compare.pl b/tests/number_compare.pl new file mode 100755 index 000000000..69e55cb1e --- /dev/null +++ b/tests/number_compare.pl @@ -0,0 +1,84 @@ +#!/usr/local/bin/perl + eval 'exec perl -S $0 "$@"' + if $runnning_under_some_shell; + +######################################################################## +# +# Simple program to emulate numdiff +# It compares two files which must have the same number of lines +# ignoring small numeric differences or/and different numeric formats +# +# The user can specify an absolute tolerance for the comparisons +# +# It exits with status 0 if the files are considered the same +# otherwise status is 1. +# The actual differences are NOT printed +# +######################################################################## + +use strict; +use File::Compare; +use Getopt::Long; + +# Process arguments. Must be at least two files +if (scalar @ARGV < 2) { + &usage; +} +# Tolerance is optional +my $tolerance = 0.0000000000001; +my $result = GetOptions ( + "t=s" => \$tolerance + ); + +my $fileA = $ARGV[0]; +my $fileB = $ARGV[1]; +die "$!" unless (-e $fileA && -e $fileB); + +#print "DEBUG: f1=$fileA, f2=$fileB, tol=$tolerance\n"; + +use File::Compare 'cmp'; + +sub munge($) { + my $line = $_[0]; + for ($line) { + s/^\s+//; # Trim leading whitespace. + s/\s+$//; # Trim trailing whitespace. + } + return ($line); +} + +my $delta = $tolerance; + +if (not cmp($fileA, $fileB, sub {abs(munge $_[0] - munge $_[1])>$delta} )) +{ + #print "FLOAT: fileA and fileB are considered the same. HOORA\n"; + exit 0; +} + +# Comparison failed. Check if the files have different number of lines +my $linesA = 0; +my $linesB = 0; +open (FILE, $fileA) or die "Can't open $fileA: $!"; +$linesA++ while (); +close FILE; + +open (FILE, $fileB) or die "Can't open $fileB: $!"; +$linesB++ while (); +close FILE; + +if ($linesA != $linesB) { + print STDERR "Files do not have the same number of lines\n"; +} + +exit 1; # Files considered different + + +################################################### +sub usage { + print <