File: __init__.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 (90 lines) | stat: -rwxr-xr-x 2,576 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
"""!

@brief Unit-test runner for core wrapper.

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

"""

import unittest
from pyclustering.tests.suite_holder import suite_holder

# Generate images without having a window appear.
import matplotlib
matplotlib.use('Agg')


from pyclustering.core.tests import ut_package as core_package_unit_tests

import os

from pyclustering.core.definitions import PATH_PYCLUSTERING_CCORE_LIBRARY
from pyclustering.core.wrapper import ccore_library


class remove_library(object):
    """!
    @brief Decorator for tests where ccore library should be removed.
    
    """
    def __init__(self, call_object):
        self.call_object = call_object

    def __call__(self, *args):
        print("[TEST] Ignore next print related to problems with pyclustering ccore")
        test_result = True

        try:
            os.rename(PATH_PYCLUSTERING_CCORE_LIBRARY, PATH_PYCLUSTERING_CCORE_LIBRARY + "_corrupted")
            ccore_library.initialize()

            self.call_object(args)

        except:
            test_result = False

        os.rename(PATH_PYCLUSTERING_CCORE_LIBRARY + "_corrupted", PATH_PYCLUSTERING_CCORE_LIBRARY)
        ccore_library.initialize()

        if test_result is False:
            raise AssertionError("Test failed")


class corrupt_library(object):
    """!
    @brief Decorator for tests where ccore library should be corrupted.
    
    """
    def __init__(self, call_object):
        self.call_object = call_object

    def __create_corrupted_library(self, filepath):
        with open(filepath, 'wb') as binary_file_descriptor:
            binary_file_descriptor.write(bytes("corrupted binary library", 'UTF-8'))

    def __remove_corrupted_library(self, filepath):
        os.remove(filepath)

    def __call__(self, *args):
        os.rename(PATH_PYCLUSTERING_CCORE_LIBRARY, PATH_PYCLUSTERING_CCORE_LIBRARY + "_corrupted")
        self.__create_corrupted_library(PATH_PYCLUSTERING_CCORE_LIBRARY)
        
        ccore_library.initialize()

        self.call_object(args)

        self.__remove_corrupted_library(PATH_PYCLUSTERING_CCORE_LIBRARY)
        os.rename(PATH_PYCLUSTERING_CCORE_LIBRARY + "_corrupted", PATH_PYCLUSTERING_CCORE_LIBRARY)
        ccore_library.initialize()


class core_tests(suite_holder):
    def __init__(self):
        super().__init__()
        core_tests.fill_suite(self.get_suite())

    @staticmethod
    def fill_suite(core_suite):
        core_suite.addTests(unittest.TestLoader().loadTestsFromModule(core_package_unit_tests))