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/core/lib/core/stringpiece.h" 17 18 #include <unordered_map> 19 #include "tensorflow/core/platform/test.h" 20 21 namespace tensorflow { 22 23 TEST(StringPiece, Ctor) { 24 { 25 // const char* without size. 26 const char* hello = "hello"; 27 StringPiece s20(hello); 28 EXPECT_TRUE(s20.data() == hello); 29 EXPECT_EQ(5, s20.size()); 30 31 // const char* with size. 32 StringPiece s21(hello, 4); 33 EXPECT_TRUE(s21.data() == hello); 34 EXPECT_EQ(4, s21.size()); 35 36 // Not recommended, but valid C++ 37 StringPiece s22(hello, 6); 38 EXPECT_TRUE(s22.data() == hello); 39 EXPECT_EQ(6, s22.size()); 40 } 41 42 { 43 string hola = "hola"; 44 StringPiece s30(hola); 45 EXPECT_TRUE(s30.data() == hola.data()); 46 EXPECT_EQ(4, s30.size()); 47 48 // std::string with embedded '\0'. 49 hola.push_back('\0'); 50 hola.append("h2"); 51 hola.push_back('\0'); 52 StringPiece s31(hola); 53 EXPECT_TRUE(s31.data() == hola.data()); 54 EXPECT_EQ(8, s31.size()); 55 } 56 } 57 58 TEST(StringPiece, Contains) { 59 StringPiece a("abcdefg"); 60 StringPiece b("abcd"); 61 StringPiece c("efg"); 62 StringPiece d("gh"); 63 EXPECT_TRUE(a.contains(b)); 64 EXPECT_TRUE(a.contains(c)); 65 EXPECT_TRUE(!a.contains(d)); 66 } 67 68 TEST(StringPieceHasher, Equality) { 69 StringPieceHasher hasher; 70 71 StringPiece s1("foo"); 72 StringPiece s2("bar"); 73 StringPiece s3("baz"); 74 StringPiece s4("zot"); 75 76 EXPECT_TRUE(hasher(s1) != hasher(s2)); 77 EXPECT_TRUE(hasher(s1) != hasher(s3)); 78 EXPECT_TRUE(hasher(s1) != hasher(s4)); 79 EXPECT_TRUE(hasher(s2) != hasher(s3)); 80 EXPECT_TRUE(hasher(s2) != hasher(s4)); 81 EXPECT_TRUE(hasher(s3) != hasher(s4)); 82 83 EXPECT_TRUE(hasher(s1) == hasher(s1)); 84 EXPECT_TRUE(hasher(s2) == hasher(s2)); 85 EXPECT_TRUE(hasher(s3) == hasher(s3)); 86 EXPECT_TRUE(hasher(s4) == hasher(s4)); 87 } 88 89 TEST(StringPieceHasher, HashMap) { 90 string s1("foo"); 91 string s2("bar"); 92 string s3("baz"); 93 94 StringPiece p1(s1); 95 StringPiece p2(s2); 96 StringPiece p3(s3); 97 98 std::unordered_map<StringPiece, int, StringPieceHasher> map; 99 100 map.insert(std::make_pair(p1, 0)); 101 map.insert(std::make_pair(p2, 1)); 102 map.insert(std::make_pair(p3, 2)); 103 EXPECT_EQ(map.size(), 3); 104 105 bool found[3] = {false, false, false}; 106 for (auto const& val : map) { 107 int x = val.second; 108 EXPECT_TRUE(x >= 0 && x < 3); 109 EXPECT_TRUE(!found[x]); 110 found[x] = true; 111 } 112 EXPECT_EQ(found[0], true); 113 EXPECT_EQ(found[1], true); 114 EXPECT_EQ(found[2], true); 115 116 auto new_iter = map.find("zot"); 117 EXPECT_TRUE(new_iter == map.end()); 118 119 new_iter = map.find("bar"); 120 EXPECT_TRUE(new_iter != map.end()); 121 122 map.erase(new_iter); 123 EXPECT_EQ(map.size(), 2); 124 125 found[0] = false; 126 found[1] = false; 127 found[2] = false; 128 for (const auto& iter : map) { 129 int x = iter.second; 130 EXPECT_TRUE(x >= 0 && x < 3); 131 EXPECT_TRUE(!found[x]); 132 found[x] = true; 133 } 134 EXPECT_EQ(found[0], true); 135 EXPECT_EQ(found[1], false); 136 EXPECT_EQ(found[2], true); 137 } 138 } // namespace tensorflow 139