def testAdd(self): x = prefix("1 + 2") self.assertEqual(x, ("+", 1, 2)) x = prefix("1 + 2 + 3") self.assertEqual(x, ("+", ("+", 1, 2), 3)) x = prefix("1 + 2 + 3 + 4") self.assertEqual(x, ("+", ("+", ("+", 1, 2), 3), 4))
def testParenNumber(self): x = prefix("( 1 )") self.assertEqual(x, 1) x = prefix("( 10 )") self.assertEqual(x, 10) x = prefix("( 999 )") self.assertEqual(x, 999)
def testLetter(self): x = prefix("a") self.assertEqual(x, "a") x = prefix("z") self.assertEqual(x, "z") x = prefix("a + b + c + d") self.assertEqual(x, ("+", ("+", ("+", "a", "b"), "c"), "d"))
def testParenAdd(self): x = prefix("( 3 + 2 )") self.assertEqual(x, ("+", 3, 2)) x = prefix("5 + ( 3 + 2 )") self.assertEqual(x, ("+", 5, ("+", 3, 2))) x = prefix("( 3 + 2 ) + 5") self.assertEqual(x, ("+", ("+", 3, 2), 5))
def testNumber(self): x = prefix("1") self.assertEqual(x, 1) x = prefix("10") self.assertEqual(x, 10) x = prefix("999") self.assertEqual(x, 999)
def testParenMult(self): x = prefix("( 3 * 2 )") self.assertEqual(x, ("*", 3, 2)) x = prefix("5 * ( 3 * 2 )") self.assertEqual(x, ("*", 5, ("*", 3, 2))) x = prefix("( 3 * 2 ) * 5") self.assertEqual(x, ("*", ("*", 3, 2), 5))
def testEmpty(self): x = prefix("") self.assertEqual(x, None) x = prefix(" ") self.assertEqual(x, None) x = prefix("( )") self.assertEqual(x, None) x = prefix("( ( ) )") self.assertEqual(x, None)
def testPrecedence(self): x = prefix("2 * 3 + 5") self.assertEqual(x, ("+", ("*", 2, 3), 5)) x = prefix("5 + 2 * 3") self.assertEqual(x, ("+", 5, ("*", 2, 3))) x = prefix("1 + 2 * 3 + 5") self.assertEqual(x, ("+", ("+", 1, ("*", 2, 3)), 5)) x = prefix("1 * 2 + 3 * 5") self.assertEqual(x, ("+", ("*", 1, 2), ("*", 3, 5)))
def testPrefixExample(self): x = prefix("3") self.assertEqual(x, 3) x = prefix("1 + 1") self.assertEqual(x, ("+", 1, 1)) x = prefix("2 * 5 + 1") self.assertEqual(x, ("+", ("*", 2, 5), 1)) x = prefix("2 * ( 5 + 1 )") self.assertEqual(x, ("*", 2, ("+", 5, 1))) x = prefix("3 * x + ( 9 + y ) / 4") self.assertEqual(x, ("+", ("*", 3, "x"), ("/", ("+", 9, "y"), 4)))
def testParenError(self): with self.assertRaises(ParseError): prefix("( 1 ")
def testEmbedParenNumber(self): x = prefix("( ( 1 ) )") self.assertEqual(x, 1) x = prefix("( ( ( 1 ) ) )") self.assertEqual(x, 1)
def testParenLetter(self): x = prefix("( a )") self.assertEqual(x, "a") x = prefix("( z )") self.assertEqual(x, "z")
def testParenMixed(self): x = prefix("5 * ( 3 + 2 )") self.assertEqual(x, ("*", 5, ("+", 3, 2))) x = prefix("( 3 + 2 ) * 5") self.assertEqual(x, ("*", ("+", 3, 2), 5))
def testMult(self): x = prefix("3 * 2") self.assertEqual(x, ("*", 3, 2)) x = prefix("3 * 2 * 4") self.assertEqual(x, ("*", ("*", 3, 2), 4))