Compare commits

...

4 Commits

Author SHA1 Message Date
Thorsten Klein
6d98087682
Merge 55da83185f39dd0b198e20a135eeee3b2865125e into a0f06a70e3da7afa88da9527c43951bca1f7cef2 2026-07-29 21:22:23 +00:00
Abseil Team
a0f06a70e3 Make GoogleTest handle std::monostate and valueless_by_exception() for std::variant similarly to how it handles std::nullopt
Fixes: #2066
PiperOrigin-RevId: 955981465
Change-Id: I6b34dc2e634d4b727a50fb3e21e5d8e25c39a13b
2026-07-29 11:08:13 -07:00
Thorsten Klein
55da83185f graceful exit main on zephyr 2025-12-12 23:10:14 +01:00
Alexandre Bailon
fcdc66adcb Add support of Zephyr OS
Although Zephyr has it own test suite, it doesn't work well with C++.
gtest and gmock seem more adapted.
This adds support of Zephyr OS in order to test C++ libraries and
applications.

Signed-off-by: Alexandre Bailon <abailon@baylibre.com>
2025-12-12 22:37:48 +01:00
6 changed files with 84 additions and 21 deletions

View File

@ -945,14 +945,14 @@ template <typename T>
class [[nodiscard]] UniversalPrinter<std::optional<T>> {
public:
static void Print(const std::optional<T>& value, ::std::ostream* os) {
*os << '(';
if (!value) {
*os << "nullopt";
UniversalPrint(std::nullopt, os);
} else {
*os << '(';
UniversalPrint(*value, os);
}
*os << ')';
}
}
};
template <>
@ -961,27 +961,38 @@ class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
};
struct UniversalPrinterVisitor {
template <typename T>
void operator()(const T& arg) const {
*os << "'" << GetTypeName<T>() << "(index = " << index << ")' with value ";
UniversalPrint(arg, os);
}
::std::ostream* os;
std::size_t index;
};
// Printer for std::variant
template <typename... T>
class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
public:
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
if (value.valueless_by_exception()) {
*os << "(valueless)";
} else {
*os << '(';
std::visit(Visitor{os, value.index()}, value);
std::visit(UniversalPrinterVisitor{os, value.index()}, value);
*os << ')';
}
private:
struct Visitor {
template <typename U>
void operator()(const U& u) const {
*os << "'" << GetTypeName<U>() << "(index = " << index
<< ")' with value ";
UniversalPrint(u, os);
}
::std::ostream* os;
std::size_t index;
};
// Printer for std::monostate
template <>
class [[nodiscard]] UniversalPrinter<std::monostate> {
public:
static void Print(std::monostate, ::std::ostream* os) {
*os << "(monostate)";
}
};
// UniversalPrintArray(begin, len, os) prints an array of 'len'

View File

@ -84,6 +84,9 @@
#define GTEST_OS_GNU_HURD 1
#elif defined(__GLIBC__) && defined(__FreeBSD_kernel__)
#define GTEST_OS_GNU_KFREEBSD 1
#elif defined(__ZEPHYR__)
// Define it before linux as it could be built as a linux application
#define GTEST_OS_ZEPHYR 1
#elif defined __linux__
#define GTEST_OS_LINUX 1
#if defined __ANDROID__

View File

@ -141,6 +141,7 @@
// GTEST_OS_WINDOWS_PHONE - Windows Phone
// GTEST_OS_WINDOWS_RT - Windows Store App/WinRT
// GTEST_OS_ZOS - z/OS
// GTEST_OS_ZEPHYR - Zephyr OS
//
// Among the platforms, Cygwin, Linux, Mac OS X, and Windows have the
// most stable support. Since core members of the Google Test project
@ -516,6 +517,10 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#endif
#endif // GTEST_HAS_STD_WSTRING
#ifdef GTEST_OS_ZEPHYR
#define GTEST_HAS_FILE_SYSTEM 0
#endif
#ifndef GTEST_HAS_FILE_SYSTEM
// Most platforms support a file system.
#define GTEST_HAS_FILE_SYSTEM 1
@ -2063,6 +2068,18 @@ inline int RmDir(const char* dir) { return rmdir(dir); }
inline bool IsDir(const StatStruct& st) { return S_ISDIR(st.st_mode); }
#endif
#elif defined(GTEST_OS_ZEPHYR)
static inline int FileNo(FILE* file) {
if (file == stdin)
return 1;
else if (file == stdout)
return 2;
else if (file == stderr)
return 3;
return -EINVAL;
}
static inline int isatty(int fd) { return true; }
#else
typedef struct stat StatStruct;

View File

@ -43,7 +43,9 @@
#include <algorithm>
#include <chrono> // NOLINT
#include <cmath>
#ifndef GTEST_OS_ZEPHYR
#include <csignal> // NOLINT: raise(3) is used on some platforms
#endif
#include <cstdint>
#include <cstdlib>
#include <cstring>

View File

@ -47,13 +47,23 @@ void loop() { RUN_ALL_TESTS(); }
}
#endif
#elif defined(GTEST_OS_QURT)
// QuRT: program entry point is main, but argc/argv are unusable.
#elif defined(GTEST_OS_QURT) || defined(GTEST_OS_ZEPHYR)
// Program entry point is main, but argc/argv are unusable.
#if defined(GTEST_OS_ZEPHYR)
#undef GTEST_API_
#define GTEST_API_
#endif
GTEST_API_ int main() {
printf("Running main() from %s\n", __FILE__);
testing::InitGoogleTest();
return RUN_ALL_TESTS();
int ret = RUN_ALL_TESTS();
#if defined(GTEST_OS_ZEPHYR)
_exit( ret );
#endif
return ret;
}
#else
// Normal platforms: program entry point is main, argc/argv are initialized.

View File

@ -2030,6 +2030,26 @@ TEST(PrintOneofTest, Basic) {
PrintToString(Type(NonPrintable{})));
}
TEST(PrintVariantTest, Monostate) {
EXPECT_EQ("(monostate)", PrintToString(std::monostate()));
#if GTEST_HAS_EXCEPTIONS
struct ThrowOnMove {
ThrowOnMove() = default;
ThrowOnMove(ThrowOnMove&& other) { *this = std::move(other); }
ThrowOnMove& operator=(ThrowOnMove&&) {
(void)std::vector<bool>().at(0);
return *this;
}
};
std::variant<std::monostate, ThrowOnMove> v = std::monostate();
EXPECT_EQ("('std::monostate(index = 0)' with value (monostate))",
PrintToString(v));
EXPECT_THROW(v = ThrowOnMove(), std::out_of_range);
EXPECT_EQ("(valueless)", PrintToString(v));
#endif
}
#if GTEST_INTERNAL_HAS_COMPARE_LIB
TEST(PrintOrderingTest, Basic) {
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));