"""Tests for tensorflow.ops.ops."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow.python.platform

import numpy as np
import tensorflow as tf

from tensorflow.python.framework import random_seed
from tensorflow.python.ops import init_ops


# Returns true iff the two initalizers produce the same tensor to
# within a tiny tolerance.
def identicaltest(tc, init1, init2, use_gpu):
  """Tests if two initializations are identical to within tiny tolerances.

  Args:
    tc: An instance of TensorFlowTestCase.
    init1: An Initializer that generates a tensor of a given shape
    init2: An Initializer that generates a tensor of a given shape
    use_gpu: Use gpu if true.
  Returns:
    True or False as determined by test.
  """
  num = 100
  with tc.test_session(use_gpu=use_gpu, graph=tf.Graph()):
    t1 = init1([num]).eval()
  with tc.test_session(use_gpu=use_gpu, graph=tf.Graph()):
    t2 = init2([num]).eval()
  return np.allclose(t1, t2, rtol=1e-15, atol=1e-15)


def duplicated_initializer(tc, init, use_gpu, graph_seed):
  """Tests duplicated random initializer within the same graph.

  This test generates two random kernels from the same initializer to the same
  graph, and checks if the results are close enough. Even given the same global,
  seed, two different instances of random kernels should generate different
  results.

  Args:
    tc: An instance of TensorFlowTestCase.
    init: An Initializer that generates a tensor of a given shape
    use_gpu: Use gpu if true.
    graph_seed: A graph-level seed to use.
  Returns:
    True or False as determined by test.
  """
  num = 100
  with tc.test_session(use_gpu=use_gpu, graph=tf.Graph()):
    random_seed.set_random_seed(graph_seed)
    t1 = init([num]).eval()
    t2 = init([num]).eval()
    return np.allclose(t1, t2, rtol=1e-15, atol=1e-15)


def _init_sampler(tc, init, num, use_gpu):
  """Returns a func to generate a random tensor of shape [num].

  Args:
    tc: An instance of TensorFlowTestCase.
    init: An Initializer that generates a tensor of a given shape
    num: Size of 1D tensor to create.
    use_gpu: Use gpu if true.
  Returns:
    Function to generate a random tensor.
  """
  def func():
    with tc.test_session(use_gpu=use_gpu):
      return init([num]).eval()
  return func


class RandomNormalInitializationTest(tf.test.TestCase):

  def testInitializerIdentical(self):
    for use_gpu in [False, True]:
      init1 = tf.random_normal_initializer(0.0, 1.0, seed=1)
      init2 = tf.random_normal_initializer(0.0, 1.0, seed=1)
      self.assertTrue(identicaltest(self, init1, init2, use_gpu))

  def testInitializerDifferent(self):
    for use_gpu in [False, True]:
      init1 = tf.random_normal_initializer(0.0, 1.0, seed=1)
      init2 = tf.random_normal_initializer(0.0, 1.0, seed=2)
      self.assertFalse(identicaltest(self, init1, init2, use_gpu=use_gpu))

  def testDuplicatedInitializer(self):
    for use_gpu in [False, True]: