aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/data/experimental/kernel_tests/indexed_dataset_ops_test.py
blob: c93a8353ce01063f52ecc68253df7d02a7689603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright 2017 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 experimental indexed dataset ops."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import unittest

from tensorflow.python.data.experimental.ops import indexed_dataset_ops
from tensorflow.python.data.kernel_tests import test_base
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops
from tensorflow.python.platform import test


class IndexedDatasetOpsTest(test_base.DatasetTestBase):

  def testLowLevelIndexedDatasetOps(self):
    identity = ged_ops.experimental_identity_indexed_dataset(
        ops.convert_to_tensor(16, dtype=dtypes.uint64))
    handle = ged_ops.experimental_materialized_index_dataset_handle(
        container="",
        shared_name="",
        output_types=[dtypes.uint64],
        output_shapes=[[]])
    materialize = ged_ops.experimental_indexed_dataset_materialize(
        identity, handle)
    index = array_ops.placeholder(dtypes.uint64)
    get_op = ged_ops.experimental_indexed_dataset_get(
        handle, index, output_types=[dtypes.uint64], output_shapes=[[]])

    with self.cached_session() as sess:
      sess.run(materialize)
      self.assertEqual([3], sess.run(get_op, feed_dict={index: 3}))

  def testIdentityIndexedDataset(self):
    ds = indexed_dataset_ops.IdentityIndexedDataset(16)
    materialized = ds.materialize()
    with self.cached_session() as sess:
      sess.run(materialized.initializer)
      placeholder = array_ops.placeholder(dtypes.uint64, shape=[])
      for i in range(16):
        output = sess.run(
            materialized.get(placeholder), feed_dict={placeholder: i})
        self.assertEqual([i], output)
      with self.assertRaises(errors.InvalidArgumentError):
        sess.run(materialized.get(placeholder), feed_dict={placeholder: 16})

  @unittest.skip("Requisite functionality currently unimplemented.")
  def testIdentityIndexedDatasetIterator(self):
    ds = indexed_dataset_ops.IdentityIndexedDataset(16)
    itr = ds.make_initializable_iterator()
    n = itr.get_next()
    with self.cached_session() as sess:
      sess.run(itr.initializer)
      for i in range(16):
        output = sess.run(n)
        self.assertEqual(i, output)
      with self.assertRaises(errors.OutOfRangeError):
        sess.run(n)

if __name__ == "__main__":
  test.main()