diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel index 9fb1d8c..c6f0e4d 100644 --- a/absl/base/BUILD.bazel +++ b/absl/base/BUILD.bazel
@@ -18,9 +18,9 @@ "//absl:copts/configure_copts.bzl", "ABSL_DEFAULT_COPTS", "ABSL_DEFAULT_LINKOPTS", - "ABSL_TEST_COPTS", "ABSL_EXCEPTIONS_FLAG", "ABSL_EXCEPTIONS_FLAG_LINKOPTS", + "ABSL_TEST_COPTS", ) package(default_visibility = ["//visibility:public"]) @@ -444,7 +444,7 @@ cc_test( name = "low_level_alloc_test", - size = "small", + size = "medium", srcs = ["internal/low_level_alloc_test.cc"], copts = ABSL_TEST_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
diff --git a/absl/base/config.h b/absl/base/config.h index 3b81e26..2a14fe7 100644 --- a/absl/base/config.h +++ b/absl/base/config.h
@@ -191,15 +191,13 @@ // * On Clang: // * Building using Clang for Windows, where the Clang runtime library has // 128-bit support only on LP64 architectures, but Windows is LLP64. -// * Building for aarch64, where __int128 exists but has exhibits a sporadic -// compiler crashing bug. // * On Nvidia's nvcc: // * nvcc also defines __GNUC__ and __SIZEOF_INT128__, but not all versions // actually support __int128. #ifdef ABSL_HAVE_INTRINSIC_INT128 #error ABSL_HAVE_INTRINSIC_INT128 cannot be directly set #elif defined(__SIZEOF_INT128__) -#if (defined(__clang__) && !defined(_WIN32) && !defined(__aarch64__)) || \ +#if (defined(__clang__) && !defined(_WIN32)) || \ (defined(__CUDACC__) && __CUDACC_VER_MAJOR__ >= 9) || \ (defined(__GNUC__) && !defined(__clang__) && !defined(__CUDACC__)) #define ABSL_HAVE_INTRINSIC_INT128 1
diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel index f25a9ff..99a7248 100644 --- a/absl/container/BUILD.bazel +++ b/absl/container/BUILD.bazel
@@ -198,11 +198,23 @@ deps = [ ":inlined_vector", "//absl/base", + "//absl/base:core_headers", "//absl/strings", "@com_github_google_benchmark//:benchmark_main", ], ) +cc_test( + name = "inlined_vector_exception_safety_test", + srcs = ["inlined_vector_exception_safety_test.cc"], + copts = ABSL_TEST_COPTS + ABSL_EXCEPTIONS_FLAG, + deps = [ + ":inlined_vector", + "//absl/base:exception_safety_testing", + "@com_google_googletest//:gtest_main", + ], +) + cc_library( name = "test_instance_tracker", testonly = 1, @@ -213,6 +225,7 @@ visibility = [ "//absl:__subpackages__", ], + deps = ["//absl/types:compare"], ) cc_test(
diff --git a/absl/container/CMakeLists.txt b/absl/container/CMakeLists.txt index 9531d7f..526e37a 100644 --- a/absl/container/CMakeLists.txt +++ b/absl/container/CMakeLists.txt
@@ -195,6 +195,22 @@ gmock_main ) +absl_cc_test( + NAME + inlined_vector_exception_safety_test + SRCS + "inlined_vector_exception_safety_test.cc" + COPTS + ${ABSL_TEST_COPTS} + ${ABSL_EXCEPTIONS_FLAG} + LINKOPTS + ${ABSL_EXCEPTIONS_FLAG_LINKOPTS} + DEPS + absl::inlined_vector + absl::exception_safety_testing + gmock_main +) + absl_cc_library( NAME test_instance_tracker @@ -204,6 +220,8 @@ "internal/test_instance_tracker.cc" COPTS ${ABSL_DEFAULT_COPTS} + DEPS + absl::compare TESTONLY )
diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index 34e9aa0..61e0cfb 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h
@@ -69,7 +69,7 @@ static_assert( N > 0, "InlinedVector cannot be instantiated with `0` inlined elements."); - using Storage = inlined_vector_internal::Storage<InlinedVector>; + using Storage = inlined_vector_internal::Storage<T, N, A>; using AllocatorTraits = typename Storage::AllocatorTraits; template <typename Iterator> @@ -784,16 +784,20 @@ // Destroys all elements in the inlined vector, sets the size of `0` and // deallocates the heap allocation if the inlined vector was allocated. void clear() noexcept { - size_type s = size(); - if (storage_.GetIsAllocated()) { - Destroy(storage_.GetAllocatedData(), storage_.GetAllocatedData() + s); - AllocatorTraits::deallocate(storage_.GetAllocator(), - storage_.GetAllocatedData(), + const bool is_allocated = storage_.GetIsAllocated(); + + pointer the_data = + is_allocated ? storage_.GetAllocatedData() : storage_.GetInlinedData(); + + inlined_vector_internal::DestroyElements(storage_.GetAllocator(), the_data, + storage_.GetSize()); + + if (is_allocated) { + AllocatorTraits::deallocate(storage_.GetAllocator(), the_data, storage_.GetAllocatedCapacity()); - } else if (s != 0) { // do nothing for empty vectors - Destroy(storage_.GetInlinedData(), storage_.GetInlinedData() + s); } - storage_.SetInlinedSize(0); + + storage_.SetInlinedSize(/* size = */ 0); } // `InlinedVector::reserve()`
diff --git a/absl/container/inlined_vector_benchmark.cc b/absl/container/inlined_vector_benchmark.cc index 867a29e..7bb3271 100644 --- a/absl/container/inlined_vector_benchmark.cc +++ b/absl/container/inlined_vector_benchmark.cc
@@ -1,4 +1,4 @@ -// Copyright 2017 The Abseil Authors. +// Copyright 2019 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. @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "absl/container/inlined_vector.h" - #include <string> #include <vector> #include "benchmark/benchmark.h" #include "absl/base/internal/raw_logging.h" +#include "absl/base/macros.h" +#include "absl/container/inlined_vector.h" #include "absl/strings/str_cat.h" namespace { @@ -373,4 +373,72 @@ } BENCHMARK(BM_StdVectorEmpty); +constexpr size_t kInlineElements = 4; +constexpr size_t kSmallSize = kInlineElements / 2; +constexpr size_t kLargeSize = kInlineElements * 2; +constexpr size_t kBatchSize = 100; + +struct TrivialType { + size_t val; +}; + +using TrivialVec = absl::InlinedVector<TrivialType, kInlineElements>; + +class NontrivialType { + public: + ABSL_ATTRIBUTE_NOINLINE NontrivialType() : val_() {} + + ABSL_ATTRIBUTE_NOINLINE NontrivialType(const NontrivialType& other) + : val_(other.val_) {} + + ABSL_ATTRIBUTE_NOINLINE NontrivialType& operator=( + const NontrivialType& other) { + val_ = other.val_; + return *this; + } + + ABSL_ATTRIBUTE_NOINLINE ~NontrivialType() noexcept {} + + private: + size_t val_; +}; + +using NontrivialVec = absl::InlinedVector<NontrivialType, kInlineElements>; + +#define BENCHMARK_OPERATION(BM_Function) \ + BENCHMARK_TEMPLATE(BM_Function, TrivialVec, kSmallSize); \ + BENCHMARK_TEMPLATE(BM_Function, TrivialVec, kLargeSize); \ + BENCHMARK_TEMPLATE(BM_Function, NontrivialVec, kSmallSize); \ + BENCHMARK_TEMPLATE(BM_Function, NontrivialVec, kLargeSize) + +template <typename VecT, typename PrepareVec, typename TestVec> +void BatchedBenchmark(benchmark::State& state, PrepareVec prepare_vec, + TestVec test_vec) { + VecT vectors[kBatchSize]; + + while (state.KeepRunningBatch(kBatchSize)) { + // Prepare batch + state.PauseTiming(); + for (auto& vec : vectors) { + prepare_vec(&vec); + } + benchmark::DoNotOptimize(vectors); + state.ResumeTiming(); + + // Test batch + for (auto& vec : vectors) { + test_vec(&vec); + } + } +} + +template <typename VecT, size_t Size> +void BM_Clear(benchmark::State& state) { + BatchedBenchmark<VecT>( + state, + /* prepare_vec = */ [](VecT* vec) { vec->resize(Size); }, + /* test_vec = */ [](VecT* vec) { vec->clear(); }); +} +BENCHMARK_OPERATION(BM_Clear); + } // namespace
diff --git a/absl/container/inlined_vector_exception_safety_test.cc b/absl/container/inlined_vector_exception_safety_test.cc new file mode 100644 index 0000000..0af048b --- /dev/null +++ b/absl/container/inlined_vector_exception_safety_test.cc
@@ -0,0 +1,55 @@ +// Copyright 2019 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include <memory> + +#include "gtest/gtest.h" +#include "absl/base/internal/exception_safety_testing.h" +#include "absl/container/inlined_vector.h" + +namespace { + +constexpr size_t kInlined = 4; +constexpr size_t kSmallSize = kInlined / 2; +constexpr size_t kLargeSize = kInlined * 2; + +using Thrower = testing::ThrowingValue<>; +using ThrowerAlloc = testing::ThrowingAllocator<Thrower>; + +template <typename Allocator = std::allocator<Thrower>> +using InlVec = absl::InlinedVector<Thrower, kInlined, Allocator>; + +TEST(InlinedVector, DefaultConstructor) { + testing::TestThrowingCtor<InlVec<>>(); + + testing::TestThrowingCtor<InlVec<ThrowerAlloc>>(); +} + +TEST(InlinedVector, AllocConstructor) { + auto alloc = std::allocator<Thrower>(); + testing::TestThrowingCtor<InlVec<>>(alloc); + + auto throw_alloc = ThrowerAlloc(); + testing::TestThrowingCtor<InlVec<ThrowerAlloc>>(throw_alloc); +} + +TEST(InlinedVector, Clear) { + auto small_vec = InlVec<>(kSmallSize); + EXPECT_TRUE(testing::TestNothrowOp([&]() { small_vec.clear(); })); + + auto large_vec = InlVec<>(kLargeSize); + EXPECT_TRUE(testing::TestNothrowOp([&]() { large_vec.clear(); })); +} + +} // namespace
diff --git a/absl/container/internal/inlined_vector.h b/absl/container/internal/inlined_vector.h index b8b4f4c..4589ce0 100644 --- a/absl/container/internal/inlined_vector.h +++ b/absl/container/internal/inlined_vector.h
@@ -16,6 +16,7 @@ #define ABSL_CONTAINER_INTERNAL_INLINED_VECTOR_INTERNAL_H_ #include <cstddef> +#include <cstring> #include <iterator> #include <memory> #include <utility> @@ -31,12 +32,35 @@ typename std::iterator_traits<Iterator>::iterator_category, std::forward_iterator_tag>; -template <typename InlinedVector> -class Storage; +template <typename AllocatorType, typename ValueType, typename SizeType> +void DestroyElements(AllocatorType alloc, ValueType* destroy_first, + SizeType destroy_size) { + using AllocatorTraits = std::allocator_traits<AllocatorType>; -template <template <typename, size_t, typename> class InlinedVector, typename T, - size_t N, typename A> -class Storage<InlinedVector<T, N, A>> { + // Destroys `destroy_size` elements from `destroy_first`. + // + // Destroys the range + // [`destroy_first`, `destroy_first + destroy_size`). + // + // NOTE: We assume destructors do not throw and thus make no attempt to roll + // back. + for (SizeType i = 0; i < destroy_size; ++i) { + AllocatorTraits::destroy(alloc, destroy_first + i); + } + +#ifndef NDEBUG + // Overwrite unused memory with `0xab` so we can catch uninitialized usage. + // + // Cast to `void*` to tell the compiler that we don't care that we might be + // scribbling on a vtable pointer. + void* memory = reinterpret_cast<void*>(destroy_first); + size_t memory_size = sizeof(ValueType) * destroy_size; + std::memset(memory, 0xab, memory_size); +#endif // NDEBUG +} + +template <typename T, size_t N, typename A> +class Storage { public: using allocator_type = A; using value_type = typename allocator_type::value_type;
diff --git a/absl/container/internal/test_instance_tracker.h b/absl/container/internal/test_instance_tracker.h index 032d16d..3d4b298 100644 --- a/absl/container/internal/test_instance_tracker.h +++ b/absl/container/internal/test_instance_tracker.h
@@ -18,6 +18,8 @@ #include <cstdlib> #include <ostream> +#include "absl/types/compare.h" + namespace absl { namespace test_internal { @@ -96,6 +98,14 @@ return value_ >= x.value_; } + absl::weak_ordering compare(const BaseCountedInstance& x) const { + ++num_comparisons_; + return value_ < x.value_ + ? absl::weak_ordering::less + : value_ == x.value_ ? absl::weak_ordering::equivalent + : absl::weak_ordering::greater; + } + int value() const { if (!is_live_) std::abort(); return value_;
diff --git a/absl/container/internal/test_instance_tracker_test.cc b/absl/container/internal/test_instance_tracker_test.cc index 091f428..1c6a4fa 100644 --- a/absl/container/internal/test_instance_tracker_test.cc +++ b/absl/container/internal/test_instance_tracker_test.cc
@@ -174,6 +174,8 @@ EXPECT_EQ(5, tracker.comparisons()); EXPECT_FALSE(one >= two); EXPECT_EQ(6, tracker.comparisons()); + EXPECT_TRUE(one.compare(two) < 0); // NOLINT + EXPECT_EQ(7, tracker.comparisons()); tracker.ResetCopiesMovesSwaps(); EXPECT_EQ(0, tracker.comparisons());
diff --git a/absl/copts/GENERATED_AbseilCopts.cmake b/absl/copts/GENERATED_AbseilCopts.cmake index f968ba7..c4948d4 100644 --- a/absl/copts/GENERATED_AbseilCopts.cmake +++ b/absl/copts/GENERATED_AbseilCopts.cmake
@@ -41,6 +41,7 @@ "-Wno-unreachable-code" "-Wno-unused-macros" "-Wno-weak-vtables" + "-Wno-zero-as-null-pointer-constant" "-Wbitfield-enum-conversion" "-Wbool-conversion" "-Wconstant-conversion" @@ -149,6 +150,7 @@ "-Wno-unreachable-code" "-Wno-unused-macros" "-Wno-weak-vtables" + "-Wno-zero-as-null-pointer-constant" "-Wbitfield-enum-conversion" "-Wbool-conversion" "-Wconstant-conversion"
diff --git a/absl/copts/GENERATED_copts.bzl b/absl/copts/GENERATED_copts.bzl index 2b4790a..422b3a9 100644 --- a/absl/copts/GENERATED_copts.bzl +++ b/absl/copts/GENERATED_copts.bzl
@@ -42,6 +42,7 @@ "-Wno-unreachable-code", "-Wno-unused-macros", "-Wno-weak-vtables", + "-Wno-zero-as-null-pointer-constant", "-Wbitfield-enum-conversion", "-Wbool-conversion", "-Wconstant-conversion", @@ -150,6 +151,7 @@ "-Wno-unreachable-code", "-Wno-unused-macros", "-Wno-weak-vtables", + "-Wno-zero-as-null-pointer-constant", "-Wbitfield-enum-conversion", "-Wbool-conversion", "-Wconstant-conversion",
diff --git a/absl/copts/copts.py b/absl/copts/copts.py index 8a0ecf6..5bede34 100644 --- a/absl/copts/copts.py +++ b/absl/copts/copts.py
@@ -70,6 +70,8 @@ # Causes warnings on include guards "-Wno-unused-macros", "-Wno-weak-vtables", + # Causes warnings on usage of types/compare.h comparison operators. + "-Wno-zero-as-null-pointer-constant", ### # Implicit conversion warnings turned off by -Wno-conversion # which are re-enabled below.
diff --git a/absl/debugging/BUILD.bazel b/absl/debugging/BUILD.bazel index 0854314..e4aed5e 100644 --- a/absl/debugging/BUILD.bazel +++ b/absl/debugging/BUILD.bazel
@@ -244,6 +244,7 @@ "//conditions:default": [], }), linkopts = ABSL_LSAN_LINKOPTS + ABSL_DEFAULT_LINKOPTS, + tags = ["notsan"], deps = [ ":leak_check_api_enabled_for_testing", "//absl/base", @@ -256,6 +257,7 @@ srcs = ["leak_check_test.cc"], copts = ["-UABSL_EXPECT_LEAK_SANITIZER"], linkopts = ABSL_DEFAULT_LINKOPTS, + tags = ["noasan"], deps = [ ":leak_check_api_disabled_for_testing", "//absl/base", # for raw_logging @@ -271,6 +273,7 @@ name = "disabled_leak_check_test", srcs = ["leak_check_fail_test.cc"], linkopts = ABSL_LSAN_LINKOPTS + ABSL_DEFAULT_LINKOPTS, + tags = ["notsan"], deps = [ ":leak_check_api_enabled_for_testing", ":leak_check_disable",
diff --git a/absl/flags/BUILD.bazel b/absl/flags/BUILD.bazel index 2d868b0..bb1a7aa 100644 --- a/absl/flags/BUILD.bazel +++ b/absl/flags/BUILD.bazel
@@ -220,6 +220,20 @@ ) cc_test( + name = "config_test", + size = "small", + srcs = [ + "config_test.cc", + ], + copts = ABSL_TEST_COPTS, + linkopts = ABSL_DEFAULT_LINKOPTS, + deps = [ + ":config", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( name = "flag_test", size = "small", srcs = [
diff --git a/absl/flags/CMakeLists.txt b/absl/flags/CMakeLists.txt index 284d627..9e0b441 100644 --- a/absl/flags/CMakeLists.txt +++ b/absl/flags/CMakeLists.txt
@@ -203,6 +203,18 @@ absl_cc_test( NAME + flags_config_test + SRCS + "config_test.cc" + COPTS + ${ABSL_TEST_COPTS} + DEPS + absl::flags_config + gtest_main +) + +absl_cc_test( + NAME flags_flag_test SRCS "flag_test.cc"
diff --git a/absl/flags/config.h b/absl/flags/config.h index a734af4..a9fd97a 100644 --- a/absl/flags/config.h +++ b/absl/flags/config.h
@@ -17,38 +17,32 @@ #define ABSL_FLAGS_CONFIG_H_ // Determine if we should strip string literals from the Flag objects. +// By default we strip string literals on mobile platforms. #if !defined(ABSL_FLAGS_STRIP_NAMES) -// Non-mobile linux platforms don't strip string literals. -#if (defined(__linux__) || defined(__Fuchsia__)) && !defined(__ANDROID__) -#define ABSL_FLAGS_STRIP_NAMES 0 +#if defined(__ANDROID__) +#define ABSL_FLAGS_STRIP_NAMES 1 -// So do Macs (not iOS or embedded Apple platforms). #elif defined(__APPLE__) #include <TargetConditionals.h> -#if !TARGET_OS_IPHONE && !TARGET_OS_EMBEDDED -#define ABSL_FLAGS_STRIP_NAMES 0 +#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE +#define ABSL_FLAGS_STRIP_NAMES 1 +#elif defined(TARGET_OS_EMBEDDED) && TARGET_OS_EMBEDDED +#define ABSL_FLAGS_STRIP_NAMES 1 +#endif // TARGET_OS_* #endif -// And Windows. -#elif defined(_WIN32) -#define ABSL_FLAGS_STRIP_NAMES 0 - -// And Myriad. -#elif defined(__myriad2__) -#define ABSL_FLAGS_STRIP_NAMES 0 - -#endif #endif // !defined(ABSL_FLAGS_STRIP_NAMES) -#if ABSL_FLAGS_STRIP_NAMES -#if !defined(ABSL_FLAGS_STRIP_HELP) -#define ABSL_FLAGS_STRIP_HELP 1 +#if !defined(ABSL_FLAGS_STRIP_NAMES) +// If ABSL_FLAGS_STRIP_NAMES wasn't set on the command line or above, +// the default is not to strip. +#define ABSL_FLAGS_STRIP_NAMES 0 #endif -#else + #if !defined(ABSL_FLAGS_STRIP_HELP) -#define ABSL_FLAGS_STRIP_HELP 0 -#endif +// By default, if we strip names, we also strip help. +#define ABSL_FLAGS_STRIP_HELP ABSL_FLAGS_STRIP_NAMES #endif #endif // ABSL_FLAGS_CONFIG_H_
diff --git a/absl/flags/config_test.cc b/absl/flags/config_test.cc new file mode 100644 index 0000000..6389986 --- /dev/null +++ b/absl/flags/config_test.cc
@@ -0,0 +1,61 @@ +// Copyright 2019 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/flags/config.h" + +#ifdef __APPLE__ +#include <TargetConditionals.h> +#endif + +#include "gtest/gtest.h" + +#ifndef ABSL_FLAGS_STRIP_NAMES +#error ABSL_FLAGS_STRIP_NAMES is not defined +#endif + +#ifndef ABSL_FLAGS_STRIP_HELP +#error ABSL_FLAGS_STRIP_HELP is not defined +#endif + +namespace { + +// Test that ABSL_FLAGS_STRIP_NAMES and ABSL_FLAGS_STRIP_HELP are configured how +// we expect them to be configured by default. If you override this +// configuration, this test will fail, but the code should still be safe to use. +TEST(FlagsConfigTest, Test) { +#if defined(__ANDROID__) + EXPECT_EQ(ABSL_FLAGS_STRIP_NAMES, 1); + EXPECT_EQ(ABSL_FLAGS_STRIP_HELP, 1); +#elif defined(__myriad2__) + EXPECT_EQ(ABSL_FLAGS_STRIP_NAMES, 0); + EXPECT_EQ(ABSL_FLAGS_STRIP_HELP, 0); +#elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE + EXPECT_EQ(ABSL_FLAGS_STRIP_NAMES, 1); + EXPECT_EQ(ABSL_FLAGS_STRIP_HELP, 1); +#elif defined(TARGET_OS_EMBEDDED) && TARGET_OS_EMBEDDED + EXPECT_EQ(ABSL_FLAGS_STRIP_NAMES, 1); + EXPECT_EQ(ABSL_FLAGS_STRIP_HELP, 1); +#elif defined(__APPLE__) + EXPECT_EQ(ABSL_FLAGS_STRIP_NAMES, 0); + EXPECT_EQ(ABSL_FLAGS_STRIP_HELP, 0); +#elif defined(_WIN32) + EXPECT_EQ(ABSL_FLAGS_STRIP_NAMES, 0); + EXPECT_EQ(ABSL_FLAGS_STRIP_HELP, 0); +#elif defined(__linux__) + EXPECT_EQ(ABSL_FLAGS_STRIP_NAMES, 0); + EXPECT_EQ(ABSL_FLAGS_STRIP_HELP, 0); +#endif +} + +} // namespace
diff --git a/absl/flags/internal/registry.cc b/absl/flags/internal/registry.cc index 435c5b0..e52cbb6 100644 --- a/absl/flags/internal/registry.cc +++ b/absl/flags/internal/registry.cc
@@ -15,7 +15,6 @@ #include "absl/flags/internal/registry.h" -#include "absl/base/call_once.h" #include "absl/base/dynamic_annotations.h" #include "absl/base/internal/raw_logging.h" #include "absl/flags/config.h" @@ -151,12 +150,6 @@ FlagPtrMap flag_ptr_map_; - static FlagRegistry* global_registry_; // a singleton registry - - static absl::once_flag global_registry_once_; - - static void InitGlobalRegistry(); - absl::Mutex lock_; // Disallow @@ -164,16 +157,9 @@ FlagRegistry& operator=(const FlagRegistry&); }; -// Get the singleton FlagRegistry object -FlagRegistry* FlagRegistry::global_registry_ = nullptr; -absl::once_flag FlagRegistry::global_registry_once_; - -void FlagRegistry::InitGlobalRegistry() { global_registry_ = new FlagRegistry; } - FlagRegistry* FlagRegistry::GlobalRegistry() { - absl::call_once(global_registry_once_, &InitGlobalRegistry); - - return global_registry_; + static FlagRegistry* global_registry = new FlagRegistry; + return global_registry; } namespace {
diff --git a/absl/strings/BUILD.bazel b/absl/strings/BUILD.bazel index 2b19473..d6ce88d 100644 --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel
@@ -404,7 +404,7 @@ cc_test( name = "numbers_test", - size = "small", + size = "medium", srcs = [ "internal/numbers_test_common.h", "numbers_test.cc", @@ -628,7 +628,7 @@ cc_test( name = "str_format_convert_test", - size = "small", + size = "medium", srcs = ["internal/str_format/convert_test.cc"], copts = ABSL_TEST_COPTS, visibility = ["//visibility:private"],
diff --git a/absl/strings/str_format.h b/absl/strings/str_format.h index 539d951..da3208e 100644 --- a/absl/strings/str_format.h +++ b/absl/strings/str_format.h
@@ -50,7 +50,7 @@ // * A `ParsedFormat` instance, which encapsulates a specific, pre-compiled // format string for a specific set of type(s), and which can be passed // between API boundaries. (The `FormatSpec` type should not be used -// directly.) +// directly except as an argument type for wrapper functions.) // // The `str_format` library provides the ability to output its format strings to // arbitrary sink types: @@ -157,10 +157,15 @@ // FormatSpec // // The `FormatSpec` type defines the makeup of a format string within the -// `str_format` library. You should not need to use or manipulate this type -// directly. A `FormatSpec` is a variadic class template that is evaluated at -// compile-time, according to the format string and arguments that are passed -// to it. +// `str_format` library. It is a variadic class template that is evaluated at +// compile-time, according to the format string and arguments that are passed to +// it. +// +// You should not need to manipulate this type directly. You should only name it +// if you are writing wrapper functions which accept format arguments that will +// be provided unmodified to functions in this library. Such a wrapper function +// might be a class method that provides format arguments and/or internally uses +// the result of formatting. // // For a `FormatSpec` to be valid at compile-time, it must be provided as // either:
diff --git a/absl/strings/str_format_test.cc b/absl/strings/str_format_test.cc index d4cffa0..80830b3 100644 --- a/absl/strings/str_format_test.cc +++ b/absl/strings/str_format_test.cc
@@ -13,7 +13,7 @@ namespace { using str_format_internal::FormatArgImpl; -class FormatEntryPointTest : public ::testing::Test { }; +using FormatEntryPointTest = ::testing::Test; TEST_F(FormatEntryPointTest, Format) { std::string sink; @@ -458,7 +458,7 @@ return out; } -class ParsedFormatTest : public testing::Test {}; +using ParsedFormatTest = ::testing::Test; TEST_F(ParsedFormatTest, SimpleChecked) { EXPECT_EQ("[ABC]{d:1$d}[DEF]", @@ -600,6 +600,24 @@ EXPECT_FALSE((ExtendedParsedFormat<Conv::d, Conv::o>::New("%1$d %o"))); } +using FormatWrapperTest = ::testing::Test; + +// Plain wrapper for StrFormat. +template <typename... Args> +std::string WrappedFormat(const absl::FormatSpec<Args...>& format, + const Args&... args) { + return StrFormat(format, args...); +} + +TEST_F(FormatWrapperTest, ConstexprStringFormat) { + EXPECT_EQ(WrappedFormat("%s there", "hello"), "hello there"); +} + +TEST_F(FormatWrapperTest, ParsedFormat) { + ParsedFormat<'s'> format("%s there"); + EXPECT_EQ(WrappedFormat(format, "hello"), "hello there"); +} + } // namespace } // namespace absl
diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index 37ffff3..76bd307 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc
@@ -154,7 +154,7 @@ if (c < limit) { c++; // spin } else { - ABSL_TSAN_MUTEX_PRE_DIVERT(0, 0); + ABSL_TSAN_MUTEX_PRE_DIVERT(nullptr, 0); if (c == limit) { // yield once AbslInternalMutexYield(); c++; @@ -162,7 +162,7 @@ absl::SleepFor(absl::Microseconds(10)); c = 0; } - ABSL_TSAN_MUTEX_POST_DIVERT(0, 0); + ABSL_TSAN_MUTEX_POST_DIVERT(nullptr, 0); } return (c); } @@ -901,11 +901,15 @@ // base_internal::CycleClock::Now() is 0.5%. int policy; struct sched_param param; - pthread_getschedparam(pthread_self(), &policy, ¶m); - s->priority = param.sched_priority; - s->next_priority_read_cycles = - now_cycles + - static_cast<int64_t>(base_internal::CycleClock::Frequency()); + const int err = pthread_getschedparam(pthread_self(), &policy, ¶m); + if (err != 0) { + ABSL_RAW_LOG(ERROR, "pthread_getschedparam failed: %d", err); + } else { + s->priority = param.sched_priority; + s->next_priority_read_cycles = + now_cycles + + static_cast<int64_t>(base_internal::CycleClock::Frequency()); + } } if (s->priority > head->priority) { // s's priority is above head's // try to put s in priority-fifo order, or failing that at the front. @@ -2583,7 +2587,7 @@ } void CondVar::Signal() { - ABSL_TSAN_MUTEX_PRE_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_PRE_SIGNAL(nullptr, 0); intptr_t v; int c = 0; for (v = cv_.load(std::memory_order_relaxed); v != 0; @@ -2612,17 +2616,17 @@ if ((v & kCvEvent) != 0) { PostSynchEvent(this, SYNCH_EV_SIGNAL); } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); return; } else { c = Delay(c, GENTLE); } } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); } void CondVar::SignalAll () { - ABSL_TSAN_MUTEX_PRE_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_PRE_SIGNAL(nullptr, 0); intptr_t v; int c = 0; for (v = cv_.load(std::memory_order_relaxed); v != 0; @@ -2649,13 +2653,13 @@ if ((v & kCvEvent) != 0) { PostSynchEvent(this, SYNCH_EV_SIGNALALL); } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); return; } else { c = Delay(c, GENTLE); // try again after a delay } } - ABSL_TSAN_MUTEX_POST_SIGNAL(0, 0); + ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0); } void ReleasableMutexLock::Release() {
diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc index 1021122..2e10f09 100644 --- a/absl/synchronization/mutex_test.cc +++ b/absl/synchronization/mutex_test.cc
@@ -815,7 +815,12 @@ // Test that we correctly handle the situation when a lock is // held and then destroyed (w/o unlocking). +#ifdef THREAD_SANITIZER +// TSAN reports errors when locked Mutexes are destroyed. +TEST(Mutex, DISABLED_LockedMutexDestructionBug) NO_THREAD_SAFETY_ANALYSIS { +#else TEST(Mutex, LockedMutexDestructionBug) NO_THREAD_SAFETY_ANALYSIS { +#endif for (int i = 0; i != 10; i++) { // Create, lock and destroy 10 locks. const int kNumLocks = 10; @@ -1062,7 +1067,12 @@ const char ScopedDisableBazelTestWarnings::kVarName[] = "TEST_WARNINGS_OUTPUT_FILE"; +#ifdef THREAD_SANITIZER +// This test intentionally creates deadlocks to test the deadlock detector. +TEST(Mutex, DISABLED_DeadlockDetectorBazelWarning) { +#else TEST(Mutex, DeadlockDetectorBazelWarning) { +#endif absl::SetMutexDeadlockDetectionMode(absl::OnDeadlockCycle::kReport); // Cause deadlock detection to detect something, if it's @@ -1109,7 +1119,12 @@ } } +#ifdef THREAD_SANITIZER +// TSAN reports errors when locked Mutexes are destroyed. +TEST(Mutex, DISABLED_DeadlockIdBug) NO_THREAD_SAFETY_ANALYSIS { +#else TEST(Mutex, DeadlockIdBug) NO_THREAD_SAFETY_ANALYSIS { +#endif // Test a scenario where a cached deadlock graph node id in the // list of held locks is not invalidated when the corresponding // mutex is deleted.
diff --git a/absl/time/internal/cctz/include/cctz/civil_time_detail.h b/absl/time/internal/cctz/include/cctz/civil_time_detail.h index bb191b3..3e24340 100644 --- a/absl/time/internal/cctz/include/cctz/civil_time_detail.h +++ b/absl/time/internal/cctz/include/cctz/civil_time_detail.h
@@ -555,13 +555,43 @@ //////////////////////////////////////////////////////////////////////// CONSTEXPR_F civil_day next_weekday(civil_day cd, weekday wd) noexcept { - do { cd += 1; } while (get_weekday(cd) != wd); - return cd; + CONSTEXPR_D weekday k_weekdays_forw[14] = { + weekday::monday, weekday::tuesday, weekday::wednesday, + weekday::thursday, weekday::friday, weekday::saturday, + weekday::sunday, weekday::monday, weekday::tuesday, + weekday::wednesday, weekday::thursday, weekday::friday, + weekday::saturday, weekday::sunday, + }; + weekday base = get_weekday(cd); + for (int i = 0;; ++i) { + if (base == k_weekdays_forw[i]) { + for (int j = i + 1;; ++j) { + if (wd == k_weekdays_forw[j]) { + return cd + (j - i); + } + } + } + } } CONSTEXPR_F civil_day prev_weekday(civil_day cd, weekday wd) noexcept { - do { cd -= 1; } while (get_weekday(cd) != wd); - return cd; + CONSTEXPR_D weekday k_weekdays_back[14] = { + weekday::sunday, weekday::saturday, weekday::friday, + weekday::thursday, weekday::wednesday, weekday::tuesday, + weekday::monday, weekday::sunday, weekday::saturday, + weekday::friday, weekday::thursday, weekday::wednesday, + weekday::tuesday, weekday::monday, + }; + weekday base = get_weekday(cd); + for (int i = 0;; ++i) { + if (base == k_weekdays_back[i]) { + for (int j = i + 1;; ++j) { + if (wd == k_weekdays_back[j]) { + return cd - (j - i); + } + } + } + } } CONSTEXPR_F int get_yearday(const civil_day& cd) noexcept {
diff --git a/absl/time/internal/cctz/src/cctz_benchmark.cc b/absl/time/internal/cctz/src/cctz_benchmark.cc index 445366e..a40f504 100644 --- a/absl/time/internal/cctz/src/cctz_benchmark.cc +++ b/absl/time/internal/cctz/src/cctz_benchmark.cc
@@ -47,6 +47,56 @@ } BENCHMARK(BM_Step_Days); +void BM_GetWeekday(benchmark::State& state) { + const cctz::civil_day c(2014, 8, 22); + while (state.KeepRunning()) { + benchmark::DoNotOptimize(cctz::get_weekday(c)); + } +} +BENCHMARK(BM_GetWeekday); + +void BM_NextWeekday(benchmark::State& state) { + const cctz::civil_day kStart(2014, 8, 22); + const cctz::civil_day kDays[7] = { + kStart + 0, kStart + 1, kStart + 2, kStart + 3, + kStart + 4, kStart + 5, kStart + 6, + }; + const cctz::weekday kWeekdays[7] = { + cctz::weekday::monday, cctz::weekday::tuesday, cctz::weekday::wednesday, + cctz::weekday::thursday, cctz::weekday::friday, cctz::weekday::saturday, + cctz::weekday::sunday, + }; + while (state.KeepRunningBatch(7 * 7)) { + for (const auto from : kDays) { + for (const auto to : kWeekdays) { + benchmark::DoNotOptimize(cctz::next_weekday(from, to)); + } + } + } +} +BENCHMARK(BM_NextWeekday); + +void BM_PrevWeekday(benchmark::State& state) { + const cctz::civil_day kStart(2014, 8, 22); + const cctz::civil_day kDays[7] = { + kStart + 0, kStart + 1, kStart + 2, kStart + 3, + kStart + 4, kStart + 5, kStart + 6, + }; + const cctz::weekday kWeekdays[7] = { + cctz::weekday::monday, cctz::weekday::tuesday, cctz::weekday::wednesday, + cctz::weekday::thursday, cctz::weekday::friday, cctz::weekday::saturday, + cctz::weekday::sunday, + }; + while (state.KeepRunningBatch(7 * 7)) { + for (const auto from : kDays) { + for (const auto to : kWeekdays) { + benchmark::DoNotOptimize(cctz::prev_weekday(from, to)); + } + } + } +} +BENCHMARK(BM_PrevWeekday); + const char RFC3339_full[] = "%Y-%m-%dT%H:%M:%E*S%Ez"; const char RFC3339_sec[] = "%Y-%m-%dT%H:%M:%S%Ez";
diff --git a/absl/types/BUILD.bazel b/absl/types/BUILD.bazel index a254871..134f9f2 100644 --- a/absl/types/BUILD.bazel +++ b/absl/types/BUILD.bazel
@@ -308,3 +308,27 @@ "@com_google_googletest//:gtest_main", ], ) + +cc_library( + name = "compare", + hdrs = ["compare.h"], + copts = ABSL_DEFAULT_COPTS, + deps = [ + "//absl/base:core_headers", + "//absl/meta:type_traits", + ], +) + +cc_test( + name = "compare_test", + size = "small", + srcs = [ + "compare_test.cc", + ], + copts = ABSL_TEST_COPTS, + deps = [ + ":compare", + "//absl/base", + "@com_google_googletest//:gtest_main", + ], +)
diff --git a/absl/types/CMakeLists.txt b/absl/types/CMakeLists.txt index 9da94eb..4ce685d 100644 --- a/absl/types/CMakeLists.txt +++ b/absl/types/CMakeLists.txt
@@ -299,6 +299,32 @@ gmock_main ) +absl_cc_library( + NAME + compare + HDRS + "compare.h" + COPTS + ${ABSL_DEFAULT_COPTS} + DEPS + absl::core_headers + absl::type_traits + PUBLIC +) + +absl_cc_test( + NAME + compare_test + SRCS + "compare_test.cc" + COPTS + ${ABSL_TEST_COPTS} + DEPS + absl::base + absl::compare + gmock_main +) + # TODO(cohenjon,zhangxy) Figure out why this test is failing on gcc 4.8 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) else()
diff --git a/absl/types/compare.h b/absl/types/compare.h new file mode 100644 index 0000000..50361d6 --- /dev/null +++ b/absl/types/compare.h
@@ -0,0 +1,508 @@ +// Copyright 2018 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// ----------------------------------------------------------------------------- +// compare.h +// ----------------------------------------------------------------------------- +// +// This header file defines the `absl::weak_equality`, `absl::strong_equality`, +// `absl::partial_ordering`, `absl::weak_ordering`, and `absl::strong_ordering` +// types for storing the results of three way comparisons. +// +// Example: +// absl::weak_ordering compare(const std::string& a, const std::string& b); +// +// These are C++11 compatible versions of the C++20 corresponding types +// (`std::weak_equality`, etc.) and are designed to be drop-in replacements +// for code compliant with C++20. + +#ifndef ABSL_TYPES_COMPARE_H_ +#define ABSL_TYPES_COMPARE_H_ + +#include <cstddef> +#include <cstdint> +#include <cstdlib> +#include <type_traits> + +#include "absl/base/attributes.h" +#include "absl/meta/type_traits.h" + +namespace absl { +namespace compare_internal { + +using value_type = int8_t; + +template <typename T> +struct Fail { + static_assert(sizeof(T) < 0, "Only literal `0` is allowed."); +}; + +// We need the NullPtrT template to avoid triggering the modernize-use-nullptr +// ClangTidy warning in user code. +template <typename NullPtrT = std::nullptr_t> +struct OnlyLiteralZero { + constexpr OnlyLiteralZero(NullPtrT) noexcept {} // NOLINT + + // Fails compilation when `nullptr` or integral type arguments other than + // `int` are passed. This constructor doesn't accept `int` because literal `0` + // has type `int`. Literal `0` arguments will be implicitly converted to + // `std::nullptr_t` and accepted by the above constructor, while other `int` + // arguments will fail to be converted and cause compilation failure. + template < + typename T, + typename = typename std::enable_if< + std::is_same<T, std::nullptr_t>::value || + (std::is_integral<T>::value && !std::is_same<T, int>::value)>::type, + typename = typename Fail<T>::type> + OnlyLiteralZero(T); // NOLINT +}; + +enum class eq : value_type { + equal = 0, + equivalent = equal, + nonequal = 1, + nonequivalent = nonequal, +}; + +enum class ord : value_type { less = -1, greater = 1 }; + +enum class ncmp : value_type { unordered = -127 }; + +// These template base classes allow for defining the values of the constants +// in the header file (for performance) without using inline variables (which +// aren't available in C++11). +template <typename T> +struct weak_equality_base { + ABSL_CONST_INIT static const T equivalent; + ABSL_CONST_INIT static const T nonequivalent; +}; +template <typename T> +const T weak_equality_base<T>::equivalent(eq::equivalent); +template <typename T> +const T weak_equality_base<T>::nonequivalent(eq::nonequivalent); + +template <typename T> +struct strong_equality_base { + ABSL_CONST_INIT static const T equal; + ABSL_CONST_INIT static const T nonequal; + ABSL_CONST_INIT static const T equivalent; + ABSL_CONST_INIT static const T nonequivalent; +}; +template <typename T> +const T strong_equality_base<T>::equal(eq::equal); +template <typename T> +const T strong_equality_base<T>::nonequal(eq::nonequal); +template <typename T> +const T strong_equality_base<T>::equivalent(eq::equivalent); +template <typename T> +const T strong_equality_base<T>::nonequivalent(eq::nonequivalent); + +template <typename T> +struct partial_ordering_base { + ABSL_CONST_INIT static const T less; + ABSL_CONST_INIT static const T equivalent; + ABSL_CONST_INIT static const T greater; + ABSL_CONST_INIT static const T unordered; +}; +template <typename T> +const T partial_ordering_base<T>::less(ord::less); +template <typename T> +const T partial_ordering_base<T>::equivalent(eq::equivalent); +template <typename T> +const T partial_ordering_base<T>::greater(ord::greater); +template <typename T> +const T partial_ordering_base<T>::unordered(ncmp::unordered); + +template <typename T> +struct weak_ordering_base { + ABSL_CONST_INIT static const T less; + ABSL_CONST_INIT static const T equivalent; + ABSL_CONST_INIT static const T greater; +}; +template <typename T> +const T weak_ordering_base<T>::less(ord::less); +template <typename T> +const T weak_ordering_base<T>::equivalent(eq::equivalent); +template <typename T> +const T weak_ordering_base<T>::greater(ord::greater); + +template <typename T> +struct strong_ordering_base { + ABSL_CONST_INIT static const T less; + ABSL_CONST_INIT static const T equal; + ABSL_CONST_INIT static const T equivalent; + ABSL_CONST_INIT static const T greater; +}; +template <typename T> +const T strong_ordering_base<T>::less(ord::less); +template <typename T> +const T strong_ordering_base<T>::equal(eq::equal); +template <typename T> +const T strong_ordering_base<T>::equivalent(eq::equivalent); +template <typename T> +const T strong_ordering_base<T>::greater(ord::greater); + +} // namespace compare_internal + +class weak_equality + : public compare_internal::weak_equality_base<weak_equality> { + explicit constexpr weak_equality(compare_internal::eq v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + friend struct compare_internal::weak_equality_base<weak_equality>; + + public: + // Comparisons + friend constexpr bool operator==( + weak_equality v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ == 0; + } + friend constexpr bool operator!=( + weak_equality v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ != 0; + } + friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>, + weak_equality v) noexcept { + return 0 == v.value_; + } + friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>, + weak_equality v) noexcept { + return 0 != v.value_; + } + + private: + compare_internal::value_type value_; +}; + +class strong_equality + : public compare_internal::strong_equality_base<strong_equality> { + explicit constexpr strong_equality(compare_internal::eq v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + friend struct compare_internal::strong_equality_base<strong_equality>; + + public: + // Conversion + constexpr operator weak_equality() const noexcept { // NOLINT + return value_ == 0 ? weak_equality::equivalent + : weak_equality::nonequivalent; + } + // Comparisons + friend constexpr bool operator==( + strong_equality v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ == 0; + } + friend constexpr bool operator!=( + strong_equality v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ != 0; + } + friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>, + strong_equality v) noexcept { + return 0 == v.value_; + } + friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>, + strong_equality v) noexcept { + return 0 != v.value_; + } + + private: + compare_internal::value_type value_; +}; + +class partial_ordering + : public compare_internal::partial_ordering_base<partial_ordering> { + explicit constexpr partial_ordering(compare_internal::eq v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + explicit constexpr partial_ordering(compare_internal::ord v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + explicit constexpr partial_ordering(compare_internal::ncmp v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + friend struct compare_internal::partial_ordering_base<partial_ordering>; + + constexpr bool is_ordered() const noexcept { + return value_ != + compare_internal::value_type(compare_internal::ncmp::unordered); + } + + public: + // Conversion + constexpr operator weak_equality() const noexcept { // NOLINT + return value_ == 0 ? weak_equality::equivalent + : weak_equality::nonequivalent; + } + // Comparisons + friend constexpr bool operator==( + partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.is_ordered() && v.value_ == 0; + } + friend constexpr bool operator!=( + partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return !v.is_ordered() || v.value_ != 0; + } + friend constexpr bool operator<( + partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.is_ordered() && v.value_ < 0; + } + friend constexpr bool operator<=( + partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.is_ordered() && v.value_ <= 0; + } + friend constexpr bool operator>( + partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.is_ordered() && v.value_ > 0; + } + friend constexpr bool operator>=( + partial_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.is_ordered() && v.value_ >= 0; + } + friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>, + partial_ordering v) noexcept { + return v.is_ordered() && 0 == v.value_; + } + friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>, + partial_ordering v) noexcept { + return !v.is_ordered() || 0 != v.value_; + } + friend constexpr bool operator<(compare_internal::OnlyLiteralZero<>, + partial_ordering v) noexcept { + return v.is_ordered() && 0 < v.value_; + } + friend constexpr bool operator<=(compare_internal::OnlyLiteralZero<>, + partial_ordering v) noexcept { + return v.is_ordered() && 0 <= v.value_; + } + friend constexpr bool operator>(compare_internal::OnlyLiteralZero<>, + partial_ordering v) noexcept { + return v.is_ordered() && 0 > v.value_; + } + friend constexpr bool operator>=(compare_internal::OnlyLiteralZero<>, + partial_ordering v) noexcept { + return v.is_ordered() && 0 >= v.value_; + } + + private: + compare_internal::value_type value_; +}; + +class weak_ordering + : public compare_internal::weak_ordering_base<weak_ordering> { + explicit constexpr weak_ordering(compare_internal::eq v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + explicit constexpr weak_ordering(compare_internal::ord v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + friend struct compare_internal::weak_ordering_base<weak_ordering>; + + public: + // Conversions + constexpr operator weak_equality() const noexcept { // NOLINT + return value_ == 0 ? weak_equality::equivalent + : weak_equality::nonequivalent; + } + constexpr operator partial_ordering() const noexcept { // NOLINT + return value_ == 0 ? partial_ordering::equivalent + : (value_ < 0 ? partial_ordering::less + : partial_ordering::greater); + } + // Comparisons + friend constexpr bool operator==( + weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ == 0; + } + friend constexpr bool operator!=( + weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ != 0; + } + friend constexpr bool operator<( + weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ < 0; + } + friend constexpr bool operator<=( + weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ <= 0; + } + friend constexpr bool operator>( + weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ > 0; + } + friend constexpr bool operator>=( + weak_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ >= 0; + } + friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>, + weak_ordering v) noexcept { + return 0 == v.value_; + } + friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>, + weak_ordering v) noexcept { + return 0 != v.value_; + } + friend constexpr bool operator<(compare_internal::OnlyLiteralZero<>, + weak_ordering v) noexcept { + return 0 < v.value_; + } + friend constexpr bool operator<=(compare_internal::OnlyLiteralZero<>, + weak_ordering v) noexcept { + return 0 <= v.value_; + } + friend constexpr bool operator>(compare_internal::OnlyLiteralZero<>, + weak_ordering v) noexcept { + return 0 > v.value_; + } + friend constexpr bool operator>=(compare_internal::OnlyLiteralZero<>, + weak_ordering v) noexcept { + return 0 >= v.value_; + } + + private: + compare_internal::value_type value_; +}; + +class strong_ordering + : public compare_internal::strong_ordering_base<strong_ordering> { + explicit constexpr strong_ordering(compare_internal::eq v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + explicit constexpr strong_ordering(compare_internal::ord v) noexcept + : value_(static_cast<compare_internal::value_type>(v)) {} + friend struct compare_internal::strong_ordering_base<strong_ordering>; + + public: + // Conversions + constexpr operator weak_equality() const noexcept { // NOLINT + return value_ == 0 ? weak_equality::equivalent + : weak_equality::nonequivalent; + } + constexpr operator strong_equality() const noexcept { // NOLINT + return value_ == 0 ? strong_equality::equal : strong_equality::nonequal; + } + constexpr operator partial_ordering() const noexcept { // NOLINT + return value_ == 0 ? partial_ordering::equivalent + : (value_ < 0 ? partial_ordering::less + : partial_ordering::greater); + } + constexpr operator weak_ordering() const noexcept { // NOLINT + return value_ == 0 + ? weak_ordering::equivalent + : (value_ < 0 ? weak_ordering::less : weak_ordering::greater); + } + // Comparisons + friend constexpr bool operator==( + strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ == 0; + } + friend constexpr bool operator!=( + strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ != 0; + } + friend constexpr bool operator<( + strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ < 0; + } + friend constexpr bool operator<=( + strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ <= 0; + } + friend constexpr bool operator>( + strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ > 0; + } + friend constexpr bool operator>=( + strong_ordering v, compare_internal::OnlyLiteralZero<>) noexcept { + return v.value_ >= 0; + } + friend constexpr bool operator==(compare_internal::OnlyLiteralZero<>, + strong_ordering v) noexcept { + return 0 == v.value_; + } + friend constexpr bool operator!=(compare_internal::OnlyLiteralZero<>, + strong_ordering v) noexcept { + return 0 != v.value_; + } + friend constexpr bool operator<(compare_internal::OnlyLiteralZero<>, + strong_ordering v) noexcept { + return 0 < v.value_; + } + friend constexpr bool operator<=(compare_internal::OnlyLiteralZero<>, + strong_ordering v) noexcept { + return 0 <= v.value_; + } + friend constexpr bool operator>(compare_internal::OnlyLiteralZero<>, + strong_ordering v) noexcept { + return 0 > v.value_; + } + friend constexpr bool operator>=(compare_internal::OnlyLiteralZero<>, + strong_ordering v) noexcept { + return 0 >= v.value_; + } + + private: + compare_internal::value_type value_; +}; + +namespace compare_internal { +// We also provide these comparator adapter functions for internal absl use. + +// Helper functions to do a boolean comparison of two keys given a boolean +// or three-way comparator. +// SFINAE prevents implicit conversions to bool (such as from int). +template <typename Bool, + absl::enable_if_t<std::is_same<bool, Bool>::value, int> = 0> +constexpr bool compare_result_as_less_than(const Bool r) { return r; } +constexpr bool compare_result_as_less_than(const absl::weak_ordering r) { + return r < 0; +} + +template <typename Compare, typename K, typename LK> +constexpr bool do_less_than_comparison(const Compare &compare, const K &x, + const LK &y) { + return compare_result_as_less_than(compare(x, y)); +} + +// Helper functions to do a three-way comparison of two keys given a boolean or +// three-way comparator. +// SFINAE prevents implicit conversions to int (such as from bool). +template <typename Int, + absl::enable_if_t<std::is_same<int, Int>::value, int> = 0> +constexpr absl::weak_ordering compare_result_as_ordering(const Int c) { + return c < 0 ? absl::weak_ordering::less + : c == 0 ? absl::weak_ordering::equivalent + : absl::weak_ordering::greater; +} +constexpr absl::weak_ordering compare_result_as_ordering( + const absl::weak_ordering c) { + return c; +} + +template < + typename Compare, typename K, typename LK, + absl::enable_if_t<!std::is_same<bool, absl::result_of_t<Compare( + const K &, const LK &)>>::value, + int> = 0> +constexpr absl::weak_ordering do_three_way_comparison(const Compare &compare, + const K &x, const LK &y) { + return compare_result_as_ordering(compare(x, y)); +} +template < + typename Compare, typename K, typename LK, + absl::enable_if_t<std::is_same<bool, absl::result_of_t<Compare( + const K &, const LK &)>>::value, + int> = 0> +constexpr absl::weak_ordering do_three_way_comparison(const Compare &compare, + const K &x, const LK &y) { + return compare(x, y) ? absl::weak_ordering::less + : compare(y, x) ? absl::weak_ordering::greater + : absl::weak_ordering::equivalent; +} + +} // namespace compare_internal +} // namespace absl + +#endif // ABSL_TYPES_COMPARE_H_
diff --git a/absl/types/compare_test.cc b/absl/types/compare_test.cc new file mode 100644 index 0000000..3a85542 --- /dev/null +++ b/absl/types/compare_test.cc
@@ -0,0 +1,311 @@ +// Copyright 2018 The Abseil Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/types/compare.h" + +#include "gtest/gtest.h" +#include "absl/base/casts.h" + +namespace absl { +namespace { + +// This is necessary to avoid a bunch of lint warnings suggesting that we use +// EXPECT_EQ/etc., which doesn't work in this case because they convert the `0` +// to an int, which can't be converted to the unspecified zero type. +bool Identity(bool b) { return b; } + +TEST(Compare, WeakEquality) { + EXPECT_TRUE(Identity(weak_equality::equivalent == 0)); + EXPECT_TRUE(Identity(0 == weak_equality::equivalent)); + EXPECT_TRUE(Identity(weak_equality::nonequivalent != 0)); + EXPECT_TRUE(Identity(0 != weak_equality::nonequivalent)); +} + +TEST(Compare, StrongEquality) { + EXPECT_TRUE(Identity(strong_equality::equal == 0)); + EXPECT_TRUE(Identity(0 == strong_equality::equal)); + EXPECT_TRUE(Identity(strong_equality::nonequal != 0)); + EXPECT_TRUE(Identity(0 != strong_equality::nonequal)); + EXPECT_TRUE(Identity(strong_equality::equivalent == 0)); + EXPECT_TRUE(Identity(0 == strong_equality::equivalent)); + EXPECT_TRUE(Identity(strong_equality::nonequivalent != 0)); + EXPECT_TRUE(Identity(0 != strong_equality::nonequivalent)); +} + +TEST(Compare, PartialOrdering) { + EXPECT_TRUE(Identity(partial_ordering::less < 0)); + EXPECT_TRUE(Identity(0 > partial_ordering::less)); + EXPECT_TRUE(Identity(partial_ordering::less <= 0)); + EXPECT_TRUE(Identity(0 >= partial_ordering::less)); + EXPECT_TRUE(Identity(partial_ordering::equivalent == 0)); + EXPECT_TRUE(Identity(0 == partial_ordering::equivalent)); + EXPECT_TRUE(Identity(partial_ordering::greater > 0)); + EXPECT_TRUE(Identity(0 < partial_ordering::greater)); + EXPECT_TRUE(Identity(partial_ordering::greater >= 0)); + EXPECT_TRUE(Identity(0 <= partial_ordering::greater)); + EXPECT_TRUE(Identity(partial_ordering::unordered != 0)); + EXPECT_TRUE(Identity(0 != partial_ordering::unordered)); + EXPECT_FALSE(Identity(partial_ordering::unordered < 0)); + EXPECT_FALSE(Identity(0 < partial_ordering::unordered)); + EXPECT_FALSE(Identity(partial_ordering::unordered <= 0)); + EXPECT_FALSE(Identity(0 <= partial_ordering::unordered)); + EXPECT_FALSE(Identity(partial_ordering::unordered > 0)); + EXPECT_FALSE(Identity(0 > partial_ordering::unordered)); + EXPECT_FALSE(Identity(partial_ordering::unordered >= 0)); + EXPECT_FALSE(Identity(0 >= partial_ordering::unordered)); +} + +TEST(Compare, WeakOrdering) { + EXPECT_TRUE(Identity(weak_ordering::less < 0)); + EXPECT_TRUE(Identity(0 > weak_ordering::less)); + EXPECT_TRUE(Identity(weak_ordering::less <= 0)); + EXPECT_TRUE(Identity(0 >= weak_ordering::less)); + EXPECT_TRUE(Identity(weak_ordering::equivalent == 0)); + EXPECT_TRUE(Identity(0 == weak_ordering::equivalent)); + EXPECT_TRUE(Identity(weak_ordering::greater > 0)); + EXPECT_TRUE(Identity(0 < weak_ordering::greater)); + EXPECT_TRUE(Identity(weak_ordering::greater >= 0)); + EXPECT_TRUE(Identity(0 <= weak_ordering::greater)); +} + +TEST(Compare, StrongOrdering) { + EXPECT_TRUE(Identity(strong_ordering::less < 0)); + EXPECT_TRUE(Identity(0 > strong_ordering::less)); + EXPECT_TRUE(Identity(strong_ordering::less <= 0)); + EXPECT_TRUE(Identity(0 >= strong_ordering::less)); + EXPECT_TRUE(Identity(strong_ordering::equal == 0)); + EXPECT_TRUE(Identity(0 == strong_ordering::equal)); + EXPECT_TRUE(Identity(strong_ordering::equivalent == 0)); + EXPECT_TRUE(Identity(0 == strong_ordering::equivalent)); + EXPECT_TRUE(Identity(strong_ordering::greater > 0)); + EXPECT_TRUE(Identity(0 < strong_ordering::greater)); + EXPECT_TRUE(Identity(strong_ordering::greater >= 0)); + EXPECT_TRUE(Identity(0 <= strong_ordering::greater)); +} + +TEST(Compare, Conversions) { + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(strong_equality::equal) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(strong_equality::nonequal) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(strong_equality::equivalent) == 0)); + EXPECT_TRUE(Identity( + implicit_cast<weak_equality>(strong_equality::nonequivalent) != 0)); + + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(partial_ordering::less) != 0)); + EXPECT_TRUE(Identity( + implicit_cast<weak_equality>(partial_ordering::equivalent) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(partial_ordering::greater) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(partial_ordering::unordered) != 0)); + + EXPECT_TRUE(implicit_cast<weak_equality>(weak_ordering::less) != 0); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(weak_ordering::equivalent) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(weak_ordering::greater) != 0)); + + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(weak_ordering::less) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(weak_ordering::less) < 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(weak_ordering::less) <= 0)); + EXPECT_TRUE(Identity( + implicit_cast<partial_ordering>(weak_ordering::equivalent) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(weak_ordering::greater) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(weak_ordering::greater) > 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(weak_ordering::greater) >= 0)); + + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(strong_ordering::less) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(strong_ordering::equal) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(strong_ordering::equivalent) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_equality>(strong_ordering::greater) != 0)); + + EXPECT_TRUE( + Identity(implicit_cast<strong_equality>(strong_ordering::less) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<strong_equality>(strong_ordering::equal) == 0)); + EXPECT_TRUE(Identity( + implicit_cast<strong_equality>(strong_ordering::equivalent) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<strong_equality>(strong_ordering::greater) != 0)); + + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(strong_ordering::less) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(strong_ordering::less) < 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(strong_ordering::less) <= 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(strong_ordering::equal) == 0)); + EXPECT_TRUE(Identity( + implicit_cast<partial_ordering>(strong_ordering::equivalent) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(strong_ordering::greater) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(strong_ordering::greater) > 0)); + EXPECT_TRUE( + Identity(implicit_cast<partial_ordering>(strong_ordering::greater) >= 0)); + + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::less) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::less) < 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::less) <= 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::equal) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::equivalent) == 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::greater) != 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::greater) > 0)); + EXPECT_TRUE( + Identity(implicit_cast<weak_ordering>(strong_ordering::greater) >= 0)); +} + +struct WeakOrderingLess { + template <typename T> + absl::weak_ordering operator()(const T &a, const T &b) const { + return a < b ? absl::weak_ordering::less + : a == b ? absl::weak_ordering::equivalent + : absl::weak_ordering::greater; + } +}; + +TEST(CompareResultAsLessThan, SanityTest) { + EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than(false)); + EXPECT_TRUE(absl::compare_internal::compare_result_as_less_than(true)); + + EXPECT_TRUE( + absl::compare_internal::compare_result_as_less_than(weak_ordering::less)); + EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than( + weak_ordering::equivalent)); + EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than( + weak_ordering::greater)); +} + +TEST(DoLessThanComparison, SanityTest) { + std::less<int> less; + WeakOrderingLess weak; + + EXPECT_TRUE(absl::compare_internal::do_less_than_comparison(less, -1, 0)); + EXPECT_TRUE(absl::compare_internal::do_less_than_comparison(weak, -1, 0)); + + EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(less, 10, 10)); + EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(weak, 10, 10)); + + EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(less, 10, 5)); + EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(weak, 10, 5)); +} + +TEST(CompareResultAsOrdering, SanityTest) { + EXPECT_TRUE(Identity( + absl::compare_internal::compare_result_as_ordering(-1) < 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::compare_result_as_ordering(-1) == 0)); + EXPECT_FALSE( + Identity(absl::compare_internal::compare_result_as_ordering(-1) > 0)); + EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::less) < 0)); + EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::less) == 0)); + EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::less) > 0)); + + EXPECT_FALSE(Identity( + absl::compare_internal::compare_result_as_ordering(0) < 0)); + EXPECT_TRUE(Identity( + absl::compare_internal::compare_result_as_ordering(0) == 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::compare_result_as_ordering(0) > 0)); + EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::equivalent) < 0)); + EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::equivalent) == 0)); + EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::equivalent) > 0)); + + EXPECT_FALSE(Identity( + absl::compare_internal::compare_result_as_ordering(1) < 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::compare_result_as_ordering(1) == 0)); + EXPECT_TRUE(Identity( + absl::compare_internal::compare_result_as_ordering(1) > 0)); + EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::greater) < 0)); + EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::greater) == 0)); + EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering( + weak_ordering::greater) > 0)); +} + +TEST(DoThreeWayComparison, SanityTest) { + std::less<int> less; + WeakOrderingLess weak; + + EXPECT_TRUE(Identity( + absl::compare_internal::do_three_way_comparison(less, -1, 0) < 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(less, -1, 0) == 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(less, -1, 0) > 0)); + EXPECT_TRUE(Identity( + absl::compare_internal::do_three_way_comparison(weak, -1, 0) < 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(weak, -1, 0) == 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(weak, -1, 0) > 0)); + + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(less, 10, 10) < 0)); + EXPECT_TRUE(Identity( + absl::compare_internal::do_three_way_comparison(less, 10, 10) == 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(less, 10, 10) > 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(weak, 10, 10) < 0)); + EXPECT_TRUE(Identity( + absl::compare_internal::do_three_way_comparison(weak, 10, 10) == 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(weak, 10, 10) > 0)); + + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(less, 10, 5) < 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(less, 10, 5) == 0)); + EXPECT_TRUE(Identity( + absl::compare_internal::do_three_way_comparison(less, 10, 5) > 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(weak, 10, 5) < 0)); + EXPECT_FALSE(Identity( + absl::compare_internal::do_three_way_comparison(weak, 10, 5) == 0)); + EXPECT_TRUE(Identity( + absl::compare_internal::do_three_way_comparison(weak, 10, 5) > 0)); +} + +} // namespace +} // namespace absl
diff --git a/ci/linux_clang-latest_libcxx_asan_bazel.sh b/ci/linux_clang-latest_libcxx_asan_bazel.sh new file mode 100755 index 0000000..113acdb --- /dev/null +++ b/ci/linux_clang-latest_libcxx_asan_bazel.sh
@@ -0,0 +1,81 @@ +#!/bin/bash +# +# Copyright 2019 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script that can be invoked to test abseil-cpp in a hermetic environment +# using a Docker image on Linux. You must have Docker installed to use this +# script. + +set -euox pipefail + +if [ -z ${ABSEIL_ROOT:-} ]; then + ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" +fi + +if [ -z ${STD:-} ]; then + STD="c++11 c++14 c++17" +fi + +if [ -z ${COMPILATION_MODE:-} ]; then + COMPILATION_MODE="fastbuild opt" +fi + +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" + +# USE_BAZEL_CACHE=1 only works on Kokoro. +# Without access to the credentials this won't work. +if [ ${USE_BAZEL_CACHE:-0} -ne 0 ]; then + DOCKER_EXTRA_ARGS="--volume=${KOKORO_KEYSTORE_DIR}:/keystore:ro ${DOCKER_EXTRA_ARGS:-}" + # Bazel doesn't track changes to tools outside of the workspace + # (e.g. /usr/bin/gcc), so by appending the docker container to the + # remote_http_cache url, we make changes to the container part of + # the cache key. Hashing the key is to make it shorter and url-safe. + container_key=$(echo ${DOCKER_CONTAINER} | sha256sum | head -c 16) + BAZEL_EXTRA_ARGS="--remote_http_cache=https://storage.googleapis.com/absl-bazel-remote-cache/${container_key} --google_credentials=/keystore/73103_absl-bazel-remote-cache ${BAZEL_EXTRA_ARGS:-}" +fi + +for std in ${STD}; do + for compilation_mode in ${COMPILATION_MODE}; do + echo "--------------------------------------------------------------------" + echo "Testing with --compilation_mode=${compilation_mode} and --std=${std}" + + time docker run \ + --volume="${ABSEIL_ROOT}:/abseil-cpp:ro" \ + --workdir=/abseil-cpp \ + --cap-add=SYS_PTRACE \ + --rm \ + -e CC="/opt/llvm/clang/bin/clang" \ + -e BAZEL_COMPILER="llvm" \ + -e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \ + -e BAZEL_LINKOPTS="-L/opt/llvm/libcxx/lib:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx/lib" \ + -e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx/include/c++/v1" \ + ${DOCKER_EXTRA_ARGS:-} \ + ${DOCKER_CONTAINER} \ + /usr/local/bin/bazel test ... \ + --compilation_mode=${compilation_mode} \ + --copt="-DDYNAMIC_ANNOTATIONS_ENABLED=1" \ + --copt="-DADDRESS_SANITIZER" \ + --copt="-fsanitize=address" \ + --copt=-Werror \ + --keep_going \ + --linkopt="-fsanitize=address" \ + --show_timestamps \ + --test_env="ASAN_SYMBOLIZER_PATH=/opt/llvm/clang/bin/llvm-symbolizer" \ + --test_env="TZDIR=/abseil-cpp/absl/time/internal/cctz/testdata/zoneinfo" \ + --test_output=errors \ + --test_tag_filters="-benchmark,-noasan" \ + ${BAZEL_EXTRA_ARGS:-} + done +done
diff --git a/ci/linux_clang-latest_libcxx_bazel.sh b/ci/linux_clang-latest_libcxx_bazel.sh index fe9d6aa..f32cbef 100755 --- a/ci/linux_clang-latest_libcxx_bazel.sh +++ b/ci/linux_clang-latest_libcxx_bazel.sh
@@ -32,7 +32,7 @@ COMPILATION_MODE="fastbuild opt" fi -readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190329" +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" # USE_BAZEL_CACHE=1 only works on Kokoro. # Without access to the credentials this won't work.
diff --git a/ci/linux_clang-latest_libcxx_tsan_bazel.sh b/ci/linux_clang-latest_libcxx_tsan_bazel.sh new file mode 100755 index 0000000..c4edd19 --- /dev/null +++ b/ci/linux_clang-latest_libcxx_tsan_bazel.sh
@@ -0,0 +1,83 @@ +#!/bin/bash +# +# Copyright 2019 The Abseil Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This script that can be invoked to test abseil-cpp in a hermetic environment +# using a Docker image on Linux. You must have Docker installed to use this +# script. + +set -euox pipefail + +if [ -z ${ABSEIL_ROOT:-} ]; then + ABSEIL_ROOT="$(realpath $(dirname ${0})/..)" +fi + +if [ -z ${STD:-} ]; then + STD="c++11 c++14 c++17" +fi + +if [ -z ${COMPILATION_MODE:-} ]; then + COMPILATION_MODE="fastbuild opt" +fi + +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" + +# USE_BAZEL_CACHE=1 only works on Kokoro. +# Without access to the credentials this won't work. +if [ ${USE_BAZEL_CACHE:-0} -ne 0 ]; then + DOCKER_EXTRA_ARGS="--volume=${KOKORO_KEYSTORE_DIR}:/keystore:ro ${DOCKER_EXTRA_ARGS:-}" + # Bazel doesn't track changes to tools outside of the workspace + # (e.g. /usr/bin/gcc), so by appending the docker container to the + # remote_http_cache url, we make changes to the container part of + # the cache key. Hashing the key is to make it shorter and url-safe. + container_key=$(echo ${DOCKER_CONTAINER} | sha256sum | head -c 16) + BAZEL_EXTRA_ARGS="--remote_http_cache=https://storage.googleapis.com/absl-bazel-remote-cache/${container_key} --google_credentials=/keystore/73103_absl-bazel-remote-cache ${BAZEL_EXTRA_ARGS:-}" +fi + +for std in ${STD}; do + for compilation_mode in ${COMPILATION_MODE}; do + echo "--------------------------------------------------------------------" + echo "Testing with --compilation_mode=${compilation_mode} and --std=${std}" + + time docker run \ + --volume="${ABSEIL_ROOT}:/abseil-cpp:ro" \ + --workdir=/abseil-cpp \ + --cap-add=SYS_PTRACE \ + --rm \ + -e CC="/opt/llvm/clang/bin/clang" \ + -e BAZEL_COMPILER="llvm" \ + -e BAZEL_CXXOPTS="-std=${std}:-nostdinc++" \ + -e BAZEL_LINKOPTS="-L/opt/llvm/libcxx-tsan/lib:-lc++:-lc++abi:-lm:-Wl,-rpath=/opt/llvm/libcxx-tsan/lib" \ + -e CPLUS_INCLUDE_PATH="/opt/llvm/libcxx-tsan/include/c++/v1" \ + ${DOCKER_EXTRA_ARGS:-} \ + ${DOCKER_CONTAINER} \ + /usr/local/bin/bazel test ... \ + --build_tag_filters="-notsan" \ + --compilation_mode=${compilation_mode} \ + --copt="-DDYNAMIC_ANNOTATIONS_ENABLED=1" \ + --copt="-DTHREAD_SANITIZER" \ + --copt="-fsanitize=thread" \ + --copt=-Werror \ + --keep_going \ + --linkopt="-fsanitize=thread" \ + --show_timestamps \ + --test_env="TSAN_OPTIONS=report_atomic_races=0" \ + --test_env="TSAN_SYMBOLIZER_PATH=/opt/llvm/clang/bin/llvm-symbolizer" \ + --test_env="TZDIR=/abseil-cpp/absl/time/internal/cctz/testdata/zoneinfo" \ + --test_output=errors \ + --test_tag_filters="-benchmark,-notsan" \ + ${BAZEL_EXTRA_ARGS:-} + done +done
diff --git a/ci/linux_clang-latest_libstdcxx_bazel.sh b/ci/linux_clang-latest_libstdcxx_bazel.sh index 611b7f2..7bbecd6 100755 --- a/ci/linux_clang-latest_libstdcxx_bazel.sh +++ b/ci/linux_clang-latest_libstdcxx_bazel.sh
@@ -32,7 +32,7 @@ COMPILATION_MODE="fastbuild opt" fi -readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190329" +readonly DOCKER_CONTAINER="gcr.io/google.com/absl-177019/linux_clang-latest:20190508" # USE_BAZEL_CACHE=1 only works on Kokoro. # Without access to the credentials this won't work.