1 /* Copyright 2016 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 
16 #include "tensorflow/contrib/bigtable/kernels/bigtable_range_helpers.h"
17 #include "tensorflow/core/platform/test.h"
18 
19 namespace tensorflow {
20 namespace {
21 
TEST(MultiModeKeyRangeTest,SimplePrefix)22 TEST(MultiModeKeyRangeTest, SimplePrefix) {
23   MultiModeKeyRange r = MultiModeKeyRange::FromPrefix("prefix");
24   EXPECT_EQ("prefix", r.begin_key());
25   EXPECT_EQ("prefiy", r.end_key());
26   EXPECT_TRUE(r.contains_key("prefixed_key"));
27   EXPECT_FALSE(r.contains_key("not-prefixed-key"));
28   EXPECT_FALSE(r.contains_key("prefi"));
29   EXPECT_FALSE(r.contains_key("prefiy"));
30   EXPECT_FALSE(r.contains_key("early"));
31   EXPECT_FALSE(r.contains_key(""));
32 }
33 
TEST(MultiModeKeyRangeTest,Range)34 TEST(MultiModeKeyRangeTest, Range) {
35   MultiModeKeyRange r = MultiModeKeyRange::FromRange("a", "b");
36   EXPECT_EQ("a", r.begin_key());
37   EXPECT_EQ("b", r.end_key());
38   EXPECT_TRUE(r.contains_key("a"));
39   EXPECT_TRUE(r.contains_key("ab"));
40   EXPECT_FALSE(r.contains_key("b"));
41   EXPECT_FALSE(r.contains_key("bc"));
42   EXPECT_FALSE(r.contains_key("A"));
43   EXPECT_FALSE(r.contains_key("B"));
44   EXPECT_FALSE(r.contains_key(""));
45 }
46 
TEST(MultiModeKeyRangeTest,InvertedRange)47 TEST(MultiModeKeyRangeTest, InvertedRange) {
48   MultiModeKeyRange r = MultiModeKeyRange::FromRange("b", "a");
49   EXPECT_FALSE(r.contains_key("a"));
50   EXPECT_FALSE(r.contains_key("b"));
51   EXPECT_FALSE(r.contains_key(""));
52 }
53 
TEST(MultiModeKeyRangeTest,EmptyPrefix)54 TEST(MultiModeKeyRangeTest, EmptyPrefix) {
55   MultiModeKeyRange r = MultiModeKeyRange::FromPrefix("");
56   EXPECT_EQ("", r.begin_key());
57   EXPECT_EQ("", r.end_key());
58   EXPECT_TRUE(r.contains_key(""));
59   EXPECT_TRUE(r.contains_key("a"));
60   EXPECT_TRUE(r.contains_key("z"));
61   EXPECT_TRUE(r.contains_key("A"));
62   EXPECT_TRUE(r.contains_key("ZZZZZZ"));
63 }
64 
TEST(MultiModeKeyRangeTest,HalfRange)65 TEST(MultiModeKeyRangeTest, HalfRange) {
66   MultiModeKeyRange r = MultiModeKeyRange::FromRange("start", "");
67   EXPECT_EQ("start", r.begin_key());
68   EXPECT_EQ("", r.end_key());
69   EXPECT_TRUE(r.contains_key("start"));
70   EXPECT_TRUE(r.contains_key("starting"));
71   EXPECT_TRUE(r.contains_key("z-end"));
72   EXPECT_FALSE(r.contains_key(""));
73   EXPECT_FALSE(r.contains_key("early"));
74 }
75 
TEST(MultiModeKeyRangeTest,PrefixWrapAround)76 TEST(MultiModeKeyRangeTest, PrefixWrapAround) {
77   string prefix = "abc\xff";
78   MultiModeKeyRange r = MultiModeKeyRange::FromPrefix(prefix);
79   EXPECT_EQ(prefix, r.begin_key());
80   EXPECT_EQ("abd", r.end_key());
81 
82   EXPECT_TRUE(r.contains_key("abc\xff\x07"));
83   EXPECT_TRUE(r.contains_key("abc\xff\x15"));
84   EXPECT_TRUE(r.contains_key("abc\xff\x61"));
85   EXPECT_TRUE(r.contains_key("abc\xff\xff"));
86   EXPECT_FALSE(r.contains_key("abc\0"));
87   EXPECT_FALSE(r.contains_key("abd"));
88 }
89 
TEST(MultiModeKeyRangeTest,PrefixSignedWrapAround)90 TEST(MultiModeKeyRangeTest, PrefixSignedWrapAround) {
91   string prefix = "abc\x7f";
92   MultiModeKeyRange r = MultiModeKeyRange::FromPrefix(prefix);
93   EXPECT_EQ(prefix, r.begin_key());
94   EXPECT_EQ("abc\x80", r.end_key());
95 
96   EXPECT_TRUE(r.contains_key("abc\x7f\x07"));
97   EXPECT_TRUE(r.contains_key("abc\x7f\x15"));
98   EXPECT_TRUE(r.contains_key("abc\x7f\x61"));
99   EXPECT_TRUE(r.contains_key("abc\x7f\xff"));
100   EXPECT_FALSE(r.contains_key("abc\0"));
101   EXPECT_FALSE(r.contains_key("abc\x01"));
102   EXPECT_FALSE(r.contains_key("abd"));
103   EXPECT_FALSE(r.contains_key("ab\x80"));
104 }
105 
106 }  // namespace
107 }  // namespace tensorflow
108