test_strcount.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import unittest
  2. class string_count(unittest.TestCase):
  3. def test_singles(self):
  4. self.assertEqual(1, 'abc'.count('a'))
  5. self.assertEqual(2, 'abcab'.count('b'))
  6. self.assertEqual(2, 'abc..abc'.count('.'))
  7. def test_multiples(self):
  8. self.assertEqual(2, 'abab'.count('ab'))
  9. self.assertEqual(2, 'abcab'.count('ab'))
  10. self.assertEqual(1, 'aaa'.count('aa'))
  11. def test_specials(self):
  12. self.assertEqual(2, '-abc-'.count('-'))
  13. self.assertEqual(2, '[[abc]'.count('['))
  14. self.assertEqual(2, '\\abc\\'.count('\\'))
  15. self.assertEqual(4, ']]]abc]'.count(']'))
  16. self.assertEqual(2, '{{}}'.count('{'))
  17. self.assertEqual(3, '{}}}'.count('}'))
  18. self.assertEqual(1, '(abc]'.count('('))
  19. self.assertEqual(2, 'abc))'.count(')'))
  20. self.assertEqual(3, 'a*b*c*d'.count('*'))
  21. self.assertEqual(2, 'a+b+c'.count('+'))
  22. self.assertEqual(2, '?abc?'.count('?'))
  23. self.assertEqual(2, 'abc..abc'.count('.'))
  24. self.assertEqual(4, 'a, b, c, d, '.count(','))
  25. self.assertEqual(1, 'a^b*c'.count('^'))
  26. self.assertEqual(3, '$$$'.count('$'))
  27. self.assertEqual(2, 'a|b|c'.count('|'))
  28. self.assertEqual(1, '#abc'.count('#'))
  29. self.assertEqual(1, ' '.count(' '))
  30. self.assertEqual(3, ' '.count(' '))
  31. if __name__ == "__main__":
  32. unittest.main()