From 1602ae37471959f0c65e45871e59d715193d54ee Mon Sep 17 00:00:00 2001 From: Imran Ashraf Date: Mon, 29 Oct 2018 05:30:03 +0100 Subject: [PATCH] updated option testing, closes 168 test now sets all possible values for all the options to make sure it should not be problem. test also checks if the options which are selected are indeed set. tested that illegal option values should raise exception. --- ql/options.h | 2 +- tests/test_options.py | 83 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/ql/options.h b/ql/options.h index 176d2b2f3..7294fc00a 100644 --- a/ql/options.h +++ b/ql/options.h @@ -69,7 +69,7 @@ namespace ql { app->reset(); EOUT("Un-known option:"<< e.what()); - throw ql::exception("[x] Error parsing options. "+std::string(e.what())+" !",false); + throw ql::exception("Error parsing options. "+std::string(e.what())+" !",false); } app->reset(); } diff --git a/tests/test_options.py b/tests/test_options.py index 315b9a796..0c6e9dd57 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -7,20 +7,85 @@ class Test_options(unittest.TestCase): - def test_ok(self): - ql.set_option('output_dir', output_dir) + def tearDown(self): + ql.set_option('log_level', 'LOG_NOTHING') ql.set_option('optimize', 'no') ql.set_option('scheduler', 'ALAP') + ql.set_option('scheduler_uniform', 'no') + ql.set_option('use_default_gates', 'yes') + ql.set_option('decompose_toffoli', 'no') + + + def test_set_all_options(self): + # try to set all legal values of options + ql.set_option('log_level', 'LOG_NOTHING') + ql.set_option('log_level', 'LOG_CRITICAL') + ql.set_option('log_level', 'LOG_ERROR') ql.set_option('log_level', 'LOG_WARNING') + ql.set_option('log_level', 'LOG_INFO') + ql.set_option('log_level', 'LOG_DEBUG') + + ql.set_option('output_dir', output_dir) + + ql.set_option('optimize', 'yes') + ql.set_option('optimize', 'no') + + ql.set_option('scheduler', 'ALAP') + ql.set_option('scheduler', 'ASAP') + + ql.set_option('scheduler_uniform', 'yes') + ql.set_option('scheduler_uniform', 'no') + + ql.set_option('use_default_gates', 'yes') + ql.set_option('use_default_gates', 'no') + + ql.set_option('decompose_toffoli', 'no') + ql.set_option('decompose_toffoli', 'NC') + ql.set_option('decompose_toffoli', 'MA') + - @unittest.skip def test_nok(self): - # with self.assertRaises(RuntimeError) as cm: - try: - ql.set_option('optimize', 'Nope') - raise - except: - pass + # supress error printing first as the following will print errors + ql.set_option('log_level', 'LOG_NOTHING') + + # illegal values for options should raise errors + with self.assertRaises(Exception) as cm: + ql.set_option('optimize', 'nope') + + self.assertEqual(str(cm.exception), 'Error parsing options. The value nope is not an allowed value for --optimize !') + + + with self.assertRaises(Exception) as cm: + ql.set_option('scheduler', 'best') + + self.assertEqual(str(cm.exception), 'Error parsing options. The value best is not an allowed value for --scheduler !') + + + def test_get_values(self): + # try to set a legal value and then test if it is indeed set + ql.set_option('log_level', 'LOG_INFO') + self.assertEqual(ql.get_option('log_level'), 'LOG_INFO') + + ql.set_option('output_dir', output_dir) + self.assertEqual(ql.get_option('output_dir'), output_dir) + + ql.set_option('optimize', 'yes') + self.assertEqual(ql.get_option('optimize'), 'yes') + + + ql.set_option('scheduler', 'ALAP') + self.assertEqual(ql.get_option('scheduler'), 'ALAP') + + ql.set_option('scheduler_uniform', 'yes') + self.assertEqual(ql.get_option('scheduler_uniform'), 'yes') + + + ql.set_option('use_default_gates', 'yes') + self.assertEqual(ql.get_option('use_default_gates'), 'yes') + + ql.set_option('decompose_toffoli', 'NC') + self.assertEqual(ql.get_option('decompose_toffoli'), 'NC') + def test_default_scheduler(self): # tests if 'ALAP' is indeed the default scheduler policy