test_unary.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Test compiler changes for unary ops (+, -, ~) introduced in Python 2.2"""
  2. import unittest
  3. class UnaryOpTestCase(unittest.TestCase):
  4. def test_negative(self):
  5. self.assertTrue(-2 == 0 - 2)
  6. self.assertTrue(-0 == 0)
  7. self.assertTrue(--2 == 2)
  8. self.assertTrue(-2L == 0 - 2L)
  9. self.assertTrue(-2.0 == 0 - 2.0)
  10. #self.assertTrue(-2j == 0 - 2j)
  11. def test_positive(self):
  12. self.assertTrue(+2 == 2)
  13. self.assertTrue(+0 == 0)
  14. self.assertTrue(++2 == 2)
  15. self.assertTrue(+2L == 2L)
  16. self.assertTrue(+2.0 == 2.0)
  17. #self.assertTrue(+2j == 2j)
  18. def test_invert(self):
  19. self.assertTrue(-2 == 0 - 2)
  20. self.assertTrue(-0 == 0)
  21. self.assertTrue(--2 == 2)
  22. self.assertTrue(-2L == 0 - 2L)
  23. self.assertTrue(~1L == 0 - 2L)
  24. self.assertTrue(~1000000 == -1000001L)
  25. def test_no_overflow(self):
  26. nines = "9" * 32
  27. #self.assertTrue(eval("+" + nines) == eval("+" + nines + "L"))
  28. #self.assertTrue(eval("-" + nines) == eval("-" + nines + "L"))
  29. #self.assertTrue(eval("~" + nines) == eval("~" + nines + "L"))
  30. def test_negation_of_exponentiation(self):
  31. # Make sure '**' does the right thing; these form a
  32. # regression test for SourceForge bug #456756.
  33. self.assertEqual(-2 ** 3, -8)
  34. self.assertEqual((-2) ** 3, -8)
  35. self.assertEqual(-2 ** 4, -16)
  36. self.assertEqual((-2) ** 4, 16)
  37. # def test_bad_types(self):
  38. # for op in '+', '-', '~':
  39. # self.assertRaises(TypeError, eval, op + "'a'")
  40. # if have_unicode:
  41. # self.assertRaises(TypeError, eval, op + "u'a'")
  42. # self.assertRaises(TypeError, eval, "~2j")
  43. # self.assertRaises(TypeError, eval, "~2.0")
  44. if __name__ == "__main__":
  45. unittest.main()