Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 183a5a4439
Fetching contributors…

Cannot retrieve contributors at this time

file 239 lines (182 sloc) 7.03 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
# Test the array slots

import unittest
from cgkit import _core
from cgkit.all import *

class TestArraySlot(unittest.TestCase):
    
    def testConstructor(self):
        
        asl = _core.DoubleArraySlot(1)
        # array size==0?
        self.assertEqual(asl.size(), 0)
        # multiplicity==1?
        self.assertEqual(asl.multiplicity(), 1)
        self.assertEqual(list(asl), [])
        self.assertEqual(asl.getController(), None)

        asl = _core.DoubleArraySlot()
        # array size==0?
        self.assertEqual(asl.size(), 0)
        # multiplicity==1?
        self.assertEqual(asl.multiplicity(), 1)
        self.assertEqual(list(asl), [])

        asl = _core.DoubleArraySlot(3)
        # array size==0?
        self.assertEqual(asl.size(), 0)
        # multiplicity==3?
        self.assertEqual(asl.multiplicity(), 3)
        self.assertEqual(list(asl), [])

    def testResize(self):

        asl = _core.DoubleArraySlot(1)

        # Create 8 elements
        asl.resize(8)
        self.assertEqual(asl.size(), 8)
        self.assertEqual(list(asl), 8*[0])

        # Set values
        for i in range(8):
            asl[i]=i+1

        # Check if the values were properly set
        self.assertEqual(list(asl), range(1,9))

        # Enlarge the array
        asl.resize(20)
        self.assertEqual(asl.size(), 20)
        # Check if the values were properly copied
        self.assertEqual(list(asl), range(1,9)+12*[0])

        # Shrink the array
        asl.resize(4)
        # Check if the values were properly copied
        self.assertEqual(list(asl), range(1,5))

        # Negative size must equal 0
        asl.resize(-2)
        self.assertEqual(asl.size(), 0)

    def testSizeConstraints(self):
        """Check if the size constraint really constrains the size.
"""
        
        usc = UserSizeConstraint(4)
        at = _core.DoubleArraySlot(1, usc)
        # Size must be 4
        self.assertEqual(at.size(), 4)
        # resize() must raise an exception
        self.assertRaises(ValueError, lambda: at.resize(5))

        # Resize the constraint
        usc.setSize(7)
        self.assertEqual(at.size(), 7)

        # Check that a slot with a different size cannot serve as controller
        a2 = _core.DoubleArraySlot(1)
        a2.resize(10)
        self.assertRaises(ValueError, lambda: at.setController(a2))

        # But it can be controller when it has the same size...
        a2.resize(7)
        at.setController(a2)
        # Check that the controller cannot be resized then...
        self.assertRaises(ValueError, lambda: a2.resize(10))
        self.assertEqual(usc.getSize(), 7)
        self.assertEqual(at.size(), 7)

        # Check the isResizable() method
        self.assertEqual(True, a2.isResizable(7))
        self.assertEqual(True, at.isResizable(7))
        self.assertEqual(False, a2.isResizable(42))
        self.assertEqual(False, at.isResizable(42))

    def testLinearSizeConstraints(self):
        """Test the LinearSizeConstraint class.
"""

        a1 = _core.IntArraySlot(1)
        a1.resize(4)
        
        lsc = LinearSizeConstraint(a1, 2, 1)
        self.assertEqual(lsc.getSize(), 9)

        # Create a slot that depends on the size of a1
        a2 = _core.IntArraySlot(1, lsc)
        self.assertEqual(a2.size(), 9)

        a1.resize(6)
        self.assertEqual(a2.size(), 13)

    def testMultipleSizeConstraints(self):
        """Test several constraints that influence each other.
"""

        a1 = _core.IntArraySlot(1)
        a1.resize(4)

        lsc = LinearSizeConstraint(a1,1,1)
        a2 = _core.IntArraySlot(1, lsc)

        usc = UserSizeConstraint(5)
        a3 = _core.IntArraySlot(1, usc)

        a4 = _core.IntArraySlot(1)
        
        self.assertEqual(0, a3[1])

        a2.connect(a4)
        a2.connect(a3)
        self.assertEqual(5, a4.size())
        a2[1] = 12;
        self.assertEqual(12, a2[1])
        self.assertEqual(12, a3[1])
        self.assertEqual(12, a4[1])

        # Resizing the first slot must fail because of the other, size
        # constraint slots...
        self.assertRaises(ValueError, lambda: a1.resize(2))

        # Check that the slots still have the original size...
        self.assertEqual(4, a1.size())
        self.assertEqual(5, a2.size())
        self.assertEqual(5, a3.size())
        self.assertEqual(5, a4.size())

    def testGetSetMult1(self):

        # Multiplicity 1
        asl = _core.Vec3ArraySlot(1)
        asl.resize(10)

        for i in range(10):
            asl.setValue(i, vec3(i+1))

        for i in range(10):
            self.assertEqual(asl.getValue(i), vec3(i+1))

        for i in range(10):
            self.assertEqual(asl[i], vec3(i+1))

    def testGetSetMultn(self):

        # Multiplicity 3
        asl = _core.IntArraySlot(3)
        asl.resize(10)

        for i in range(10):
            asl.setValue(i, (i, i+1, i+2))

        for i in range(10):
            self.assertEqual(asl.getValue(i), (i,i+1,i+2))

        for i in range(10):
            self.assertEqual(asl[i], (i,i+1,i+2))

    def testController(self):

        # Controller slot
        ctrl = _core.Vec3ArraySlot()
        ctrl.resize(10)
        for i in range(10):
            ctrl[i] = vec3(i+1)

        # "Target" slot
        asl = _core.Vec3ArraySlot()
        asl.setController(ctrl)
        self.assertEqual(asl.size(), 10)
        for i in range(10):
            self.assertEqual(asl[i], vec3(i+1))

        # Change an item in the controller...
        ctrl[1]=vec3(-1,-2,-3)
        # ...and see if the target also has changed
        for i in range(10):
            if i==1:
                self.assertEqual(asl[i], vec3(-1,-2,-3))
            else:
                self.assertEqual(asl[i], vec3(i+1))

        # Disconnect
        asl.setController(None)
        # modify an element
        asl[1] = vec3(2,2,2)
        # and check if ctrl and target are separate
        for i in range(10):
            if i==1:
                self.assertEqual(ctrl[i], vec3(-1,-2,-3))
            else:
                self.assertEqual(ctrl[i], vec3(i+1))
        
        for i in range(10):
            self.assertEqual(asl[i], vec3(i+1))
        

    def testExceptions(self):
        
        asl = _core.DoubleArraySlot(1)
        asl.resize(8)
        self.assertRaises(IndexError, lambda: asl[8])
        self.assertRaises(IndexError, lambda: asl.getValue(8))
        self.assertRaises(IndexError, lambda: asl.setValue(8,0))
        self.assertRaises(TypeError, lambda: asl.setValue(0,vec3()))

        asl = _core.DoubleArraySlot(2)
        asl.resize(8)
        self.assertRaises(IndexError, lambda: asl[8])
        self.assertRaises(IndexError, lambda: asl.getValue(8))
        self.assertRaises(IndexError, lambda: asl.setValue(8,(1,2)))
        self.assertRaises(ValueError, lambda: asl.setValue(0,5))


######################################################################

if __name__=="__main__":
    unittest.main()
Something went wrong with that request. Please try again.