tvl-depot/absl/strings/string_view_test.cc

1265 lines
41 KiB
C++
Raw Normal View History

2017-09-19 22:54:40 +02:00
// 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
2017-09-19 22:54:40 +02:00
//
// 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.
#include "absl/strings/string_view.h"
Changes imported from Abseil "staging" branch: - b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Fix some typos in the usage examples by Jorg Brown <jorg@google.com> - 82be4a9adf3bb0ddafc0d46274969c99afffe870 Fix typo in optional.h comment. by Abseil Team <absl-team@google.com> - d6ee63bf8fc51fba074c23b33cebc28c808d7f07 Remove internal-only identifiers from code. by Daniel Katz <katzdm@google.com> - f9c3ad2f0d73f53b21603638af8b4bed636e79f4 Use easier understandable names for absl::StartsWith and ... by Abseil Team <absl-team@google.com> - 7c16c14fefee89c927b8789d6043c4691bcffc9b Add -Wno-missing-prototypes back to the LLVM copts. by Derek Mauro <dmauro@google.com> - 2f4b7d2e50c7023240242f1e15db60ccd7e8768d IWYU | absl/strings by Juemin Yang <jueminyang@google.com> - a99cbcc1daa34a2d6a2bb26de275e05173cc77e9 IWYU | absl/type by Juemin Yang <jueminyang@google.com> - 12e1146d0fc76c071d7e0ebaabb62f0a984fae66 Use LLVM_FLAGS and LLVM_TEST_FLAGS when --compiler=llvm. by Derek Mauro <dmauro@google.com> - cd6bea616abda558d0bace5bd77455662a233688 IWYU | absl/debugging by Juemin Yang <jueminyang@google.com> - d9a7382e59d46a8581b6b7a31cd5a48bb89326e9 IWYU | absl/synchronization by Juemin Yang <jueminyang@google.com> - 07ec7d6d5a4a666f4183c5d0ed9c342baa7b24bc IWYU | absl/numeric by Juemin Yang <jueminyang@google.com> - 12bfe40051f4270f8707e191af5652f83f2f750c Remove the RoundTrip{Float,Double}ToBuffer routines from ... by Jorg Brown <jorg@google.com> - eeb4fd67c9d97f66cb9475c3c5e51ab132f1c810 Adds conversion functions for converting between absl/tim... by Greg Miller <jgm@google.com> - 59a2108d05d4ea85dc5cc11e49b2cd2335d4295a Change Substitute to use %.6g formatting rather than 15/1... by Jorg Brown <jorg@google.com> - 394becb48e0fcd161642cdaac5120d32567e0ef8 IWYU | absl/meta by Juemin Yang <jueminyang@google.com> - 1e5da6e8da336699b2469dcf6dda025b9b0ec4c9 Rewrite atomic_hook.h to not use std::atomic<T*> under Wi... by Greg Falcon <gfalcon@google.com> GitOrigin-RevId: b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Change-Id: I14e331d91c956ef045ac7927091a9f179716de0c
2017-09-24 17:20:48 +02:00
#include <stdlib.h>
2017-09-19 22:54:40 +02:00
#include <iomanip>
#include <iterator>
Changes imported from Abseil "staging" branch: - b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Fix some typos in the usage examples by Jorg Brown <jorg@google.com> - 82be4a9adf3bb0ddafc0d46274969c99afffe870 Fix typo in optional.h comment. by Abseil Team <absl-team@google.com> - d6ee63bf8fc51fba074c23b33cebc28c808d7f07 Remove internal-only identifiers from code. by Daniel Katz <katzdm@google.com> - f9c3ad2f0d73f53b21603638af8b4bed636e79f4 Use easier understandable names for absl::StartsWith and ... by Abseil Team <absl-team@google.com> - 7c16c14fefee89c927b8789d6043c4691bcffc9b Add -Wno-missing-prototypes back to the LLVM copts. by Derek Mauro <dmauro@google.com> - 2f4b7d2e50c7023240242f1e15db60ccd7e8768d IWYU | absl/strings by Juemin Yang <jueminyang@google.com> - a99cbcc1daa34a2d6a2bb26de275e05173cc77e9 IWYU | absl/type by Juemin Yang <jueminyang@google.com> - 12e1146d0fc76c071d7e0ebaabb62f0a984fae66 Use LLVM_FLAGS and LLVM_TEST_FLAGS when --compiler=llvm. by Derek Mauro <dmauro@google.com> - cd6bea616abda558d0bace5bd77455662a233688 IWYU | absl/debugging by Juemin Yang <jueminyang@google.com> - d9a7382e59d46a8581b6b7a31cd5a48bb89326e9 IWYU | absl/synchronization by Juemin Yang <jueminyang@google.com> - 07ec7d6d5a4a666f4183c5d0ed9c342baa7b24bc IWYU | absl/numeric by Juemin Yang <jueminyang@google.com> - 12bfe40051f4270f8707e191af5652f83f2f750c Remove the RoundTrip{Float,Double}ToBuffer routines from ... by Jorg Brown <jorg@google.com> - eeb4fd67c9d97f66cb9475c3c5e51ab132f1c810 Adds conversion functions for converting between absl/tim... by Greg Miller <jgm@google.com> - 59a2108d05d4ea85dc5cc11e49b2cd2335d4295a Change Substitute to use %.6g formatting rather than 15/1... by Jorg Brown <jorg@google.com> - 394becb48e0fcd161642cdaac5120d32567e0ef8 IWYU | absl/meta by Juemin Yang <jueminyang@google.com> - 1e5da6e8da336699b2469dcf6dda025b9b0ec4c9 Rewrite atomic_hook.h to not use std::atomic<T*> under Wi... by Greg Falcon <gfalcon@google.com> GitOrigin-RevId: b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Change-Id: I14e331d91c956ef045ac7927091a9f179716de0c
2017-09-24 17:20:48 +02:00
#include <limits>
2017-09-19 22:54:40 +02:00
#include <map>
#include <sstream>
Changes imported from Abseil "staging" branch: - b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Fix some typos in the usage examples by Jorg Brown <jorg@google.com> - 82be4a9adf3bb0ddafc0d46274969c99afffe870 Fix typo in optional.h comment. by Abseil Team <absl-team@google.com> - d6ee63bf8fc51fba074c23b33cebc28c808d7f07 Remove internal-only identifiers from code. by Daniel Katz <katzdm@google.com> - f9c3ad2f0d73f53b21603638af8b4bed636e79f4 Use easier understandable names for absl::StartsWith and ... by Abseil Team <absl-team@google.com> - 7c16c14fefee89c927b8789d6043c4691bcffc9b Add -Wno-missing-prototypes back to the LLVM copts. by Derek Mauro <dmauro@google.com> - 2f4b7d2e50c7023240242f1e15db60ccd7e8768d IWYU | absl/strings by Juemin Yang <jueminyang@google.com> - a99cbcc1daa34a2d6a2bb26de275e05173cc77e9 IWYU | absl/type by Juemin Yang <jueminyang@google.com> - 12e1146d0fc76c071d7e0ebaabb62f0a984fae66 Use LLVM_FLAGS and LLVM_TEST_FLAGS when --compiler=llvm. by Derek Mauro <dmauro@google.com> - cd6bea616abda558d0bace5bd77455662a233688 IWYU | absl/debugging by Juemin Yang <jueminyang@google.com> - d9a7382e59d46a8581b6b7a31cd5a48bb89326e9 IWYU | absl/synchronization by Juemin Yang <jueminyang@google.com> - 07ec7d6d5a4a666f4183c5d0ed9c342baa7b24bc IWYU | absl/numeric by Juemin Yang <jueminyang@google.com> - 12bfe40051f4270f8707e191af5652f83f2f750c Remove the RoundTrip{Float,Double}ToBuffer routines from ... by Jorg Brown <jorg@google.com> - eeb4fd67c9d97f66cb9475c3c5e51ab132f1c810 Adds conversion functions for converting between absl/tim... by Greg Miller <jgm@google.com> - 59a2108d05d4ea85dc5cc11e49b2cd2335d4295a Change Substitute to use %.6g formatting rather than 15/1... by Jorg Brown <jorg@google.com> - 394becb48e0fcd161642cdaac5120d32567e0ef8 IWYU | absl/meta by Juemin Yang <jueminyang@google.com> - 1e5da6e8da336699b2469dcf6dda025b9b0ec4c9 Rewrite atomic_hook.h to not use std::atomic<T*> under Wi... by Greg Falcon <gfalcon@google.com> GitOrigin-RevId: b527a3e4b36b644ac424e3c525b1cd393f6f6c40 Change-Id: I14e331d91c956ef045ac7927091a9f179716de0c
2017-09-24 17:20:48 +02:00
#include <stdexcept>
2017-09-19 22:54:40 +02:00
#include <string>
#include <type_traits>
#include <utility>
#include "gtest/gtest.h"
#include "absl/base/config.h"
#include "absl/base/dynamic_annotations.h"
Export of internal Abseil changes -- 79913a12f0cad4baf948430315aabf53f03b6475 by Abseil Team <absl-team@google.com>: Don't inline (Un)LockSlow. PiperOrigin-RevId: 302502344 -- 6b340e80f0690655f24799c8de6707b3a95b8579 by Derek Mauro <dmauro@google.com>: Add hardening assertions to absl::optional's dereference operators PiperOrigin-RevId: 302492862 -- a9951bf4852d8c1aec472cb4b539830411270e4c by Derek Mauro <dmauro@google.com>: Correctly add hardware AES compiler flags under Linux X86-64 Fixes #643 PiperOrigin-RevId: 302490673 -- 314c3621ee4d57b6bc8d64338a1f1d48a69741d1 by Derek Mauro <dmauro@google.com>: Upgrade to hardening assertions in absl::Span::remove_prefix and absl::Span::remove_suffix PiperOrigin-RevId: 302481191 -- a142b8c6c62705c5f0d4fe3113150f0c0b7822b9 by Derek Mauro <dmauro@google.com>: Update docker containers to Bazel 2.2.0, GCC 9.3, and new Clang snapshot PiperOrigin-RevId: 302454042 -- afedeb70a2adc87010030c9ba6f06fe35ec26407 by Derek Mauro <dmauro@google.com>: Add hardening assertions for the preconditions of absl::FixedArray PiperOrigin-RevId: 302441767 -- 44442bfbc0a9a742df32f07cee86a47712efb8b4 by Derek Mauro <dmauro@google.com>: Fix new Clang warning about SpinLock doing operations on enums of different types PiperOrigin-RevId: 302430387 -- 69eaff7f97231779f696321c2ba8b88debf6dd9e by Derek Mauro <dmauro@google.com>: Convert precondition assertions to ABSL_HARDENING_ASSERT for absl::InlinedVector PiperOrigin-RevId: 302427894 -- 26b6db906a0942fd18583dc2cdd1bab32919d964 by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 302425283 -- e62e81422979e922505d2cd9000e1de58123c088 by Derek Mauro <dmauro@google.com>: Add an option to build Abseil in hardened mode In hardened mode, the ABSL_HARDENING_ASSERT() macro is active even when NDEBUG is defined. This allows Abseil to perform runtime checks even in release mode. This should be used to implement things like bounds checks that could otherwise lead to security vulnerabilities. Use the new assertion in absl::string_view and absl::Span to test it. PiperOrigin-RevId: 302119187 GitOrigin-RevId: 79913a12f0cad4baf948430315aabf53f03b6475 Change-Id: I0cc3341fd333a1df313167bab72dc5a759c4a048
2020-03-23 21:16:18 +01:00
#include "absl/base/options.h"
2017-09-19 22:54:40 +02:00
#if defined(ABSL_HAVE_STD_STRING_VIEW) || defined(__ANDROID__)
// We don't control the death messaging when using std::string_view.
// Android assert messages only go to system log, so death tests cannot inspect
// the message for matching.
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
EXPECT_DEATH_IF_SUPPORTED(statement, ".*")
#else
#define ABSL_EXPECT_DEATH_IF_SUPPORTED(statement, regex) \
EXPECT_DEATH_IF_SUPPORTED(statement, regex)
#endif
2017-09-19 22:54:40 +02:00
namespace {
// A minimal allocator that uses malloc().
template <typename T>
struct Mallocator {
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
size_type max_size() const {
return size_t(std::numeric_limits<size_type>::max()) / sizeof(value_type);
}
template <typename U>
struct rebind {
typedef Mallocator<U> other;
};
Mallocator() = default;
template <class U>
Mallocator(const Mallocator<U>&) {} // NOLINT(runtime/explicit)
2017-09-19 22:54:40 +02:00
T* allocate(size_t n) { return static_cast<T*>(std::malloc(n * sizeof(T))); }
void deallocate(T* p, size_t) { std::free(p); }
};
template <typename T, typename U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) {
return true;
}
template <typename T, typename U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&) {
return false;
}
TEST(StringViewTest, Ctor) {
{
// Null.
absl::string_view s10;
EXPECT_TRUE(s10.data() == nullptr);
EXPECT_EQ(0, s10.length());
}
{
// const char* without length.
const char* hello = "hello";
absl::string_view s20(hello);
EXPECT_TRUE(s20.data() == hello);
EXPECT_EQ(5, s20.length());
// const char* with length.
absl::string_view s21(hello, 4);
EXPECT_TRUE(s21.data() == hello);
EXPECT_EQ(4, s21.length());
// Not recommended, but valid C++
absl::string_view s22(hello, 6);
EXPECT_TRUE(s22.data() == hello);
EXPECT_EQ(6, s22.length());
}
{
// std::string.
std::string hola = "hola";
absl::string_view s30(hola);
EXPECT_TRUE(s30.data() == hola.data());
EXPECT_EQ(4, s30.length());
// std::string with embedded '\0'.
hola.push_back('\0');
hola.append("h2");
hola.push_back('\0');
absl::string_view s31(hola);
EXPECT_TRUE(s31.data() == hola.data());
EXPECT_EQ(8, s31.length());
}
{
using mstring =
std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
mstring str1("BUNGIE-JUMPING!");
const mstring str2("SLEEPING!");
absl::string_view s1(str1);
s1.remove_prefix(strlen("BUNGIE-JUM"));
absl::string_view s2(str2);
s2.remove_prefix(strlen("SLEE"));
EXPECT_EQ(s1, s2);
EXPECT_EQ(s1, "PING!");
}
// TODO(mec): absl::string_view(const absl::string_view&);
}
TEST(StringViewTest, Swap) {
absl::string_view a("a");
absl::string_view b("bbb");
EXPECT_TRUE(noexcept(a.swap(b)));
a.swap(b);
EXPECT_EQ(a, "bbb");
EXPECT_EQ(b, "a");
a.swap(b);
EXPECT_EQ(a, "a");
EXPECT_EQ(b, "bbb");
}
TEST(StringViewTest, STLComparator) {
std::string s1("foo");
std::string s2("bar");
std::string s3("baz");
absl::string_view p1(s1);
absl::string_view p2(s2);
absl::string_view p3(s3);
typedef std::map<absl::string_view, int> TestMap;
TestMap map;
map.insert(std::make_pair(p1, 0));
map.insert(std::make_pair(p2, 1));
map.insert(std::make_pair(p3, 2));
EXPECT_EQ(map.size(), 3);
TestMap::const_iterator iter = map.begin();
EXPECT_EQ(iter->second, 1);
++iter;
EXPECT_EQ(iter->second, 2);
++iter;
EXPECT_EQ(iter->second, 0);
++iter;
EXPECT_TRUE(iter == map.end());
TestMap::iterator new_iter = map.find("zot");
EXPECT_TRUE(new_iter == map.end());
new_iter = map.find("bar");
EXPECT_TRUE(new_iter != map.end());
map.erase(new_iter);
EXPECT_EQ(map.size(), 2);
iter = map.begin();
EXPECT_EQ(iter->second, 2);
++iter;
EXPECT_EQ(iter->second, 0);
++iter;
EXPECT_TRUE(iter == map.end());
}
#define COMPARE(result, op, x, y) \
EXPECT_EQ(result, absl::string_view((x)) op absl::string_view((y))); \
EXPECT_EQ(result, absl::string_view((x)).compare(absl::string_view((y))) op 0)
TEST(StringViewTest, ComparisonOperators) {
COMPARE(true, ==, "", "");
COMPARE(true, ==, "", absl::string_view());
COMPARE(true, ==, absl::string_view(), "");
COMPARE(true, ==, "a", "a");
COMPARE(true, ==, "aa", "aa");
COMPARE(false, ==, "a", "");
COMPARE(false, ==, "", "a");
COMPARE(false, ==, "a", "b");
COMPARE(false, ==, "a", "aa");
COMPARE(false, ==, "aa", "a");
COMPARE(false, !=, "", "");
COMPARE(false, !=, "a", "a");
COMPARE(false, !=, "aa", "aa");
COMPARE(true, !=, "a", "");
COMPARE(true, !=, "", "a");
COMPARE(true, !=, "a", "b");
COMPARE(true, !=, "a", "aa");
COMPARE(true, !=, "aa", "a");
COMPARE(true, <, "a", "b");
COMPARE(true, <, "a", "aa");
COMPARE(true, <, "aa", "b");
COMPARE(true, <, "aa", "bb");
COMPARE(false, <, "a", "a");
COMPARE(false, <, "b", "a");
COMPARE(false, <, "aa", "a");
COMPARE(false, <, "b", "aa");
COMPARE(false, <, "bb", "aa");
COMPARE(true, <=, "a", "a");
COMPARE(true, <=, "a", "b");
COMPARE(true, <=, "a", "aa");
COMPARE(true, <=, "aa", "b");
COMPARE(true, <=, "aa", "bb");
COMPARE(false, <=, "b", "a");
COMPARE(false, <=, "aa", "a");
COMPARE(false, <=, "b", "aa");
COMPARE(false, <=, "bb", "aa");
COMPARE(false, >=, "a", "b");
COMPARE(false, >=, "a", "aa");
COMPARE(false, >=, "aa", "b");
COMPARE(false, >=, "aa", "bb");
COMPARE(true, >=, "a", "a");
COMPARE(true, >=, "b", "a");
COMPARE(true, >=, "aa", "a");
COMPARE(true, >=, "b", "aa");
COMPARE(true, >=, "bb", "aa");
COMPARE(false, >, "a", "a");
COMPARE(false, >, "a", "b");
COMPARE(false, >, "a", "aa");
COMPARE(false, >, "aa", "b");
COMPARE(false, >, "aa", "bb");
COMPARE(true, >, "b", "a");
COMPARE(true, >, "aa", "a");
COMPARE(true, >, "b", "aa");
COMPARE(true, >, "bb", "aa");
}
TEST(StringViewTest, ComparisonOperatorsByCharacterPosition) {
std::string x;
for (int i = 0; i < 256; i++) {
x += 'a';
std::string y = x;
COMPARE(true, ==, x, y);
for (int j = 0; j < i; j++) {
std::string z = x;
z[j] = 'b'; // Differs in position 'j'
COMPARE(false, ==, x, z);
COMPARE(true, <, x, z);
COMPARE(true, >, z, x);
if (j + 1 < i) {
z[j + 1] = 'A'; // Differs in position 'j+1' as well
COMPARE(false, ==, x, z);
COMPARE(true, <, x, z);
COMPARE(true, >, z, x);
z[j + 1] = 'z'; // Differs in position 'j+1' as well
COMPARE(false, ==, x, z);
COMPARE(true, <, x, z);
COMPARE(true, >, z, x);
}
}
}
}
#undef COMPARE
// Sadly, our users often confuse std::string::npos with
// absl::string_view::npos; So much so that we test here that they are the same.
// They need to both be unsigned, and both be the maximum-valued integer of
// their type.
2017-09-19 22:54:40 +02:00
template <typename T>
struct is_type {
template <typename U>
static bool same(U) {
return false;
}
static bool same(T) { return true; }
};
TEST(StringViewTest, NposMatchesStdStringView) {
EXPECT_EQ(absl::string_view::npos, std::string::npos);
EXPECT_TRUE(is_type<size_t>::same(absl::string_view::npos));
EXPECT_FALSE(is_type<size_t>::same(""));
// Make sure absl::string_view::npos continues to be a header constant.
char test[absl::string_view::npos & 1] = {0};
EXPECT_EQ(0, test[0]);
}
TEST(StringViewTest, STL1) {
const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
const absl::string_view b("abc");
const absl::string_view c("xyz");
const absl::string_view d("foobar");
const absl::string_view e;
std::string temp("123");
temp += '\0';
temp += "456";
const absl::string_view f(temp);
EXPECT_EQ(a[6], 'g');
EXPECT_EQ(b[0], 'a');
EXPECT_EQ(c[2], 'z');
EXPECT_EQ(f[3], '\0');
EXPECT_EQ(f[5], '5');
EXPECT_EQ(*d.data(), 'f');
EXPECT_EQ(d.data()[5], 'r');
EXPECT_TRUE(e.data() == nullptr);
EXPECT_EQ(*a.begin(), 'a');
EXPECT_EQ(*(b.begin() + 2), 'c');
EXPECT_EQ(*(c.end() - 1), 'z');
EXPECT_EQ(*a.rbegin(), 'z');
EXPECT_EQ(*(b.rbegin() + 2), 'a');
EXPECT_EQ(*(c.rend() - 1), 'x');
EXPECT_TRUE(a.rbegin() + 26 == a.rend());
EXPECT_EQ(a.size(), 26);
EXPECT_EQ(b.size(), 3);
EXPECT_EQ(c.size(), 3);
EXPECT_EQ(d.size(), 6);
EXPECT_EQ(e.size(), 0);
EXPECT_EQ(f.size(), 7);
EXPECT_TRUE(!d.empty());
EXPECT_TRUE(d.begin() != d.end());
EXPECT_TRUE(d.begin() + 6 == d.end());
EXPECT_TRUE(e.empty());
EXPECT_TRUE(e.begin() == e.end());
char buf[4] = { '%', '%', '%', '%' };
EXPECT_EQ(a.copy(buf, 4), 4);
EXPECT_EQ(buf[0], a[0]);
EXPECT_EQ(buf[1], a[1]);
EXPECT_EQ(buf[2], a[2]);
EXPECT_EQ(buf[3], a[3]);
EXPECT_EQ(a.copy(buf, 3, 7), 3);
EXPECT_EQ(buf[0], a[7]);
EXPECT_EQ(buf[1], a[8]);
EXPECT_EQ(buf[2], a[9]);
EXPECT_EQ(buf[3], a[3]);
EXPECT_EQ(c.copy(buf, 99), 3);
EXPECT_EQ(buf[0], c[0]);
EXPECT_EQ(buf[1], c[1]);
EXPECT_EQ(buf[2], c[2]);
EXPECT_EQ(buf[3], a[3]);
#ifdef ABSL_HAVE_EXCEPTIONS
EXPECT_THROW(a.copy(buf, 1, 27), std::out_of_range);
#else
ABSL_EXPECT_DEATH_IF_SUPPORTED(a.copy(buf, 1, 27), "absl::string_view::copy");
#endif
2017-09-19 22:54:40 +02:00
}
// Separated from STL1() because some compilers produce an overly
// large stack frame for the combined function.
TEST(StringViewTest, STL2) {
const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
const absl::string_view b("abc");
const absl::string_view c("xyz");
absl::string_view d("foobar");
const absl::string_view e;
const absl::string_view f(
"123"
"\0"
"456",
7);
d = absl::string_view();
EXPECT_EQ(d.size(), 0);
EXPECT_TRUE(d.empty());
EXPECT_TRUE(d.data() == nullptr);
EXPECT_TRUE(d.begin() == d.end());
EXPECT_EQ(a.find(b), 0);
EXPECT_EQ(a.find(b, 1), absl::string_view::npos);
EXPECT_EQ(a.find(c), 23);
EXPECT_EQ(a.find(c, 9), 23);
EXPECT_EQ(a.find(c, absl::string_view::npos), absl::string_view::npos);
EXPECT_EQ(b.find(c), absl::string_view::npos);
EXPECT_EQ(b.find(c, absl::string_view::npos), absl::string_view::npos);
EXPECT_EQ(a.find(d), 0);
EXPECT_EQ(a.find(e), 0);
EXPECT_EQ(a.find(d, 12), 12);
EXPECT_EQ(a.find(e, 17), 17);
absl::string_view g("xx not found bb");
EXPECT_EQ(a.find(g), absl::string_view::npos);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(d.find(b), absl::string_view::npos);
EXPECT_EQ(e.find(b), absl::string_view::npos);
EXPECT_EQ(d.find(b, 4), absl::string_view::npos);
EXPECT_EQ(e.find(b, 7), absl::string_view::npos);
size_t empty_search_pos = std::string().find(std::string());
EXPECT_EQ(d.find(d), empty_search_pos);
EXPECT_EQ(d.find(e), empty_search_pos);
EXPECT_EQ(e.find(d), empty_search_pos);
EXPECT_EQ(e.find(e), empty_search_pos);
EXPECT_EQ(d.find(d, 4), std::string().find(std::string(), 4));
EXPECT_EQ(d.find(e, 4), std::string().find(std::string(), 4));
EXPECT_EQ(e.find(d, 4), std::string().find(std::string(), 4));
EXPECT_EQ(e.find(e, 4), std::string().find(std::string(), 4));
EXPECT_EQ(a.find('a'), 0);
EXPECT_EQ(a.find('c'), 2);
EXPECT_EQ(a.find('z'), 25);
EXPECT_EQ(a.find('$'), absl::string_view::npos);
EXPECT_EQ(a.find('\0'), absl::string_view::npos);
EXPECT_EQ(f.find('\0'), 3);
EXPECT_EQ(f.find('3'), 2);
EXPECT_EQ(f.find('5'), 5);
EXPECT_EQ(g.find('o'), 4);
EXPECT_EQ(g.find('o', 4), 4);
EXPECT_EQ(g.find('o', 5), 8);
EXPECT_EQ(a.find('b', 5), absl::string_view::npos);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(d.find('\0'), absl::string_view::npos);
EXPECT_EQ(e.find('\0'), absl::string_view::npos);
EXPECT_EQ(d.find('\0', 4), absl::string_view::npos);
EXPECT_EQ(e.find('\0', 7), absl::string_view::npos);
EXPECT_EQ(d.find('x'), absl::string_view::npos);
EXPECT_EQ(e.find('x'), absl::string_view::npos);
EXPECT_EQ(d.find('x', 4), absl::string_view::npos);
EXPECT_EQ(e.find('x', 7), absl::string_view::npos);
EXPECT_EQ(a.rfind(b), 0);
EXPECT_EQ(a.rfind(b, 1), 0);
EXPECT_EQ(a.rfind(c), 23);
EXPECT_EQ(a.rfind(c, 22), absl::string_view::npos);
EXPECT_EQ(a.rfind(c, 1), absl::string_view::npos);
EXPECT_EQ(a.rfind(c, 0), absl::string_view::npos);
EXPECT_EQ(b.rfind(c), absl::string_view::npos);
EXPECT_EQ(b.rfind(c, 0), absl::string_view::npos);
EXPECT_EQ(a.rfind(d), std::string(a).rfind(std::string()));
EXPECT_EQ(a.rfind(e), std::string(a).rfind(std::string()));
EXPECT_EQ(a.rfind(d, 12), 12);
EXPECT_EQ(a.rfind(e, 17), 17);
EXPECT_EQ(a.rfind(g), absl::string_view::npos);
EXPECT_EQ(d.rfind(b), absl::string_view::npos);
EXPECT_EQ(e.rfind(b), absl::string_view::npos);
EXPECT_EQ(d.rfind(b, 4), absl::string_view::npos);
EXPECT_EQ(e.rfind(b, 7), absl::string_view::npos);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(d.rfind(d, 4), std::string().rfind(std::string()));
EXPECT_EQ(e.rfind(d, 7), std::string().rfind(std::string()));
EXPECT_EQ(d.rfind(e, 4), std::string().rfind(std::string()));
EXPECT_EQ(e.rfind(e, 7), std::string().rfind(std::string()));
EXPECT_EQ(d.rfind(d), std::string().rfind(std::string()));
EXPECT_EQ(e.rfind(d), std::string().rfind(std::string()));
EXPECT_EQ(d.rfind(e), std::string().rfind(std::string()));
EXPECT_EQ(e.rfind(e), std::string().rfind(std::string()));
EXPECT_EQ(g.rfind('o'), 8);
EXPECT_EQ(g.rfind('q'), absl::string_view::npos);
EXPECT_EQ(g.rfind('o', 8), 8);
EXPECT_EQ(g.rfind('o', 7), 4);
EXPECT_EQ(g.rfind('o', 3), absl::string_view::npos);
EXPECT_EQ(f.rfind('\0'), 3);
EXPECT_EQ(f.rfind('\0', 12), 3);
EXPECT_EQ(f.rfind('3'), 2);
EXPECT_EQ(f.rfind('5'), 5);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(d.rfind('o'), absl::string_view::npos);
EXPECT_EQ(e.rfind('o'), absl::string_view::npos);
EXPECT_EQ(d.rfind('o', 4), absl::string_view::npos);
EXPECT_EQ(e.rfind('o', 7), absl::string_view::npos);
}
// Continued from STL2
TEST(StringViewTest, STL2FindFirst) {
const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
const absl::string_view b("abc");
const absl::string_view c("xyz");
absl::string_view d("foobar");
const absl::string_view e;
const absl::string_view f(
"123"
"\0"
"456",
7);
absl::string_view g("xx not found bb");
d = absl::string_view();
EXPECT_EQ(a.find_first_of(b), 0);
EXPECT_EQ(a.find_first_of(b, 0), 0);
EXPECT_EQ(a.find_first_of(b, 1), 1);
EXPECT_EQ(a.find_first_of(b, 2), 2);
EXPECT_EQ(a.find_first_of(b, 3), absl::string_view::npos);
EXPECT_EQ(a.find_first_of(c), 23);
EXPECT_EQ(a.find_first_of(c, 23), 23);
EXPECT_EQ(a.find_first_of(c, 24), 24);
EXPECT_EQ(a.find_first_of(c, 25), 25);
EXPECT_EQ(a.find_first_of(c, 26), absl::string_view::npos);
EXPECT_EQ(g.find_first_of(b), 13);
EXPECT_EQ(g.find_first_of(c), 0);
EXPECT_EQ(a.find_first_of(f), absl::string_view::npos);
EXPECT_EQ(f.find_first_of(a), absl::string_view::npos);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(a.find_first_of(d), absl::string_view::npos);
EXPECT_EQ(a.find_first_of(e), absl::string_view::npos);
EXPECT_EQ(d.find_first_of(b), absl::string_view::npos);
EXPECT_EQ(e.find_first_of(b), absl::string_view::npos);
EXPECT_EQ(d.find_first_of(d), absl::string_view::npos);
EXPECT_EQ(e.find_first_of(d), absl::string_view::npos);
EXPECT_EQ(d.find_first_of(e), absl::string_view::npos);
EXPECT_EQ(e.find_first_of(e), absl::string_view::npos);
EXPECT_EQ(a.find_first_not_of(b), 3);
EXPECT_EQ(a.find_first_not_of(c), 0);
EXPECT_EQ(b.find_first_not_of(a), absl::string_view::npos);
EXPECT_EQ(c.find_first_not_of(a), absl::string_view::npos);
EXPECT_EQ(f.find_first_not_of(a), 0);
EXPECT_EQ(a.find_first_not_of(f), 0);
EXPECT_EQ(a.find_first_not_of(d), 0);
EXPECT_EQ(a.find_first_not_of(e), 0);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(a.find_first_not_of(d), 0);
EXPECT_EQ(a.find_first_not_of(e), 0);
EXPECT_EQ(a.find_first_not_of(d, 1), 1);
EXPECT_EQ(a.find_first_not_of(e, 1), 1);
EXPECT_EQ(a.find_first_not_of(d, a.size() - 1), a.size() - 1);
EXPECT_EQ(a.find_first_not_of(e, a.size() - 1), a.size() - 1);
EXPECT_EQ(a.find_first_not_of(d, a.size()), absl::string_view::npos);
EXPECT_EQ(a.find_first_not_of(e, a.size()), absl::string_view::npos);
EXPECT_EQ(a.find_first_not_of(d, absl::string_view::npos),
absl::string_view::npos);
EXPECT_EQ(a.find_first_not_of(e, absl::string_view::npos),
absl::string_view::npos);
EXPECT_EQ(d.find_first_not_of(a), absl::string_view::npos);
EXPECT_EQ(e.find_first_not_of(a), absl::string_view::npos);
EXPECT_EQ(d.find_first_not_of(d), absl::string_view::npos);
EXPECT_EQ(e.find_first_not_of(d), absl::string_view::npos);
EXPECT_EQ(d.find_first_not_of(e), absl::string_view::npos);
EXPECT_EQ(e.find_first_not_of(e), absl::string_view::npos);
absl::string_view h("====");
EXPECT_EQ(h.find_first_not_of('='), absl::string_view::npos);
EXPECT_EQ(h.find_first_not_of('=', 3), absl::string_view::npos);
EXPECT_EQ(h.find_first_not_of('\0'), 0);
EXPECT_EQ(g.find_first_not_of('x'), 2);
EXPECT_EQ(f.find_first_not_of('\0'), 0);
EXPECT_EQ(f.find_first_not_of('\0', 3), 4);
EXPECT_EQ(f.find_first_not_of('\0', 2), 2);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(d.find_first_not_of('x'), absl::string_view::npos);
EXPECT_EQ(e.find_first_not_of('x'), absl::string_view::npos);
EXPECT_EQ(d.find_first_not_of('\0'), absl::string_view::npos);
EXPECT_EQ(e.find_first_not_of('\0'), absl::string_view::npos);
}
// Continued from STL2
TEST(StringViewTest, STL2FindLast) {
const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
const absl::string_view b("abc");
const absl::string_view c("xyz");
absl::string_view d("foobar");
const absl::string_view e;
const absl::string_view f(
"123"
"\0"
"456",
7);
absl::string_view g("xx not found bb");
absl::string_view h("====");
absl::string_view i("56");
d = absl::string_view();
EXPECT_EQ(h.find_last_of(a), absl::string_view::npos);
EXPECT_EQ(g.find_last_of(a), g.size()-1);
EXPECT_EQ(a.find_last_of(b), 2);
EXPECT_EQ(a.find_last_of(c), a.size()-1);
EXPECT_EQ(f.find_last_of(i), 6);
EXPECT_EQ(a.find_last_of('a'), 0);
EXPECT_EQ(a.find_last_of('b'), 1);
EXPECT_EQ(a.find_last_of('z'), 25);
EXPECT_EQ(a.find_last_of('a', 5), 0);
EXPECT_EQ(a.find_last_of('b', 5), 1);
EXPECT_EQ(a.find_last_of('b', 0), absl::string_view::npos);
EXPECT_EQ(a.find_last_of('z', 25), 25);
EXPECT_EQ(a.find_last_of('z', 24), absl::string_view::npos);
EXPECT_EQ(f.find_last_of(i, 5), 5);
EXPECT_EQ(f.find_last_of(i, 6), 6);
EXPECT_EQ(f.find_last_of(a, 4), absl::string_view::npos);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(f.find_last_of(d), absl::string_view::npos);
EXPECT_EQ(f.find_last_of(e), absl::string_view::npos);
EXPECT_EQ(f.find_last_of(d, 4), absl::string_view::npos);
EXPECT_EQ(f.find_last_of(e, 4), absl::string_view::npos);
EXPECT_EQ(d.find_last_of(d), absl::string_view::npos);
EXPECT_EQ(d.find_last_of(e), absl::string_view::npos);
EXPECT_EQ(e.find_last_of(d), absl::string_view::npos);
EXPECT_EQ(e.find_last_of(e), absl::string_view::npos);
EXPECT_EQ(d.find_last_of(f), absl::string_view::npos);
EXPECT_EQ(e.find_last_of(f), absl::string_view::npos);
EXPECT_EQ(d.find_last_of(d, 4), absl::string_view::npos);
EXPECT_EQ(d.find_last_of(e, 4), absl::string_view::npos);
EXPECT_EQ(e.find_last_of(d, 4), absl::string_view::npos);
EXPECT_EQ(e.find_last_of(e, 4), absl::string_view::npos);
EXPECT_EQ(d.find_last_of(f, 4), absl::string_view::npos);
EXPECT_EQ(e.find_last_of(f, 4), absl::string_view::npos);
EXPECT_EQ(a.find_last_not_of(b), a.size()-1);
EXPECT_EQ(a.find_last_not_of(c), 22);
EXPECT_EQ(b.find_last_not_of(a), absl::string_view::npos);
EXPECT_EQ(b.find_last_not_of(b), absl::string_view::npos);
EXPECT_EQ(f.find_last_not_of(i), 4);
EXPECT_EQ(a.find_last_not_of(c, 24), 22);
EXPECT_EQ(a.find_last_not_of(b, 3), 3);
EXPECT_EQ(a.find_last_not_of(b, 2), absl::string_view::npos);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(f.find_last_not_of(d), f.size()-1);
EXPECT_EQ(f.find_last_not_of(e), f.size()-1);
EXPECT_EQ(f.find_last_not_of(d, 4), 4);
EXPECT_EQ(f.find_last_not_of(e, 4), 4);
EXPECT_EQ(d.find_last_not_of(d), absl::string_view::npos);
EXPECT_EQ(d.find_last_not_of(e), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of(d), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of(e), absl::string_view::npos);
EXPECT_EQ(d.find_last_not_of(f), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of(f), absl::string_view::npos);
EXPECT_EQ(d.find_last_not_of(d, 4), absl::string_view::npos);
EXPECT_EQ(d.find_last_not_of(e, 4), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of(d, 4), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of(e, 4), absl::string_view::npos);
EXPECT_EQ(d.find_last_not_of(f, 4), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of(f, 4), absl::string_view::npos);
EXPECT_EQ(h.find_last_not_of('x'), h.size() - 1);
EXPECT_EQ(h.find_last_not_of('='), absl::string_view::npos);
EXPECT_EQ(b.find_last_not_of('c'), 1);
EXPECT_EQ(h.find_last_not_of('x', 2), 2);
EXPECT_EQ(h.find_last_not_of('=', 2), absl::string_view::npos);
EXPECT_EQ(b.find_last_not_of('b', 1), 0);
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(d.find_last_not_of('x'), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of('x'), absl::string_view::npos);
EXPECT_EQ(d.find_last_not_of('\0'), absl::string_view::npos);
EXPECT_EQ(e.find_last_not_of('\0'), absl::string_view::npos);
}
// Continued from STL2
TEST(StringViewTest, STL2Substr) {
const absl::string_view a("abcdefghijklmnopqrstuvwxyz");
const absl::string_view b("abc");
const absl::string_view c("xyz");
absl::string_view d("foobar");
const absl::string_view e;
d = absl::string_view();
EXPECT_EQ(a.substr(0, 3), b);
EXPECT_EQ(a.substr(23), c);
EXPECT_EQ(a.substr(23, 3), c);
EXPECT_EQ(a.substr(23, 99), c);
EXPECT_EQ(a.substr(0), a);
EXPECT_EQ(a.substr(3, 2), "de");
// empty string nonsense
2017-09-19 22:54:40 +02:00
EXPECT_EQ(d.substr(0, 99), e);
// use of npos
EXPECT_EQ(a.substr(0, absl::string_view::npos), a);
EXPECT_EQ(a.substr(23, absl::string_view::npos), c);
// throw exception
#ifdef ABSL_HAVE_EXCEPTIONS
EXPECT_THROW((void)a.substr(99, 2), std::out_of_range);
2017-09-19 22:54:40 +02:00
#else
ABSL_EXPECT_DEATH_IF_SUPPORTED((void)a.substr(99, 2),
"absl::string_view::substr");
2017-09-19 22:54:40 +02:00
#endif
}
TEST(StringViewTest, TruncSubstr) {
const absl::string_view hi("hi");
EXPECT_EQ("", absl::ClippedSubstr(hi, 0, 0));
EXPECT_EQ("h", absl::ClippedSubstr(hi, 0, 1));
EXPECT_EQ("hi", absl::ClippedSubstr(hi, 0));
EXPECT_EQ("i", absl::ClippedSubstr(hi, 1));
EXPECT_EQ("", absl::ClippedSubstr(hi, 2));
EXPECT_EQ("", absl::ClippedSubstr(hi, 3)); // truncation
EXPECT_EQ("", absl::ClippedSubstr(hi, 3, 2)); // truncation
}
TEST(StringViewTest, UTF8) {
std::string utf8 = "\u00E1";
std::string utf8_twice = utf8 + " " + utf8;
int utf8_len = strlen(utf8.data());
EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" "));
EXPECT_EQ(utf8_len, absl::string_view(utf8_twice).find_first_of(" \t"));
2017-09-19 22:54:40 +02:00
}
TEST(StringViewTest, FindConformance) {
struct {
std::string haystack;
std::string needle;
} specs[] = {
{"", ""},
{"", "a"},
{"a", ""},
{"a", "a"},
{"a", "b"},
{"aa", ""},
{"aa", "a"},
{"aa", "b"},
{"ab", "a"},
{"ab", "b"},
{"abcd", ""},
{"abcd", "a"},
{"abcd", "d"},
{"abcd", "ab"},
{"abcd", "bc"},
{"abcd", "cd"},
{"abcd", "abcd"},
};
for (const auto& s : specs) {
SCOPED_TRACE(s.haystack);
SCOPED_TRACE(s.needle);
std::string st = s.haystack;
absl::string_view sp = s.haystack;
for (size_t i = 0; i <= sp.size(); ++i) {
size_t pos = (i == sp.size()) ? absl::string_view::npos : i;
SCOPED_TRACE(pos);
EXPECT_EQ(sp.find(s.needle, pos),
st.find(s.needle, pos));
EXPECT_EQ(sp.rfind(s.needle, pos),
st.rfind(s.needle, pos));
EXPECT_EQ(sp.find_first_of(s.needle, pos),
st.find_first_of(s.needle, pos));
EXPECT_EQ(sp.find_first_not_of(s.needle, pos),
st.find_first_not_of(s.needle, pos));
EXPECT_EQ(sp.find_last_of(s.needle, pos),
st.find_last_of(s.needle, pos));
EXPECT_EQ(sp.find_last_not_of(s.needle, pos),
st.find_last_not_of(s.needle, pos));
}
}
}
TEST(StringViewTest, Remove) {
absl::string_view a("foobar");
std::string s1("123");
s1 += '\0';
s1 += "456";
absl::string_view e;
std::string s2;
// remove_prefix
absl::string_view c(a);
c.remove_prefix(3);
EXPECT_EQ(c, "bar");
c = a;
c.remove_prefix(0);
EXPECT_EQ(c, a);
c.remove_prefix(c.size());
EXPECT_EQ(c, e);
// remove_suffix
c = a;
c.remove_suffix(3);
EXPECT_EQ(c, "foo");
c = a;
c.remove_suffix(0);
EXPECT_EQ(c, a);
c.remove_suffix(c.size());
EXPECT_EQ(c, e);
}
TEST(StringViewTest, Set) {
absl::string_view a("foobar");
absl::string_view empty;
absl::string_view b;
// set
b = absl::string_view("foobar", 6);
EXPECT_EQ(b, a);
b = absl::string_view("foobar", 0);
EXPECT_EQ(b, empty);
b = absl::string_view("foobar", 7);
EXPECT_NE(b, a);
b = absl::string_view("foobar");
EXPECT_EQ(b, a);
}
TEST(StringViewTest, FrontBack) {
static const char arr[] = "abcd";
const absl::string_view csp(arr, 4);
EXPECT_EQ(&arr[0], &csp.front());
EXPECT_EQ(&arr[3], &csp.back());
}
TEST(StringViewTest, FrontBackSingleChar) {
static const char c = 'a';
const absl::string_view csp(&c, 1);
EXPECT_EQ(&c, &csp.front());
EXPECT_EQ(&c, &csp.back());
}
Export of internal Abseil changes -- 0b924fe4e9871200792617329d32beb8356daa9b by Derek Mauro <dmauro@google.com>: Use less threads in the GetTID() test to avoid test timeouts PiperOrigin-RevId: 292566826 -- 0b519c4fd48d61b7c4ea94ed6a6be6e981b9c51a by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292563778 -- 3204f6e07bcc2b5e9098d45f1a20998f25ab808e by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292550551 -- 09fbbe73833478d3f26f3e33c8291b991fd3be51 by Derek Mauro <dmauro@google.com>: Add a debug bounds-check to absl::string_view::operator[] string_view accesses that are out-of-bounds are undefined behavior: https://en.cppreference.com/w/cpp/string/basic_string_view/operator_at This change causes code to abort in debug mode, indicating a bug and possibly a security issue like a buffer overflow. Code broken by this change should be investigated. PiperOrigin-RevId: 292544735 -- bf2c19cb45682628f963d4067c0cd6deed7e656d by Derek Mauro <dmauro@google.com>: Add debug assertions to absl::string_view::front and absl::string_view::back Calling front() or back() on an empty string_view is undefined behavior. This assertion is to help catch broken code. https://en.cppreference.com/w/cpp/string/basic_string_view/front https://en.cppreference.com/w/cpp/string/basic_string_view/back PiperOrigin-RevId: 292453255 -- 47f573679b322f8c0fd2cb037cc87e7bc822ac6b by Xiaoyi Zhang <zhangxy@google.com>: Release functional/CMakeList.txt. PiperOrigin-RevId: 292417025 GitOrigin-RevId: 0b924fe4e9871200792617329d32beb8356daa9b Change-Id: Ie6980fb1ac351d72a2ce4468f25bd31db396f88a
2020-01-31 19:09:12 +01:00
TEST(StringViewTest, FrontBackEmpty) {
#ifndef ABSL_USES_STD_STRING_VIEW
Export of internal Abseil changes -- 79913a12f0cad4baf948430315aabf53f03b6475 by Abseil Team <absl-team@google.com>: Don't inline (Un)LockSlow. PiperOrigin-RevId: 302502344 -- 6b340e80f0690655f24799c8de6707b3a95b8579 by Derek Mauro <dmauro@google.com>: Add hardening assertions to absl::optional's dereference operators PiperOrigin-RevId: 302492862 -- a9951bf4852d8c1aec472cb4b539830411270e4c by Derek Mauro <dmauro@google.com>: Correctly add hardware AES compiler flags under Linux X86-64 Fixes #643 PiperOrigin-RevId: 302490673 -- 314c3621ee4d57b6bc8d64338a1f1d48a69741d1 by Derek Mauro <dmauro@google.com>: Upgrade to hardening assertions in absl::Span::remove_prefix and absl::Span::remove_suffix PiperOrigin-RevId: 302481191 -- a142b8c6c62705c5f0d4fe3113150f0c0b7822b9 by Derek Mauro <dmauro@google.com>: Update docker containers to Bazel 2.2.0, GCC 9.3, and new Clang snapshot PiperOrigin-RevId: 302454042 -- afedeb70a2adc87010030c9ba6f06fe35ec26407 by Derek Mauro <dmauro@google.com>: Add hardening assertions for the preconditions of absl::FixedArray PiperOrigin-RevId: 302441767 -- 44442bfbc0a9a742df32f07cee86a47712efb8b4 by Derek Mauro <dmauro@google.com>: Fix new Clang warning about SpinLock doing operations on enums of different types PiperOrigin-RevId: 302430387 -- 69eaff7f97231779f696321c2ba8b88debf6dd9e by Derek Mauro <dmauro@google.com>: Convert precondition assertions to ABSL_HARDENING_ASSERT for absl::InlinedVector PiperOrigin-RevId: 302427894 -- 26b6db906a0942fd18583dc2cdd1bab32919d964 by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 302425283 -- e62e81422979e922505d2cd9000e1de58123c088 by Derek Mauro <dmauro@google.com>: Add an option to build Abseil in hardened mode In hardened mode, the ABSL_HARDENING_ASSERT() macro is active even when NDEBUG is defined. This allows Abseil to perform runtime checks even in release mode. This should be used to implement things like bounds checks that could otherwise lead to security vulnerabilities. Use the new assertion in absl::string_view and absl::Span to test it. PiperOrigin-RevId: 302119187 GitOrigin-RevId: 79913a12f0cad4baf948430315aabf53f03b6475 Change-Id: I0cc3341fd333a1df313167bab72dc5a759c4a048
2020-03-23 21:16:18 +01:00
#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
Export of internal Abseil changes -- 0b924fe4e9871200792617329d32beb8356daa9b by Derek Mauro <dmauro@google.com>: Use less threads in the GetTID() test to avoid test timeouts PiperOrigin-RevId: 292566826 -- 0b519c4fd48d61b7c4ea94ed6a6be6e981b9c51a by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292563778 -- 3204f6e07bcc2b5e9098d45f1a20998f25ab808e by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292550551 -- 09fbbe73833478d3f26f3e33c8291b991fd3be51 by Derek Mauro <dmauro@google.com>: Add a debug bounds-check to absl::string_view::operator[] string_view accesses that are out-of-bounds are undefined behavior: https://en.cppreference.com/w/cpp/string/basic_string_view/operator_at This change causes code to abort in debug mode, indicating a bug and possibly a security issue like a buffer overflow. Code broken by this change should be investigated. PiperOrigin-RevId: 292544735 -- bf2c19cb45682628f963d4067c0cd6deed7e656d by Derek Mauro <dmauro@google.com>: Add debug assertions to absl::string_view::front and absl::string_view::back Calling front() or back() on an empty string_view is undefined behavior. This assertion is to help catch broken code. https://en.cppreference.com/w/cpp/string/basic_string_view/front https://en.cppreference.com/w/cpp/string/basic_string_view/back PiperOrigin-RevId: 292453255 -- 47f573679b322f8c0fd2cb037cc87e7bc822ac6b by Xiaoyi Zhang <zhangxy@google.com>: Release functional/CMakeList.txt. PiperOrigin-RevId: 292417025 GitOrigin-RevId: 0b924fe4e9871200792617329d32beb8356daa9b Change-Id: Ie6980fb1ac351d72a2ce4468f25bd31db396f88a
2020-01-31 19:09:12 +01:00
// Abseil's string_view implementation has debug assertions that check that
// front() and back() are not called on an empty string_view.
absl::string_view sv;
ABSL_EXPECT_DEATH_IF_SUPPORTED(sv.front(), "");
ABSL_EXPECT_DEATH_IF_SUPPORTED(sv.back(), "");
#endif
#endif
}
// `std::string_view::string_view(const char*)` calls
// `std::char_traits<char>::length(const char*)` to get the string length. In
// libc++, it doesn't allow `nullptr` in the constexpr context, with the error
// "read of dereferenced null pointer is not allowed in a constant expression".
// At run time, the behavior of `std::char_traits::length()` on `nullptr` is
// undefined by the standard and usually results in crash with libc++.
Export of internal Abseil changes -- 62058c9c008e23c787f35c1a5fe05851046a71f1 by Abseil Team <absl-team@google.com>: Fix some strange usage of INSTANTIATE_TEST_SUITE_P PiperOrigin-RevId: 264185105 -- 4400d84027d86415a2f9b81996ff22e7fd7aa30f by Derek Mauro <dmauro@google.com>: Disable testing std::string_view from nullptr on GCC >= GCC9. PiperOrigin-RevId: 264150587 -- 656d5a742ba48d025589709fad33ddae4b02c620 by Matt Calabrese <calabrese@google.com>: Fix `absl::any_cast` such that it properly works with qualifications. PiperOrigin-RevId: 263843429 -- 6ec89214a4ef2170bf069623a56ffd22863b748d by Abseil Team <absl-team@google.com>: Use macros to enable inline constexpr variables in compare.h when the compiler supports the feature. PiperOrigin-RevId: 263790677 -- a5171e0897195a0367fc08abce9504f813d027ff by Derek Mauro <dmauro@google.com>: Add the Apache License to files that are missing it. PiperOrigin-RevId: 263774164 -- 19e09a7ce8a0aac0a7d534e1799e4d73b63a1bb5 by Abseil Team <absl-team@google.com>: Update iter.position when moving up the tree in rebalance_after_delete. This field isn't read after the first iteration in rebalance_after_delete, and I think it's not a correctness issue, but it is read in try_merge_or_rebalance and potentially affects rebalancing decisions so it can affect performance. There's also an extremely unlikely potential for undefined behavior due to signed integer overflow since this field is only ever incremented in try_merge_or_rebalance (and position is an int). Basically though, I just don't think it makes sense to have this invalid iterator floating around here. PiperOrigin-RevId: 263770305 GitOrigin-RevId: 62058c9c008e23c787f35c1a5fe05851046a71f1 Change-Id: I1e2fb7cbfac7507dddedd181414ee35a5778f8f5
2019-08-19 19:27:18 +02:00
// GCC also started rejected this in libstdc++ starting in GCC9.
// In MSVC, creating a constexpr string_view from nullptr also triggers an
// "unevaluable pointer value" error. This compiler implementation conforms
// to the standard, but `absl::string_view` implements a different
// behavior for historical reasons. We work around tests that construct
// `string_view` from `nullptr` when using libc++.
Export of internal Abseil changes -- 2f49cb9009386bc67bf54a2908c8720b749c8b7f by Greg Falcon <gfalcon@google.com>: docs: fix typo Import of https://github.com/abseil/abseil-cpp/pull/397 PiperOrigin-RevId: 277504420 -- f2bed362c1c12d3fa9c22d11f2b918668e8c37b7 by Abseil Team <absl-team@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277502334 -- e33de894ffd49848b9e088f59acc9743d1661948 by Derek Mauro <dmauro@google.com>: Update rules_cc version. The mirror.bazel.build URL does not exist (cache expiration?) PiperOrigin-RevId: 277498394 -- b23757b0747c64634d2d701433782c969effef19 by Abseil Team <absl-team@google.com>: Fix https://github.com/abseil/abseil-cpp/issues/394. PiperOrigin-RevId: 277491405 -- 54c75b8b29813531c52d67cf0ba7063baae4a4f3 by Abseil Team <absl-team@google.com>: Fix comment typos: waker => waiter. PiperOrigin-RevId: 277376952 -- 874eeaa3b3af808fc88b6355245f643674f5e36e by Abseil Team <absl-team@google.com>: Don't use atomic ops on waiter and wakeup counts in CONDVAR waiter mode. Just guard the waiter and wakeup counts with the mutex. This eliminates the race. Also fix a typo in the error message for pthread_cond_timedwait. PiperOrigin-RevId: 277366017 -- ce8c9a63109214519b5a7eaecef2c663c4d566df by Greg Falcon <gfalcon@google.com>: Implement the config options for our four main C++ forward compatibility types. These options control whether the names `any`, `optional`, `string_view`, and `variant` in namespace `absl` are aliases to the corresponding C++17 types in namespace `std`. By default, we continue to auto-detect the configuration of the compiler being run. These options are not intended to be modified on the command line (as -D flags, say). Instead, the options.h file can be modified by distributors of Abseil (e.g., binary packagers, maintainers of local copies of Abseil, etc.) Changing options will change Abseil in an ODR sense. Any program must only link in a single version of Abseil. Linking libraries that use Abseil configured with different options is an error: there is no ABI compatibility guarantee when linking different configurations, even if the Abseil versions used are otherwise exactly identical. PiperOrigin-RevId: 277364298 -- 5ed3ad42ae43a05862070f92f9ffd07f5c1f2b81 by Chris Kennelly <ckennelly@google.com>: Suppress -Wimplicit-int-float-conversion. On recent builds of Clang, this is an error/warning. PiperOrigin-RevId: 277346168 -- 9b9b0055243c705189bb27d912e6d45a7789cdee by Eric Fiselier <ericwf@google.com>: Allow building Abseil as a shared library with CMake. By default CMake's `add_library` creates the target as a static library. However, users can override the default using the builtin CMake option -DBUILD_SHARED_LIBS=ON. This changes Abseil's CMake to respect this configuration option by removing the explicit `STATIC` in our usages of `add_library`. PiperOrigin-RevId: 277337753 -- 63a8b7b8ede3a9d851916929d6b8537f4f2508ca by Abseil Team <absl-team@google.com>: Improve AlphaNum Hex performance by using absl::numbers_internal::FastHexToBufferZeroPad16. PiperOrigin-RevId: 277318108 -- dd047f7e92032682d94b27732df0e4d0670e24a4 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 277316913 -- d19ee7074929fed08973cc5b40a844573ce1e0a6 by Abseil Team <absl-team@google.com>: Handle invoking [[nodiscard]] functions correctly in our tests. PiperOrigin-RevId: 277301500 -- 5373f3737894ba9b8481e95e5f58c7957c00d26a by Chris Kennelly <ckennelly@google.com>: For internal reasons, loosen visibility restrictions of `//absl/base:malloc_internal`. As an internal-namespace interface, this module remains unsupported. We reserve the right to change, delete, or re-restrict visibility to this target at any time. PiperOrigin-RevId: 277118689 -- 44e4f6655e05393a375d09b3625c192b1fde5909 by Abseil Team <absl-team@google.com>: Fix error in example civil day comment. PiperOrigin-RevId: 277103315 -- 7657392b4ce48469106f11cdb952a0bc76a28df3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 277056076 -- c75bda76f40b01fa249b75b5a70c1f5907e56c76 by Abseil Team <absl-team@google.com>: Suppress lifetime constant-initialization tests when building with MSVC versions > 19.0. These are broken due to non-compliant initialization order in these versions: https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html We don't know when Microsoft will fix this bug. PiperOrigin-RevId: 277049770 -- 16c3b9bf2a1796efa57f97b00bcd6227fbccca1f by Matt Calabrese <calabrese@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277048670 -- e91003fa3ee6026d9b80624a23fc144fa5d74810 by Chris Kennelly <ckennelly@google.com>: Fix -Wimplicit-int-float-conversion warning in latest clang PiperOrigin-RevId: 276771618 -- 53087ca6603e86ad815f3dd9ab795cc0f79792c1 by Andy Soffer <asoffer@google.com>: Add documentation on absl::SNPrintF. PiperOrigin-RevId: 276694032 -- a9426af8cbd4c3a8f9053e7446c821852e41ff61 by Jorg Brown <jorg@google.com>: Stop including kern/OSByteOrder.h in order to support __APPLE__ Apple compiles with clang now anyway, and clang has support for the built-in compiler swap functions that are much faster than any function call to the OS. PiperOrigin-RevId: 276625231 -- df974be5aa5b4dc1b09c356cb8816edfc7867e63 by Jorg Brown <jorg@google.com>: Fix the build for Android x86-64 builds, where __SSE4_2__ is defined but _bswap64 is not. PiperOrigin-RevId: 276542642 -- d99dc092b3a5ad17643005e55f3b3cb6b9187ccc by Jorg Brown <jorg@google.com>: Remove a byteswap from the non-SSE path of FastHexToBufferZeroPad16 Remove the need for including absl/base/internal/endian.h from the SSE case (since if we have the Intel SSE intrinsics, then clearly we also have the Intel Byte-Swap intrinsics.) PiperOrigin-RevId: 276532608 -- d67b106dc930d8558810ae3983613bb2ab1e0d36 by Abseil Team <absl-team@google.com>: Use explicit static_cast<double> for int64_t to double conversion This uses an explicit static_cast<double>() in the int64_t to double comparisons in duration.cc's SafeAddRepHi. This satisfies clang's -Wimplicit-int-to-float-conversion warning (with https://reviews.llvm.org/D64666). This may also make it easier for humans to realize that the comparison is happening between two floating point double precision values. It should have no impact on the behavior or generated code. Tested: No behavior change PiperOrigin-RevId: 276529211 GitOrigin-RevId: 2f49cb9009386bc67bf54a2908c8720b749c8b7f Change-Id: I71e0781893ce219960b8290d54b20532779cb0ff
2019-10-30 16:00:44 +01:00
#if !defined(ABSL_USES_STD_STRING_VIEW) || \
Export of internal Abseil changes -- 62058c9c008e23c787f35c1a5fe05851046a71f1 by Abseil Team <absl-team@google.com>: Fix some strange usage of INSTANTIATE_TEST_SUITE_P PiperOrigin-RevId: 264185105 -- 4400d84027d86415a2f9b81996ff22e7fd7aa30f by Derek Mauro <dmauro@google.com>: Disable testing std::string_view from nullptr on GCC >= GCC9. PiperOrigin-RevId: 264150587 -- 656d5a742ba48d025589709fad33ddae4b02c620 by Matt Calabrese <calabrese@google.com>: Fix `absl::any_cast` such that it properly works with qualifications. PiperOrigin-RevId: 263843429 -- 6ec89214a4ef2170bf069623a56ffd22863b748d by Abseil Team <absl-team@google.com>: Use macros to enable inline constexpr variables in compare.h when the compiler supports the feature. PiperOrigin-RevId: 263790677 -- a5171e0897195a0367fc08abce9504f813d027ff by Derek Mauro <dmauro@google.com>: Add the Apache License to files that are missing it. PiperOrigin-RevId: 263774164 -- 19e09a7ce8a0aac0a7d534e1799e4d73b63a1bb5 by Abseil Team <absl-team@google.com>: Update iter.position when moving up the tree in rebalance_after_delete. This field isn't read after the first iteration in rebalance_after_delete, and I think it's not a correctness issue, but it is read in try_merge_or_rebalance and potentially affects rebalancing decisions so it can affect performance. There's also an extremely unlikely potential for undefined behavior due to signed integer overflow since this field is only ever incremented in try_merge_or_rebalance (and position is an int). Basically though, I just don't think it makes sense to have this invalid iterator floating around here. PiperOrigin-RevId: 263770305 GitOrigin-RevId: 62058c9c008e23c787f35c1a5fe05851046a71f1 Change-Id: I1e2fb7cbfac7507dddedd181414ee35a5778f8f5
2019-08-19 19:27:18 +02:00
(!(defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 9) && \
!defined(_LIBCPP_VERSION) && !defined(_MSC_VER))
#define ABSL_HAVE_STRING_VIEW_FROM_NULLPTR 1
Export of internal Abseil changes -- 62058c9c008e23c787f35c1a5fe05851046a71f1 by Abseil Team <absl-team@google.com>: Fix some strange usage of INSTANTIATE_TEST_SUITE_P PiperOrigin-RevId: 264185105 -- 4400d84027d86415a2f9b81996ff22e7fd7aa30f by Derek Mauro <dmauro@google.com>: Disable testing std::string_view from nullptr on GCC >= GCC9. PiperOrigin-RevId: 264150587 -- 656d5a742ba48d025589709fad33ddae4b02c620 by Matt Calabrese <calabrese@google.com>: Fix `absl::any_cast` such that it properly works with qualifications. PiperOrigin-RevId: 263843429 -- 6ec89214a4ef2170bf069623a56ffd22863b748d by Abseil Team <absl-team@google.com>: Use macros to enable inline constexpr variables in compare.h when the compiler supports the feature. PiperOrigin-RevId: 263790677 -- a5171e0897195a0367fc08abce9504f813d027ff by Derek Mauro <dmauro@google.com>: Add the Apache License to files that are missing it. PiperOrigin-RevId: 263774164 -- 19e09a7ce8a0aac0a7d534e1799e4d73b63a1bb5 by Abseil Team <absl-team@google.com>: Update iter.position when moving up the tree in rebalance_after_delete. This field isn't read after the first iteration in rebalance_after_delete, and I think it's not a correctness issue, but it is read in try_merge_or_rebalance and potentially affects rebalancing decisions so it can affect performance. There's also an extremely unlikely potential for undefined behavior due to signed integer overflow since this field is only ever incremented in try_merge_or_rebalance (and position is an int). Basically though, I just don't think it makes sense to have this invalid iterator floating around here. PiperOrigin-RevId: 263770305 GitOrigin-RevId: 62058c9c008e23c787f35c1a5fe05851046a71f1 Change-Id: I1e2fb7cbfac7507dddedd181414ee35a5778f8f5
2019-08-19 19:27:18 +02:00
#endif
2017-09-19 22:54:40 +02:00
TEST(StringViewTest, NULLInput) {
absl::string_view s;
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
2017-09-19 22:54:40 +02:00
s = absl::string_view(nullptr);
EXPECT_EQ(s.data(), nullptr);
EXPECT_EQ(s.size(), 0);
// .ToString() on a absl::string_view with nullptr should produce the empty
// string.
2017-09-19 22:54:40 +02:00
EXPECT_EQ("", std::string(s));
#endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
2017-09-19 22:54:40 +02:00
}
TEST(StringViewTest, Comparisons2) {
// The `compare` member has 6 overloads (v: string_view, s: const char*):
// (1) compare(v)
// (2) compare(pos1, count1, v)
// (3) compare(pos1, count1, v, pos2, count2)
// (4) compare(s)
// (5) compare(pos1, count1, s)
// (6) compare(pos1, count1, s, count2)
absl::string_view abc("abcdefghijklmnopqrstuvwxyz");
// check comparison operations on strings longer than 4 bytes.
EXPECT_EQ(abc, absl::string_view("abcdefghijklmnopqrstuvwxyz"));
EXPECT_EQ(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxyz")), 0);
EXPECT_LT(abc, absl::string_view("abcdefghijklmnopqrstuvwxzz"));
EXPECT_LT(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxzz")), 0);
EXPECT_GT(abc, absl::string_view("abcdefghijklmnopqrstuvwxyy"));
EXPECT_GT(abc.compare(absl::string_view("abcdefghijklmnopqrstuvwxyy")), 0);
// The "substr" variants of `compare`.
absl::string_view digits("0123456789");
auto npos = absl::string_view::npos;
// Taking string_view
EXPECT_EQ(digits.compare(3, npos, absl::string_view("3456789")), 0); // 2
EXPECT_EQ(digits.compare(3, 4, absl::string_view("3456")), 0); // 2
EXPECT_EQ(digits.compare(10, 0, absl::string_view()), 0); // 2
EXPECT_EQ(digits.compare(3, 4, absl::string_view("0123456789"), 3, 4),
0); // 3
EXPECT_LT(digits.compare(3, 4, absl::string_view("0123456789"), 3, 5),
0); // 3
EXPECT_LT(digits.compare(0, npos, absl::string_view("0123456789"), 3, 5),
0); // 3
// Taking const char*
EXPECT_EQ(digits.compare(3, 4, "3456"), 0); // 5
EXPECT_EQ(digits.compare(3, npos, "3456789"), 0); // 5
EXPECT_EQ(digits.compare(10, 0, ""), 0); // 5
EXPECT_EQ(digits.compare(3, 4, "0123456789", 3, 4), 0); // 6
EXPECT_LT(digits.compare(3, 4, "0123456789", 3, 5), 0); // 6
EXPECT_LT(digits.compare(0, npos, "0123456789", 3, 5), 0); // 6
}
TEST(StringViewTest, At) {
absl::string_view abc = "abc";
EXPECT_EQ(abc.at(0), 'a');
EXPECT_EQ(abc.at(1), 'b');
EXPECT_EQ(abc.at(2), 'c');
#ifdef ABSL_HAVE_EXCEPTIONS
EXPECT_THROW(abc.at(3), std::out_of_range);
#else
ABSL_EXPECT_DEATH_IF_SUPPORTED(abc.at(3), "absl::string_view::at");
#endif
}
2017-09-19 22:54:40 +02:00
struct MyCharAlloc : std::allocator<char> {};
TEST(StringViewTest, ExplicitConversionOperator) {
absl::string_view sp = "hi";
EXPECT_EQ(sp, std::string(sp));
}
TEST(StringViewTest, NullSafeStringView) {
{
absl::string_view s = absl::NullSafeStringView(nullptr);
EXPECT_EQ(nullptr, s.data());
EXPECT_EQ(0, s.size());
EXPECT_EQ(absl::string_view(), s);
}
{
static const char kHi[] = "hi";
absl::string_view s = absl::NullSafeStringView(kHi);
EXPECT_EQ(kHi, s.data());
EXPECT_EQ(strlen(kHi), s.size());
EXPECT_EQ(absl::string_view("hi"), s);
}
}
Export of internal Abseil changes -- 9beb68204986a015c9cb065b9fae4f9a8879a788 by Abseil Team <absl-team@google.com>: Move Base64EscapeInternal and CalculateBase64EscapedLenInternal to an internal header. PiperOrigin-RevId: 288917378 -- 90acfbe03b3f9f6de3ffa49c39343dfaa2c5d38c by Greg Falcon <gfalcon@google.com>: Update macos CI script to support the ALTERNATE_OPTIONS environment variable. PiperOrigin-RevId: 288913564 -- f1572e870678cdcda6b48cb39780d1ad984e4c1b by Derek Mauro <dmauro@google.com>: Makes absl::NullSafeStringView constexpr Fixes https://github.com/abseil/abseil-cpp/issues/583 PiperOrigin-RevId: 288906940 -- d28a8471e32c10caa64bfffe6d6d4d0a8d144013 by Abseil Team <absl-team@google.com>: absl::GetFlag is lock free for small trivially copyable types. PiperOrigin-RevId: 288768172 -- 2643b8ed1a1dc836b38ab9e46538a1af129ffd67 by Gennadiy Rozental <rogeeff@google.com>: Eliminate call to callback from flag initialization. We do not need to have this invocation inside FlagImpl::Init since SetCallback performs invocation anyways. Calling InitCallback from inside of Init complicates separation of value initialization from data guard initialization, which is about to happen. PiperOrigin-RevId: 288732526 -- 22caa880b7a4cb6da34e16a2e064a473c99e880b by Abseil Team <absl-team@google.com>: Fix the documentation on how to create a null string_view. PiperOrigin-RevId: 288727968 -- 10727c5cadc561837141176f4c9b9717cec9233a by Greg Falcon <gfalcon@google.com>: Change CI scripts for gcc to use the ALTERNATE_OPTIONS file as well. PiperOrigin-RevId: 288718855 -- 5d1e2dd6c7fb12af8aa4337a0f61872f5f0c5992 by Greg Falcon <gfalcon@google.com>: Add an option for using inline namespaces in Abseil. PiperOrigin-RevId: 288614491 GitOrigin-RevId: 9beb68204986a015c9cb065b9fae4f9a8879a788 Change-Id: If9acd46301e3df8cb231b4c16f7ed651bf4fb3c3
2020-01-09 18:58:48 +01:00
TEST(StringViewTest, ConstexprNullSafeStringView) {
{
constexpr absl::string_view s = absl::NullSafeStringView(nullptr);
EXPECT_EQ(nullptr, s.data());
EXPECT_EQ(0, s.size());
EXPECT_EQ(absl::string_view(), s);
}
#if !defined(_MSC_VER) || _MSC_VER >= 1910
// MSVC 2017+ is required for good constexpr string_view support.
// See the implementation of `absl::string_view::StrlenInternal()`.
{
static constexpr char kHi[] = "hi";
absl::string_view s = absl::NullSafeStringView(kHi);
EXPECT_EQ(kHi, s.data());
EXPECT_EQ(strlen(kHi), s.size());
EXPECT_EQ(absl::string_view("hi"), s);
}
{
constexpr absl::string_view s = absl::NullSafeStringView("hello");
EXPECT_EQ(s.size(), 5);
EXPECT_EQ("hello", s);
}
#endif
}
2017-09-19 22:54:40 +02:00
TEST(StringViewTest, ConstexprCompiles) {
constexpr absl::string_view sp;
#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
2017-09-19 22:54:40 +02:00
constexpr absl::string_view cstr(nullptr);
#endif
2017-09-19 22:54:40 +02:00
constexpr absl::string_view cstr_len("cstr", 4);
Export of internal Abseil changes -- 2f49cb9009386bc67bf54a2908c8720b749c8b7f by Greg Falcon <gfalcon@google.com>: docs: fix typo Import of https://github.com/abseil/abseil-cpp/pull/397 PiperOrigin-RevId: 277504420 -- f2bed362c1c12d3fa9c22d11f2b918668e8c37b7 by Abseil Team <absl-team@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277502334 -- e33de894ffd49848b9e088f59acc9743d1661948 by Derek Mauro <dmauro@google.com>: Update rules_cc version. The mirror.bazel.build URL does not exist (cache expiration?) PiperOrigin-RevId: 277498394 -- b23757b0747c64634d2d701433782c969effef19 by Abseil Team <absl-team@google.com>: Fix https://github.com/abseil/abseil-cpp/issues/394. PiperOrigin-RevId: 277491405 -- 54c75b8b29813531c52d67cf0ba7063baae4a4f3 by Abseil Team <absl-team@google.com>: Fix comment typos: waker => waiter. PiperOrigin-RevId: 277376952 -- 874eeaa3b3af808fc88b6355245f643674f5e36e by Abseil Team <absl-team@google.com>: Don't use atomic ops on waiter and wakeup counts in CONDVAR waiter mode. Just guard the waiter and wakeup counts with the mutex. This eliminates the race. Also fix a typo in the error message for pthread_cond_timedwait. PiperOrigin-RevId: 277366017 -- ce8c9a63109214519b5a7eaecef2c663c4d566df by Greg Falcon <gfalcon@google.com>: Implement the config options for our four main C++ forward compatibility types. These options control whether the names `any`, `optional`, `string_view`, and `variant` in namespace `absl` are aliases to the corresponding C++17 types in namespace `std`. By default, we continue to auto-detect the configuration of the compiler being run. These options are not intended to be modified on the command line (as -D flags, say). Instead, the options.h file can be modified by distributors of Abseil (e.g., binary packagers, maintainers of local copies of Abseil, etc.) Changing options will change Abseil in an ODR sense. Any program must only link in a single version of Abseil. Linking libraries that use Abseil configured with different options is an error: there is no ABI compatibility guarantee when linking different configurations, even if the Abseil versions used are otherwise exactly identical. PiperOrigin-RevId: 277364298 -- 5ed3ad42ae43a05862070f92f9ffd07f5c1f2b81 by Chris Kennelly <ckennelly@google.com>: Suppress -Wimplicit-int-float-conversion. On recent builds of Clang, this is an error/warning. PiperOrigin-RevId: 277346168 -- 9b9b0055243c705189bb27d912e6d45a7789cdee by Eric Fiselier <ericwf@google.com>: Allow building Abseil as a shared library with CMake. By default CMake's `add_library` creates the target as a static library. However, users can override the default using the builtin CMake option -DBUILD_SHARED_LIBS=ON. This changes Abseil's CMake to respect this configuration option by removing the explicit `STATIC` in our usages of `add_library`. PiperOrigin-RevId: 277337753 -- 63a8b7b8ede3a9d851916929d6b8537f4f2508ca by Abseil Team <absl-team@google.com>: Improve AlphaNum Hex performance by using absl::numbers_internal::FastHexToBufferZeroPad16. PiperOrigin-RevId: 277318108 -- dd047f7e92032682d94b27732df0e4d0670e24a4 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 277316913 -- d19ee7074929fed08973cc5b40a844573ce1e0a6 by Abseil Team <absl-team@google.com>: Handle invoking [[nodiscard]] functions correctly in our tests. PiperOrigin-RevId: 277301500 -- 5373f3737894ba9b8481e95e5f58c7957c00d26a by Chris Kennelly <ckennelly@google.com>: For internal reasons, loosen visibility restrictions of `//absl/base:malloc_internal`. As an internal-namespace interface, this module remains unsupported. We reserve the right to change, delete, or re-restrict visibility to this target at any time. PiperOrigin-RevId: 277118689 -- 44e4f6655e05393a375d09b3625c192b1fde5909 by Abseil Team <absl-team@google.com>: Fix error in example civil day comment. PiperOrigin-RevId: 277103315 -- 7657392b4ce48469106f11cdb952a0bc76a28df3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 277056076 -- c75bda76f40b01fa249b75b5a70c1f5907e56c76 by Abseil Team <absl-team@google.com>: Suppress lifetime constant-initialization tests when building with MSVC versions > 19.0. These are broken due to non-compliant initialization order in these versions: https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html We don't know when Microsoft will fix this bug. PiperOrigin-RevId: 277049770 -- 16c3b9bf2a1796efa57f97b00bcd6227fbccca1f by Matt Calabrese <calabrese@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277048670 -- e91003fa3ee6026d9b80624a23fc144fa5d74810 by Chris Kennelly <ckennelly@google.com>: Fix -Wimplicit-int-float-conversion warning in latest clang PiperOrigin-RevId: 276771618 -- 53087ca6603e86ad815f3dd9ab795cc0f79792c1 by Andy Soffer <asoffer@google.com>: Add documentation on absl::SNPrintF. PiperOrigin-RevId: 276694032 -- a9426af8cbd4c3a8f9053e7446c821852e41ff61 by Jorg Brown <jorg@google.com>: Stop including kern/OSByteOrder.h in order to support __APPLE__ Apple compiles with clang now anyway, and clang has support for the built-in compiler swap functions that are much faster than any function call to the OS. PiperOrigin-RevId: 276625231 -- df974be5aa5b4dc1b09c356cb8816edfc7867e63 by Jorg Brown <jorg@google.com>: Fix the build for Android x86-64 builds, where __SSE4_2__ is defined but _bswap64 is not. PiperOrigin-RevId: 276542642 -- d99dc092b3a5ad17643005e55f3b3cb6b9187ccc by Jorg Brown <jorg@google.com>: Remove a byteswap from the non-SSE path of FastHexToBufferZeroPad16 Remove the need for including absl/base/internal/endian.h from the SSE case (since if we have the Intel SSE intrinsics, then clearly we also have the Intel Byte-Swap intrinsics.) PiperOrigin-RevId: 276532608 -- d67b106dc930d8558810ae3983613bb2ab1e0d36 by Abseil Team <absl-team@google.com>: Use explicit static_cast<double> for int64_t to double conversion This uses an explicit static_cast<double>() in the int64_t to double comparisons in duration.cc's SafeAddRepHi. This satisfies clang's -Wimplicit-int-to-float-conversion warning (with https://reviews.llvm.org/D64666). This may also make it easier for humans to realize that the comparison is happening between two floating point double precision values. It should have no impact on the behavior or generated code. Tested: No behavior change PiperOrigin-RevId: 276529211 GitOrigin-RevId: 2f49cb9009386bc67bf54a2908c8720b749c8b7f Change-Id: I71e0781893ce219960b8290d54b20532779cb0ff
2019-10-30 16:00:44 +01:00
#if defined(ABSL_USES_STD_STRING_VIEW)
2017-09-19 22:54:40 +02:00
// In libstdc++ (as of 7.2), `std::string_view::string_view(const char*)`
// calls `std::char_traits<char>::length(const char*)` to get the string
2017-09-19 22:54:40 +02:00
// length, but it is not marked constexpr yet. See GCC bug:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78156
// Also, there is a LWG issue that adds constexpr to length() which was just
// resolved 2017-06-02. See
// http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2232
// TODO(zhangxy): Update the condition when libstdc++ adopts the constexpr
// length().
#if !defined(__GLIBCXX__)
#define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
#endif // !__GLIBCXX__
Export of internal Abseil changes -- 2f49cb9009386bc67bf54a2908c8720b749c8b7f by Greg Falcon <gfalcon@google.com>: docs: fix typo Import of https://github.com/abseil/abseil-cpp/pull/397 PiperOrigin-RevId: 277504420 -- f2bed362c1c12d3fa9c22d11f2b918668e8c37b7 by Abseil Team <absl-team@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277502334 -- e33de894ffd49848b9e088f59acc9743d1661948 by Derek Mauro <dmauro@google.com>: Update rules_cc version. The mirror.bazel.build URL does not exist (cache expiration?) PiperOrigin-RevId: 277498394 -- b23757b0747c64634d2d701433782c969effef19 by Abseil Team <absl-team@google.com>: Fix https://github.com/abseil/abseil-cpp/issues/394. PiperOrigin-RevId: 277491405 -- 54c75b8b29813531c52d67cf0ba7063baae4a4f3 by Abseil Team <absl-team@google.com>: Fix comment typos: waker => waiter. PiperOrigin-RevId: 277376952 -- 874eeaa3b3af808fc88b6355245f643674f5e36e by Abseil Team <absl-team@google.com>: Don't use atomic ops on waiter and wakeup counts in CONDVAR waiter mode. Just guard the waiter and wakeup counts with the mutex. This eliminates the race. Also fix a typo in the error message for pthread_cond_timedwait. PiperOrigin-RevId: 277366017 -- ce8c9a63109214519b5a7eaecef2c663c4d566df by Greg Falcon <gfalcon@google.com>: Implement the config options for our four main C++ forward compatibility types. These options control whether the names `any`, `optional`, `string_view`, and `variant` in namespace `absl` are aliases to the corresponding C++17 types in namespace `std`. By default, we continue to auto-detect the configuration of the compiler being run. These options are not intended to be modified on the command line (as -D flags, say). Instead, the options.h file can be modified by distributors of Abseil (e.g., binary packagers, maintainers of local copies of Abseil, etc.) Changing options will change Abseil in an ODR sense. Any program must only link in a single version of Abseil. Linking libraries that use Abseil configured with different options is an error: there is no ABI compatibility guarantee when linking different configurations, even if the Abseil versions used are otherwise exactly identical. PiperOrigin-RevId: 277364298 -- 5ed3ad42ae43a05862070f92f9ffd07f5c1f2b81 by Chris Kennelly <ckennelly@google.com>: Suppress -Wimplicit-int-float-conversion. On recent builds of Clang, this is an error/warning. PiperOrigin-RevId: 277346168 -- 9b9b0055243c705189bb27d912e6d45a7789cdee by Eric Fiselier <ericwf@google.com>: Allow building Abseil as a shared library with CMake. By default CMake's `add_library` creates the target as a static library. However, users can override the default using the builtin CMake option -DBUILD_SHARED_LIBS=ON. This changes Abseil's CMake to respect this configuration option by removing the explicit `STATIC` in our usages of `add_library`. PiperOrigin-RevId: 277337753 -- 63a8b7b8ede3a9d851916929d6b8537f4f2508ca by Abseil Team <absl-team@google.com>: Improve AlphaNum Hex performance by using absl::numbers_internal::FastHexToBufferZeroPad16. PiperOrigin-RevId: 277318108 -- dd047f7e92032682d94b27732df0e4d0670e24a4 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 277316913 -- d19ee7074929fed08973cc5b40a844573ce1e0a6 by Abseil Team <absl-team@google.com>: Handle invoking [[nodiscard]] functions correctly in our tests. PiperOrigin-RevId: 277301500 -- 5373f3737894ba9b8481e95e5f58c7957c00d26a by Chris Kennelly <ckennelly@google.com>: For internal reasons, loosen visibility restrictions of `//absl/base:malloc_internal`. As an internal-namespace interface, this module remains unsupported. We reserve the right to change, delete, or re-restrict visibility to this target at any time. PiperOrigin-RevId: 277118689 -- 44e4f6655e05393a375d09b3625c192b1fde5909 by Abseil Team <absl-team@google.com>: Fix error in example civil day comment. PiperOrigin-RevId: 277103315 -- 7657392b4ce48469106f11cdb952a0bc76a28df3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 277056076 -- c75bda76f40b01fa249b75b5a70c1f5907e56c76 by Abseil Team <absl-team@google.com>: Suppress lifetime constant-initialization tests when building with MSVC versions > 19.0. These are broken due to non-compliant initialization order in these versions: https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html We don't know when Microsoft will fix this bug. PiperOrigin-RevId: 277049770 -- 16c3b9bf2a1796efa57f97b00bcd6227fbccca1f by Matt Calabrese <calabrese@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277048670 -- e91003fa3ee6026d9b80624a23fc144fa5d74810 by Chris Kennelly <ckennelly@google.com>: Fix -Wimplicit-int-float-conversion warning in latest clang PiperOrigin-RevId: 276771618 -- 53087ca6603e86ad815f3dd9ab795cc0f79792c1 by Andy Soffer <asoffer@google.com>: Add documentation on absl::SNPrintF. PiperOrigin-RevId: 276694032 -- a9426af8cbd4c3a8f9053e7446c821852e41ff61 by Jorg Brown <jorg@google.com>: Stop including kern/OSByteOrder.h in order to support __APPLE__ Apple compiles with clang now anyway, and clang has support for the built-in compiler swap functions that are much faster than any function call to the OS. PiperOrigin-RevId: 276625231 -- df974be5aa5b4dc1b09c356cb8816edfc7867e63 by Jorg Brown <jorg@google.com>: Fix the build for Android x86-64 builds, where __SSE4_2__ is defined but _bswap64 is not. PiperOrigin-RevId: 276542642 -- d99dc092b3a5ad17643005e55f3b3cb6b9187ccc by Jorg Brown <jorg@google.com>: Remove a byteswap from the non-SSE path of FastHexToBufferZeroPad16 Remove the need for including absl/base/internal/endian.h from the SSE case (since if we have the Intel SSE intrinsics, then clearly we also have the Intel Byte-Swap intrinsics.) PiperOrigin-RevId: 276532608 -- d67b106dc930d8558810ae3983613bb2ab1e0d36 by Abseil Team <absl-team@google.com>: Use explicit static_cast<double> for int64_t to double conversion This uses an explicit static_cast<double>() in the int64_t to double comparisons in duration.cc's SafeAddRepHi. This satisfies clang's -Wimplicit-int-to-float-conversion warning (with https://reviews.llvm.org/D64666). This may also make it easier for humans to realize that the comparison is happening between two floating point double precision values. It should have no impact on the behavior or generated code. Tested: No behavior change PiperOrigin-RevId: 276529211 GitOrigin-RevId: 2f49cb9009386bc67bf54a2908c8720b749c8b7f Change-Id: I71e0781893ce219960b8290d54b20532779cb0ff
2019-10-30 16:00:44 +01:00
#else // ABSL_USES_STD_STRING_VIEW
2017-09-19 22:54:40 +02:00
// This duplicates the check for __builtin_strlen in the header.
#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
(defined(__GNUC__) && !defined(__clang__))
#define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
#elif defined(__GNUC__) // GCC or clang
#error GCC/clang should have constexpr string_view.
#endif
// MSVC 2017+ should be able to construct a constexpr string_view from a cstr.
#if defined(_MSC_VER) && _MSC_VER >= 1910
#define ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR 1
#endif
Export of internal Abseil changes -- 2f49cb9009386bc67bf54a2908c8720b749c8b7f by Greg Falcon <gfalcon@google.com>: docs: fix typo Import of https://github.com/abseil/abseil-cpp/pull/397 PiperOrigin-RevId: 277504420 -- f2bed362c1c12d3fa9c22d11f2b918668e8c37b7 by Abseil Team <absl-team@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277502334 -- e33de894ffd49848b9e088f59acc9743d1661948 by Derek Mauro <dmauro@google.com>: Update rules_cc version. The mirror.bazel.build URL does not exist (cache expiration?) PiperOrigin-RevId: 277498394 -- b23757b0747c64634d2d701433782c969effef19 by Abseil Team <absl-team@google.com>: Fix https://github.com/abseil/abseil-cpp/issues/394. PiperOrigin-RevId: 277491405 -- 54c75b8b29813531c52d67cf0ba7063baae4a4f3 by Abseil Team <absl-team@google.com>: Fix comment typos: waker => waiter. PiperOrigin-RevId: 277376952 -- 874eeaa3b3af808fc88b6355245f643674f5e36e by Abseil Team <absl-team@google.com>: Don't use atomic ops on waiter and wakeup counts in CONDVAR waiter mode. Just guard the waiter and wakeup counts with the mutex. This eliminates the race. Also fix a typo in the error message for pthread_cond_timedwait. PiperOrigin-RevId: 277366017 -- ce8c9a63109214519b5a7eaecef2c663c4d566df by Greg Falcon <gfalcon@google.com>: Implement the config options for our four main C++ forward compatibility types. These options control whether the names `any`, `optional`, `string_view`, and `variant` in namespace `absl` are aliases to the corresponding C++17 types in namespace `std`. By default, we continue to auto-detect the configuration of the compiler being run. These options are not intended to be modified on the command line (as -D flags, say). Instead, the options.h file can be modified by distributors of Abseil (e.g., binary packagers, maintainers of local copies of Abseil, etc.) Changing options will change Abseil in an ODR sense. Any program must only link in a single version of Abseil. Linking libraries that use Abseil configured with different options is an error: there is no ABI compatibility guarantee when linking different configurations, even if the Abseil versions used are otherwise exactly identical. PiperOrigin-RevId: 277364298 -- 5ed3ad42ae43a05862070f92f9ffd07f5c1f2b81 by Chris Kennelly <ckennelly@google.com>: Suppress -Wimplicit-int-float-conversion. On recent builds of Clang, this is an error/warning. PiperOrigin-RevId: 277346168 -- 9b9b0055243c705189bb27d912e6d45a7789cdee by Eric Fiselier <ericwf@google.com>: Allow building Abseil as a shared library with CMake. By default CMake's `add_library` creates the target as a static library. However, users can override the default using the builtin CMake option -DBUILD_SHARED_LIBS=ON. This changes Abseil's CMake to respect this configuration option by removing the explicit `STATIC` in our usages of `add_library`. PiperOrigin-RevId: 277337753 -- 63a8b7b8ede3a9d851916929d6b8537f4f2508ca by Abseil Team <absl-team@google.com>: Improve AlphaNum Hex performance by using absl::numbers_internal::FastHexToBufferZeroPad16. PiperOrigin-RevId: 277318108 -- dd047f7e92032682d94b27732df0e4d0670e24a4 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 277316913 -- d19ee7074929fed08973cc5b40a844573ce1e0a6 by Abseil Team <absl-team@google.com>: Handle invoking [[nodiscard]] functions correctly in our tests. PiperOrigin-RevId: 277301500 -- 5373f3737894ba9b8481e95e5f58c7957c00d26a by Chris Kennelly <ckennelly@google.com>: For internal reasons, loosen visibility restrictions of `//absl/base:malloc_internal`. As an internal-namespace interface, this module remains unsupported. We reserve the right to change, delete, or re-restrict visibility to this target at any time. PiperOrigin-RevId: 277118689 -- 44e4f6655e05393a375d09b3625c192b1fde5909 by Abseil Team <absl-team@google.com>: Fix error in example civil day comment. PiperOrigin-RevId: 277103315 -- 7657392b4ce48469106f11cdb952a0bc76a28df3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 277056076 -- c75bda76f40b01fa249b75b5a70c1f5907e56c76 by Abseil Team <absl-team@google.com>: Suppress lifetime constant-initialization tests when building with MSVC versions > 19.0. These are broken due to non-compliant initialization order in these versions: https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html We don't know when Microsoft will fix this bug. PiperOrigin-RevId: 277049770 -- 16c3b9bf2a1796efa57f97b00bcd6227fbccca1f by Matt Calabrese <calabrese@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277048670 -- e91003fa3ee6026d9b80624a23fc144fa5d74810 by Chris Kennelly <ckennelly@google.com>: Fix -Wimplicit-int-float-conversion warning in latest clang PiperOrigin-RevId: 276771618 -- 53087ca6603e86ad815f3dd9ab795cc0f79792c1 by Andy Soffer <asoffer@google.com>: Add documentation on absl::SNPrintF. PiperOrigin-RevId: 276694032 -- a9426af8cbd4c3a8f9053e7446c821852e41ff61 by Jorg Brown <jorg@google.com>: Stop including kern/OSByteOrder.h in order to support __APPLE__ Apple compiles with clang now anyway, and clang has support for the built-in compiler swap functions that are much faster than any function call to the OS. PiperOrigin-RevId: 276625231 -- df974be5aa5b4dc1b09c356cb8816edfc7867e63 by Jorg Brown <jorg@google.com>: Fix the build for Android x86-64 builds, where __SSE4_2__ is defined but _bswap64 is not. PiperOrigin-RevId: 276542642 -- d99dc092b3a5ad17643005e55f3b3cb6b9187ccc by Jorg Brown <jorg@google.com>: Remove a byteswap from the non-SSE path of FastHexToBufferZeroPad16 Remove the need for including absl/base/internal/endian.h from the SSE case (since if we have the Intel SSE intrinsics, then clearly we also have the Intel Byte-Swap intrinsics.) PiperOrigin-RevId: 276532608 -- d67b106dc930d8558810ae3983613bb2ab1e0d36 by Abseil Team <absl-team@google.com>: Use explicit static_cast<double> for int64_t to double conversion This uses an explicit static_cast<double>() in the int64_t to double comparisons in duration.cc's SafeAddRepHi. This satisfies clang's -Wimplicit-int-to-float-conversion warning (with https://reviews.llvm.org/D64666). This may also make it easier for humans to realize that the comparison is happening between two floating point double precision values. It should have no impact on the behavior or generated code. Tested: No behavior change PiperOrigin-RevId: 276529211 GitOrigin-RevId: 2f49cb9009386bc67bf54a2908c8720b749c8b7f Change-Id: I71e0781893ce219960b8290d54b20532779cb0ff
2019-10-30 16:00:44 +01:00
#endif // ABSL_USES_STD_STRING_VIEW
2017-09-19 22:54:40 +02:00
#ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_FROM_CSTR
constexpr absl::string_view cstr_strlen("foo");
EXPECT_EQ(cstr_strlen.length(), 3);
constexpr absl::string_view cstr_strlen2 = "bar";
EXPECT_EQ(cstr_strlen2, "bar");
Export of internal Abseil changes -- f13697e3d33803f9667d124072da4f6dd8bfbf85 by Andy Soffer <asoffer@google.com>: Addressing https://github.com/abseil/abseil-cpp/issues/314, fixing CMakeLists.txt to reference ABSL_TEST_COPTS rather than ABSL_DEFAULT_COPTS. ABSL_TEST_COPTS should be preferred for all tests so that they are configured consistently (moreover, CMake should agree with Bazel). PiperOrigin-RevId: 274932312 -- c31c24a1fa6bb98136adf51ef37c0818ac366690 by Derek Mauro <dmauro@google.com>: Silence MSAN in the stack consumption test utility PiperOrigin-RevId: 274912950 -- 2412913c05a246cd527cd4c31452f126e9129f3a by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 274847103 -- 75e984a93b5760873501b96ac3229ccfd955daf8 by Abseil Team <absl-team@google.com>: Reformat BUILD file to current standards. PiperOrigin-RevId: 274815392 -- a2780e085f1df1e4ca2c814a58c893d1b78a1d9c by Samuel Benzaquen <sbenza@google.com>: Fix invalid result regarding leading zeros in the exponent. PiperOrigin-RevId: 274808017 -- dd402e1cb5c4ebacb576372ae24bf289d729d323 by Samuel Benzaquen <sbenza@google.com>: Make string_view's relational operators constexpr when possible. PiperOrigin-RevId: 274807873 -- b4ef32565653a5da1cb8bb8d0351586d23519658 by Abseil Team <absl-team@google.com>: Internal rework. PiperOrigin-RevId: 274787159 -- 70d81971c5914e6785b8e8a9d4f6eb2655dd62c0 by Gennadiy Rozental <rogeeff@google.com>: Internal rework. PiperOrigin-RevId: 274715557 -- 14f5b0440e353b899cafaaa15b53e77f98f401af by Gennadiy Rozental <rogeeff@google.com>: Make deprecated statements about ParseFLag/UnparseFlag consistent in a file. PiperOrigin-RevId: 274668123 -- 2e85adbdbb92612e4d750bc34fbca3333128b42d by Abseil Team <absl-team@google.com>: Allow absl::c_equal to be used with arrays. This is achieved by allowing container size computation for arrays. PiperOrigin-RevId: 274426830 -- 219719f107226d328773e6cec99fb473f5d3119c by Gennadiy Rozental <rogeeff@google.com>: Release correct extension interfaces to support usage of absl::Time and absl::Duration as ABSL_FLAG PiperOrigin-RevId: 274273788 -- 47a77f93fda23b69b4a6bdbd506fe643c69a5579 by Gennadiy Rozental <rogeeff@google.com>: Rework of flags persistence/FlagSaver internals. PiperOrigin-RevId: 274225213 -- 7807be3fe757c19e3b0c487298387683d4c9f5b3 by Abseil Team <absl-team@google.com>: Switch reference to sdkddkver.h to lowercase, matching conventions used in the Windows SDK and other uses. This helps to avoid confusion on case-sensitive filesystems. PiperOrigin-RevId: 274061877 -- 561304090087a19f1d10f0475f564fe132ebf06e by Andy Getzendanner <durandal@google.com>: Fix ABSL_WAITER_MODE detection for mingw Import of https://github.com/abseil/abseil-cpp/pull/342 PiperOrigin-RevId: 274030071 -- 9b3caac2cf202b9d440dfa1b4ffd538ac4bf715b by Derek Mauro <dmauro@google.com>: Support using Abseil with the musl libc implementation. Only test changes were required: * Workaround for a bug in sigaltstack() on musl * printf-style pointer formatting (%p) is implementation defined, so verify StrFromat produces something compatible * Fix detection of feenableexcept() PiperOrigin-RevId: 274011666 -- 73e8a938fc139e1cc8670d4513a445bacc855539 by Abseil Team <absl-team@google.com>: nvcc workaround: explicitly specify the definition of node_handle::Base PiperOrigin-RevId: 274011392 -- ab9cc6d042aca7d48e16c504ab10eab39433f4b2 by Andy Soffer <asoffer@google.com>: Internal change PiperOrigin-RevId: 273996318 -- e567c4979ca99c7e71821ec1523b8f5edd2c76ac by Abseil Team <absl-team@google.com>: Introduce a type alias to work around an nvcc bug. On the previous code, nvcc gets confused thinking that T has to be a parameter pack, as IsDecomposable accepts one. PiperOrigin-RevId: 273980472 -- 105b6e6339b77a32f4432de05f44cd3f9c436751 by Eric Fiselier <ericwf@google.com>: Import of CCTZ from GitHub. PiperOrigin-RevId: 273955589 -- 8feb87ff1d7e721fe094855e67c19539d5e582b7 by Abseil Team <absl-team@google.com>: Avoid dual-exporting scheduling_mode.h PiperOrigin-RevId: 273825112 -- fbc37854776d295dae98fb9d06a541f296daab95 by Andy Getzendanner <durandal@google.com>: Fix ABSL_HAVE_ALARM check on mingw Import of https://github.com/abseil/abseil-cpp/pull/341 PiperOrigin-RevId: 273817839 -- 6aedcd63a735b9133e143b043744ba0a25407f6f by Andy Soffer <asoffer@google.com>: Remove bit_gen_view.h now that all callers have been migrated to bit_gen_ref.h Tested: TGP - https://test.corp.google.com/ui#id=OCL:273762409:BASE:273743370:1570639020744:3001bcb5 PiperOrigin-RevId: 273810331 -- 6573de24a66ba715c579f7f32b5c48a1d743c7f8 by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 273589963 -- 91c8c28b6dca26d98b39e8e06a8ed17c701ff793 by Abseil Team <absl-team@google.com>: Update macro name for `ABSL_GUARDED_BY()` in the example section. PiperOrigin-RevId: 273286983 -- 0ff7d1a93d70f8ecd693f8dbb98b7a4a016ca2a4 by Abseil Team <absl-team@google.com>: Fix potential integer overflow in the absl time library. In absl::FromTM, the tm.tm_year is added by 1900 regarding that tm.tm_year represents the years since 1900. This change checks integer overflow before doing the arithmetic operation. PiperOrigin-RevId: 273092952 -- b41c2a1310086807be09a833099ae6d4009f037c by Gennadiy Rozental <rogeeff@google.com>: Correctly Unlock the global mutex in case of concurrent flag initialization. Fixes #386 PiperOrigin-RevId: 272979749 -- c53103e71b2a6063af3c6d4ff68aa2d8f9ae9e06 by Abseil Team <absl-team@google.com>: Try to become idle only when there is no wakeup. Immediately after waking up (when futex wait returns), the current thread tries to become idle doing bunch of memory loads and a branch. Problem is that there is a good chance that we woke up due to a wakeup, especially for actively used threads. For such wakeups, calling MaybeBecomeIdle() would be a waste of cycles. Instead, call MaybeBecomeIdle() only when we are sure there is no wakeup. For idle threads the net effect should be the same. For active, threads this will be more efficient. Moreover, since MaybeBecomeIdle() is called before waiting on the futex, the current thread will try to become idle before sleeping. This should result in more accurate idleness and more efficient release of thread resources. PiperOrigin-RevId: 272940381 GitOrigin-RevId: f13697e3d33803f9667d124072da4f6dd8bfbf85 Change-Id: I36de05aec12595183725652dd362dfa58fb095d0
2019-10-16 03:18:40 +02:00
#if ABSL_HAVE_BUILTIN(__builtin_memcmp) || \
(defined(__GNUC__) && !defined(__clang__))
#define ABSL_HAVE_CONSTEXPR_STRING_VIEW_COMPARISON 1
#endif
#ifdef ABSL_HAVE_CONSTEXPR_STRING_VIEW_COMPARISON
constexpr absl::string_view foo = "foo";
constexpr absl::string_view bar = "bar";
constexpr bool foo_eq_bar = foo == bar;
constexpr bool foo_ne_bar = foo != bar;
constexpr bool foo_lt_bar = foo < bar;
constexpr bool foo_le_bar = foo <= bar;
constexpr bool foo_gt_bar = foo > bar;
constexpr bool foo_ge_bar = foo >= bar;
constexpr int foo_compare_bar = foo.compare(bar);
EXPECT_FALSE(foo_eq_bar);
EXPECT_TRUE(foo_ne_bar);
EXPECT_FALSE(foo_lt_bar);
EXPECT_FALSE(foo_le_bar);
EXPECT_TRUE(foo_gt_bar);
EXPECT_TRUE(foo_ge_bar);
EXPECT_GT(foo_compare_bar, 0);
#endif
2017-09-19 22:54:40 +02:00
#endif
#if !defined(__clang__) || 3 < __clang_major__ || \
(3 == __clang_major__ && 4 < __clang_minor__)
// older clang versions (< 3.5) complain that:
// "cannot perform pointer arithmetic on null pointer"
constexpr absl::string_view::iterator const_begin_empty = sp.begin();
constexpr absl::string_view::iterator const_end_empty = sp.end();
EXPECT_EQ(const_begin_empty, const_end_empty);
#ifdef ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
constexpr absl::string_view::iterator const_begin_nullptr = cstr.begin();
constexpr absl::string_view::iterator const_end_nullptr = cstr.end();
EXPECT_EQ(const_begin_nullptr, const_end_nullptr);
#endif // ABSL_HAVE_STRING_VIEW_FROM_NULLPTR
#endif // !defined(__clang__) || ...
2017-09-19 22:54:40 +02:00
constexpr absl::string_view::iterator const_begin = cstr_len.begin();
constexpr absl::string_view::iterator const_end = cstr_len.end();
constexpr absl::string_view::size_type const_size = cstr_len.size();
constexpr absl::string_view::size_type const_length = cstr_len.length();
Export of internal Abseil changes -- 3dbb096e4662311f81df1017a8e0975e903936cf by Derek Mauro <dmauro@google.com>: Document and workaround a known MSVC bug doing constexpr pointer arithmetic PiperOrigin-RevId: 262604652 -- b5fa7f1a0c776f6ba20d52772a1679ec42ad21fd by Derek Mauro <dmauro@google.com>: Fix typo in macos_xcode_bazel.sh PiperOrigin-RevId: 262591285 -- 89dd77ab5bb44d76b6cb6b2f288e21536e16a85a by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 262582747 -- 32295ed9a0c6c8ab143a912194040eede05d3ea3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 262569140 -- 7f0f5b94197369228024529022d727439d2c894f by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 262563554 -- 314aed043639abbd221074125c57b7c68616de7e by Derek Mauro <dmauro@google.com>: Release absl::btree PiperOrigin-RevId: 262553526 -- 72b44056c6ce9000c4a6cd9aec58b82067c82a13 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 262421185 -- 4e2c12151edf534f929e8e810f1334073f90489a by Abseil Team <absl-team@google.com>: Update documentation to make it less likely for users to write `Hours(24)` without considering using civil dates instead. PiperOrigin-RevId: 262420758 -- 92b85b9573e800bd96b019408eefbc5ce4f68780 by Derek Mauro <dmauro@google.com>: Add the ability to override the bazel version in the macos_xcode_bazel.sh test script. PiperOrigin-RevId: 262412063 GitOrigin-RevId: 3dbb096e4662311f81df1017a8e0975e903936cf Change-Id: I423b2b829dc0c5f814e37bec4d68c7470f43f041
2019-08-09 20:54:24 +02:00
static_assert(const_begin + const_size == const_end,
"pointer arithmetic check");
static_assert(const_begin + const_length == const_end,
"pointer arithmetic check");
#ifndef _MSC_VER
// MSVC has bugs doing constexpr pointer arithmetic.
// https://developercommunity.visualstudio.com/content/problem/482192/bad-pointer-arithmetic-in-constepxr-2019-rc1-svc1.html
2017-09-19 22:54:40 +02:00
EXPECT_EQ(const_begin + const_size, const_end);
EXPECT_EQ(const_begin + const_length, const_end);
Export of internal Abseil changes -- 3dbb096e4662311f81df1017a8e0975e903936cf by Derek Mauro <dmauro@google.com>: Document and workaround a known MSVC bug doing constexpr pointer arithmetic PiperOrigin-RevId: 262604652 -- b5fa7f1a0c776f6ba20d52772a1679ec42ad21fd by Derek Mauro <dmauro@google.com>: Fix typo in macos_xcode_bazel.sh PiperOrigin-RevId: 262591285 -- 89dd77ab5bb44d76b6cb6b2f288e21536e16a85a by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 262582747 -- 32295ed9a0c6c8ab143a912194040eede05d3ea3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 262569140 -- 7f0f5b94197369228024529022d727439d2c894f by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 262563554 -- 314aed043639abbd221074125c57b7c68616de7e by Derek Mauro <dmauro@google.com>: Release absl::btree PiperOrigin-RevId: 262553526 -- 72b44056c6ce9000c4a6cd9aec58b82067c82a13 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 262421185 -- 4e2c12151edf534f929e8e810f1334073f90489a by Abseil Team <absl-team@google.com>: Update documentation to make it less likely for users to write `Hours(24)` without considering using civil dates instead. PiperOrigin-RevId: 262420758 -- 92b85b9573e800bd96b019408eefbc5ce4f68780 by Derek Mauro <dmauro@google.com>: Add the ability to override the bazel version in the macos_xcode_bazel.sh test script. PiperOrigin-RevId: 262412063 GitOrigin-RevId: 3dbb096e4662311f81df1017a8e0975e903936cf Change-Id: I423b2b829dc0c5f814e37bec4d68c7470f43f041
2019-08-09 20:54:24 +02:00
#endif
2017-09-19 22:54:40 +02:00
constexpr bool isempty = sp.empty();
EXPECT_TRUE(isempty);
constexpr const char c = cstr_len[2];
EXPECT_EQ(c, 't');
constexpr const char cfront = cstr_len.front();
constexpr const char cback = cstr_len.back();
EXPECT_EQ(cfront, 'c');
EXPECT_EQ(cback, 'r');
constexpr const char* np = sp.data();
constexpr const char* cstr_ptr = cstr_len.data();
EXPECT_EQ(np, nullptr);
EXPECT_NE(cstr_ptr, nullptr);
constexpr size_t sp_npos = sp.npos;
EXPECT_EQ(sp_npos, -1);
}
Export of internal Abseil changes -- 09c1e7877210fe85c43631538303af801c233e89 by Gennadiy Rozental <rogeeff@google.com>: Change CordRep::length to size_t to be compatible with ChunkIterator. PiperOrigin-RevId: 297901255 -- 226d5e79fb4e20fb09d75f034624d5be770f4ece by Derek Mauro <dmauro@google.com>: Makes absl::string_view::substr constexpr for std::string_view compatibility Fixes #627 PiperOrigin-RevId: 297872778 -- 851aa24a22d0ba5552098bf7e5735c95e4a8d4f7 by Abseil Team <absl-team@google.com>: Reformat one line. PiperOrigin-RevId: 297839574 -- 4f449c462583797455375fa6f1365a6b2cfa7e0a by Benjamin Barenblat <bbaren@google.com>: Internal change PiperOrigin-RevId: 297677173 -- 2d41a250e9a8f272946bc3262463e4025d88fba3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 297537076 -- 6e7fbe1e90999a58556d41955bef44033c61876c by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 297376994 -- 5acf79338191b31cec158b06cd96ca8dfa3e81fe by Derek Mauro <dmauro@google.com>: Add a debug-mode bounds-check on absl::Span::operator[]. PiperOrigin-RevId: 297355826 -- 1c540d06a56c7e92bb07b90f16b4e00b014ef18f by CJ Johnson <johnsoncj@google.com>: Adding new LTS to the list PiperOrigin-RevId: 297235265 -- 696ce48bea6927436ff89f59b887e5869b1b0f38 by Derek Mauro <dmauro@google.com>: Fix build on FreeBSD/powerpc (implement UnscaledCycleClock) Merges/Fixes GitHub #616 PiperOrigin-RevId: 297188640 GitOrigin-RevId: 09c1e7877210fe85c43631538303af801c233e89 Change-Id: I5d97b16bb6378792d2fcf7d29080cca18aa7729a
2020-02-28 21:18:28 +01:00
TEST(StringViewTest, ConstexprSubstr) {
constexpr absl::string_view foobar("foobar", 6);
constexpr absl::string_view foo = foobar.substr(0, 3);
constexpr absl::string_view bar = foobar.substr(3);
EXPECT_EQ(foo, "foo");
EXPECT_EQ(bar, "bar");
}
2017-09-19 22:54:40 +02:00
TEST(StringViewTest, Noexcept) {
EXPECT_TRUE((std::is_nothrow_constructible<absl::string_view,
const std::string&>::value));
EXPECT_TRUE((std::is_nothrow_constructible<absl::string_view,
const std::string&>::value));
2017-09-19 22:54:40 +02:00
EXPECT_TRUE(std::is_nothrow_constructible<absl::string_view>::value);
constexpr absl::string_view sp;
EXPECT_TRUE(noexcept(sp.begin()));
EXPECT_TRUE(noexcept(sp.end()));
EXPECT_TRUE(noexcept(sp.cbegin()));
EXPECT_TRUE(noexcept(sp.cend()));
EXPECT_TRUE(noexcept(sp.rbegin()));
EXPECT_TRUE(noexcept(sp.rend()));
EXPECT_TRUE(noexcept(sp.crbegin()));
EXPECT_TRUE(noexcept(sp.crend()));
EXPECT_TRUE(noexcept(sp.size()));
EXPECT_TRUE(noexcept(sp.length()));
EXPECT_TRUE(noexcept(sp.empty()));
EXPECT_TRUE(noexcept(sp.data()));
EXPECT_TRUE(noexcept(sp.compare(sp)));
EXPECT_TRUE(noexcept(sp.find(sp)));
EXPECT_TRUE(noexcept(sp.find('f')));
EXPECT_TRUE(noexcept(sp.rfind(sp)));
EXPECT_TRUE(noexcept(sp.rfind('f')));
EXPECT_TRUE(noexcept(sp.find_first_of(sp)));
EXPECT_TRUE(noexcept(sp.find_first_of('f')));
EXPECT_TRUE(noexcept(sp.find_last_of(sp)));
EXPECT_TRUE(noexcept(sp.find_last_of('f')));
EXPECT_TRUE(noexcept(sp.find_first_not_of(sp)));
EXPECT_TRUE(noexcept(sp.find_first_not_of('f')));
EXPECT_TRUE(noexcept(sp.find_last_not_of(sp)));
EXPECT_TRUE(noexcept(sp.find_last_not_of('f')));
}
Export of internal Abseil changes -- 0b924fe4e9871200792617329d32beb8356daa9b by Derek Mauro <dmauro@google.com>: Use less threads in the GetTID() test to avoid test timeouts PiperOrigin-RevId: 292566826 -- 0b519c4fd48d61b7c4ea94ed6a6be6e981b9c51a by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292563778 -- 3204f6e07bcc2b5e9098d45f1a20998f25ab808e by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292550551 -- 09fbbe73833478d3f26f3e33c8291b991fd3be51 by Derek Mauro <dmauro@google.com>: Add a debug bounds-check to absl::string_view::operator[] string_view accesses that are out-of-bounds are undefined behavior: https://en.cppreference.com/w/cpp/string/basic_string_view/operator_at This change causes code to abort in debug mode, indicating a bug and possibly a security issue like a buffer overflow. Code broken by this change should be investigated. PiperOrigin-RevId: 292544735 -- bf2c19cb45682628f963d4067c0cd6deed7e656d by Derek Mauro <dmauro@google.com>: Add debug assertions to absl::string_view::front and absl::string_view::back Calling front() or back() on an empty string_view is undefined behavior. This assertion is to help catch broken code. https://en.cppreference.com/w/cpp/string/basic_string_view/front https://en.cppreference.com/w/cpp/string/basic_string_view/back PiperOrigin-RevId: 292453255 -- 47f573679b322f8c0fd2cb037cc87e7bc822ac6b by Xiaoyi Zhang <zhangxy@google.com>: Release functional/CMakeList.txt. PiperOrigin-RevId: 292417025 GitOrigin-RevId: 0b924fe4e9871200792617329d32beb8356daa9b Change-Id: Ie6980fb1ac351d72a2ce4468f25bd31db396f88a
2020-01-31 19:09:12 +01:00
TEST(StringViewTest, BoundsCheck) {
#ifndef ABSL_USES_STD_STRING_VIEW
Export of internal Abseil changes -- 79913a12f0cad4baf948430315aabf53f03b6475 by Abseil Team <absl-team@google.com>: Don't inline (Un)LockSlow. PiperOrigin-RevId: 302502344 -- 6b340e80f0690655f24799c8de6707b3a95b8579 by Derek Mauro <dmauro@google.com>: Add hardening assertions to absl::optional's dereference operators PiperOrigin-RevId: 302492862 -- a9951bf4852d8c1aec472cb4b539830411270e4c by Derek Mauro <dmauro@google.com>: Correctly add hardware AES compiler flags under Linux X86-64 Fixes #643 PiperOrigin-RevId: 302490673 -- 314c3621ee4d57b6bc8d64338a1f1d48a69741d1 by Derek Mauro <dmauro@google.com>: Upgrade to hardening assertions in absl::Span::remove_prefix and absl::Span::remove_suffix PiperOrigin-RevId: 302481191 -- a142b8c6c62705c5f0d4fe3113150f0c0b7822b9 by Derek Mauro <dmauro@google.com>: Update docker containers to Bazel 2.2.0, GCC 9.3, and new Clang snapshot PiperOrigin-RevId: 302454042 -- afedeb70a2adc87010030c9ba6f06fe35ec26407 by Derek Mauro <dmauro@google.com>: Add hardening assertions for the preconditions of absl::FixedArray PiperOrigin-RevId: 302441767 -- 44442bfbc0a9a742df32f07cee86a47712efb8b4 by Derek Mauro <dmauro@google.com>: Fix new Clang warning about SpinLock doing operations on enums of different types PiperOrigin-RevId: 302430387 -- 69eaff7f97231779f696321c2ba8b88debf6dd9e by Derek Mauro <dmauro@google.com>: Convert precondition assertions to ABSL_HARDENING_ASSERT for absl::InlinedVector PiperOrigin-RevId: 302427894 -- 26b6db906a0942fd18583dc2cdd1bab32919d964 by Gennadiy Rozental <rogeeff@google.com>: Internal change PiperOrigin-RevId: 302425283 -- e62e81422979e922505d2cd9000e1de58123c088 by Derek Mauro <dmauro@google.com>: Add an option to build Abseil in hardened mode In hardened mode, the ABSL_HARDENING_ASSERT() macro is active even when NDEBUG is defined. This allows Abseil to perform runtime checks even in release mode. This should be used to implement things like bounds checks that could otherwise lead to security vulnerabilities. Use the new assertion in absl::string_view and absl::Span to test it. PiperOrigin-RevId: 302119187 GitOrigin-RevId: 79913a12f0cad4baf948430315aabf53f03b6475 Change-Id: I0cc3341fd333a1df313167bab72dc5a759c4a048
2020-03-23 21:16:18 +01:00
#if !defined(NDEBUG) || ABSL_OPTION_HARDENED
Export of internal Abseil changes -- 0b924fe4e9871200792617329d32beb8356daa9b by Derek Mauro <dmauro@google.com>: Use less threads in the GetTID() test to avoid test timeouts PiperOrigin-RevId: 292566826 -- 0b519c4fd48d61b7c4ea94ed6a6be6e981b9c51a by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292563778 -- 3204f6e07bcc2b5e9098d45f1a20998f25ab808e by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 292550551 -- 09fbbe73833478d3f26f3e33c8291b991fd3be51 by Derek Mauro <dmauro@google.com>: Add a debug bounds-check to absl::string_view::operator[] string_view accesses that are out-of-bounds are undefined behavior: https://en.cppreference.com/w/cpp/string/basic_string_view/operator_at This change causes code to abort in debug mode, indicating a bug and possibly a security issue like a buffer overflow. Code broken by this change should be investigated. PiperOrigin-RevId: 292544735 -- bf2c19cb45682628f963d4067c0cd6deed7e656d by Derek Mauro <dmauro@google.com>: Add debug assertions to absl::string_view::front and absl::string_view::back Calling front() or back() on an empty string_view is undefined behavior. This assertion is to help catch broken code. https://en.cppreference.com/w/cpp/string/basic_string_view/front https://en.cppreference.com/w/cpp/string/basic_string_view/back PiperOrigin-RevId: 292453255 -- 47f573679b322f8c0fd2cb037cc87e7bc822ac6b by Xiaoyi Zhang <zhangxy@google.com>: Release functional/CMakeList.txt. PiperOrigin-RevId: 292417025 GitOrigin-RevId: 0b924fe4e9871200792617329d32beb8356daa9b Change-Id: Ie6980fb1ac351d72a2ce4468f25bd31db396f88a
2020-01-31 19:09:12 +01:00
// Abseil's string_view implementation has bounds-checking in debug mode.
absl::string_view h = "hello";
ABSL_EXPECT_DEATH_IF_SUPPORTED(h[5], "");
ABSL_EXPECT_DEATH_IF_SUPPORTED(h[-1], "");
#endif
#endif
}
2017-09-19 22:54:40 +02:00
TEST(ComparisonOpsTest, StringCompareNotAmbiguous) {
EXPECT_EQ("hello", std::string("hello"));
EXPECT_LT("hello", std::string("world"));
}
TEST(ComparisonOpsTest, HeterogenousStringViewEquals) {
EXPECT_EQ(absl::string_view("hello"), std::string("hello"));
EXPECT_EQ("hello", absl::string_view("hello"));
}
TEST(FindOneCharTest, EdgeCases) {
absl::string_view a("xxyyyxx");
// Set a = "xyyyx".
a.remove_prefix(1);
a.remove_suffix(1);
EXPECT_EQ(0, a.find('x'));
EXPECT_EQ(0, a.find('x', 0));
EXPECT_EQ(4, a.find('x', 1));
EXPECT_EQ(4, a.find('x', 4));
EXPECT_EQ(absl::string_view::npos, a.find('x', 5));
EXPECT_EQ(4, a.rfind('x'));
EXPECT_EQ(4, a.rfind('x', 5));
EXPECT_EQ(4, a.rfind('x', 4));
EXPECT_EQ(0, a.rfind('x', 3));
EXPECT_EQ(0, a.rfind('x', 0));
// Set a = "yyy".
a.remove_prefix(1);
a.remove_suffix(1);
EXPECT_EQ(absl::string_view::npos, a.find('x'));
EXPECT_EQ(absl::string_view::npos, a.rfind('x'));
}
#ifndef THREAD_SANITIZER // Allocates too much memory for tsan.
TEST(HugeStringView, TwoPointTwoGB) {
if (sizeof(size_t) <= 4 || RunningOnValgrind())
return;
// Try a huge string piece.
2017-09-19 22:54:40 +02:00
const size_t size = size_t{2200} * 1000 * 1000;
std::string s(size, 'a');
absl::string_view sp(s);
EXPECT_EQ(size, sp.length());
sp.remove_prefix(1);
EXPECT_EQ(size - 1, sp.length());
sp.remove_suffix(2);
EXPECT_EQ(size - 1 - 2, sp.length());
}
#endif // THREAD_SANITIZER
Export of internal Abseil changes -- 2f49cb9009386bc67bf54a2908c8720b749c8b7f by Greg Falcon <gfalcon@google.com>: docs: fix typo Import of https://github.com/abseil/abseil-cpp/pull/397 PiperOrigin-RevId: 277504420 -- f2bed362c1c12d3fa9c22d11f2b918668e8c37b7 by Abseil Team <absl-team@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277502334 -- e33de894ffd49848b9e088f59acc9743d1661948 by Derek Mauro <dmauro@google.com>: Update rules_cc version. The mirror.bazel.build URL does not exist (cache expiration?) PiperOrigin-RevId: 277498394 -- b23757b0747c64634d2d701433782c969effef19 by Abseil Team <absl-team@google.com>: Fix https://github.com/abseil/abseil-cpp/issues/394. PiperOrigin-RevId: 277491405 -- 54c75b8b29813531c52d67cf0ba7063baae4a4f3 by Abseil Team <absl-team@google.com>: Fix comment typos: waker => waiter. PiperOrigin-RevId: 277376952 -- 874eeaa3b3af808fc88b6355245f643674f5e36e by Abseil Team <absl-team@google.com>: Don't use atomic ops on waiter and wakeup counts in CONDVAR waiter mode. Just guard the waiter and wakeup counts with the mutex. This eliminates the race. Also fix a typo in the error message for pthread_cond_timedwait. PiperOrigin-RevId: 277366017 -- ce8c9a63109214519b5a7eaecef2c663c4d566df by Greg Falcon <gfalcon@google.com>: Implement the config options for our four main C++ forward compatibility types. These options control whether the names `any`, `optional`, `string_view`, and `variant` in namespace `absl` are aliases to the corresponding C++17 types in namespace `std`. By default, we continue to auto-detect the configuration of the compiler being run. These options are not intended to be modified on the command line (as -D flags, say). Instead, the options.h file can be modified by distributors of Abseil (e.g., binary packagers, maintainers of local copies of Abseil, etc.) Changing options will change Abseil in an ODR sense. Any program must only link in a single version of Abseil. Linking libraries that use Abseil configured with different options is an error: there is no ABI compatibility guarantee when linking different configurations, even if the Abseil versions used are otherwise exactly identical. PiperOrigin-RevId: 277364298 -- 5ed3ad42ae43a05862070f92f9ffd07f5c1f2b81 by Chris Kennelly <ckennelly@google.com>: Suppress -Wimplicit-int-float-conversion. On recent builds of Clang, this is an error/warning. PiperOrigin-RevId: 277346168 -- 9b9b0055243c705189bb27d912e6d45a7789cdee by Eric Fiselier <ericwf@google.com>: Allow building Abseil as a shared library with CMake. By default CMake's `add_library` creates the target as a static library. However, users can override the default using the builtin CMake option -DBUILD_SHARED_LIBS=ON. This changes Abseil's CMake to respect this configuration option by removing the explicit `STATIC` in our usages of `add_library`. PiperOrigin-RevId: 277337753 -- 63a8b7b8ede3a9d851916929d6b8537f4f2508ca by Abseil Team <absl-team@google.com>: Improve AlphaNum Hex performance by using absl::numbers_internal::FastHexToBufferZeroPad16. PiperOrigin-RevId: 277318108 -- dd047f7e92032682d94b27732df0e4d0670e24a4 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 277316913 -- d19ee7074929fed08973cc5b40a844573ce1e0a6 by Abseil Team <absl-team@google.com>: Handle invoking [[nodiscard]] functions correctly in our tests. PiperOrigin-RevId: 277301500 -- 5373f3737894ba9b8481e95e5f58c7957c00d26a by Chris Kennelly <ckennelly@google.com>: For internal reasons, loosen visibility restrictions of `//absl/base:malloc_internal`. As an internal-namespace interface, this module remains unsupported. We reserve the right to change, delete, or re-restrict visibility to this target at any time. PiperOrigin-RevId: 277118689 -- 44e4f6655e05393a375d09b3625c192b1fde5909 by Abseil Team <absl-team@google.com>: Fix error in example civil day comment. PiperOrigin-RevId: 277103315 -- 7657392b4ce48469106f11cdb952a0bc76a28df3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 277056076 -- c75bda76f40b01fa249b75b5a70c1f5907e56c76 by Abseil Team <absl-team@google.com>: Suppress lifetime constant-initialization tests when building with MSVC versions > 19.0. These are broken due to non-compliant initialization order in these versions: https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html We don't know when Microsoft will fix this bug. PiperOrigin-RevId: 277049770 -- 16c3b9bf2a1796efa57f97b00bcd6227fbccca1f by Matt Calabrese <calabrese@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277048670 -- e91003fa3ee6026d9b80624a23fc144fa5d74810 by Chris Kennelly <ckennelly@google.com>: Fix -Wimplicit-int-float-conversion warning in latest clang PiperOrigin-RevId: 276771618 -- 53087ca6603e86ad815f3dd9ab795cc0f79792c1 by Andy Soffer <asoffer@google.com>: Add documentation on absl::SNPrintF. PiperOrigin-RevId: 276694032 -- a9426af8cbd4c3a8f9053e7446c821852e41ff61 by Jorg Brown <jorg@google.com>: Stop including kern/OSByteOrder.h in order to support __APPLE__ Apple compiles with clang now anyway, and clang has support for the built-in compiler swap functions that are much faster than any function call to the OS. PiperOrigin-RevId: 276625231 -- df974be5aa5b4dc1b09c356cb8816edfc7867e63 by Jorg Brown <jorg@google.com>: Fix the build for Android x86-64 builds, where __SSE4_2__ is defined but _bswap64 is not. PiperOrigin-RevId: 276542642 -- d99dc092b3a5ad17643005e55f3b3cb6b9187ccc by Jorg Brown <jorg@google.com>: Remove a byteswap from the non-SSE path of FastHexToBufferZeroPad16 Remove the need for including absl/base/internal/endian.h from the SSE case (since if we have the Intel SSE intrinsics, then clearly we also have the Intel Byte-Swap intrinsics.) PiperOrigin-RevId: 276532608 -- d67b106dc930d8558810ae3983613bb2ab1e0d36 by Abseil Team <absl-team@google.com>: Use explicit static_cast<double> for int64_t to double conversion This uses an explicit static_cast<double>() in the int64_t to double comparisons in duration.cc's SafeAddRepHi. This satisfies clang's -Wimplicit-int-to-float-conversion warning (with https://reviews.llvm.org/D64666). This may also make it easier for humans to realize that the comparison is happening between two floating point double precision values. It should have no impact on the behavior or generated code. Tested: No behavior change PiperOrigin-RevId: 276529211 GitOrigin-RevId: 2f49cb9009386bc67bf54a2908c8720b749c8b7f Change-Id: I71e0781893ce219960b8290d54b20532779cb0ff
2019-10-30 16:00:44 +01:00
#if !defined(NDEBUG) && !defined(ABSL_USES_STD_STRING_VIEW)
2017-09-19 22:54:40 +02:00
TEST(NonNegativeLenTest, NonNegativeLen) {
ABSL_EXPECT_DEATH_IF_SUPPORTED(absl::string_view("xyz", -1),
"len <= kMaxSize");
2017-09-19 22:54:40 +02:00
}
2018-04-18 14:56:39 +02:00
TEST(LenExceedsMaxSizeTest, LenExceedsMaxSize) {
auto max_size = absl::string_view().max_size();
// This should construct ok (although the view itself is obviously invalid).
absl::string_view ok_view("", max_size);
// Adding one to the max should trigger an assertion.
ABSL_EXPECT_DEATH_IF_SUPPORTED(absl::string_view("", max_size + 1),
"len <= kMaxSize");
2018-04-18 14:56:39 +02:00
}
Export of internal Abseil changes -- 2f49cb9009386bc67bf54a2908c8720b749c8b7f by Greg Falcon <gfalcon@google.com>: docs: fix typo Import of https://github.com/abseil/abseil-cpp/pull/397 PiperOrigin-RevId: 277504420 -- f2bed362c1c12d3fa9c22d11f2b918668e8c37b7 by Abseil Team <absl-team@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277502334 -- e33de894ffd49848b9e088f59acc9743d1661948 by Derek Mauro <dmauro@google.com>: Update rules_cc version. The mirror.bazel.build URL does not exist (cache expiration?) PiperOrigin-RevId: 277498394 -- b23757b0747c64634d2d701433782c969effef19 by Abseil Team <absl-team@google.com>: Fix https://github.com/abseil/abseil-cpp/issues/394. PiperOrigin-RevId: 277491405 -- 54c75b8b29813531c52d67cf0ba7063baae4a4f3 by Abseil Team <absl-team@google.com>: Fix comment typos: waker => waiter. PiperOrigin-RevId: 277376952 -- 874eeaa3b3af808fc88b6355245f643674f5e36e by Abseil Team <absl-team@google.com>: Don't use atomic ops on waiter and wakeup counts in CONDVAR waiter mode. Just guard the waiter and wakeup counts with the mutex. This eliminates the race. Also fix a typo in the error message for pthread_cond_timedwait. PiperOrigin-RevId: 277366017 -- ce8c9a63109214519b5a7eaecef2c663c4d566df by Greg Falcon <gfalcon@google.com>: Implement the config options for our four main C++ forward compatibility types. These options control whether the names `any`, `optional`, `string_view`, and `variant` in namespace `absl` are aliases to the corresponding C++17 types in namespace `std`. By default, we continue to auto-detect the configuration of the compiler being run. These options are not intended to be modified on the command line (as -D flags, say). Instead, the options.h file can be modified by distributors of Abseil (e.g., binary packagers, maintainers of local copies of Abseil, etc.) Changing options will change Abseil in an ODR sense. Any program must only link in a single version of Abseil. Linking libraries that use Abseil configured with different options is an error: there is no ABI compatibility guarantee when linking different configurations, even if the Abseil versions used are otherwise exactly identical. PiperOrigin-RevId: 277364298 -- 5ed3ad42ae43a05862070f92f9ffd07f5c1f2b81 by Chris Kennelly <ckennelly@google.com>: Suppress -Wimplicit-int-float-conversion. On recent builds of Clang, this is an error/warning. PiperOrigin-RevId: 277346168 -- 9b9b0055243c705189bb27d912e6d45a7789cdee by Eric Fiselier <ericwf@google.com>: Allow building Abseil as a shared library with CMake. By default CMake's `add_library` creates the target as a static library. However, users can override the default using the builtin CMake option -DBUILD_SHARED_LIBS=ON. This changes Abseil's CMake to respect this configuration option by removing the explicit `STATIC` in our usages of `add_library`. PiperOrigin-RevId: 277337753 -- 63a8b7b8ede3a9d851916929d6b8537f4f2508ca by Abseil Team <absl-team@google.com>: Improve AlphaNum Hex performance by using absl::numbers_internal::FastHexToBufferZeroPad16. PiperOrigin-RevId: 277318108 -- dd047f7e92032682d94b27732df0e4d0670e24a4 by CJ Johnson <johnsoncj@google.com>: Internal change PiperOrigin-RevId: 277316913 -- d19ee7074929fed08973cc5b40a844573ce1e0a6 by Abseil Team <absl-team@google.com>: Handle invoking [[nodiscard]] functions correctly in our tests. PiperOrigin-RevId: 277301500 -- 5373f3737894ba9b8481e95e5f58c7957c00d26a by Chris Kennelly <ckennelly@google.com>: For internal reasons, loosen visibility restrictions of `//absl/base:malloc_internal`. As an internal-namespace interface, this module remains unsupported. We reserve the right to change, delete, or re-restrict visibility to this target at any time. PiperOrigin-RevId: 277118689 -- 44e4f6655e05393a375d09b3625c192b1fde5909 by Abseil Team <absl-team@google.com>: Fix error in example civil day comment. PiperOrigin-RevId: 277103315 -- 7657392b4ce48469106f11cdb952a0bc76a28df3 by Abseil Team <absl-team@google.com>: Internal change PiperOrigin-RevId: 277056076 -- c75bda76f40b01fa249b75b5a70c1f5907e56c76 by Abseil Team <absl-team@google.com>: Suppress lifetime constant-initialization tests when building with MSVC versions > 19.0. These are broken due to non-compliant initialization order in these versions: https://developercommunity.visualstudio.com/content/problem/336946/class-with-constexpr-constructor-not-using-static.html We don't know when Microsoft will fix this bug. PiperOrigin-RevId: 277049770 -- 16c3b9bf2a1796efa57f97b00bcd6227fbccca1f by Matt Calabrese <calabrese@google.com>: Avoid our is_[copy/move]_assignable workarounds in MSVC 19.20 and on, since that release introduces a regression that breaks our workaround. We should ideally use the std forms in more cases, but branching when our workarounds fail is simpler to maintain. PiperOrigin-RevId: 277048670 -- e91003fa3ee6026d9b80624a23fc144fa5d74810 by Chris Kennelly <ckennelly@google.com>: Fix -Wimplicit-int-float-conversion warning in latest clang PiperOrigin-RevId: 276771618 -- 53087ca6603e86ad815f3dd9ab795cc0f79792c1 by Andy Soffer <asoffer@google.com>: Add documentation on absl::SNPrintF. PiperOrigin-RevId: 276694032 -- a9426af8cbd4c3a8f9053e7446c821852e41ff61 by Jorg Brown <jorg@google.com>: Stop including kern/OSByteOrder.h in order to support __APPLE__ Apple compiles with clang now anyway, and clang has support for the built-in compiler swap functions that are much faster than any function call to the OS. PiperOrigin-RevId: 276625231 -- df974be5aa5b4dc1b09c356cb8816edfc7867e63 by Jorg Brown <jorg@google.com>: Fix the build for Android x86-64 builds, where __SSE4_2__ is defined but _bswap64 is not. PiperOrigin-RevId: 276542642 -- d99dc092b3a5ad17643005e55f3b3cb6b9187ccc by Jorg Brown <jorg@google.com>: Remove a byteswap from the non-SSE path of FastHexToBufferZeroPad16 Remove the need for including absl/base/internal/endian.h from the SSE case (since if we have the Intel SSE intrinsics, then clearly we also have the Intel Byte-Swap intrinsics.) PiperOrigin-RevId: 276532608 -- d67b106dc930d8558810ae3983613bb2ab1e0d36 by Abseil Team <absl-team@google.com>: Use explicit static_cast<double> for int64_t to double conversion This uses an explicit static_cast<double>() in the int64_t to double comparisons in duration.cc's SafeAddRepHi. This satisfies clang's -Wimplicit-int-to-float-conversion warning (with https://reviews.llvm.org/D64666). This may also make it easier for humans to realize that the comparison is happening between two floating point double precision values. It should have no impact on the behavior or generated code. Tested: No behavior change PiperOrigin-RevId: 276529211 GitOrigin-RevId: 2f49cb9009386bc67bf54a2908c8720b749c8b7f Change-Id: I71e0781893ce219960b8290d54b20532779cb0ff
2019-10-30 16:00:44 +01:00
#endif // !defined(NDEBUG) && !defined(ABSL_USES_STD_STRING_VIEW)
2017-09-19 22:54:40 +02:00
class StringViewStreamTest : public ::testing::Test {
public:
// Set negative 'width' for right justification.
template <typename T>
std::string Pad(const T& s, int width, char fill = 0) {
std::ostringstream oss;
if (fill != 0) {
oss << std::setfill(fill);
}
if (width < 0) {
width = -width;
oss << std::right;
}
oss << std::setw(width) << s;
return oss.str();
}
};
TEST_F(StringViewStreamTest, Padding) {
std::string s("hello");
absl::string_view sp(s);
for (int w = -64; w < 64; ++w) {
SCOPED_TRACE(w);
EXPECT_EQ(Pad(s, w), Pad(sp, w));
}
for (int w = -64; w < 64; ++w) {
SCOPED_TRACE(w);
EXPECT_EQ(Pad(s, w, '#'), Pad(sp, w, '#'));
}
}
TEST_F(StringViewStreamTest, ResetsWidth) {
// Width should reset after one formatted write.
// If we weren't resetting width after formatting the string_view,
// we'd have width=5 carrying over to the printing of the "]",
// creating "[###hi####]".
std::string s = "hi";
absl::string_view sp = s;
{
std::ostringstream oss;
oss << "[" << std::setfill('#') << std::setw(5) << s << "]";
ASSERT_EQ("[###hi]", oss.str());
}
{
std::ostringstream oss;
oss << "[" << std::setfill('#') << std::setw(5) << sp << "]";
EXPECT_EQ("[###hi]", oss.str());
}
}
} // namespace