1 /* Copyright 2015 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/strings/stringprintf.h" 17 18 #include <string> 19 20 #include "tensorflow/core/platform/test.h" 21 22 namespace tensorflow { 23 namespace strings { 24 namespace { 25 26 TEST(PrintfTest, Empty) { 27 EXPECT_EQ("", Printf("%s", string().c_str())); 28 EXPECT_EQ("", Printf("%s", "")); 29 } 30 31 TEST(PrintfTest, Misc) { 32 // MSVC does not support $ format specifier. 33 #if !defined(COMPILER_MSVC) 34 EXPECT_EQ("123hello w", Printf("%3$d%2$s %1$c", 'w', "hello", 123)); 35 #endif // !COMPILER_MSVC 36 } 37 38 TEST(AppendfTest, Empty) { 39 string value("Hello"); 40 const char* empty = ""; 41 Appendf(&value, "%s", empty); 42 EXPECT_EQ("Hello", value); 43 } 44 45 TEST(AppendfTest, EmptyString) { 46 string value("Hello"); 47 Appendf(&value, "%s", ""); 48 EXPECT_EQ("Hello", value); 49 } 50 51 TEST(AppendfTest, String) { 52 string value("Hello"); 53 Appendf(&value, " %s", "World"); 54 EXPECT_EQ("Hello World", value); 55 } 56 57 TEST(AppendfTest, Int) { 58 string value("Hello"); 59 Appendf(&value, " %d", 123); 60 EXPECT_EQ("Hello 123", value); 61 } 62 63 TEST(PrintfTest, Multibyte) { 64 // If we are in multibyte mode and feed invalid multibyte sequence, 65 // Printf should return an empty string instead of running 66 // out of memory while trying to determine destination buffer size. 67 // see b/4194543. 68 69 char* old_locale = setlocale(LC_CTYPE, nullptr); 70 // Push locale with multibyte mode 71 setlocale(LC_CTYPE, "en_US.utf8"); 72 73 const char kInvalidCodePoint[] = "\375\067s"; 74 string value = Printf("%.*s", 3, kInvalidCodePoint); 75 76 // In some versions of glibc (e.g. eglibc-2.11.1, aka GRTEv2), snprintf 77 // returns error given an invalid codepoint. Other versions 78 // (e.g. eglibc-2.15, aka pre-GRTEv3) emit the codepoint verbatim. 79 // We test that the output is one of the above. 80 EXPECT_TRUE(value.empty() || value == kInvalidCodePoint); 81 82 // Repeat with longer string, to make sure that the dynamically 83 // allocated path in StringAppendV is handled correctly. 84 int n = 2048; 85 char* buf = new char[n + 1]; 86 memset(buf, ' ', n - 3); 87 memcpy(buf + n - 3, kInvalidCodePoint, 4); 88 value = Printf("%.*s", n, buf); 89 // See GRTEv2 vs. GRTEv3 comment above. 90 EXPECT_TRUE(value.empty() || value == buf); 91 delete[] buf; 92 93 setlocale(LC_CTYPE, old_locale); 94 } 95 96 TEST(PrintfTest, NoMultibyte) { 97 // No multibyte handling, but the string contains funny chars. 98 char* old_locale = setlocale(LC_CTYPE, nullptr); 99 setlocale(LC_CTYPE, "POSIX"); 100 string value = Printf("%.*s", 3, "\375\067s"); 101 setlocale(LC_CTYPE, old_locale); 102 EXPECT_EQ("\375\067s", value); 103 } 104 105 TEST(PrintfTest, DontOverwriteErrno) { 106 // Check that errno isn't overwritten unless we're printing 107 // something significantly larger than what people are normally 108 // printing in their badly written PLOG() statements. 109 errno = ECHILD; 110 string value = Printf("Hello, %s!", "World"); 111 EXPECT_EQ(ECHILD, errno); 112 } 113 114 TEST(PrintfTest, LargeBuf) { 115 // Check that the large buffer is handled correctly. 116 int n = 2048; 117 char* buf = new char[n + 1]; 118 memset(buf, ' ', n); 119 buf[n] = 0; 120 string value = Printf("%s", buf); 121 EXPECT_EQ(buf, value); 122 delete[] buf; 123 } 124 125 } // namespace 126 127 } // namespace strings 128 } // namespace tensorflow 129