Commit 2793bb43 by Peter Baratta

Add few more tests; fix a small bug with parallel resistors

parent 2486f7c2
...@@ -144,6 +144,8 @@ def evaluator(variables, functions, string, cs=False): ...@@ -144,6 +144,8 @@ def evaluator(variables, functions, string, cs=False):
return x return x
def parallel(x): # Parallel resistors [ 1 2 ] => 2/3 def parallel(x): # Parallel resistors [ 1 2 ] => 2/3
# convert from pyparsing.ParseResults, which doesn't support '0 in x'
x = list(x)
if len(x) == 1: if len(x) == 1:
return x[0] return x[0]
if 0 in x: if 0 in x:
......
...@@ -92,6 +92,10 @@ class EvaluatorTest(unittest.TestCase): ...@@ -92,6 +92,10 @@ class EvaluatorTest(unittest.TestCase):
""" """
self.assertRaises(ZeroDivisionError, calc.evaluator, self.assertRaises(ZeroDivisionError, calc.evaluator,
{}, {}, '1/0') {}, {}, '1/0')
self.assertRaises(ZeroDivisionError, calc.evaluator,
{}, {}, '1/0.0')
self.assertRaises(ZeroDivisionError, calc.evaluator,
{'x': 0.0}, {}, '1/x')
def test_parallel_resistors(self): def test_parallel_resistors(self):
""" """
...@@ -107,12 +111,13 @@ class EvaluatorTest(unittest.TestCase): ...@@ -107,12 +111,13 @@ class EvaluatorTest(unittest.TestCase):
self.assertEqual(calc.evaluator({}, {}, '1||1||2'), 0.4) self.assertEqual(calc.evaluator({}, {}, '1||1||2'), 0.4)
self.assertEqual(calc.evaluator({}, {}, "j||1"), 0.5 + 0.5j) self.assertEqual(calc.evaluator({}, {}, "j||1"), 0.5 + 0.5j)
def test_parallel_resistors_zero_error(self): def test_parallel_resistors_with_zero(self):
""" """
Check the behavior of the || operator with 0 Check the behavior of the || operator with 0
""" """
self.assertRaises(ZeroDivisionError, calc.evaluator, self.assertTrue(numpy.isnan(calc.evaluator({}, {}, '0||1')))
{}, {}, '0||1') self.assertTrue(numpy.isnan(calc.evaluator({}, {}, '0.0||1')))
self.assertTrue(numpy.isnan(calc.evaluator({'x': 0.0}, {}, 'x||1')))
def assert_function_values(self, fname, ins, outs, tolerance=1e-3): def assert_function_values(self, fname, ins, outs, tolerance=1e-3):
""" """
......
...@@ -10,7 +10,6 @@ import random ...@@ -10,7 +10,6 @@ import random
import unittest import unittest
import textwrap import textwrap
import mock import mock
import textwrap
from . import new_loncapa_problem, test_system from . import new_loncapa_problem, test_system
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment