From cd661801df778b7a4e2111a376c4f82b7bf36a1e Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Tue, 8 Mar 2016 14:22:58 +0100 Subject: [PATCH] word: throw an exception when printing an empty word Fixes #146. * spot/twaalgos/word.cc: Here. * tests/python/word.ipynb: Add a test case. --- spot/twaalgos/word.cc | 3 ++- tests/python/word.ipynb | 42 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/spot/twaalgos/word.cc b/spot/twaalgos/word.cc index 9db5c7ed5..746873d5c 100644 --- a/spot/twaalgos/word.cc +++ b/spot/twaalgos/word.cc @@ -93,6 +93,8 @@ namespace spot std::ostream& operator<<(std::ostream& os, const twa_word& w) { + if (w.cycle.empty()) + throw std::runtime_error("a twa_word may not have an empty cycle"); auto d = w.get_dict(); if (!w.prefix.empty()) for (auto& i: w.prefix) @@ -100,7 +102,6 @@ namespace spot bdd_print_formula(os, d, i); os << "; "; } - assert(!w.cycle.empty()); bool notfirst = false; os << "cycle{"; for (auto& i: w.cycle) diff --git a/tests/python/word.ipynb b/tests/python/word.ipynb index 09ce3a6c9..ad820de94 100644 --- a/tests/python/word.ipynb +++ b/tests/python/word.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:a4f8f6e783c38abbf8cce75fb3eb8d9344ece79c11ad5c58556df2db3e40d6a2" + "signature": "sha256:261c16336ba5deefb7e9ebe705cc5c24f1cbba8622030874d2719f5045289c53" }, "nbformat": 3, "nbformat_minor": 0, @@ -139,7 +139,7 @@ "\n" ], "text": [ - " *' at 0x7fcdb40da870> >" + " *' at 0x7fa33c302870> >" ] } ], @@ -454,7 +454,7 @@ "\n" ], "text": [ - " *' at 0x7fcdb40da8d0> >" + " *' at 0x7fa33c3028d0> >" ] } ], @@ -546,6 +546,42 @@ } ], "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Creating an empty word is OK...\n", + "w = spot.twa_word(spot._bdd_dict)" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 16 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# ... as long as this word is not printed.\n", + "print(w)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "RuntimeError", + "evalue": "a twa_word may not have an empty cycle", + "output_type": "pyerr", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# ... as long as this word is not printed.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mw\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/home/adl/git/spot/python/spot/impl.py\u001b[0m in \u001b[0;36m__str__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 3511\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3512\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__str__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;34m\"std::string\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3513\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_impl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtwa_word___str__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3514\u001b[0m \u001b[0mtwa_word_swigregister\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_impl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtwa_word_swigregister\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3515\u001b[0m \u001b[0mtwa_word_swigregister\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtwa_word\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mRuntimeError\u001b[0m: a twa_word may not have an empty cycle" + ] + } + ], + "prompt_number": 17 } ], "metadata": {}