Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(62)

Side by Side Diff: Lib/test/test_collections.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 6 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_codeop.py ('k') | Lib/test/test_compile.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Unit tests for collections.py.""" 1 """Unit tests for collections.py."""
2 2
3 import unittest, doctest, operator 3 import unittest, doctest, operator
4 from test.support import TESTFN, forget, unlink 4 from test.support import TESTFN, forget, unlink
5 import inspect 5 import inspect
6 from test import support 6 from test import support
7 from collections import namedtuple, Counter, OrderedDict, _count_elements 7 from collections import namedtuple, Counter, OrderedDict, _count_elements
8 from test import mapping_tests 8 from test import mapping_tests
9 import pickle, copy 9 import pickle, copy
10 from random import randrange, shuffle 10 from random import randrange, shuffle
(...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 self.contents = [1, 2, 3] 630 self.contents = [1, 2, 3]
631 def __contains__(self, x): 631 def __contains__(self, x):
632 return x in self.contents 632 return x in self.contents
633 def __len__(self): 633 def __len__(self):
634 return len(self.contents) 634 return len(self.contents)
635 def __iter__(self): 635 def __iter__(self):
636 return iter(self.contents) 636 return iter(self.contents)
637 def __hash__(self): 637 def __hash__(self):
638 return self._hash() 638 return self._hash()
639 a, b = OneTwoThreeSet(), OneTwoThreeSet() 639 a, b = OneTwoThreeSet(), OneTwoThreeSet()
640 self.assertTrue(hash(a) == hash(b)) 640 self.assertEqual(hash(a), hash(b))
641 641
642 def test_MutableSet(self): 642 def test_MutableSet(self):
643 self.assertIsInstance(set(), MutableSet) 643 self.assertIsInstance(set(), MutableSet)
644 self.assertTrue(issubclass(set, MutableSet)) 644 self.assertTrue(issubclass(set, MutableSet))
645 self.assertNotIsInstance(frozenset(), MutableSet) 645 self.assertNotIsInstance(frozenset(), MutableSet)
646 self.assertFalse(issubclass(frozenset, MutableSet)) 646 self.assertFalse(issubclass(frozenset, MutableSet))
647 self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', ' __len__', 647 self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', ' __len__',
648 'add', 'discard') 648 'add', 'discard')
649 649
650 def test_issue_5647(self): 650 def test_issue_5647(self):
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 copy.deepcopy(words), 923 copy.deepcopy(words),
924 pickle.loads(pickle.dumps(words, 0)), 924 pickle.loads(pickle.dumps(words, 0)),
925 pickle.loads(pickle.dumps(words, 1)), 925 pickle.loads(pickle.dumps(words, 1)),
926 pickle.loads(pickle.dumps(words, 2)), 926 pickle.loads(pickle.dumps(words, 2)),
927 pickle.loads(pickle.dumps(words, -1)), 927 pickle.loads(pickle.dumps(words, -1)),
928 eval(repr(words)), 928 eval(repr(words)),
929 update_test, 929 update_test,
930 Counter(words), 930 Counter(words),
931 ]): 931 ]):
932 msg = (i, dup, words) 932 msg = (i, dup, words)
933 self.assertTrue(dup is not words) 933 self.assertIsNot(dup, words)
934 self.assertEqual(dup, words) 934 self.assertEqual(dup, words)
935 self.assertEqual(len(dup), len(words)) 935 self.assertEqual(len(dup), len(words))
936 self.assertEqual(type(dup), type(words)) 936 self.assertEqual(type(dup), type(words))
937 937
938 def test_copy_subclass(self): 938 def test_copy_subclass(self):
939 class MyCounter(Counter): 939 class MyCounter(Counter):
940 pass 940 pass
941 c = MyCounter('slartibartfast') 941 c = MyCounter('slartibartfast')
942 d = c.copy() 942 d = c.copy()
943 self.assertEqual(d, c) 943 self.assertEqual(d, c)
944 self.assertEqual(len(d), len(c)) 944 self.assertEqual(len(d), len(c))
945 self.assertEqual(type(d), type(c)) 945 self.assertEqual(type(d), type(c))
946 946
947 def test_conversions(self): 947 def test_conversions(self):
948 # Convert to: set, list, dict 948 # Convert to: set, list, dict
949 s = 'she sells sea shells by the sea shore' 949 s = 'she sells sea shells by the sea shore'
950 self.assertEqual(sorted(Counter(s).elements()), sorted(s)) 950 self.assertEqual(sorted(Counter(s).elements()), sorted(s))
951 self.assertEqual(sorted(Counter(s)), sorted(set(s))) 951 self.assertEqual(sorted(Counter(s)), sorted(set(s)))
952 self.assertEqual(dict(Counter(s)), dict(Counter(s).items())) 952 self.assertEqual(dict(Counter(s)), dict(Counter(s).items()))
953 self.assertEqual(set(Counter(s)), set(s)) 953 self.assertEqual(set(Counter(s)), set(s))
954 954
955 def test_invariant_for_the_in_operator(self): 955 def test_invariant_for_the_in_operator(self):
956 c = Counter(a=10, b=-2, c=0) 956 c = Counter(a=10, b=-2, c=0)
957 for elem in c: 957 for elem in c:
958 self.assertTrue(elem in c) 958 self.assertIn(elem, c)
959 self.assertIn(elem, c) 959 self.assertIn(elem, c)
960 960
961 def test_multiset_operations(self): 961 def test_multiset_operations(self):
962 # Verify that adding a zero counter will strip zeros and negatives 962 # Verify that adding a zero counter will strip zeros and negatives
963 c = Counter(a=10, b=-2, c=0) + Counter() 963 c = Counter(a=10, b=-2, c=0) + Counter()
964 self.assertEqual(dict(c), dict(a=10)) 964 self.assertEqual(dict(c), dict(a=10))
965 965
966 elements = 'abcd' 966 elements = 'abcd'
967 for i in range(1000): 967 for i in range(1000):
968 # test random pairs of multisets 968 # test random pairs of multisets
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
1218 copy.deepcopy(od), 1218 copy.deepcopy(od),
1219 pickle.loads(pickle.dumps(od, 0)), 1219 pickle.loads(pickle.dumps(od, 0)),
1220 pickle.loads(pickle.dumps(od, 1)), 1220 pickle.loads(pickle.dumps(od, 1)),
1221 pickle.loads(pickle.dumps(od, 2)), 1221 pickle.loads(pickle.dumps(od, 2)),
1222 pickle.loads(pickle.dumps(od, 3)), 1222 pickle.loads(pickle.dumps(od, 3)),
1223 pickle.loads(pickle.dumps(od, -1)), 1223 pickle.loads(pickle.dumps(od, -1)),
1224 eval(repr(od)), 1224 eval(repr(od)),
1225 update_test, 1225 update_test,
1226 OrderedDict(od), 1226 OrderedDict(od),
1227 ]): 1227 ]):
1228 self.assertTrue(dup is not od) 1228 self.assertIsNot(dup, od)
1229 self.assertEqual(dup, od) 1229 self.assertEqual(dup, od)
1230 self.assertEqual(list(dup.items()), list(od.items())) 1230 self.assertEqual(list(dup.items()), list(od.items()))
1231 self.assertEqual(len(dup), len(od)) 1231 self.assertEqual(len(dup), len(od))
1232 self.assertEqual(type(dup), type(od)) 1232 self.assertEqual(type(dup), type(od))
1233 1233
1234 def test_yaml_linkage(self): 1234 def test_yaml_linkage(self):
1235 # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature. 1235 # Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
1236 # In yaml, lists are native but tuples are not. 1236 # In yaml, lists are native but tuples are not.
1237 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)] 1237 pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
1238 od = OrderedDict(pairs) 1238 od = OrderedDict(pairs)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
1346 NamedTupleDocs = doctest.DocTestSuite(module=collections) 1346 NamedTupleDocs = doctest.DocTestSuite(module=collections)
1347 test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, 1347 test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs,
1348 TestCollectionABCs, TestCounter, TestChainMap, 1348 TestCollectionABCs, TestCounter, TestChainMap,
1349 TestOrderedDict, GeneralMappingTests, SubclassMappingTests] 1349 TestOrderedDict, GeneralMappingTests, SubclassMappingTests]
1350 support.run_unittest(*test_classes) 1350 support.run_unittest(*test_classes)
1351 support.run_doctest(collections, verbose) 1351 support.run_doctest(collections, verbose)
1352 1352
1353 1353
1354 if __name__ == "__main__": 1354 if __name__ == "__main__":
1355 test_main(verbose=True) 1355 test_main(verbose=True)
OLDNEW
« no previous file with comments | « Lib/test/test_codeop.py ('k') | Lib/test/test_compile.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+