# =============================================================================
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
"""Test case base for testing proto operations."""

# Python3 preparedness imports.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import ctypes as ct
import os

from tensorflow.core.framework import types_pb2
from tensorflow.python.kernel_tests.proto import test_example_pb2
from tensorflow.python.platform import test


class ProtoOpTestBase(test.TestCase):
  """Base class for testing proto decoding and encoding ops."""

  def __init__(self, methodName="runTest"):  # pylint: disable=invalid-name
    super(ProtoOpTestBase, self).__init__(methodName)
    lib = os.path.join(os.path.dirname(__file__), "libtestexample.so")
    if os.path.isfile(lib):
      ct.cdll.LoadLibrary(lib)

  @staticmethod
  def named_parameters(extension=True):
    parameters = [("defaults", ProtoOpTestBase.defaults_test_case()),
                  ("minmax", ProtoOpTestBase.minmax_test_case()),
                  ("nested", ProtoOpTestBase.nested_test_case()),
                  ("optional", ProtoOpTestBase.optional_test_case()),
                  ("promote", ProtoOpTestBase.promote_test_case()),
                  ("ragged", ProtoOpTestBase.ragged_test_case()),
                  ("shaped_batch", ProtoOpTestBase.shaped_batch_test_case()),
                  ("simple", ProtoOpTestBase.simple_test_case())]
    if extension:
      parameters.append(("extension", ProtoOpTestBase.extension_test_case()))
    return parameters

  @staticmethod
  def defaults_test_case():
    test_case = test_example_pb2.TestCase()
    test_case.values.add()  # No fields specified, so we get all defaults.
    test_case.shapes.append(1)
    test_case.sizes.append(0)
    field = test_case.fields.add()
    field.name = "double_value_with_default"
    field.dtype = types_pb2.DT_DOUBLE
    field.value.double_value.append(1.0)
    test_case.sizes.append(0)
    field = test_case.fields.add()
    field.name = "float_value_with_default"
    field.dtype = types_pb2.DT_FLOAT
    field.value.float_value.append(2.0)
    test_case.sizes.append(0)
    field = test_case.fields.add()
    field.name = "int64_value_with_default"
    field.dtype = types_pb2.DT_INT64
    field.value.int64_value.append(3)
    test_case.sizes.append(0)
    field = test_case.fields.add()
    field.name = "sfixed64_value_with_default"