1 /* Copyright 2017 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/compiler/xla/service/name_uniquer.h"
17 
18 #include <memory>
19 #include <utility>
20 #include <vector>
21 
22 #include "tensorflow/core/platform/test.h"
23 
24 namespace xla {
25 namespace {
26 
27 class NameUniquerTest : public ::testing::Test {};
28 
TEST_F(NameUniquerTest,SimpleUniquer)29 TEST_F(NameUniquerTest, SimpleUniquer) {
30   NameUniquer uniquer;
31 
32   EXPECT_EQ("foo", uniquer.GetUniqueName("foo"));
33   EXPECT_EQ("foo__1", uniquer.GetUniqueName("foo"));
34   EXPECT_EQ("foo__2", uniquer.GetUniqueName("foo"));
35   EXPECT_EQ("bar", uniquer.GetUniqueName("bar"));
36   EXPECT_EQ("foo__3", uniquer.GetUniqueName("foo"));
37   EXPECT_EQ("bar__1", uniquer.GetUniqueName("bar"));
38   EXPECT_EQ("qux", uniquer.GetUniqueName("qux"));
39 }
40 
TEST_F(NameUniquerTest,DifferentSeparator)41 TEST_F(NameUniquerTest, DifferentSeparator) {
42   NameUniquer uniquer(".");
43 
44   EXPECT_EQ("foo", uniquer.GetUniqueName("foo"));
45   EXPECT_EQ("foo.1", uniquer.GetUniqueName("foo"));
46   EXPECT_EQ("foo.2", uniquer.GetUniqueName("foo"));
47   EXPECT_EQ("bar", uniquer.GetUniqueName("bar"));
48   EXPECT_EQ("foo.3", uniquer.GetUniqueName("foo"));
49   EXPECT_EQ("bar.1", uniquer.GetUniqueName("bar"));
50 }
51 
TEST_F(NameUniquerTest,NumericSuffixes)52 TEST_F(NameUniquerTest, NumericSuffixes) {
53   NameUniquer uniquer(".");
54 
55   EXPECT_EQ("foo", uniquer.GetUniqueName("foo"));
56   EXPECT_EQ("foo.54", uniquer.GetUniqueName("foo.54"));
57   EXPECT_EQ("foo.1", uniquer.GetUniqueName("foo"));
58   EXPECT_EQ("foo.55.1", uniquer.GetUniqueName("foo.55.1"));
59   EXPECT_EQ("foo.55.0", uniquer.GetUniqueName("foo.55.1"));
60   EXPECT_EQ("bar.1000", uniquer.GetUniqueName("bar.1000"));
61   EXPECT_EQ("bar.2000", uniquer.GetUniqueName("bar.2000"));
62   EXPECT_EQ("bar.-2000", uniquer.GetUniqueName("bar.-2000"));
63   EXPECT_EQ("bar.1", uniquer.GetUniqueName("bar.1"));
64 }
65 
TEST_F(NameUniquerTest,PrefixHasSuffix)66 TEST_F(NameUniquerTest, PrefixHasSuffix) {
67   NameUniquer uniquer(".");
68 
69   EXPECT_EQ("foo.11.0", uniquer.GetUniqueName("foo.11.0"));
70   EXPECT_EQ("foo.11", uniquer.GetUniqueName("foo.11"));
71 }
72 
TEST_F(NameUniquerTest,Sanitize)73 TEST_F(NameUniquerTest, Sanitize) {
74   NameUniquer uniquer("_");
75 
76   EXPECT_EQ("foo", uniquer.GetUniqueName("foo"));
77   EXPECT_EQ("foo_1", uniquer.GetUniqueName("foo"));
78   EXPECT_EQ("foo.54", uniquer.GetUniqueName("foo.54"));
79   EXPECT_EQ("foo_54", uniquer.GetUniqueName("foo_54"));
80   EXPECT_EQ("foo_54.1", uniquer.GetUniqueName("foo_54.1"));
81   EXPECT_EQ("foo_2", uniquer.GetUniqueName("foo"));
82 
83   // Invalid characters will be replaced with '_'.
84   EXPECT_EQ("bar_1000", uniquer.GetUniqueName("bar<1000"));
85   EXPECT_EQ("bar_2000", uniquer.GetUniqueName("bar<2000"));
86   EXPECT_EQ("bar_1", uniquer.GetUniqueName("bar_1"));
87 
88   // Separator is only recognized in the middle of the prefix.
89   EXPECT_EQ("_10", uniquer.GetUniqueName(
90                        ".10"));  // the leading '.' is replaced with '_'.
91   EXPECT_EQ("_10_1", uniquer.GetUniqueName(".10"));
92   EXPECT_EQ("_10_2", uniquer.GetUniqueName("_10"));
93   EXPECT_EQ("foobar_", uniquer.GetUniqueName("foobar_"));
94   EXPECT_EQ("foobar__1", uniquer.GetUniqueName("foobar_"));
95 }
96 
TEST_F(NameUniquerTest,KeepNamesInRandomOrder)97 TEST_F(NameUniquerTest, KeepNamesInRandomOrder) {
98   NameUniquer uniquer(".");
99 
100   EXPECT_EQ("foo.11", uniquer.GetUniqueName("foo.11"));
101   EXPECT_EQ("foo.10", uniquer.GetUniqueName("foo.10"));
102   EXPECT_EQ("foo.1", uniquer.GetUniqueName("foo.1"));
103   EXPECT_EQ("foo.12", uniquer.GetUniqueName("foo.12"));
104   EXPECT_EQ("foo.3", uniquer.GetUniqueName("foo.3"));
105 }
106 
TEST_F(NameUniquerTest,AvoidKeywords)107 TEST_F(NameUniquerTest, AvoidKeywords) {
108   NameUniquer uniquer(".");
109 
110   EXPECT_EQ("f32_", uniquer.GetUniqueName("f32"));
111   EXPECT_EQ("s64_", uniquer.GetUniqueName("s64"));
112   EXPECT_EQ("pred_", uniquer.GetUniqueName("pred"));
113 
114   // Though a primitive type, "tuple" is not a keyword.
115   EXPECT_EQ("tuple", uniquer.GetUniqueName("tuple"));
116 
117   // Keywords are not capitalized.
118   EXPECT_EQ("F32", uniquer.GetUniqueName("F32"));
119   EXPECT_EQ("S32", uniquer.GetUniqueName("S32"));
120   EXPECT_EQ("Pred", uniquer.GetUniqueName("Pred"));
121 }
122 
123 }  // namespace
124 }  // namespace xla
125