File size: 2,972 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
# ex: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:

# Author: Anton Deguet
# Date: 2010-01-20
#
# (C) Copyright 2010-2026 Johns Hopkins University (JHU), All Rights Reserved.

# --- begin cisst license - do not edit ---
# 
# This software is provided "as is" under an open source license, with
# no warranty.  The complete license can be found in license.txt and
# http://www.cisst.org/cisst/license.txt.
# 
# --- end cisst license ---

import unittest
import numpy

import cisstCommonPython
import cisstVectorPython
import cisstMultiTaskPython

class MatrixTest(unittest.TestCase):
    def setUp(self):
        """Call before every test case."""

    def tearDown(self):
        """Call after every test case."""
        
    def TestType(self):
        """Test mtsIntMat"""
        variable = cisstMultiTaskPython.mtsIntMat(3, 4)
        # check type
        self.assertTrue(isinstance(variable, cisstMultiTaskPython.mtsIntMat))
        self.assertTrue(isinstance(variable, cisstMultiTaskPython.mtsGenericObject))
        self.assertTrue(isinstance(variable, cisstCommonPython.cmnGenericObject))

    def TestDefaultConstructor(self):
        """Test default constructor"""
        variable = cisstMultiTaskPython.mtsDoubleMat()
        self.assertTrue(isinstance(variable, cisstMultiTaskPython.mtsDoubleMat))
        data = variable.Data()
        # this is an array
        self.assertTrue(isinstance(data, numpy.ndarray))
        # dimension is 2
        self.assertTrue(numpy.ndim(data) == 2)
        # size is 0 by default
        self.assertTrue(data.size == 0)

    def TestSizeConstructor(self):
        """Test constructor with size"""
        variable = cisstMultiTaskPython.mtsDoubleMat(10, 5)
        self.assertTrue(isinstance(variable, cisstMultiTaskPython.mtsDoubleMat))
        data = variable.Data()
        # this is an array
        self.assertTrue(isinstance(data, numpy.ndarray))
        # dimension is 2
        self.assertTrue(numpy.ndim(data) == 2)
        # size is 10 * 5 based on constructor parameter
        self.assertTrue(data.size == 10 * 5)
        self.assertTrue(data.shape == (10, 5))
        # make sure content is zeros
        zeroMatrix = numpy.zeros((10, 5), numpy.float64)
        allEqual = (zeroMatrix == data).all()
        self.assertTrue(allEqual)
        
    def TestModify(self):
        """Test data modification"""
        variable = cisstMultiTaskPython.mtsDoubleMat(3, 7)
        # data is a reference so modifying it should
        data = variable.Data()
        data.fill(5.0)
        data2 = variable.Data()
        # these are two different objects (Python)
        self.assertTrue(data is not data2)
        # but they should be equal
        allEqual = (data2 == data).all()
        self.assertTrue(allEqual)
        # always!
        data2[2, 2] = 10.0
        allEqual = (data2 == data).all()
        self.assertTrue(allEqual)