File: ut_package.py

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (136 lines) | stat: -rwxr-xr-x 4,514 bytes parent folder | download | duplicates (2)
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
"""!

@brief Unit-tests for pyclustering package that is used for exchange between ccore library and python code.

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

"""


import unittest

import numpy

from pyclustering.core.pyclustering_package import package_builder, package_extractor

from ctypes import c_ulong, c_size_t, c_double, c_uint, c_float, c_char_p


class Test(unittest.TestCase):
    def templatePackUnpack(self, dataset, c_type_data=None):
        package_pointer = package_builder(dataset, c_type_data).create()
        unpacked_package = package_extractor(package_pointer).extract()

        packing_data = dataset
        if isinstance(packing_data, numpy.ndarray):
            packing_data = dataset.tolist()

        if isinstance(packing_data, str):
            self.assertEqual(dataset, unpacked_package)
        else:
            self.assertTrue(self.compare_containers(packing_data, unpacked_package))


    def compare_containers(self, container1, container2):
        def is_container(container):
            return isinstance(container, list) or isinstance(container, tuple)
        
        if len(container1) == 0 and len(container2) == 0:
            return True

        if len(container1) != len(container2):
            return False
        
        for index in range(len(container1)):
            if is_container(container1[index]) and is_container(container2[index]):
                return self.compare_containers(container1[index], container2[index])
            
            elif is_container(container1[index]) == is_container(container2[index]):
                if container1[index] != container2[index]:
                    return False
            
            else:
                return False
            
            return True


    def testListInteger(self):
        self.templatePackUnpack([1, 2, 3, 4, 5])

    def testListIntegerSingle(self):
        self.templatePackUnpack([2])

    def testListIntegerNegative(self):
        self.templatePackUnpack([-1, -2, -10, -20])

    def testListIntegerNegativeAndPositive(self):
        self.templatePackUnpack([-1, 26, -10, -20, 13])

    def testListFloat(self):
        self.templatePackUnpack([1.1, 1.2, 1.3, 1.4, 1.5, 1.6])

    def testListFloatNegativeAndPositive(self):
        self.templatePackUnpack([1.1, -1.2, -1.3, -1.4, 1.5, -1.6])

    def testListLong(self):
        self.templatePackUnpack([100000000, 2000000000])

    def testListEmpty(self):
        self.templatePackUnpack([])

    def testListOfListInteger(self):
        self.templatePackUnpack([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ])

    def testListOfListDouble(self):
        self.templatePackUnpack([ [1.1, 5.4], [1.3], [1.4, -9.4] ])

    def testListOfListWithGaps(self):
        self.templatePackUnpack([ [], [1, 2, 3], [], [4], [], [5, 6, 7] ])

    def testListSpecifyUnsignedLong(self):
        self.templatePackUnpack([1, 2, 3, 4, 5], c_ulong)

    def testListSpecifyUnsignedSizeT(self):
        self.templatePackUnpack([1, 2, 3, 4, 5], c_size_t)

    def testListSpecifyDouble(self):
        self.templatePackUnpack([1.1, 1.6, -7.8], c_double)

    def testListOfListSpecifySizeT(self):
        self.templatePackUnpack([ [1, 2, 3], [4, 5] ], c_size_t)

    def testListOfListSpecifyUnsignedIntWithGaps(self):
        self.templatePackUnpack([ [1, 2, 3], [], [4, 5], [], [] ], c_uint)

    def testListOfListEmpty(self):
        self.templatePackUnpack([ [], [], [] ])

    def testListOfListOfListInteger(self):
        self.templatePackUnpack([ [ [1], [2] ], [ [3], [4] ], [ [5, 6], [7, 8] ] ])

    def testTupleInterger(self):
        self.templatePackUnpack([ (1, 2, 3), (4, 5), (6, 7, 8, 9) ], c_uint)

    def testTupleFloat(self):
        self.templatePackUnpack([ (1.0, 2.0, 3.8), (4.6, 5.0), (6.8, 7.4, 8.5, 9.6) ], c_float)

    def testTupleEmpty(self):
        self.templatePackUnpack([(), (), ()])

    def testNumpyMatrixOneColumn(self):
        self.templatePackUnpack(numpy.array([[1.0], [2.0], [3.0]]), c_double)

    def testNumpyMatrixTwoColumns(self):
        self.templatePackUnpack(numpy.array([[1.0, 1.0], [2.0, 2.0]]), c_double)

    def testNumpyMatrixThreeColumns(self):
        self.templatePackUnpack(numpy.array([[1.1, 2.2, 3.3], [2.2, 3.3, 4.4], [3.3, 4.4, 5.5]]), c_double)

    def testString(self):
        self.templatePackUnpack("Test message number one".encode('utf-8'))

    def testEmptyString(self):
        self.templatePackUnpack("".encode('utf-8'))