pytilities.test.geometry.vector

Package pytilities :: Package test :: Package geometry :: Module vector |
[hide private]
[frames] | no frames]

# Source Code for Module pytilities.test.geometry.vector

      1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net>
      2  #
      3  # This file is part of pytilities.
      4  #
      5  # pytilities is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # pytilities is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>.
     17  #
     18
     19  __docformat__ = 'reStructuredText'
     20
     21  from unittest import TestCase
     22  import unittest
     23
     24  from pytilities.geometry import Vector, BoundVector, ImmutableVector, VerboseVector
     25
     26 -class VectorTestCase(TestCase):
     27 -    def setUp(self):
     28          self.p = Vector(0, 5)
     29          self.p2 = Vector(2.0, 5.0)
     30
     31 -    def test_get_xy(self):
     32          # V(0, 5)
     33          self.assertEqual(self.p.x, 0)
     34          self.assertEqual(self.p.y, 5)
     35          self.assertEqual(self.p.xy, (0, 5))
     36
     37 -    def test_set_x(self):
     38          self.p.x = 2
     39          self.assertEqual(self.p.xy, (2, 5))
     40
     41 -    def test_set_y(self):
     42          self.p.y = 4
     43          self.assertEqual(self.p.xy, (0, 4))
     44
     45 -    def test_move_to(self):
     46          self.p.move_to(4, 7)
     47          self.assertEqual(self.p.xy, (4, 7))
     48
     49 -    def test_move_by(self):
     50          self.p.move_by(-1, 2)
     51          self.assertEqual(self.p.xy, (-1, 7))
     52
     53 -    def test_immutable_operators(self):
     54          p2 = Vector(2.0, 5.0)
     55
     56          v = -self.p
     57          self.assert_(isinstance(v, Vector))
     58          self.assertEqual(v.xy, (0, -5))
     59
     60          v = self.p + p2
     61          self.assert_(isinstance(v, Vector))
     62          self.assertEqual(v.xy, (2, 10))
     63
     64          v = self.p - p2
     65          self.assert_(isinstance(v, Vector))
     66          self.assertEqual(v.xy, (-2, 0))
     67
     68          v = self.p * 2
     69          self.assert_(isinstance(v, Vector))
     70          self.assertEqual(v.xy, (0, 10))
     71
     72          v = self.p / 2
     73          self.assert_(isinstance(v, Vector))
     74          self.assertEqual(v.xy, (0, 2))
     75
     76          v = p2 / 2
     77          self.assert_(isinstance(v, Vector))
     78          self.assertEqual(v.xy, (1, 2.5))
     79
     80 -    def test_iadd(self):
     81          if isinstance(self.p, BoundVector):
     82              pass #pdb.set_trace()
     83
     84          self.p += self.p2
     85          self.assertEqual(self.p.xy, (2, 10))
     86
     87 -    def test_isub(self):
     88          self.p -= self.p2
     89          self.assertEqual(self.p.xy, (-2, 0))
     90
     91 -    def test_imul(self):
     92          self.p *= 2
     93          self.assertEqual(self.p.xy, (0, 10))
     94
     95 -    def test_idiv(self):
     96          self.p /= 2
     97          self.assertEqual(self.p.xy, (0, 2))
     98
     99 -    def test_copy(self):
    100          clone = self.p.copy()
    101          self.assert_(self.p is not clone)
    102          self.assert_(isinstance(clone, Vector))
    103          self.assertEqual(clone.xy, self.p.xy)
    104          self.assertEqual(clone, self.p)
    105
    106 -    def test_assign(self):
    107          v = Vector(2, 7)
    108          self.p.assign(v)
    109          self.assertEqual(self.p.xy, (2, 7))
    110
    111  # BoundVector test helper
    112 -class VHolder(object):
    113      @property
    114 -    def x(self):
    115          return self.__x
    116
    117      @x.setter
    118 -    def x(self, value):
    119          self.__x = value
    120
    121      @property
    122 -    def y(self):
    123          return self.__y
    124
    125      @y.setter
    126 -    def y(self, value):
    127          self.__y = value
    128
    129 -    def __init__(self):
    130          self.__x = 0
    131          self.__y = 5
    132          self.p = BoundVector(self, "x", "y")
    133
    134  # Bound vectors should support the same things as regular vectors
    135 -class BoundVectorTestCase(VectorTestCase):
    136 -    def setUp(self):
    137          VectorTestCase.setUp(self)
    138
    139          self.vholder = VHolder()
    140          self.p = self.vholder.p
    141
    142 -    def test_boundness(self):
    143          # V(0, 5)
    144          self.p.x = 3
    145          self.assertEqual(self.p.xy, (3, 5))
    146          self.assertEqual(self.vholder.x, 3)
    147
    148  # test whether 2 bound vectors don't affect each other
    149 -class DoubleBoundVectorTestCase(TestCase):
    150 -    def test_double_bound_vector(self):
    151          a = VHolder()
    152          b = VHolder()
    153
    154          self.assert_(isinstance(a.p, BoundVector))
    155          self.assertFalse(a.p is b.p)
    156          self.assertEquals(a.p.xy, (0, 5))
    157          self.assertEquals(b.p.xy, (0, 5))
    158
    159          b.p.y = 3
    160          self.assertEquals(a.p.xy, (0, 5))
    161          self.assertEquals(b.p.xy, (0, 3))
    162
    163          b.p.assign(Vector(3, 8))
    164          self.assertEquals(a.p.xy, (0, 5))
    165          self.assertEquals(b.p.xy, (3, 8))
    166
    167
    168 -class ImmutableVectorTestCase(VectorTestCase):
    169 -    def setUp(self):
    170          VectorTestCase.setUp(self)
    171          self.p = ImmutableVector(self.p)
    172
    173 -    def test_set_x(self):
    174          self.assertRaises(AssertionError,
    175                            VectorTestCase.test_set_x, self)
    176
    177 -    def test_set_y(self):
    178          self.assertRaises(AssertionError,
    179                            VectorTestCase.test_set_y, self)
    180
    181 -    def test_move_to(self):
    182          self.assertRaises(AssertionError,
    183                            VectorTestCase.test_move_to, self)
    184
    185 -    def test_move_by(self):
    186          self.assertRaises(AssertionError,
    187                            VectorTestCase.test_move_by, self)
    188
    189 -    def test_iadd(self):
    190          self.assertRaises(AssertionError,
    191                            VectorTestCase.test_iadd, self)
    192
    193 -    def test_isub(self):
    194          self.assertRaises(AssertionError,
    195                            VectorTestCase.test_isub, self)
    196
    197 -    def test_imul(self):
    198          self.assertRaises(AssertionError,
    199                            VectorTestCase.test_imul, self)
    200
    201 -    def test_idiv(self):
    202          self.assertRaises(AssertionError,
    203                            VectorTestCase.test_idiv, self)
    204
    205 -    def test_assign(self):
    206          self.assertRaises(AssertionError,
    207                            VectorTestCase.test_assign, self)
    208
    209
    210 -class VerboseVectorTestCase(VectorTestCase):
    211 -    def setUp(self):
    212          VectorTestCase.setUp(self)
    213          self.p = VerboseVector(self.p)
    214