1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/core/data/compression_utils.h"
16
17 #include "tensorflow/core/data/dataset_test_base.h"
18 #include "tensorflow/core/framework/tensor_testutil.h"
19 #include "tensorflow/core/platform/status_matchers.h"
20 #include "tensorflow/core/platform/test.h"
21
22 namespace tensorflow {
23 namespace data {
24
25 namespace {
26 using ::tensorflow::testing::StatusIs;
27 using ::testing::HasSubstr;
28 } // namespace
29
TEST(CompressionUtilsTest,Exceeds4GB)30 TEST(CompressionUtilsTest, Exceeds4GB) {
31 std::vector<Tensor> element = {
32 CreateTensor<int64_t>(TensorShape{1024, 1024, 513})}; // Just over 4GB.
33 CompressedElement compressed;
34 EXPECT_THAT(CompressElement(element, &compressed),
35 StatusIs(error::OUT_OF_RANGE,
36 HasSubstr("exceeding the 4GB Snappy limit")));
37 }
38
39 class ParameterizedCompressionUtilsTest
40 : public DatasetOpsTestBase,
41 public ::testing::WithParamInterface<std::vector<Tensor>> {};
42
TEST_P(ParameterizedCompressionUtilsTest,RoundTrip)43 TEST_P(ParameterizedCompressionUtilsTest, RoundTrip) {
44 std::vector<Tensor> element = GetParam();
45 CompressedElement compressed;
46 TF_ASSERT_OK(CompressElement(element, &compressed));
47 std::vector<Tensor> round_trip_element;
48 TF_ASSERT_OK(UncompressElement(compressed, &round_trip_element));
49 TF_EXPECT_OK(
50 ExpectEqual(element, round_trip_element, /*compare_order=*/true));
51 }
52
TestCases()53 std::vector<std::vector<Tensor>> TestCases() {
54 return {
55 CreateTensors<int64_t>(TensorShape{1}, {{1}}), // int64
56 CreateTensors<int64_t>(TensorShape{1}, {{1}, {2}}), // multiple int64
57 CreateTensors<tstring>(TensorShape{1}, {{"a"}, {"b"}}), // tstring
58 {CreateTensor<tstring>(TensorShape{1}, {"a"}),
59 CreateTensor<int64_t>(TensorShape{1}, {1})}, // mixed tstring/int64
60 {}, // empty
61 };
62 }
63
64 INSTANTIATE_TEST_SUITE_P(Instantiation, ParameterizedCompressionUtilsTest,
65 ::testing::ValuesIn(TestCases()));
66
67 } // namespace data
68 } // namespace tensorflow
69