[flang-commits] [PATCH] D109227: [flang] GET_COMMAND_ARGUMENT(LENGTH) runtime implementation
Diana Picus via Phabricator via flang-commits
flang-commits at lists.llvm.org
Fri Sep 3 04:48:03 PDT 2021
rovka created this revision.
rovka added reviewers: klausler, jeanPerier.
rovka added a project: Flang.
Herald added a subscriber: jdoerfert.
rovka requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Implement the ArgumentLength entry point of GET_COMMAND_ARGUMENT. Also
introduce a fixture for the tests.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D109227
Files:
flang/runtime/command.cpp
flang/unittests/Runtime/CommandTest.cpp
Index: flang/unittests/Runtime/CommandTest.cpp
===================================================================
--- flang/unittests/Runtime/CommandTest.cpp
+++ flang/unittests/Runtime/CommandTest.cpp
@@ -13,20 +13,61 @@
using namespace Fortran::runtime;
-TEST(ArgumentCount, ZeroArguments) {
- const char *argv[]{"aProgram"};
- RTNAME(ProgramStart)(1, argv, {});
- EXPECT_EQ(0, RTNAME(ArgumentCount)());
+class CommandFixture : public ::testing::Test {
+protected:
+ CommandFixture(int argc, const char *argv[]) {
+ RTNAME(ProgramStart)(argc, argv, {});
+ }
+};
+
+static const char *commandOnlyArgv[]{"aProgram"};
+class ZeroArguments : public CommandFixture {
+protected:
+ ZeroArguments() : CommandFixture(1, commandOnlyArgv) {}
+};
+
+TEST_F(ZeroArguments, ArgumentCount) { EXPECT_EQ(0, RTNAME(ArgumentCount)()); }
+
+TEST_F(ZeroArguments, ArgumentLength) {
+ EXPECT_EQ(0, RTNAME(ArgumentLength)(-1));
+ EXPECT_EQ(8, RTNAME(ArgumentLength)(0));
+ EXPECT_EQ(0, RTNAME(ArgumentLength)(1));
}
-TEST(ArgumentCount, OneArgument) {
- const char *argv[]{"aProgram", "anArgument"};
- RTNAME(ProgramStart)(2, argv, {});
- EXPECT_EQ(1, RTNAME(ArgumentCount)());
+static const char *oneArgArgv[]{"aProgram", "anArgumentOfLength20"};
+class OneArgument : public CommandFixture {
+protected:
+ OneArgument() : CommandFixture(2, oneArgArgv) {}
+};
+
+TEST_F(OneArgument, ArgumentCount) { EXPECT_EQ(1, RTNAME(ArgumentCount)()); }
+
+TEST_F(OneArgument, ArgumentLength) {
+ EXPECT_EQ(0, RTNAME(ArgumentLength)(-1));
+ EXPECT_EQ(8, RTNAME(ArgumentLength)(0));
+ EXPECT_EQ(20, RTNAME(ArgumentLength)(1));
+ EXPECT_EQ(0, RTNAME(ArgumentLength)(2));
}
-TEST(ArgumentCount, SeveralArguments) {
- const char *argv[]{"aProgram", "arg1", "arg2", "arg3", "arg4"};
- RTNAME(ProgramStart)(5, argv, {});
+static const char *severalArgsArgv[]{
+ "aProgram", "16-char-long-arg", "", "-22-character-long-arg", "o"};
+class SeveralArguments : public CommandFixture {
+protected:
+ SeveralArguments()
+ : CommandFixture(sizeof(severalArgsArgv) / sizeof(*severalArgsArgv),
+ severalArgsArgv) {}
+};
+
+TEST_F(SeveralArguments, ArgumentCount) {
EXPECT_EQ(4, RTNAME(ArgumentCount)());
}
+
+TEST_F(SeveralArguments, ArgumentLength) {
+ EXPECT_EQ(0, RTNAME(ArgumentLength)(-1));
+ EXPECT_EQ(8, RTNAME(ArgumentLength)(0));
+ EXPECT_EQ(16, RTNAME(ArgumentLength)(1));
+ EXPECT_EQ(0, RTNAME(ArgumentLength)(2));
+ EXPECT_EQ(22, RTNAME(ArgumentLength)(3));
+ EXPECT_EQ(1, RTNAME(ArgumentLength)(4));
+ EXPECT_EQ(0, RTNAME(ArgumentLength)(5));
+}
Index: flang/runtime/command.cpp
===================================================================
--- flang/runtime/command.cpp
+++ flang/runtime/command.cpp
@@ -18,4 +18,12 @@
}
return 0;
}
+
+CppTypeFor<TypeCategory::Integer, 4> RTNAME(ArgumentLength)(
+ CppTypeFor<TypeCategory::Integer, 4> n) {
+ if (n < 0 || n >= executionEnvironment.argc) {
+ return 0;
+ }
+ return std::strlen(executionEnvironment.argv[n]);
+}
} // namespace Fortran::runtime
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109227.370548.patch
Type: text/x-patch
Size: 3046 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210903/77e41f24/attachment.bin>
More information about the flang-commits
mailing list