class ListBasedMutableSet(MutableSet):
def __init__(self, iterable=()):
self.elements = []
for item in iterable:
self.add(item)
def __contains__(self, item):
return item in self.elements
def __iter__(self):
return iter(self.elements)
def __len__(self):
return len(self.elements)
def add(self, item):
if item not in self.elements:
self.elements.append(item)
def discard(self, item):
try:
del self.elements[self.elements.index(item)]
except ValueError:
pass
So, to begin writing a data structure for a Set using TDD, you would first determine what sort of data will be stored, how that data might be represented in memory conceptually, and then figure out what sort of behaviors you will need to store, retrieve and manipulate that data.
That should be more than enough information to begin writing tests for methods and code that implement your Set data structure.
- it 'can be initialized with no values'
- it 'can be initialized with values'
- it 'adds a value to the set' do
- it 'does not duplicate values' do
- it 'removes a value from the set'when the given element is in the Set, return True
- 'when the given element is not in the Set'
length 'updates when elements are added'
' describe '#map'do
| | subject { Set.new([1, 2, 3]).map.to_a} | | :--- | :--- | | | | | | it 'iterates over the set'do | | | expect(subject).to include(1) | | | expect(subject).to include(2) | | | expect(subject).to include(3) | | | end |