From 085effe1d1620f525da4ef6057db7ec662fce9df Mon Sep 17 00:00:00 2001 From: bleudev Date: Tue, 28 May 2024 20:55:22 +0300 Subject: [PATCH] final tests + fix math operations --- tests/test_udict.py | 69 +++++++++++++++++++++++++++++++++++++++------ ufpy/udict.py | 8 +++--- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/tests/test_udict.py b/tests/test_udict.py index 71c030c..cc0ba55 100644 --- a/tests/test_udict.py +++ b/tests/test_udict.py @@ -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__': diff --git a/ufpy/udict.py b/ufpy/udict.py index a5a305d..64777bf 100644 --- a/ufpy/udict.py +++ b/ufpy/udict.py @@ -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 @@ -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 @@ -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 @@ -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