7079464 Add math functions to the Size class
authorDrew Fisher <drew.fisher@oracle.com>
Tue, 16 Aug 2011 10:06:20 -0600
changeset 1390 b05ba57bf117
parent 1389 39e1a166f0c6
child 1391 3304042aeed6
7079464 Add math functions to the Size class
usr/src/lib/install_target/size.py
usr/src/lib/install_target/test/test_shadow_list.py
--- a/usr/src/lib/install_target/size.py	Tue Aug 16 07:55:17 2011 -0700
+++ b/usr/src/lib/install_target/size.py	Tue Aug 16 10:06:20 2011 -0600
@@ -181,3 +181,19 @@
             return 0
 
         raise TypeError("Size value is being compared to non-Size value")
+
+    def __add__(self, other):
+        """ eumulated method for adding two Size objects
+        """
+        return Size(str(self.byte_value + other.byte_value) + Size.byte_units)
+
+    def __sub__(self, other):
+        """ eumulated method for subtracting two Size objects
+        """
+        return Size(str(self.byte_value - other.byte_value) + Size.byte_units)
+
+    def __iadd__(self, other):
+        """ eumulated method for the augmented assignment for +=
+        """
+        self.byte_value += other.byte_value
+        return self
--- a/usr/src/lib/install_target/test/test_shadow_list.py	Tue Aug 16 07:55:17 2011 -0700
+++ b/usr/src/lib/install_target/test/test_shadow_list.py	Tue Aug 16 10:06:20 2011 -0600
@@ -1749,3 +1749,18 @@
         errsvc.clear_error_list()
         s.delete()
         self.assertFalse(errsvc._ERRORS)
+
+
+class TestSize(unittest.TestCase):
+
+    def test_add_size(self):
+        size1 = Size("1024mb")
+        size2 = Size("1024mb")
+        self.assertEqual(size1 + size2, Size("2gb"))
+        size1 += Size("3gb")
+        self.assertEqual(size1, Size("4gb"))
+
+    def test_sub_size(self):
+        size1 = Size("4096mb")
+        size2 = Size("2048mb")
+        self.assertEqual(size1 - size2, Size("2gb"))