コード例 #1
0
ファイル: test_eugene.py プロジェクト: tmthydvnprt/eugene
    def setUp(self):
        """Setup Tests"""
        self.variable_node = Node('x')
        self.constant_node = Node(3)
        self.ephemeral_node = Node(EPHEMERAL[0])
        self.unary_node = Node('n_abs', Node(-3))
        self.binary_node = Node('n_add', Node(-3), Node(10))
        self.node = Node('n_add',
            Node('x'),
            Node('n_mul',
                Node(10),
                Node('n_abs',
                    Node(-4)
                )
            )
        )

        self.variable_node.set_nums()
        self.constant_node.set_nums()
        self.ephemeral_node.set_nums()
        self.unary_node.set_nums()
        self.binary_node.set_nums()
        self.node.set_nums()
コード例 #2
0
ファイル: test_eugene.py プロジェクト: tmthydvnprt/eugene
class NodeTests(unittest.TestCase):
    """
    Test Node Module.
    """

    def setUp(self):
        """Setup Tests"""
        self.variable_node = Node('x')
        self.constant_node = Node(3)
        self.ephemeral_node = Node(EPHEMERAL[0])
        self.unary_node = Node('n_abs', Node(-3))
        self.binary_node = Node('n_add', Node(-3), Node(10))
        self.node = Node('n_add',
            Node('x'),
            Node('n_mul',
                Node(10),
                Node('n_abs',
                    Node(-4)
                )
            )
        )

        self.variable_node.set_nums()
        self.constant_node.set_nums()
        self.ephemeral_node.set_nums()
        self.unary_node.set_nums()
        self.binary_node.set_nums()
        self.node.set_nums()

    def test_07_node_numbers(self):
        """Check node numbers"""
        node_nums = self.node.set_nums()
        self.assertEqual(node_nums, (5, 6, 4, 3, 5, 15))

    def test_08_node_string(self):
        """Check node string printing"""
        correct_str = 'n_add(eugene.Config.VAR[\'x\'], n_mul(10, n_abs(-4)))'
        node_str = self.node.__repr__()
        self.assertEqual(node_str, correct_str)

    def test_09_node_arity(self):
        """Check node Arity"""

        nodes = [
            self.variable_node,
            self.constant_node,
            self.ephemeral_node,
            self.unary_node,
            self.binary_node,
            self.node
        ]
        arities = [n.ary for n in nodes]
        self.assertEqual(arities, [0, 0, 0, 1, 2, 2])

    def test_10_node_leafiness(self):
        """Check node leafiness"""

        nodes = [
            self.variable_node,
            self.constant_node,
            self.ephemeral_node,
            self.unary_node,
            self.binary_node,
            self.binary_node.children[0],
            self.node
        ]
        leafiness = [n.is_leaf for n in nodes]
        self.assertEqual(leafiness, [True, True, True, False, False, True, False])

    def test_11_node_attributes(self):
        """Check node attributes"""
        correct_attributes = {
            # Current position of node
            'num'   : 0,
            'level' : 0,
            # Summary of children
            'height'   : 3,
            'node_num' : 6,
            'leaf_num' : 3,
            'edge_num' : 5,
            # Sum of subtrees
            'complexity' : 15
        }
        attributes = {
            # Current position of node
            'num'   : self.node.num,
            'level' : self.node.level,
            # Summary of children
            'height'   : self.node.height,
            'node_num' : self.node.node_num,
            'leaf_num' : self.node.leaf_num,
            'edge_num' : self.node.edge_num,
            # Sum of subtrees
            'complexity' : self.node.complexity
        }

        self.assertEqual(attributes, correct_attributes)