diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index 5468d70..5899793 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -28,9 +28,6 @@ register_toolchains( dev_dependency = True, ) -# Utility for testing C++ code -bazel_dep(name = "googletest", version = "1.15.2", dev_dependency = True) - # `clang_tools` for clang-tidy bazel_dep(name = "clang_tools", version = "", dev_dependency = True) local_path_override( diff --git a/examples/cc/BUILD.bazel b/examples/cc/BUILD.bazel index 49b4c00..e69de29 100644 --- a/examples/cc/BUILD.bazel +++ b/examples/cc/BUILD.bazel @@ -1,25 +0,0 @@ -load( - "@libc_replacer//:cc.bzl", - "cc_libc_replacer_binary", - "cc_libc_replacer_test", -) - -cc_libc_replacer_binary( - name = "time_test_binary", - testonly = True, - srcs = ["time_test.cpp"], - deps = ["@googletest//:gtest_main"], -) - -cc_libc_replacer_test( - name = "time_test", - timeout = "short", - srcs = ["time_test.cpp"], - deps = ["@googletest//:gtest_main"], -) - -sh_test( - name = "binary_test", - timeout = "short", - srcs = [":time_test_binary"], -) diff --git a/examples/cc/clock/BUILD.bazel b/examples/cc/clock/BUILD.bazel new file mode 100644 index 0000000..ba3eb86 --- /dev/null +++ b/examples/cc/clock/BUILD.bazel @@ -0,0 +1,23 @@ +load( + "@libc_replacer//:cc.bzl", + "cc_libc_replacer_binary", + "cc_libc_replacer_test", +) + +cc_libc_replacer_binary( + name = "test_binary", + testonly = True, + srcs = ["test.c"], +) + +cc_libc_replacer_test( + name = "test", + timeout = "short", + srcs = ["test.c"], +) + +sh_test( + name = "binary_test", + timeout = "short", + srcs = [":test_binary"], +) diff --git a/examples/cc/clock/test.c b/examples/cc/clock/test.c new file mode 100644 index 0000000..1e1fe73 --- /dev/null +++ b/examples/cc/clock/test.c @@ -0,0 +1,17 @@ +#include + +#include +#include + +static clock_t mock_clock(void) { return 123.0; } + +int main(void) { + libc_replacer_overwrite_clock(mock_clock); + const clock_t got = clock(); + libc_replacer_reset_clock(); + assert(got == 123.0); + const clock_t got_after_reset = clock(); + assert(got_after_reset != 123.0); + + return 0; +} diff --git a/examples/cc/time/BUILD.bazel b/examples/cc/time/BUILD.bazel new file mode 100644 index 0000000..ba3eb86 --- /dev/null +++ b/examples/cc/time/BUILD.bazel @@ -0,0 +1,23 @@ +load( + "@libc_replacer//:cc.bzl", + "cc_libc_replacer_binary", + "cc_libc_replacer_test", +) + +cc_libc_replacer_binary( + name = "test_binary", + testonly = True, + srcs = ["test.c"], +) + +cc_libc_replacer_test( + name = "test", + timeout = "short", + srcs = ["test.c"], +) + +sh_test( + name = "binary_test", + timeout = "short", + srcs = [":test_binary"], +) diff --git a/examples/cc/time/test.c b/examples/cc/time/test.c new file mode 100644 index 0000000..217b55a --- /dev/null +++ b/examples/cc/time/test.c @@ -0,0 +1,20 @@ +#include + +#include +#include + +static time_t mock_time(time_t *tloc) { + (void)tloc; + return 123; +} + +int main(void) { + libc_replacer_overwrite_time(mock_time); + const time_t got = time(NULL); + libc_replacer_reset_time(); + assert(got == 123); + const time_t got_after_reset = time(NULL); + assert(got_after_reset != 123); + + return 0; +} diff --git a/examples/cc/time_test.cpp b/examples/cc/time_test.cpp deleted file mode 100644 index a1cd837..0000000 --- a/examples/cc/time_test.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -#include - -namespace { - -auto MockTime(time_t * /* tloc */) noexcept -> time_t { - static constexpr time_t mock_time{123}; - return mock_time; -} - -} // namespace - -GTEST_TEST(TimeTest, ReplaceTime) { - libc_replacer_overwrite_time(MockTime); - const auto got = time(nullptr); - libc_replacer_reset_time(); - GTEST_ASSERT_EQ(got, 123); - const auto got_after_reset = time(nullptr); - GTEST_ASSERT_NE(got_after_reset, 123); -} diff --git a/libc_replacer/cc/BUILD.bazel b/libc_replacer/cc/BUILD.bazel index 1505273..b7e711a 100644 --- a/libc_replacer/cc/BUILD.bazel +++ b/libc_replacer/cc/BUILD.bazel @@ -9,6 +9,7 @@ cc_library( include_prefix = "libc_replacer/cc", visibility = ["//visibility:public"], deps = [ + "//libc_replacer/cc/clock", "//libc_replacer/cc/time", ], ) diff --git a/libc_replacer/cc/clock/BUILD.bazel b/libc_replacer/cc/clock/BUILD.bazel new file mode 100644 index 0000000..849b2c1 --- /dev/null +++ b/libc_replacer/cc/clock/BUILD.bazel @@ -0,0 +1,11 @@ +"""C/C++ implementations to replace `clock`. +""" + +cc_library( + name = "clock", + srcs = ["clock.c"], + hdrs = ["clock.h"], + linkopts = ["-Wl,-wrap=clock"], + visibility = ["//libc_replacer/cc:__pkg__"], + deps = ["//libc_replacer/cc/internal"], +) diff --git a/libc_replacer/cc/clock/clock.c b/libc_replacer/cc/clock/clock.c new file mode 100644 index 0000000..0790120 --- /dev/null +++ b/libc_replacer/cc/clock/clock.c @@ -0,0 +1,6 @@ +#include "libc_replacer/cc/clock/clock.h" // IWYU pragma: associated +#include "libc_replacer/cc/internal/definition_helper.h" + +#include + +LIBC_REPLACER_INTERNAL_DEFINE(clock, clock_t, (void, )) diff --git a/libc_replacer/cc/clock/clock.h b/libc_replacer/cc/clock/clock.h new file mode 100644 index 0000000..b2b190a --- /dev/null +++ b/libc_replacer/cc/clock/clock.h @@ -0,0 +1,10 @@ +#ifndef INCLUDE_GUARD_LIBC_REPLACER_CC_TIME_CLOCK_H_ +#define INCLUDE_GUARD_LIBC_REPLACER_CC_TIME_CLOCK_H_ + +#include "libc_replacer/cc/internal/declaration_helper.h" + +#include + +LIBC_REPLACER_INTERNAL_DECLARE(clock, clock_t, (void, )) + +#endif // INCLUDE_GUARD_LIBC_REPLACER_CC_TIME_CLOCK_H_ diff --git a/libc_replacer/cc/interface.h b/libc_replacer/cc/interface.h index 6c113b7..38ebd29 100644 --- a/libc_replacer/cc/interface.h +++ b/libc_replacer/cc/interface.h @@ -2,6 +2,7 @@ #define INCLUDE_GUARD_LIBC_REPLACER_CC_INTERFACE_H_ // IWYU pragma: begin_exports +#include "libc_replacer/cc/clock/clock.h" #include "libc_replacer/cc/time/time.h" // IWYU pragma: end_exports