Skip to content

Commit

Permalink
final tests + fix math operations
Browse files Browse the repository at this point in the history
  • Loading branch information
bleudev committed May 28, 2024
1 parent d400623 commit 085effe
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
69 changes: 61 additions & 8 deletions tests/test_udict.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,73 @@ def test_set_item(self):
d[:] = [1, 2]
self.assertDictEqual(d.dictionary, {'hello': 1, 'hi': 2})

def test_del_item(self):
d = UDict(hello=1, hi=2)
del d[1]
self.assertDictEqual(d.dictionary, {'hi': 2})

def test_get(self):
d = UDict({2: 1, 4: 91, 1: 12})
self.assertEqual(d.get(index=1), d.get(key=2))
self.assertEqual(d.get(index=2), d.get(key=4))
self.assertEqual(d.get(index=3), d.get(key=1))

# TODO: test_del_item
# TODO: test_len_and_iter
# TODO: test_bool
# TODO: test_contains
# TODO: test_str_and_repr
# TODO: test_cmp_and_eq
# TODO: test_math_operations
# TODO: test_neg
def test_len_and_iter(self):
d = UDict(hello=1, hi=2)
self.assertEqual(len(d), 2)

l = []
for k, v in d:
l.append((k, v))

self.assertEqual(l, [
('hello', 1), ('hi', 2)
])

def test_bool(self):
d = UDict(hello=1, hi=2)
self.assertTrue(bool(d))

def test_contains(self):
d = UDict(hello=1, hi=2)
self.assertTrue('hello' in d)
self.assertTrue('hi' in d)
self.assertTrue(('hello', 1) in d)
self.assertTrue(('hi', 2) in d)

def test_str_and_repr(self):
d = {'hello': 1, 'hi': 2}
ud = UDict(d)

self.assertEqual(str(ud), str(d))
self.assertEqual(repr(ud), f'u{repr(d)}')

def test_cmp_and_eq(self):
d = {'hello': 1, 'hi': 2}
ud = UDict(d)

ud2 = UDict(hello=1, hi=2, world=3)

self.assertTrue(ud2 != ud)
self.assertTrue(ud2 > ud)
self.assertTrue(ud2 >= ud)
self.assertTrue(ud < ud2)
self.assertTrue(ud <= ud2)

self.assertEqual(d, ud)

def test_math_operations(self):
d = UDict(hello=1, hi=2)

self.assertEqual(d + {'world': 3}, UDict(hello=1, hi=2, world=3))
self.assertEqual(d - {'hi': 2}, UDict(hello=1))

self.assertEqual(d * 2, UDict(hello=2, hi=4))
self.assertEqual(d / 2, UDict(hello=0.5, hi=1))

def test_neg(self):
d = UDict(hello=1, hi=2)
self.assertEqual((-d).dictionary, {'hello': -1, 'hi': -2})


if __name__ == '__main__':
Expand Down
8 changes: 4 additions & 4 deletions ufpy/udict.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __eq__(self, other: 'dict[KT, VT] | UDict[KT, VT, DV]') -> bool:

# Math operations
def __add__(self, other: 'dict[KT, VT] | UDict[KT, VT, DV]') -> 'UDict[KT, VT, DV]':
new_dict = self.__dict
new_dict = self.__dict.copy()

if isinstance(other, UDict):
other: dict[KT, VT] = other.dictionary
Expand All @@ -206,7 +206,7 @@ def __add__(self, other: 'dict[KT, VT] | UDict[KT, VT, DV]') -> 'UDict[KT, VT, D
return UDict(new_dict)

def __sub__(self, other: 'dict[KT, VT] | UDict[KT, VT, DV]') -> 'UDict[KT, VT, DV]':
new_dict = self.__dict
new_dict = self.__dict.copy()

if isinstance(other, UDict):
other: dict[KT, VT] = other.dictionary
Expand All @@ -217,7 +217,7 @@ def __sub__(self, other: 'dict[KT, VT] | UDict[KT, VT, DV]') -> 'UDict[KT, VT, D
return UDict(new_dict)

def __mul__(self, other: 'dict[KT, float | int] | UDict[KT, float | int, DV] | float | int') -> 'UDict[KT, VT, DV]':
new_dict = self.__dict
new_dict = self.__dict.copy()

if isinstance(other, UDict):
other: dict[KT, VT] = other.dictionary
Expand All @@ -232,7 +232,7 @@ def __mul__(self, other: 'dict[KT, float | int] | UDict[KT, float | int, DV] | f
def __truediv__(
self, other: 'dict[KT, float | int] | UDict[KT, float | int, DV] | float | int'
) -> 'UDict[KT, VT, DV]':
new_dict = self.__dict
new_dict = self.__dict.copy()

if isinstance(other, UDict):
other: dict[KT, VT] = other.dictionary
Expand Down

0 comments on commit 085effe

Please sign in to comment.