'''
    This file contains test cases for tflearn
'''

import tensorflow.compat.v1 as tf
import tflearn
import unittest

class TestActivations(unittest.TestCase):
    '''
        This class contains test cases for the functions in tflearn/activations.py
    '''
    PLACES = 4 # Number of places to match when testing floating point values

    def test_linear(self):
        f = tflearn.linear

        # Case 1
        x = tf.placeholder(tf.float32, shape=())
        self.assertEqual(f(x), x)

        # Case 2
        x = tf.placeholder(tf.int64, shape=())
        self.assertEqual(f(x), x)

    def test_tanh(self):
        f = tflearn.tanh
        x = tf.placeholder(tf.float32, shape=())
        
        with tf.Session() as sess:
            # Case 1
            self.assertEqual(sess.run(f(x), feed_dict={x:0}), 0)

            # Case 2
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:0.5}),
                0.4621, places=TestActivations.PLACES)

            # Case 3
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:-0.25}),
                -0.2449, places=TestActivations.PLACES)

    def test_leaky_relu(self):
        f = lambda x: tflearn.leaky_relu(x, alpha=0.2)
        x = tf.placeholder(tf.float32, shape=())

        with tf.Session() as sess:
            # Case 1
            self.assertEqual(sess.run(f(x), feed_dict={x:0}), 0)

            # Case 2
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:1}),
                1, places=TestActivations.PLACES)

            # Case 3
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:-1}),
                -0.2, places=TestActivations.PLACES)

            # Case 4
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:-5}),
                -1, places=TestActivations.PLACES)

    def test_apply_activation(self):
        lrelu_02 = lambda x: tflearn.leaky_relu(x, alpha=0.2)
        x = tf.constant(-0.25, tf.float32)

        with tf.Session() as sess:
            # Case 1: 'linear'
            self.assertEqual(
                sess.run(tflearn.activation(x, 'linear')),
                -0.25)

            # Case 2: 'relu'
            self.assertEqual(
                sess.run(tflearn.activation(x, 'relu')),
                0)

            # Case 3: 'leaky_relu'
            self.assertAlmostEqual(
                sess.run(tflearn.activation(x, 'leaky_relu')),
                -0.025, places=TestActivations.PLACES)

            # Case 4: 'tanh'
            self.assertAlmostEqual(
                sess.run(tflearn.activation(x, 'tanh')),
                -0.2449, places=TestActivations.PLACES)

            # Case 5: lrelu_02 (callable)
            self.assertAlmostEqual(
                sess.run(tflearn.activation(x, lrelu_02)),
                -0.05, places=TestActivations.PLACES)

if __name__ == "__main__":
    unittest.main()
[((93, 4, 93, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((19, 12, 19, 48), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (), '', True, 'import tensorflow.compat.v1 as tf\n'), ((23, 12, 23, 46), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (), '', True, 'import tensorflow.compat.v1 as tf\n'), ((28, 12, 28, 48), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (), '', True, 'import tensorflow.compat.v1 as tf\n'), ((44, 12, 44, 48), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (), '', True, 'import tensorflow.compat.v1 as tf\n'), ((64, 12, 64, 42), 'tensorflow.compat.v1.constant', 'tf.constant', ({(64, 24, 64, 29): '-0.25', (64, 31, 64, 41): 'tf.float32'}, {}), '(-0.25, tf.float32)', True, 'import tensorflow.compat.v1 as tf\n'), ((30, 13, 30, 25), 'tensorflow.compat.v1.Session', 'tf.Session', ({}, {}), '()', True, 'import tensorflow.compat.v1 as tf\n'), ((43, 22, 43, 54), 'tflearn.leaky_relu', 'tflearn.leaky_relu', (), '', False, 'import tflearn\n'), ((46, 13, 46, 25), 'tensorflow.compat.v1.Session', 'tf.Session', ({}, {}), '()', True, 'import tensorflow.compat.v1 as tf\n'), ((63, 29, 63, 61), 'tflearn.leaky_relu', 'tflearn.leaky_relu', (), '', False, 'import tflearn\n'), ((66, 13, 66, 25), 'tensorflow.compat.v1.Session', 'tf.Session', ({}, {}), '()', True, 'import tensorflow.compat.v1 as tf\n'), ((69, 25, 69, 56), 'tflearn.activation', 'tflearn.activation', ({(69, 44, 69, 45): 'x', (69, 47, 69, 55): '"""linear"""'}, {}), "(x, 'linear')", False, 'import tflearn\n'), ((74, 25, 74, 54), 'tflearn.activation', 'tflearn.activation', ({(74, 44, 74, 45): 'x', (74, 47, 74, 53): '"""relu"""'}, {}), "(x, 'relu')", False, 'import tflearn\n'), ((79, 25, 79, 60), 'tflearn.activation', 'tflearn.activation', ({(79, 44, 79, 45): 'x', (79, 47, 79, 59): '"""leaky_relu"""'}, {}), "(x, 'leaky_relu')", False, 'import tflearn\n'), ((84, 25, 84, 54), 'tflearn.activation', 'tflearn.activation', ({(84, 44, 84, 45): 'x', (84, 47, 84, 53): '"""tanh"""'}, {}), "(x, 'tanh')", False, 'import tflearn\n'), ((89, 25, 89, 56), 'tflearn.activation', 'tflearn.activation', ({(89, 44, 89, 45): 'x', (89, 47, 89, 55): 'lrelu_02'}, {}), '(x, lrelu_02)', False, 'import tflearn\n')]