pytilities.geometry.test.vector

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

# Source Code for Module pytilities.geometry.test.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 ..vector import Vector
     25  from ..boundvector import BoundVector
     26  from ..immutablevector import ImmutableVector
     27  from ..verbosevector import VerboseVector
     28
     29 -class VectorTestCase(TestCase):
     30 -    def setUp(self):
     31          self.p = Vector(0, 5)
     32          self.p2 = Vector(2.0, 5.0)
     33
     34 -    def test_get_xy(self):
     35          # V(0, 5)
     36          self.assertEqual(self.p.x, 0)
     37          self.assertEqual(self.p.y, 5)
     38          self.assertEqual(self.p.xy, (0, 5))
     39
     40 -    def test_set_x(self):
     41          self.p.x = 2
     42          self.assertEqual(self.p.xy, (2, 5))
     43
     44 -    def test_set_y(self):
     45          self.p.y = 4
     46          self.assertEqual(self.p.xy, (0, 4))
     47
     48 -    def test_move_to(self):
     49          self.p.move_to(4, 7)
     50          self.assertEqual(self.p.xy, (4, 7))
     51
     52 -    def test_move_by(self):
     53          self.p.move_by(-1, 2)
     54          self.assertEqual(self.p.xy, (-1, 7))
     55
     56 -    def test_immutable_operators(self):
     57          p2 = Vector(2.0, 5.0)
     58
     59          v = -self.p
     60          self.assert_(isinstance(v, Vector))
     61          self.assertEqual(v.xy, (0, -5))
     62
     63          v = self.p + p2
     64          self.assert_(isinstance(v, Vector))
     65          self.assertEqual(v.xy, (2, 10))
     66
     67          v = self.p - p2
     68          self.assert_(isinstance(v, Vector))
     69          self.assertEqual(v.xy, (-2, 0))
     70
     71          v = self.p * 2
     72          self.assert_(isinstance(v, Vector))
     73          self.assertEqual(v.xy, (0, 10))
     74
     75          v = self.p / 2
     76          self.assert_(isinstance(v, Vector))
     77          self.assertEqual(v.xy, (0, 2))
     78
     79          v = p2 / 2
     80          self.assert_(isinstance(v, Vector))
     81          self.assertEqual(v.xy, (1, 2.5))
     82
     83 -    def test_iadd(self):
     84          if isinstance(self.p, BoundVector):
     85              pass #pdb.set_trace()
     86
     87          self.p += self.p2
     88          self.assertEqual(self.p.xy, (2, 10))
     89
     90 -    def test_isub(self):
     91          self.p -= self.p2
     92          self.assertEqual(self.p.xy, (-2, 0))
     93
     94 -    def test_imul(self):
     95          self.p *= 2
     96          self.assertEqual(self.p.xy, (0, 10))
     97
     98 -    def test_idiv(self):
     99          self.p /= 2
    100          self.assertEqual(self.p.xy, (0, 2))
    101
    102 -    def test_copy(self):
    103          clone = self.p.copy()
    104          self.assert_(self.p is not clone)
    105          self.assert_(isinstance(clone, Vector))
    106          self.assertEqual(clone.xy, self.p.xy)
    107          self.assertEqual(clone, self.p)
    108
    109 -    def test_assign(self):
    110          v = Vector(2, 7)
    111          self.p.assign(v)
    112          self.assertEqual(self.p.xy, (2, 7))
    113
    114  # BoundVector test helper
    115 -class VHolder(object):
    116      @property
    117 -    def x(self):
    118          return self.__x
    119
    120      @x.setter
    121 -    def x(self, value):
    122          self.__x = value
    123
    124      @property
    125 -    def y(self):
    126          return self.__y
    127
    128      @y.setter
    129 -    def y(self, value):
    130          self.__y = value
    131
    132 -    def __init__(self):
    133          self.__x = 0
    134          self.__y = 5
    135          self.p = BoundVector(self, "x", "y")
    136
    137  # Bound vectors should support the same things as regular vectors
    138 -class BoundVectorTestCase(VectorTestCase):
    139 -    def setUp(self):
    140          VectorTestCase.setUp(self)
    141
    142          self.vholder = VHolder()
    143          self.p = self.vholder.p
    144
    145 -    def test_boundness(self):
    146          # V(0, 5)
    147          self.p.x = 3
    148          self.assertEqual(self.p.xy, (3, 5))
    149          self.assertEqual(self.vholder.x, 3)
    150
    151  # test whether 2 bound vectors don't affect each other
    152 -class DoubleBoundVectorTestCase(TestCase):
    153 -    def test_double_bound_vector(self):
    154          a = VHolder()
    155          b = VHolder()
    156
    157          self.assert_(isinstance(a.p, BoundVector))
    158          self.assertFalse(a.p is b.p)
    159          self.assertEquals(a.p.xy, (0, 5))
    160          self.assertEquals(b.p.xy, (0, 5))
    161
    162          b.p.y = 3
    163          self.assertEquals(a.p.xy, (0, 5))
    164          self.assertEquals(b.p.xy, (0, 3))
    165
    166          b.p.assign(Vector(3, 8))
    167          self.assertEquals(a.p.xy, (0, 5))
    168          self.assertEquals(b.p.xy, (3, 8))
    169
    170
    171 -class ImmutableVectorTestCase(VectorTestCase):
    172 -    def setUp(self):
    173          VectorTestCase.setUp(self)
    174          self.p = ImmutableVector(self.p)
    175
    176 -    def test_set_x(self):
    177          self.assertRaises(AssertionError,
    178                            VectorTestCase.test_set_x, self)
    179
    180 -    def test_set_y(self):
    181          self.assertRaises(AssertionError,
    182                            VectorTestCase.test_set_y, self)
    183
    184 -    def test_move_to(self):
    185          self.assertRaises(AssertionError,
    186                            VectorTestCase.test_move_to, self)
    187
    188 -    def test_move_by(self):
    189          self.assertRaises(AssertionError,
    190                            VectorTestCase.test_move_by, self)
    191
    192 -    def test_iadd(self):
    193          self.assertRaises(AssertionError,
    194                            VectorTestCase.test_iadd, self)
    195
    196 -    def test_isub(self):
    197          self.assertRaises(AssertionError,
    198                            VectorTestCase.test_isub, self)
    199
    200 -    def test_imul(self):
    201          self.assertRaises(AssertionError,
    202                            VectorTestCase.test_imul, self)
    203
    204 -    def test_idiv(self):
    205          self.assertRaises(AssertionError,
    206                            VectorTestCase.test_idiv, self)
    207
    208 -    def test_assign(self):
    209          self.assertRaises(AssertionError,
    210                            VectorTestCase.test_assign, self)
    211
    212
    213 -class VerboseVectorTestCase(VectorTestCase):
    214 -    def setUp(self):
    215          VectorTestCase.setUp(self)
    216          self.p = VerboseVector(self.p)
    217
    218
    219
    220 -def run():
    221      unittest.main(__name__)
    222