Skip to content

Commit

Permalink
Rename (and make minor modifications) test/gtest/for_loop_test.cpp to…
Browse files Browse the repository at this point in the history
… test/gtest/smith_for_loop_test.cpp, and add unit tests for for-in-loops in test/gtest/smith_for_in_loop_test.cpp
  • Loading branch information
zzmic committed Jul 4, 2024
1 parent 15f735a commit 7e2f53d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
69 changes: 69 additions & 0 deletions test/gtest/smith_for_in_loop_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "gtest/gtest.h"
#include "backends/p4tools/modules/smith/smith.h"
#include "backends/p4tools/modules/smith/common/declarations.h"
#include "backends/p4tools/modules/smith/common/expressions.h"
#include "backends/p4tools/modules/smith/common/generator.h"
#include "backends/p4tools/modules/smith/common/parser.h"
#include "backends/p4tools/modules/smith/common/probabilities.h"
#include "backends/p4tools/modules/smith/common/scope.h"
#include "backends/p4tools/modules/smith/common/statements.h"
#include "backends/p4tools/modules/smith/common/table.h"
#include "backends/p4tools/modules/smith/core/target.h"
#include "ir/ir.h"
#include "ir/ir-generated.h"

namespace Test {

class P4SmithForInLoopTest : public ::testing::Test {
protected:
P4Tools::P4Smith::StatementGenerator* generator;

// TODO(zzmic): Figure out how to properly initialize and clean up the test object.
P4SmithForInLoopTest() {}
~P4SmithForInLoopTest() override {}
};

/// @brief Test the generation of a for-in-loop statement.
TEST_F(P4SmithForInLoopTest, ForLoopGeneration) {
auto forInLoopStmt = generator->genForInLoopStatement(false);
ASSERT_NE(forInLoopStmt, nullptr);
EXPECT_TRUE(forInLoopStmt->is<IR::ForInStatement>());
}

/// @brief Test the for-in-loop's declaration variable.
TEST_F(P4SmithForInLoopTest, CheckForLoopDeclarationVariable) {
auto forInLoopStmt = generator->genForInLoopStatement(false);
ASSERT_NE(forInLoopStmt, nullptr);
EXPECT_TRUE(forInLoopStmt->is<IR::ForInStatement>());

// TODO(zzmic): Figure out whether this "static_cast" is necessary.
auto forInStmt = forInLoopStmt->to<IR::ForInStatement>();
ASSERT_NE(forInStmt->decl, nullptr);
EXPECT_TRUE(forInStmt->decl->is<IR::Declaration_Variable>());
}

/// @brief Test the for-in-loop's collection.
TEST_F(P4SmithForInLoopTest, CheckForLoopCollection) {
auto forInLoopStmt = generator->genForInLoopStatement(false);
ASSERT_NE(forInLoopStmt, nullptr);
EXPECT_TRUE(forInLoopStmt->is<IR::ForInStatement>());

// TODO(zzmic): Figure out whether this "static_cast" is necessary.
auto forInStmt = forInLoopStmt->to<IR::ForInStatement>();
ASSERT_NE(forInStmt, nullptr);
EXPECT_TRUE(forInStmt->collection->is<IR::PathExpression>());
}

/// @brief Test the for-in-loop's body.
TEST_F(P4SmithForInLoopTest, CheckForLoopBody) {
auto forInLoopStmt = generator->genForInLoopStatement(false);
ASSERT_NE(forInLoopStmt, nullptr);
EXPECT_TRUE(forInLoopStmt->is<IR::ForInStatement>());

// TODO(zzmic): Figure out whether this "static_cast" is necessary.
auto forInStmt = forInLoopStmt->to<IR::ForInStatement>();
ASSERT_NE(forInStmt->body, nullptr);
EXPECT_FALSE(forInStmt->body->is<IR::Statement>());
}

} // namespace Test
22 changes: 11 additions & 11 deletions test/gtest/for_loop_test.cpp → test/gtest/smith_for_loop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ class P4SmithForLoopTest : public ::testing::Test {
protected:
P4Tools::P4Smith::StatementGenerator* generator;

// TODO(zzmic): Figure out how to initialize and clean up the test object.
// TODO(zzmic): Figure out how to properly initialize and clean up the test object.
P4SmithForLoopTest() {}
~P4SmithForLoopTest() override {}
};

/// @brief Test the generation of a for loop statement.
/// @brief Test the generation of a for-loop statement.
TEST_F(P4SmithForLoopTest, ForLoopGeneration) {
auto forLoopStmt = generator->genForLoopStatement(false);
ASSERT_NE(forLoopStmt, nullptr);
EXPECT_TRUE(forLoopStmt->is<IR::ForStatement>());
}

/// @brief Test whether the for loop contains an initialization.
TEST_F(P4SmithForLoopTest, ForLoopContainsInitialization) {
/// @brief Test the for-loop's initialization.
TEST_F(P4SmithForLoopTest, CheckForLoopContainsInitialization) {
auto forLoopStmt = generator->genForLoopStatement(false);
ASSERT_NE(forLoopStmt, nullptr);
EXPECT_TRUE(forLoopStmt->is<IR::ForStatement>());

// TODO(zzmic): Figure out whether this "static_cast" is necessary.
auto forStmt = forLoopStmt->to<IR::ForStatement>();
ASSERT_NE(forStmt->init, nullptr);
ASSERT_NE(forStmt->init, nullptr);
EXPECT_FALSE(forStmt->init.empty());
}

/// @brief Test whether the for loop contains a condition.
TEST_F(P4SmithForLoopTest, ForLoopContainsCondition) {
/// @brief Test the for-loop's condition.
TEST_F(P4SmithForLoopTest, CheckForLoopContainsCondition) {
auto forLoopStmt = generator->genForLoopStatement(false);
ASSERT_NE(forLoopStmt, nullptr);
EXPECT_TRUE(forLoopStmt->is<IR::ForStatement>());
Expand All @@ -54,8 +54,8 @@ TEST_F(P4SmithForLoopTest, ForLoopContainsCondition) {
EXPECT_TRUE(forStmt->condition->is<IR::Expression>());
}

/// @brief Test whether the for loop contains an update.
TEST_F(P4SmithForLoopTest, ForLoopContainsUpdate) {
/// @brief Test the for-loop's update.
TEST_F(P4SmithForLoopTest, CheckForLoopContainsUpdate) {
auto forLoopStmt = generator->genForLoopStatement(false);
ASSERT_NE(forLoopStmt, nullptr);
EXPECT_TRUE(forLoopStmt->is<IR::ForStatement>());
Expand All @@ -66,8 +66,8 @@ TEST_F(P4SmithForLoopTest, ForLoopContainsUpdate) {
EXPECT_FALSE(forStmt->updates.empty());
}

/// @brief Test whether the for loop contains a body.
TEST_F(P4SmithForLoopTest, ForLoopContainsBody) {
/// @brief Test the for-loop' body.
TEST_F(P4SmithForLoopTest, CheckForLoopContainsBody) {
auto forLoopStmt = generator->genForLoopStatement(false);
ASSERT_NE(forLoopStmt, nullptr);
EXPECT_TRUE(forLoopStmt->is<IR::ForStatement>());
Expand Down

0 comments on commit 7e2f53d

Please sign in to comment.