summaryrefslogtreecommitdiff
path: root/Tests/Kernel/stress-truncate.cpp
diff options
context:
space:
mode:
authorBrian Gianforcaro <bgianf@serenityos.org>2021-05-06 01:14:50 -0700
committerAndreas Kling <kling@serenityos.org>2021-05-06 17:54:28 +0200
commitfd0dbd1ebfbcbc29d46393061daa49dc7390caa7 (patch)
tree278ea94a46900e47ff7dae46b1017cd31095971a /Tests/Kernel/stress-truncate.cpp
parent6e641fadfa61d4b890db52fb60cc3709352336b6 (diff)
downloadserenity-fd0dbd1ebfbcbc29d46393061daa49dc7390caa7.zip
Tests: Establish root Tests directory, move Userland/Tests there
With the goal of centralizing all tests in the system, this is a first step to establish a Tests sub-tree. It will contain all of the unit tests and test harnesses for the various components in the system.
Diffstat (limited to 'Tests/Kernel/stress-truncate.cpp')
-rw-r--r--Tests/Kernel/stress-truncate.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/Tests/Kernel/stress-truncate.cpp b/Tests/Kernel/stress-truncate.cpp
new file mode 100644
index 0000000000..8c683477bd
--- /dev/null
+++ b/Tests/Kernel/stress-truncate.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2021, the SerenityOS developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Random.h>
+#include <LibCore/ArgsParser.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int main(int argc, char** argv)
+{
+ const char* target = nullptr;
+ int max_file_size = 1024 * 1024;
+ int count = 1024;
+
+ Core::ArgsParser args_parser;
+ args_parser.add_option(max_file_size, "Maximum file size to generate", "max-size", 's', "size");
+ args_parser.add_option(count, "Number of truncations to run", "number", 'n', "number");
+ args_parser.add_positional_argument(target, "Target file path", "target");
+ args_parser.parse(argc, argv);
+
+ int fd = creat(target, 0666);
+ if (fd < 0) {
+ perror("Couldn't create target file");
+ return EXIT_FAILURE;
+ }
+ close(fd);
+
+ for (int i = 0; i < count; i++) {
+ auto new_file_size = AK::get_random<uint64_t>() % (max_file_size + 1);
+ printf("(%d/%d)\tTruncating to %" PRIu64 " bytes...\n", i + 1, count, new_file_size);
+ if (truncate(target, new_file_size) < 0) {
+ perror("Couldn't truncate target file");
+ return EXIT_FAILURE;
+ }
+ }
+
+ if (unlink(target) < 0) {
+ perror("Couldn't remove target file");
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}