From 2d2a8aea291c4877e75c7991a61e57d7e12b5373 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 18 Mar 2020 01:31:55 -0700 Subject: Export of internal Abseil changes -- a85860e450815776c8753f34348f41ab0e918d36 by Gennadiy Rozental : Rename ComamndLineFlag's interface SetFromString as ParseFrom. This is the name approved by C++ API review. PiperOrigin-RevId: 301543521 -- 28f31bae2a136854fd89f0a32f281d12a40f702c by Abseil Team : Internal change PiperOrigin-RevId: 301524919 -- a68da3d6fbbca4c5f41a20e99ed72bb1c5dd1165 by Abseil Team : Introduce absl::base_internal::StrError, a portability wrapper around the various thread-safe alternatives to C89's strerror. PiperOrigin-RevId: 301513962 -- 92ccac3b6eb18cb41cddedbfdab53b9ad481505d by Andy Getzendanner : Introduce absl::base_internal::StrError, a portability wrapper around the various thread-safe alternatives to C89's strerror. PiperOrigin-RevId: 301493389 -- 8e196def47c250941202840d6a1de686d681cd3e by Abseil Team : Internal change PiperOrigin-RevId: 301363394 -- dd1dcffa6c47675ba4d198730a301bd408142298 by Gennadiy Rozental : Add validation for size of void* to match the size of free function pointer. PiperOrigin-RevId: 301341331 GitOrigin-RevId: a85860e450815776c8753f34348f41ab0e918d36 Change-Id: I27c9d1365d3c9b4328d1587ab3ac38e2d09a6ec2 --- absl/base/internal/strerror_test.cc | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 absl/base/internal/strerror_test.cc (limited to 'absl/base/internal/strerror_test.cc') diff --git a/absl/base/internal/strerror_test.cc b/absl/base/internal/strerror_test.cc new file mode 100644 index 00000000..a53da97f --- /dev/null +++ b/absl/base/internal/strerror_test.cc @@ -0,0 +1,86 @@ +// Copyright 2020 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/base/internal/strerror.h" + +#include +#include +#include +#include +#include +#include // NOLINT(build/c++11) +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/strings/match.h" + +namespace { +using ::testing::AnyOf; +using ::testing::Eq; + +TEST(StrErrorTest, ValidErrorCode) { + errno = ERANGE; + EXPECT_THAT(absl::base_internal::StrError(EDOM), Eq(strerror(EDOM))); + EXPECT_THAT(errno, Eq(ERANGE)); +} + +TEST(StrErrorTest, InvalidErrorCode) { + errno = ERANGE; + EXPECT_THAT(absl::base_internal::StrError(-1), + AnyOf(Eq("No error information"), Eq("Unknown error -1"))); + EXPECT_THAT(errno, Eq(ERANGE)); +} + +TEST(StrErrorTest, MultipleThreads) { + // In this test, we will start up 2 threads and have each one call + // StrError 1000 times, each time with a different errnum. We + // expect that StrError(errnum) will return a string equal to the + // one returned by strerror(errnum), if the code is known. Since + // strerror is known to be thread-hostile, collect all the expected + // strings up front. + const int kNumCodes = 1000; + std::vector expected_strings(kNumCodes); + for (int i = 0; i < kNumCodes; ++i) { + expected_strings[i] = strerror(i); + } + + std::atomic_int counter(0); + auto thread_fun = [&]() { + for (int i = 0; i < kNumCodes; ++i) { + ++counter; + errno = ERANGE; + const std::string value = absl::base_internal::StrError(i); + // Only the GNU implementation is guaranteed to provide the + // string "Unknown error nnn". POSIX doesn't say anything. + if (!absl::StartsWith(value, "Unknown error ")) { + EXPECT_THAT(absl::base_internal::StrError(i), Eq(expected_strings[i])); + } + EXPECT_THAT(errno, Eq(ERANGE)); + } + }; + + const int kNumThreads = 100; + std::vector threads; + for (int i = 0; i < kNumThreads; ++i) { + threads.push_back(std::thread(thread_fun)); + } + for (auto& thread : threads) { + thread.join(); + } + + EXPECT_THAT(counter, Eq(kNumThreads * kNumCodes)); +} + +} // namespace -- cgit v1.2.3 From e2b1bab19a782cb62bb010d1c2925ab7314fb113 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 17 Dec 2020 05:31:38 -0800 Subject: Export of internal Abseil changes -- 0b13723ab1ca5231950c3ef76e57c415ce36d9d2 by Abseil Team : Fix documentation typo PiperOrigin-RevId: 348003868 -- 2ad4875258ffd604b19f57d7cfbb9f9a093ff880 by Derek Mauro : Add missing #include Note: This file is sometimes used from C so we can't use PiperOrigin-RevId: 347931562 -- 4d0c777a3645bddea9d0d6c49ec8ef3afea8c0b7 by Chris Kennelly : Use unsigned types for BitMask helper functions. Additionally, explicitly perform narrowing cast. T will always have fewer than 2^32 bits. PiperOrigin-RevId: 347913413 -- 80c44b0b066485a25baff56d475b67be2ad027e7 by Abseil Team : Stash errno for a larger scope. Also adjust the test to account for EXPECT_* possibly modifying errno as well. PiperOrigin-RevId: 347899763 GitOrigin-RevId: 0b13723ab1ca5231950c3ef76e57c415ce36d9d2 Change-Id: I9e7c0e5d45ac778644f3ad72d215378a8cf8a7d4 --- absl/base/internal/strerror.cc | 2 +- absl/base/internal/strerror_test.cc | 6 ++++-- absl/base/optimization.h | 4 +++- absl/container/internal/raw_hash_set.h | 12 +++++++----- 4 files changed, 15 insertions(+), 9 deletions(-) (limited to 'absl/base/internal/strerror_test.cc') diff --git a/absl/base/internal/strerror.cc b/absl/base/internal/strerror.cc index d66ba120..0d6226fd 100644 --- a/absl/base/internal/strerror.cc +++ b/absl/base/internal/strerror.cc @@ -51,7 +51,6 @@ const char* StrErrorAdaptor(int errnum, char* buf, size_t buflen) { } std::string StrErrorInternal(int errnum) { - absl::base_internal::ErrnoSaver errno_saver; char buf[100]; const char* str = StrErrorAdaptor(errnum, buf, sizeof buf); if (*str == '\0') { @@ -76,6 +75,7 @@ std::array* NewStrErrorTable() { } // namespace std::string StrError(int errnum) { + absl::base_internal::ErrnoSaver errno_saver; static const auto* table = NewStrErrorTable(); if (errnum >= 0 && errnum < static_cast(table->size())) { return (*table)[errnum]; diff --git a/absl/base/internal/strerror_test.cc b/absl/base/internal/strerror_test.cc index a53da97f..e32d5b5c 100644 --- a/absl/base/internal/strerror_test.cc +++ b/absl/base/internal/strerror_test.cc @@ -62,12 +62,14 @@ TEST(StrErrorTest, MultipleThreads) { ++counter; errno = ERANGE; const std::string value = absl::base_internal::StrError(i); + // EXPECT_* could change errno. Stash it first. + int check_err = errno; + EXPECT_THAT(check_err, Eq(ERANGE)); // Only the GNU implementation is guaranteed to provide the // string "Unknown error nnn". POSIX doesn't say anything. if (!absl::StartsWith(value, "Unknown error ")) { - EXPECT_THAT(absl::base_internal::StrError(i), Eq(expected_strings[i])); + EXPECT_THAT(value, Eq(expected_strings[i])); } - EXPECT_THAT(errno, Eq(ERANGE)); } }; diff --git a/absl/base/optimization.h b/absl/base/optimization.h index 393fc3a4..57b4dccf 100644 --- a/absl/base/optimization.h +++ b/absl/base/optimization.h @@ -22,6 +22,8 @@ #ifndef ABSL_BASE_OPTIMIZATION_H_ #define ABSL_BASE_OPTIMIZATION_H_ +#include + #include "absl/base/config.h" // ABSL_BLOCK_TAIL_CALL_OPTIMIZATION @@ -216,7 +218,7 @@ // This macro forces small unique name on a static file level symbols like // static local variables or static functions. This is intended to be used in // macro definitions to optimize the cost of generated code. Do NOT use it on -// symbols exported from translation unit since it may casue a link time +// symbols exported from translation unit since it may cause a link time // conflict. // // Example: diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h index 4477f7dc..74b2ef4c 100644 --- a/absl/container/internal/raw_hash_set.h +++ b/absl/container/internal/raw_hash_set.h @@ -189,7 +189,7 @@ constexpr bool IsNoThrowSwappable(std::false_type /* is_swappable */) { } template -int TrailingZeros(T x) { +uint32_t TrailingZeros(T x) { ABSL_INTERNAL_ASSUME(x != 0); return countr_zero(x); } @@ -221,19 +221,21 @@ class BitMask { } explicit operator bool() const { return mask_ != 0; } int operator*() const { return LowestBitSet(); } - int LowestBitSet() const { + uint32_t LowestBitSet() const { return container_internal::TrailingZeros(mask_) >> Shift; } - int HighestBitSet() const { return (bit_width(mask_) - 1) >> Shift; } + uint32_t HighestBitSet() const { + return static_cast((bit_width(mask_) - 1) >> Shift); + } BitMask begin() const { return *this; } BitMask end() const { return BitMask(0); } - int TrailingZeros() const { + uint32_t TrailingZeros() const { return container_internal::TrailingZeros(mask_) >> Shift; } - int LeadingZeros() const { + uint32_t LeadingZeros() const { constexpr int total_significant_bits = SignificantBits << Shift; constexpr int extra_bits = sizeof(T) * 8 - total_significant_bits; return countl_zero(mask_ << extra_bits) >> Shift; -- cgit v1.2.3