Migrated datastructures utils doctests. Thanks to Stephan Jaekel.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13891 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-09-27 15:15:58 +00:00
parent 9abfd55a29
commit 082c5d2ae3
1 changed files with 60 additions and 45 deletions

View File

@ -1,52 +1,67 @@
""" from django.test import TestCase
>>> from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
>>> d = SortedDict() class DatastructuresTests(TestCase):
>>> d[7] = 'seven' def setUp(self):
>>> d[1] = 'one' self.d1 = SortedDict()
>>> d[9] = 'nine' self.d1[7] = 'seven'
>>> d.keys() self.d1[1] = 'one'
[7, 1, 9] self.d1[9] = 'nine'
>>> d.values()
['seven', 'one', 'nine']
>>> d.items()
[(7, 'seven'), (1, 'one'), (9, 'nine')]
# Overwriting an item keeps it's place. self.d2 = SortedDict()
>>> d[1] = 'ONE' self.d2[1] = 'one'
>>> d.values() self.d2[9] = 'nine'
['seven', 'ONE', 'nine'] self.d2[0] = 'nil'
self.d2[7] = 'seven'
# New items go to the end. def test_basic_methods(self):
>>> d[0] = 'nil' self.assertEquals(self.d1.keys(), [7, 1, 9])
>>> d.keys() self.assertEquals(self.d1.values(), ['seven', 'one', 'nine'])
[7, 1, 9, 0] self.assertEquals(self.d1.items(), [(7, 'seven'), (1, 'one'), (9, 'nine')])
# Deleting an item, then inserting the same key again will place it at the end. def test_overwrite_ordering(self):
>>> del d[7] """ Overwriting an item keeps it's place. """
>>> d.keys() self.d1[1] = 'ONE'
[1, 9, 0] self.assertEquals(self.d1.values(), ['seven', 'ONE', 'nine'])
>>> d[7] = 'lucky number 7'
>>> d.keys()
[1, 9, 0, 7]
# Changing the keys won't do anything, it's only a copy of the keys dict. def test_append_items(self):
>>> k = d.keys() """ New items go to the end. """
>>> k.remove(9) self.d1[0] = 'nil'
>>> d.keys() self.assertEquals(self.d1.keys(), [7, 1, 9, 0])
[1, 9, 0, 7]
# Initialising a SortedDict with two keys will just take the first one. A real def test_delete_and_insert(self):
# dict will actually take the second value so we will too, but we'll keep the """
# ordering from the first key found. Deleting an item, then inserting the same key again will place it
>>> tuples = ((2, 'two'), (1, 'one'), (2, 'second-two')) at the end.
>>> d = SortedDict(tuples) """
>>> d.keys() del self.d2[7]
[2, 1] self.assertEquals(self.d2.keys(), [1, 9, 0])
>>> real_dict = dict(tuples) self.d2[7] = 'lucky number 7'
>>> sorted(real_dict.values()) self.assertEquals(self.d2.keys(), [1, 9, 0, 7])
['one', 'second-two']
>>> d.values() # Here the order of SortedDict values *is* what we are testing
['second-two', 'one']
"""
def test_change_keys(self):
"""
Changing the keys won't do anything, it's only a copy of the
keys dict.
"""
k = self.d2.keys()
k.remove(9)
self.assertEquals(self.d2.keys(), [1, 9, 0, 7])
def test_init_keys(self):
"""
Initialising a SortedDict with two keys will just take the first one.
A real dict will actually take the second value so we will too, but
we'll keep the ordering from the first key found.
"""
tuples = ((2, 'two'), (1, 'one'), (2, 'second-two'))
d = SortedDict(tuples)
self.assertEquals(d.keys(), [2, 1])
real_dict = dict(tuples)
self.assertEquals(sorted(real_dict.values()), ['one', 'second-two'])
# Here the order of SortedDict values *is* what we are testing
self.assertEquals(d.values(), ['second-two', 'one'])