1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations 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 23namespace xla { 24namespace { 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. 31TEST(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 40// Some smoke tests for ContainersEqual. Keeping it simple since these are just 41// basic wrappers around std::equal. 42TEST(UtilTest, ContainersEqualDefault) { 43 std::vector<int> c1 = {1, 2, 3, 4}; 44 std::vector<int> c2 = {1, 2, 3}; 45 std::vector<int> c3 = {}; 46 std::vector<int> c4 = {1, 2, 3, 4}; 47 std::vector<int> c5 = {1, 2, 3, 4, 5}; 48 std::vector<int> c6 = {1, 3, 4, 5}; 49 50 EXPECT_TRUE(ContainersEqual(c1, c4)); 51 EXPECT_TRUE(ContainersEqual(c4, c1)); 52 EXPECT_FALSE(ContainersEqual(c1, c2)); 53 EXPECT_FALSE(ContainersEqual(c2, c1)); 54 EXPECT_FALSE(ContainersEqual(c1, c3)); 55 EXPECT_FALSE(ContainersEqual(c3, c1)); 56 EXPECT_FALSE(ContainersEqual(c1, c5)); 57 EXPECT_FALSE(ContainersEqual(c5, c1)); 58 EXPECT_FALSE(ContainersEqual(c1, c6)); 59 EXPECT_FALSE(ContainersEqual(c6, c1)); 60} 61 62TEST(UtilTest, ContainersEqualPredicate) { 63 std::vector<int> c1 = {1, 2, 3, 4}; 64 std::vector<int> c2 = {10, 20, 30, 40}; 65 66 EXPECT_TRUE(ContainersEqual( 67 c1, c2, [](const int& i1, const int& i2) { return i1 < i2; })); 68 EXPECT_FALSE(ContainersEqual( 69 c1, c2, [](const int& i1, const int& i2) { return i1 > i2; })); 70} 71 72TEST(UtilTest, ContainersEqualDifferentContainerTypes) { 73 std::vector<int> c1 = {1, 2, 3, 4}; 74 std::list<int> c2 = {1, 2, 3, 4}; 75 76 EXPECT_TRUE(ContainersEqual(c1, c2)); 77} 78 79TEST(UtilTest, HumanReadableNumFlopsExample) { 80 ASSERT_EQ("1.00GFLOP/s", HumanReadableNumFlops(1e9, 1e9)); 81} 82 83TEST(UtilTest, CommaSeparatedString) { 84 EXPECT_EQ(CommaSeparatedString({}), ""); 85 EXPECT_EQ(CommaSeparatedString({"hello world"}), "hello world"); 86 EXPECT_EQ(CommaSeparatedString({1, 57, 2}, "foo", "bar"), "foo1, 57, 2bar"); 87} 88 89TEST(UtilTest, VectorString) { 90 std::list<int64> empty_list; 91 EXPECT_EQ(VectorString(empty_list), "()"); 92 93 std::vector<float> float_vector = {5.5}; 94 EXPECT_EQ(VectorString(float_vector), "(5.5)"); 95 96 std::set<const char*> string_set = {"a", "b"}; 97 EXPECT_EQ(VectorString(string_set), "(a, b)"); 98 99 EXPECT_EQ(VectorString({}), "()"); 100 EXPECT_EQ(VectorString({1, 57, 2}), "(1, 57, 2)"); 101} 102 103TEST(UtilTest, LogLines) { 104 // Just make sure this code runs (not verifying the output). 105 LogLines(tensorflow::INFO, "hello\n\nworld", __FILE__, __LINE__); 106} 107 108TEST(UtilTest, CommonFactors) { 109 struct { 110 std::vector<int64> a, b; 111 std::vector<std::pair<int64, int64>> expected; 112 } test_cases[] = { 113 {/*.a =*/{0}, /*.b =*/{0}, /*.expected =*/{{0, 0}, {1, 1}}}, 114 {/*.a =*/{}, /*.b =*/{}, /*.expected =*/{{0, 0}}}, 115 {/*.a =*/{2, 5, 1, 3}, 116 /*.b =*/{1, 10, 3, 1}, 117 /*.expected =*/{{0, 0}, {0, 1}, {2, 2}, {3, 2}, {4, 3}, {4, 4}}}, 118 }; 119 for (const auto& test_case : test_cases) { 120 EXPECT_TRUE(ContainersEqual(test_case.expected, 121 CommonFactors(test_case.a, test_case.b))); 122 } 123} 124 125TEST(UtilTest, SanitizeFileName) { 126 EXPECT_EQ(SanitizeFileName(""), ""); 127 EXPECT_EQ(SanitizeFileName("abc"), "abc"); 128 EXPECT_EQ(SanitizeFileName("/\\[]"), "____"); 129 EXPECT_EQ(SanitizeFileName("/A\\B[C]"), "_A_B_C_"); 130} 131 132} // namespace 133} // namespace xla 134