// Copyright 2017 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
#ifdef __ANDROID__ // Android assert messages only go to system log, so death tests cannot inspect // the message for matching. #define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \
EXPECT_DEBUG_DEATH(statement, ".*") #else #define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \
EXPECT_DEBUG_DEATH(statement, regex) #endif
result = absl::StrCat(ui64s[0], ", ", ui64s[1], "!");
EXPECT_EQ(result, "12345678910, 10987654321!");
std::string one = "1"; // Actually, it's the size of this string that we want; a // 64-bit build distinguishes between size_t and uint64_t, // even though they're both unsigned 64-bit values.
result = absl::StrCat("And a ", one.size(), " and a ",
&result[2] - &result[0], " and a ", one, " 2 3 4", "!");
EXPECT_EQ(result, "And a 1 and a 2 and a 1 2 3 4!");
// result = absl::StrCat("Single chars won't compile", '!'); // result = absl::StrCat("Neither will nullptrs", nullptr);
result =
absl::StrCat("To output a char by ASCII/numeric value, use +: ", '!' + 0);
EXPECT_EQ(result, "To output a char by ASCII/numeric value, use +: 33");
float f = 100000.5;
result = absl::StrCat("A hundred K and a half is ", absl::SixDigits(f));
EXPECT_EQ(result, "A hundred K and a half is 100000");
f = 100001.5;
result =
absl::StrCat("A hundred K and one and a half is ", absl::SixDigits(f));
EXPECT_EQ(result, "A hundred K and one and a half is 100002");
double d = 100000.5;
d *= d;
result =
absl::StrCat("A hundred K and a half squared is ", absl::SixDigits(d));
EXPECT_EQ(result, "A hundred K and a half squared is 1.00001e+10");
TEST(StrCat, CustomAllocator) { using mstring =
std::basic_string<char, std::char_traits<char>, Mallocator<char>>; const mstring str1("PARACHUTE OFF A BLIMP INTO MOSCONE!!");
const mstring str2("Read this book about coffee tables");
std::string result = absl::StrCat(str1, str2);
EXPECT_EQ(result, "PARACHUTE OFF A BLIMP INTO MOSCONE!!" "Read this book about coffee tables");
}
std::string one = "1"; // Actually, it's the size of this string that we want; a // 64-bit build distinguishes between size_t and uint64_t, // even though they're both unsigned 64-bit values.
old_size = result.size();
absl::StrAppend(&result, "And a ", one.size(), " and a ",
&result[2] - &result[0], " and a ", one, " 2 3 4", "!");
EXPECT_EQ(result.substr(old_size), "And a 1 and a 2 and a 1 2 3 4!");
// result = absl::StrCat("Single chars won't compile", '!'); // result = absl::StrCat("Neither will nullptrs", nullptr);
old_size = result.size();
absl::StrAppend(&result, "To output a char by ASCII/numeric value, use +: ", '!' + 0);
EXPECT_EQ(result.substr(old_size), "To output a char by ASCII/numeric value, use +: 33");
// Test 9 arguments, the old maximum
old_size = result.size();
absl::StrAppend(&result, 1, 22, 333, 4444, 55555, 666666, 7777777, 88888888,
9);
EXPECT_EQ(result.substr(old_size), "1223334444555556666667777777888888889");
TEST(StrCat, VectorBoolReferenceTypes) {
std::vector<bool> v;
v.push_back(true);
v.push_back(false);
std::vector<bool> const& cv = v; // Test that vector<bool>::reference and vector<bool>::const_reference // are handled as if the were really bool types and not the proxy types // they really are.
std::string result = absl::StrCat(v[0], v[1], cv[0], cv[1]); // NOLINT
EXPECT_EQ(result, "1010");
}
// Passing nullptr to memcpy is undefined behavior and this test // provides coverage of codepaths that handle empty strings with nullptrs.
TEST(StrCat, AvoidsMemcpyWithNullptr) {
EXPECT_EQ(absl::StrCat(42, absl::string_view{}), "42");
void TestFastPrints() { // Test all small ints; there aren't many and they're common. for (int i = 0; i < 10000; i++) {
CheckAll(i);
}
CheckAll(std::numeric_limits<uint64_t>::max());
CheckAll(std::numeric_limits<uint64_t>::max() - 1);
CheckAll(std::numeric_limits<int64_t>::min());
CheckAll(std::numeric_limits<int64_t>::min() + 1);
CheckAll(std::numeric_limits<uint32_t>::max());
CheckAll(std::numeric_limits<uint32_t>::max() - 1);
CheckAll(std::numeric_limits<int32_t>::min());
CheckAll(std::numeric_limits<int32_t>::min() + 1);
CheckAll(999999999); // fits in 32 bits
CheckAll(1000000000); // fits in 32 bits
CheckAll(9999999999); // doesn't fit in 32 bits
CheckAll(10000000000); // doesn't fit in 32 bits
CheckAll(999999999999999999); // fits in signed 64-bit
CheckAll(9999999999999999999u); // fits in unsigned 64-bit, but not signed.
CheckAll(1000000000000000000); // fits in signed 64-bit
CheckAll(10000000000000000000u); // fits in unsigned 64-bit, but not signed.
CheckAll(999999999876543210); // check all decimal digits, signed
CheckAll(9999999999876543210u); // check all decimal digits, unsigned.
CheckAll(0x123456789abcdef0); // check all hex digits
CheckAll(0x12345678);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.