1. pygame
  2. pygame
  3. pygame

Commits

marcus  committed 9deb248

Added test cases and docs for the FRect.floor(), FRect.ceil(), FRect.trunc(), FRect.round() methods. Fixed bugs in the same methods. Limited the width height settings of the FRect class to positive values.

  • Participants
  • Parent commits e186f4e
  • Branches pgreloaded

Comments (0)

Files changed (3)

File doc/src/base.xml

View file
  • Ignore whitespace
       <desc>
         FRect.ceil () -> Rect
         
-        TODO
+        Creates a Rect from the specified FRect.
+
+        This creates a Rect using the smallest integral values greater
+        or equal to the FRect floating point values.
       </desc>
     </method>
     <attr name="center">
       <desc>
         FRect.floor () -> Rect
         
-        TODO
+        Creates a Rect from the specified FRect.
+
+        This creates a Rect using the largest integral values less than
+        or equal to the FRect floating point values.
       </desc>
     </method>
     <attr name="height">
       <desc>
         FRect.round () -> Rect
         
-        TODO
+        Creates a Rect from the specified FRect.
+
+        This creates a Rect using the FRect floating point values
+        rounded to the nearest integral value.
       </desc>
     </method>
     <attr name="size">
       <desc>
         FRect.trunc () -> Rect
         
-        TODO
+        Creates a Rect from the specified FRect.
+
+        This creates a Rect using truncated integral values from the 
+        Frect floating point values.
       </desc>
     </method>
     <method name="union">

File src/base/floatrect.c

View file
  • Ignore whitespace
     double w;
     if (!DoubleFromObj (value, &w))
         return -1;
+    if (w < 0)
+    {
+        PyErr_SetString (PyExc_ValueError, "width must not be negative");
+        return -1;
+    }
     ((PyFRect*)self)->w = w;
     return 0;
 }
     double h;
     if (!DoubleFromObj (value, &h))
         return -1;
+    if (h < 0)
+    {
+        PyErr_SetString (PyExc_ValueError, "height must not be negative");
+        return -1;
+    }
     ((PyFRect*)self)->h = h;
     return 0;
 }
     if (!DoubleFromSeqIndex (value, 1, &h))
         return -1;
 
+    if (w < 0 || h < 0)
+    {
+        PyErr_SetString (PyExc_ValueError,
+            "width and height must not be negative");
+        return -1;
+    }
+
     ((PyFRect*)self)->w = w;
     ((PyFRect*)self)->h = h;
     return 0;
     if (!rect)
         return NULL;
     rect->x = (pgint16) round (frect->x);
-    rect->x = (pgint16) round (frect->y);
+    rect->y = (pgint16) round (frect->y);
     rect->w = (pguint16) ((frect->w < 0) ? 0 : round (frect->w));
     rect->h = (pguint16) ((frect->h < 0) ? 0 : round (frect->h));
 
     if (!rect)
         return NULL;
     rect->x = (pgint16) ceil (frect->x);
-    rect->x = (pgint16) ceil (frect->y);
+    rect->y = (pgint16) ceil (frect->y);
     rect->w = (pguint16) ((frect->w < 0) ? 0 : ceil (frect->w));
     rect->h = (pguint16) ((frect->h < 0) ? 0 : ceil (frect->h));
 
     if (!rect)
         return NULL;
     rect->x = (pgint16) floor (frect->x);
-    rect->x = (pgint16) floor (frect->y);
+    rect->y = (pgint16) floor (frect->y);
     rect->w = (pguint16) ((frect->w < 0) ? 0 : floor (frect->w));
     rect->h = (pguint16) ((frect->h < 0) ? 0 : floor (frect->h));
 
     if (!rect)
         return NULL;
     rect->x = (pgint16) trunc (frect->x);
-    rect->x = (pgint16) trunc (frect->y);
+    rect->y = (pgint16) trunc (frect->y);
     rect->w = (pguint16) ((frect->w < 0) ? 0 : trunc (frect->w));
     rect->h = (pguint16) ((frect->h < 0) ? 0 : trunc (frect->h));
 

File test/base_frect_test.py

View file
  • Ignore whitespace
     import test.pgunittest as unittest
 except:
     import pgunittest as unittest
-from pygame2.base import FRect
+from pygame2.base import FRect, Rect
 
 class FRectTest (unittest.TestCase):
 
         self.assertEqual( expected_topleft, r.topleft )
         self.assertEqual( old_size, r.size )
 
-    def todo_test_pygame2_base_FRect_ceil(self):
+    def test_pygame2_base_FRect_ceil(self):
 
-        # __doc__ (as of 2008-10-17) for pygame2.base.FRect.ceil:
+        # __doc__ (as of 2008-11-04) for pygame2.base.FRect.ceil:
 
         # FRect.ceil () -> Rect
+        #
+        # Creates a Rect from the specified FRect.
         # 
-        # TODO
-
-        self.fail() 
+        # FRect.ceil () -> Rect Creates a Rect from the specified FRect.
+        # This creates a Rect using the smallest integral values greater
+        # or equal to the FRect floating point values.
+        r = FRect (2.1, -2.9, 5.8, 3.01)
+        self.assertEqual (r.ceil (), Rect (3, -2, 6, 4))
 
     def test_pygame2_base_FRect_center(self):
 
 
         self.fail() 
 
-    def todo_test_pygame2_base_FRect_floor(self):
+    def test_pygame2_base_FRect_floor(self):
 
-        # __doc__ (as of 2008-10-17) for pygame2.base.FRect.floor:
+        # __doc__ (as of 2008-11-04) for pygame2.base.FRect.floor:
 
         # FRect.floor () -> Rect
-        # 
-        # TODO
-
-        self.fail() 
+        #
+        # Creates a Rect from the specified FRect.
+        #
+        # FRect.floor () -> Rect Creates a Rect from the specified FRect.
+        # This creates a Rect using the largest integral values less than
+        # or equal to the FRect floating point values.
+        r = FRect (2.1, -2.9, 5.8, 3.01)
+        self.assertEqual (r.floor (), Rect (2, -3, 5, 3))
 
     def test_pygame2_base_FRect_height(self):
 
         self.assertEqual( expected_left, r.left )
         self.assertEqual( old_width, r.width )
 
-    def todo_test_pygame2_base_FRect_round(self):
+    def test_pygame2_base_FRect_round(self):
 
-        # __doc__ (as of 2008-10-17) for pygame2.base.FRect.round:
+        # __doc__ (as of 2008-11-04) for pygame2.base.FRect.round:
 
         # FRect.round () -> Rect
         # 
-        # TODO
-
-        self.fail() 
+        # Creates a Rect from the specified FRect.
+        # 
+        # FRect.round () -> Rect Creates a Rect from the specified FRect.
+        # This creates a Rect using the FRect floating point values
+        # rounded to the nearest integral value.
+        r = FRect (2.1, -2.9, 5.8, 3.01)
+        self.assertEqual (r.round (), Rect (2, -3, 6, 3))
 
     def test_pygame2_base_FRect_size(self):
 
         self.assertEqual( expected_topleft, r.topleft )
         self.assertEqual( old_size, r.size )
 
-    def todo_test_pygame2_base_FRect_trunc(self):
+    def test_pygame2_base_FRect_trunc(self):
 
-        # __doc__ (as of 2008-10-17) for pygame2.base.FRect.trunc:
+        # __doc__ (as of 2008-11-04) for pygame2.base.FRect.trunc:
 
         # FRect.trunc () -> Rect
-        # 
-        # TODO
+        #
+        # Creates a Rect from the specified FRect.
 
-        self.fail() 
+        # FRect.trunc() -> Rect. Creates a Rect from the specified FRect.
+        # This creates a Rect using truncated integral values from the 
+        # Frect floating point values.
+        r = FRect (-1.57, 2.99, 8.1, 5.77)
+        self.assertEqual (r.trunc (), Rect (-1, 2, 8, 5))
 
     def test_pygame2_base_FRect_union(self):