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/util.h"
17
18 #include <list>
19
20 #include "tensorflow/compiler/xla/test.h"
21 #include "tensorflow/compiler/xla/types.h"
22
23 namespace xla {
24 namespace {
25
26 // Verifies that, even with a different number of leading spaces, the
27 // Reindent routine turns them into a uniform number of leading spaces.
28 //
29 // Also throws in some trailing whitespace on the original to show it is
30 // removed.
TEST(UtilTest,ReindentsDifferentNumberOfLeadingSpacesUniformly)31 TEST(UtilTest, ReindentsDifferentNumberOfLeadingSpacesUniformly) {
32 string original = R"( hello there
33 world)";
34 string got = Reindent(original, " ");
35 string want = R"( hello there
36 world)";
37 EXPECT_EQ(want, got);
38 }
39
TEST(UtilTest,HumanReadableNumFlopsExample)40 TEST(UtilTest, HumanReadableNumFlopsExample) {
41 ASSERT_EQ("1.00GFLOP/s", HumanReadableNumFlops(1e9, 1e9));
42 }
43
TEST(UtilTest,CommaSeparatedString)44 TEST(UtilTest, CommaSeparatedString) {
45 EXPECT_EQ(CommaSeparatedString({}), "");
46 EXPECT_EQ(CommaSeparatedString({"hello world"}), "hello world");
47 EXPECT_EQ(CommaSeparatedString({1, 57, 2}, "foo", "bar"), "foo1, 57, 2bar");
48 }
49
TEST(UtilTest,VectorString)50 TEST(UtilTest, VectorString) {
51 std::list<int64> empty_list;
52 EXPECT_EQ(VectorString(empty_list), "()");
53
54 std::vector<float> float_vector = {5.5};
55 EXPECT_EQ(VectorString(float_vector), "(5.5)");
56
57 std::set<const char*> string_set = {"a", "b"};
58 EXPECT_EQ(VectorString(string_set), "(a, b)");
59
60 EXPECT_EQ(VectorString({}), "()");
61 EXPECT_EQ(VectorString({1, 57, 2}), "(1, 57, 2)");
62 }
63
TEST(UtilTest,LogLines)64 TEST(UtilTest, LogLines) {
65 // Just make sure this code runs (not verifying the output).
66 LogLines(tensorflow::INFO, "hello\n\nworld", __FILE__, __LINE__);
67 }
68
TEST(UtilTest,CommonFactors)69 TEST(UtilTest, CommonFactors) {
70 struct {
71 std::vector<int64> a, b;
72 std::vector<std::pair<int64, int64>> expected;
73 } test_cases[] = {
74 {/*.a =*/{0}, /*.b =*/{0}, /*.expected =*/{{0, 0}, {1, 1}}},
75 {/*.a =*/{}, /*.b =*/{}, /*.expected =*/{{0, 0}}},
76 {/*.a =*/{2, 5, 1, 3},
77 /*.b =*/{1, 10, 3, 1},
78 /*.expected =*/{{0, 0}, {0, 1}, {2, 2}, {3, 2}, {4, 3}, {4, 4}}},
79 };
80 for (const auto& test_case : test_cases) {
81 EXPECT_TRUE(absl::c_equal(test_case.expected,
82 CommonFactors(test_case.a, test_case.b)));
83 }
84 }
85
TEST(UtilTest,SanitizeFileName)86 TEST(UtilTest, SanitizeFileName) {
87 EXPECT_EQ(SanitizeFileName(""), "");
88 EXPECT_EQ(SanitizeFileName("abc"), "abc");
89 EXPECT_EQ(SanitizeFileName("/\\[]"), "____");
90 EXPECT_EQ(SanitizeFileName("/A\\B[C]"), "_A_B_C_");
91 }
92
93 } // namespace
94 } // namespace xla
95