# Copyright 2019 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.
# ==============================================================================
"""Tests for ragged_array_ops.stack_dynamic_partitions."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import parameterized

from tensorflow.python.eager import context
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import data_flow_ops
from tensorflow.python.ops.ragged import ragged_array_ops
from tensorflow.python.ops.ragged import ragged_concat_ops
from tensorflow.python.ops.ragged import ragged_factory_ops
from tensorflow.python.platform import googletest


@test_util.run_all_in_graph_and_eager_modes
class RaggedSegmentStackOpTest(test_util.TensorFlowTestCase,
                               parameterized.TestCase):

  @parameterized.parameters([
      dict(  # empty inputs
          data=[],
          partitions=[],
          num_partitions=0,
          expected=[],
          expected_ragged_rank=1),
      dict(  # empty data, num_partitions>0
          data=[],
          partitions=[],
          num_partitions=3,
          expected=[[], [], []]),
      dict(  # 1D data, 1D partitions (docstring example)
          data=['a', 'b', 'c', 'd', 'e'],
          partitions=[3, 0, 2, 2, 3],
          num_partitions=5,
          expected=[['b'], [], ['c', 'd'], ['a', 'e'], []]),
      dict(  # 2D data, 1D partitions
          data=[['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h']],
          data_ragged_rank=0,
          partitions=[2, 1, 2, 3],
          num_partitions=4,
          expected=[[], [['c', 'd']], [['a', 'b'], ['e', 'f']], [['g', 'h']]],
          expected_ragged_rank=1),
      dict(  # 2D ragged data, 1D partitions
          data=[['a'], ['b', 'c', 'd'], [], ['e', 'f']],
          data_ragged_rank=1,
          partitions=[2, 1, 2, 3],
          num_partitions=4,
          expected=[[], [['b', 'c', 'd']], [['a'], []], [['e', 'f']]],
          expected_ragged_rank=2),
      dict(  # 2D data, 2D partitions
          data=[['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h']],
          data_ragged_rank=0,
          partitions=[[3, 0], [2, 2], [4, 3], [2, 0]],
          num_partitions=5,
          expected=[['b', 'h'], [], ['c', 'd', 'g'], ['a', 'f'], ['e']]),
      dict(  # 2D ragged data, 2D ragged partitions
          data=[['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h']],
          data_ragged_rank=0,
          partitions=[[3, 0], [2, 2], [4, 3], [2, 0]],
          num_partitions=5,
          expected=[['b', 'h'], [], ['c', 'd', 'g'], ['a', 'f'], ['e']]),
      dict(  # 3D data, 1d partitions
          data=[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]],
          data_ragged_rank=0,
          partitions=[1, 0],
          num_partitions=2,
          expected=[[[['e', 'f'], ['g', 'h']]], [[['a', 'b'], ['c', 'd']]]],
          expected_ragged_rank=1),