Skip to content

Commit

Permalink
Implement clock
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk committed Oct 20, 2024
1 parent 3d8a802 commit 232f102
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 50 deletions.
3 changes: 0 additions & 3 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 0 additions & 25 deletions examples/cc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
23 changes: 23 additions & 0 deletions examples/cc/clock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
17 changes: 17 additions & 0 deletions examples/cc/clock/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <libc_replacer/cc/interface.h>

#include <assert.h>
#include <time.h>

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;
}
23 changes: 23 additions & 0 deletions examples/cc/time/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
20 changes: 20 additions & 0 deletions examples/cc/time/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <libc_replacer/cc/interface.h>

#include <assert.h>
#include <time.h>

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;
}
22 changes: 0 additions & 22 deletions examples/cc/time_test.cpp

This file was deleted.

1 change: 1 addition & 0 deletions libc_replacer/cc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cc_library(
include_prefix = "libc_replacer/cc",
visibility = ["//visibility:public"],
deps = [
"//libc_replacer/cc/clock",
"//libc_replacer/cc/time",
],
)
11 changes: 11 additions & 0 deletions libc_replacer/cc/clock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
6 changes: 6 additions & 0 deletions libc_replacer/cc/clock/clock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "libc_replacer/cc/clock/clock.h" // IWYU pragma: associated
#include "libc_replacer/cc/internal/definition_helper.h"

#include <time.h>

LIBC_REPLACER_INTERNAL_DEFINE(clock, clock_t, (void, ))
10 changes: 10 additions & 0 deletions libc_replacer/cc/clock/clock.h
Original file line number Diff line number Diff line change
@@ -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 <time.h>

LIBC_REPLACER_INTERNAL_DECLARE(clock, clock_t, (void, ))

#endif // INCLUDE_GUARD_LIBC_REPLACER_CC_TIME_CLOCK_H_
1 change: 1 addition & 0 deletions libc_replacer/cc/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 232f102

Please sign in to comment.