[Mumps2Py:] [49] routines for doing Mumps2Py regression testing.

[ Thread Index | Date Index | More lists.mumps2py.org/discuss Archives ]


Revision: 49
Author:   pgallot
Date:     2008-01-24 21:15:11 +0000 (Thu, 24 Jan 2008)

Log Message:
-----------
routines for doing Mumps2Py regression testing.

Added Paths:
-----------
    trunk/mumps2py_test.py


Added: trunk/mumps2py_test.py
===================================================================
--- trunk/mumps2py_test.py	                        (rev 0)
+++ trunk/mumps2py_test.py	2008-01-24 21:15:11 UTC (rev 49)
@@ -0,0 +1,126 @@
+#!/usr/bin/python
+# Copyright 2008 Patrick Gallot
+
+##    This file is part of Mumps2Py.
+##
+##    Mumps2Py is free software: you can redistribute it and/or modify
+##    it under the terms of the GNU General Public License as published by
+##    the Free Software Foundation, either version 3 of the License, or
+##    (at your option) any later version.
+##
+##    Mumps2Py 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 General Public License for more details.
+##
+##    You should have received a copy of the GNU General Public License
+##    along with Mumps2Py.  If not, see <http://www.gnu.org/licenses/>.
+
+""" Provides a regression testing system for Mumps2Py """
+
+import ConfigParser, os, tempfile
+import mumps2py.mumps_module, mumps2py.mumps2tok, mumps2py.tok2python
+
+
+def compare_files(filename_1, filename_2):
+    """returns True if two (small) files match exactly"""
+    file_1 = open(filename_1)
+    file_2 = open(filename_2)
+    try:
+        file1_text= file_1.read()
+        file2_text= file_2.read()
+    finally:
+        file_1.close()
+        file_2.close()
+
+    return file1_text == file2_text
+
+def generate_outputoutput(test_file, out_file, out_dir):
+    """given a test_file, generate out_file from the Python translation"""
+
+    the_module = mumps2py.mumps_module.ModuleInfo(test_file, \
+                                                  out_dir, \
+                                                  "temp")
+    mumps2py.mumps2tok.parseMumps(the_module)
+    translation = mumps2py.tok2python.translate(the_module)
+    pytrans_file = the_module.output_file
+    print "writing translation to", pytrans_file
+    py_outputfile = open(pytrans_file,"w+")
+    py_outputfile.writelines(translation)
+    py_outputfile.close()
+
+    print "generating output output file:", out_file
+    os.system("python %s > %s" % (pytrans_file, out_file))
+    
+    
+def run_regression_tests(cfg_file_name = "regressiontests.cfg",
+                         results_file_name = "test_results.txt"):
+    """run all the tests listed in the config file"""
+    config = ConfigParser.ConfigParser()
+    resultcfg = ConfigParser.ConfigParser()
+    config.read(cfg_file_name)
+    for test_id in config.sections():
+        test_file = config.get(test_id,"input_file")
+        compare_file = config.get(test_id,"compare_file",test_file)
+        print test_id, test_file
+        
+        out_file = "result.txt"
+        generate_outputoutput(test_file, out_file, tempfile.gettempdir())
+
+        if compare_files(out_file, compare_file):
+            result = "Pass"
+        else:
+            result = "Fail"
+            
+        resultcfg.add_section(test_id)
+        resultcfg.set(test_id,"input_file",test_file)
+        resultcfg.set(test_id,"result",result)
+
+    results_file = open(results_file_name, 'w+')
+    resultcfg.write(results_file)
+    results_file.close()
+
+    results_file = open(results_file_name, 'r')
+    print results_file.read()
+    results_file.close()
+    
+
+def add_regression_test(test_file, cfg_file_name = "regressiontests.cfg"):
+    """given a Mumps input file, set up a new regression test"""
+    
+    try:
+        cfg_file = open(cfg_file_name,'r')
+    except IOError, e:
+        cfg_file = open(cfg_file_name,'w+')
+        config = ConfigParser.ConfigParser()
+        config.set("DEFAULT", "id", 0)
+        config.set("DEFAULT", "testdir", 'testfiles')
+        config.write(cfg_file)
+    cfg_file.close()
+
+#NOTE: should I check if a file has already been added ?
+    
+    config = ConfigParser.ConfigParser()
+    config.read(cfg_file_name)
+    new_id = config.getint("DEFAULT","id") + 1
+    test_id = "Test_%d" % (new_id)
+    config.set("DEFAULT", "id", new_id)
+    test_dir = config.get("DEFAULT","testdir")
+
+    config.add_section(test_id)
+    config.set(test_id,"input_file",test_file)
+
+    if os.path.isabs(test_file):
+        outname = os.path.basename(test_file)
+    else:
+        outname = test_file
+    out_file = test_dir + "/" + outname + ".oof" # output output file
+    config.set(test_id,"compare_file",out_file)
+    cfg_file = open(cfg_file_name,'w')
+    config.write(cfg_file)
+    cfg_file.close()
+
+    generate_outputoutput(test_file, out_file, test_dir)
+
+if __name__ == '__main__':
+    run_regression_tests()


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/