File: test_creator.py

package info (click to toggle)
deap 1.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,372 kB
  • sloc: python: 9,874; ansic: 1,054; cpp: 592; javascript: 153; makefile: 95; sh: 7
file content (77 lines) | stat: -rw-r--r-- 2,591 bytes parent folder | download
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
#    This file is part of DEAP.
#
#    DEAP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of
#    the License, or (at your option) any later version.
#
#    DEAP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with DEAP. If not, see <http://www.gnu.org/licenses/>.

import unittest

import array

try:
    import numpy
except ImportError:
    numpy = False

from deap import creator

CNAME = "CLASS_NAME"


class TestCreator(unittest.TestCase):
    def tearDown(self):
        creator.__dict__.pop(CNAME)

    def test_create(self):
        creator.create(CNAME, list)
        l = creator.__dict__[CNAME]([1, 2, 3, 4])
        self.assertSequenceEqual(l, [1, 2, 3, 4])

    def test_attribute(self):
        creator.create(CNAME, list, a=1)
        l = creator.__dict__[CNAME]([1, 2, 3, 4])
        self.assertEqual(l.a, 1)

    def test_array(self):
        creator.create(CNAME, array.array, typecode="i")
        a = creator.__dict__[CNAME]([1, 2, 3, 4])
        b = creator.__dict__[CNAME]([5, 6, 7, 8])

        a[1:3], b[1:3] = b[1:3], a[1:3]
        ta = array.array("i", [1, 6, 7, 4])
        tb = array.array("i", [5, 2, 3, 8])
        self.assertSequenceEqual(a, ta)
        self.assertSequenceEqual(b, tb)

    @unittest.skipIf(not numpy, "Cannot import Numpy numerical library")
    def test_numpy_nocopy(self):
        creator.create(CNAME, numpy.ndarray)
        a = creator.__dict__[CNAME]([1, 2, 3, 4])
        b = creator.__dict__[CNAME]([5, 6, 7, 8])

        a[1:3], b[1:3] = b[1:3], a[1:3]
        ta = numpy.array([1, 6, 7, 4])
        tb = numpy.array([5, 6, 7, 8])
        numpy.testing.assert_array_equal(a, ta)
        numpy.testing.assert_array_equal(b, tb)

    @unittest.skipIf(not numpy, "Cannot import Numpy numerical library")
    def test_numpy_copy(self):
        creator.create(CNAME, numpy.ndarray)
        a = creator.__dict__[CNAME]([1, 2, 3, 4])
        b = creator.__dict__[CNAME]([5, 6, 7, 8])

        a[1:3], b[1:3] = b[1:3].copy(), a[1:3].copy()
        ta = numpy.array([1, 6, 7, 4])
        tb = numpy.array([5, 2, 3, 8])
        numpy.testing.assert_array_equal(a, ta)
        numpy.testing.assert_array_equal(b, tb)