<commit_before>#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE Main
#else
#ifndef _WIN32
#   define BOOST_TEST_MODULE ParserVarTests
#endif
#endif

#include "toslang_fixture.h"

#include "Common/opcodes.h"

BOOST_FIXTURE_TEST_SUITE( FrontEndTestSuite, TosLangFixture )

//////////////////// CORRECT USE CASES ////////////////////

BOOST_AUTO_TEST_CASE( ParseVarDeclTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_decl.tos");
    BOOST_REQUIRE_EQUAL(cNodes.size(), 3);

    BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
    const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar");

    BOOST_REQUIRE(cNodes[1]->GetKind() == ASTNode::NodeKind::VAR_DECL);
    vDecl = dynamic_cast<const VarDecl*>(cNodes[1].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyBoolVar");

	BOOST_REQUIRE(cNodes[2]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	vDecl = dynamic_cast<const VarDecl*>(cNodes[2].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyStringVar");
}

BOOST_AUTO_TEST_CASE( ParseVarInitBoolTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_init_bool.tos");
    BOOST_REQUIRE_EQUAL(cNodes.size(), 2);
	
    BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyTrueVar");
    BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BOOLEAN_EXPR);
    const BooleanExpr* bExpr = dynamic_cast<const BooleanExpr*>(vDecl->GetInitExpr());
    BOOST_REQUIRE(bExpr != nullptr);
    BOOST_REQUIRE_EQUAL(bExpr->GetValue(), true);

    BOOST_REQUIRE(cNodes[1]->GetKind() == ASTNode::NodeKind::VAR_DECL);
    vDecl = dynamic_cast<const VarDecl*>(cNodes[1].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyFalseVar");
    BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BOOLEAN_EXPR);
    bExpr = dynamic_cast<const BooleanExpr*>(vDecl->GetInitExpr());
    BOOST_REQUIRE(bExpr != nullptr);
    BOOST_REQUIRE_EQUAL(bExpr->GetValue(), false);
}

BOOST_AUTO_TEST_CASE( ParseVarInitIntTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_init_int.tos");
	BOOST_REQUIRE_EQUAL(cNodes.size(), 1);

	BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar");
	BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::NUMBER_EXPR);
	const NumberExpr* nExpr = dynamic_cast<const NumberExpr*>(vDecl->GetInitExpr());
	BOOST_REQUIRE(nExpr != nullptr);
	BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 42);
}

BOOST_AUTO_TEST_CASE( ParseVarInitIdentifierTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_init_identifier.tos");
	BOOST_REQUIRE_EQUAL(cNodes.size(), 2);

	BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar");
	BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::NUMBER_EXPR);
	const NumberExpr* nExpr = dynamic_cast<const NumberExpr*>(vDecl->GetInitExpr());
	BOOST_REQUIRE(nExpr != nullptr);
	BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 42);

	BOOST_REQUIRE(cNodes[1]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	vDecl = dynamic_cast<const VarDecl*>(cNodes[1].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar2");
	BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::IDENTIFIER_EXPR);
	const IdentifierExpr* iExpr = dynamic_cast<const IdentifierExpr*>(vDecl->GetInitExpr());
	BOOST_REQUIRE(iExpr != nullptr);
	BOOST_REQUIRE_EQUAL(iExpr->GetName(), "MyIntVar");
}

BOOST_AUTO_TEST_CASE( ParseVarInitIntBinOpTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/binary_op_int.tos");
    const size_t childExpectedSize = 9;
    BOOST_REQUIRE_EQUAL(cNodes.size(), childExpectedSize);

    TosLang::Common::Opcode operations[] =
    { 
        TosLang::Common::Opcode::PLUS,
        TosLang::Common::Opcode::MINUS,
        TosLang::Common::Opcode::MULT,
        TosLang::Common::Opcode::DIVIDE,
        TosLang::Common::Opcode::MODULO,
        TosLang::Common::Opcode::AND_INT,
        TosLang::Common::Opcode::OR_INT,
        TosLang::Common::Opcode::RIGHT_SHIFT,
        TosLang::Common::Opcode::LEFT_SHIFT,
    };

    for (size_t i = 0; i < childExpectedSize; i++)
    {
        BOOST_REQUIRE(cNodes[i]->GetKind() == ASTNode::NodeKind::VAR_DECL);
        const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[i].get());
        BOOST_REQUIRE(vDecl != nullptr);
        BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "V" + std::to_string(i + 1));
        
        BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BINARY_EXPR);
        const BinaryOpExpr* bExpr = dynamic_cast<const BinaryOpExpr*>(vDecl->GetInitExpr());
        BOOST_REQUIRE(bExpr != nullptr);
        BOOST_REQUIRE(bExpr->GetOperation() == operations[i]);
        
        const NumberExpr* nExpr = dynamic_cast<const NumberExpr*>(bExpr->GetLHS());
        BOOST_REQUIRE(nExpr != nullptr);
        BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 1);<commit_msg>TosLang: Adding test checking correct variable initialization with a boolean binary expression<commit_after>#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE Main
#else
#ifndef _WIN32
#   define BOOST_TEST_MODULE ParserVarTests
#endif
#endif

#include "toslang_fixture.h"

#include "Common/opcodes.h"

BOOST_FIXTURE_TEST_SUITE( FrontEndTestSuite, TosLangFixture )

//////////////////// CORRECT USE CASES ////////////////////

BOOST_AUTO_TEST_CASE( ParseVarDeclTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_decl.tos");
    BOOST_REQUIRE_EQUAL(cNodes.size(), 3);

    BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
    const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar");

    BOOST_REQUIRE(cNodes[1]->GetKind() == ASTNode::NodeKind::VAR_DECL);
    vDecl = dynamic_cast<const VarDecl*>(cNodes[1].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyBoolVar");

	BOOST_REQUIRE(cNodes[2]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	vDecl = dynamic_cast<const VarDecl*>(cNodes[2].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyStringVar");
}

BOOST_AUTO_TEST_CASE( ParseVarInitBoolTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_init_bool.tos");
    BOOST_REQUIRE_EQUAL(cNodes.size(), 2);
	
    BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyTrueVar");
    BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BOOLEAN_EXPR);
    const BooleanExpr* bExpr = dynamic_cast<const BooleanExpr*>(vDecl->GetInitExpr());
    BOOST_REQUIRE(bExpr != nullptr);
    BOOST_REQUIRE_EQUAL(bExpr->GetValue(), true);

    BOOST_REQUIRE(cNodes[1]->GetKind() == ASTNode::NodeKind::VAR_DECL);
    vDecl = dynamic_cast<const VarDecl*>(cNodes[1].get());
    BOOST_REQUIRE(vDecl != nullptr);
    BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyFalseVar");
    BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BOOLEAN_EXPR);
    bExpr = dynamic_cast<const BooleanExpr*>(vDecl->GetInitExpr());
    BOOST_REQUIRE(bExpr != nullptr);
    BOOST_REQUIRE_EQUAL(bExpr->GetValue(), false);
}

BOOST_AUTO_TEST_CASE( ParseVarInitIntTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_init_int.tos");
	BOOST_REQUIRE_EQUAL(cNodes.size(), 1);

	BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar");
	BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::NUMBER_EXPR);
	const NumberExpr* nExpr = dynamic_cast<const NumberExpr*>(vDecl->GetInitExpr());
	BOOST_REQUIRE(nExpr != nullptr);
	BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 42);
}

BOOST_AUTO_TEST_CASE( ParseVarInitIdentifierTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/var_init_identifier.tos");
	BOOST_REQUIRE_EQUAL(cNodes.size(), 2);

	BOOST_REQUIRE(cNodes[0]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[0].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar");
	BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::NUMBER_EXPR);
	const NumberExpr* nExpr = dynamic_cast<const NumberExpr*>(vDecl->GetInitExpr());
	BOOST_REQUIRE(nExpr != nullptr);
	BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 42);

	BOOST_REQUIRE(cNodes[1]->GetKind() == ASTNode::NodeKind::VAR_DECL);
	vDecl = dynamic_cast<const VarDecl*>(cNodes[1].get());
	BOOST_REQUIRE(vDecl != nullptr);
	BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "MyIntVar2");
	BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::IDENTIFIER_EXPR);
	const IdentifierExpr* iExpr = dynamic_cast<const IdentifierExpr*>(vDecl->GetInitExpr());
	BOOST_REQUIRE(iExpr != nullptr);
	BOOST_REQUIRE_EQUAL(iExpr->GetName(), "MyIntVar");
}

BOOST_AUTO_TEST_CASE( ParseVarInitBoolBinOpTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/binary_op_bool.tos");
    const size_t childExpectedSize = 4;
    BOOST_REQUIRE_EQUAL(cNodes.size(), childExpectedSize);

    TosLang::Common::Opcode operations[] =
    {
        TosLang::Common::Opcode::AND_BOOL,
        TosLang::Common::Opcode::OR_BOOL,
        TosLang::Common::Opcode::GREATER_THAN,
        TosLang::Common::Opcode::LESS_THAN,
    };

    size_t i = 0;
    for (; i < 2; ++i)
    {
        BOOST_REQUIRE(cNodes[i]->GetKind() == ASTNode::NodeKind::VAR_DECL);
        const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[i].get());
        BOOST_REQUIRE(vDecl != nullptr);
        BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "V" + std::to_string(i + 1));

        BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BINARY_EXPR);
        const BinaryOpExpr* bExpr = dynamic_cast<const BinaryOpExpr*>(vDecl->GetInitExpr());
        BOOST_REQUIRE(bExpr != nullptr);
        BOOST_REQUIRE(bExpr->GetOperation() == operations[i]);

        const BooleanExpr* boolExpr = dynamic_cast<const BooleanExpr*>(bExpr->GetLHS());
        BOOST_REQUIRE(boolExpr != nullptr);
        BOOST_REQUIRE_EQUAL(boolExpr->GetValue(), true);

        boolExpr = dynamic_cast<const BooleanExpr*>(bExpr->GetRHS());
        BOOST_REQUIRE(boolExpr!= nullptr);
        BOOST_REQUIRE_EQUAL(boolExpr->GetValue(), false);
    }

    for (; i < childExpectedSize; i++)
    {
        BOOST_REQUIRE(cNodes[i]->GetKind() == ASTNode::NodeKind::VAR_DECL);
        const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[i].get());
        BOOST_REQUIRE(vDecl != nullptr);
        BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "V" + std::to_string(i + 1));

        BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BINARY_EXPR);
        const BinaryOpExpr* bExpr = dynamic_cast<const BinaryOpExpr*>(vDecl->GetInitExpr());
        BOOST_REQUIRE(bExpr != nullptr);
        BOOST_REQUIRE(bExpr->GetOperation() == operations[i]);

        const NumberExpr* nExpr = dynamic_cast<const NumberExpr*>(bExpr->GetLHS());
        BOOST_REQUIRE(nExpr != nullptr);
        BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 1);

        nExpr = dynamic_cast<const NumberExpr*>(bExpr->GetRHS());
        BOOST_REQUIRE(nExpr != nullptr);
        BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 2);
    }
}

BOOST_AUTO_TEST_CASE( ParseVarInitIntBinOpTest )
{
    auto& cNodes = GetProgramAST("../inputs/var/binary_op_int.tos");
    const size_t childExpectedSize = 9;
    BOOST_REQUIRE_EQUAL(cNodes.size(), childExpectedSize);

    TosLang::Common::Opcode operations[] =
    { 
        TosLang::Common::Opcode::PLUS,
        TosLang::Common::Opcode::MINUS,
        TosLang::Common::Opcode::MULT,
        TosLang::Common::Opcode::DIVIDE,
        TosLang::Common::Opcode::MODULO,
        TosLang::Common::Opcode::AND_INT,
        TosLang::Common::Opcode::OR_INT,
        TosLang::Common::Opcode::RIGHT_SHIFT,
        TosLang::Common::Opcode::LEFT_SHIFT,
    };

    for (size_t i = 0; i < childExpectedSize; ++i)
    {
        BOOST_REQUIRE(cNodes[i]->GetKind() == ASTNode::NodeKind::VAR_DECL);
        const VarDecl* vDecl = dynamic_cast<const VarDecl*>(cNodes[i].get());
        BOOST_REQUIRE(vDecl != nullptr);
        BOOST_REQUIRE_EQUAL(vDecl->GetVarName(), "V" + std::to_string(i + 1));
        
        BOOST_REQUIRE(vDecl->GetInitExpr()->GetKind() == ASTNode::NodeKind::BINARY_EXPR);
        const BinaryOpExpr* bExpr = dynamic_cast<const BinaryOpExpr*>(vDecl->GetInitExpr());
        BOOST_REQUIRE(bExpr != nullptr);
        BOOST_REQUIRE(bExpr->GetOperation() == operations[i]);
        
        const NumberExpr* nExpr = dynamic_cast<const NumberExpr*>(bExpr->GetLHS());
        BOOST_REQUIRE(nExpr != nullptr);
        BOOST_REQUIRE_EQUAL(nExpr->GetValue(), 1);
{'fraction_non_alphanumeric': 0.1654729380491767, 'fraction_numerical': 0.005094882965369564, 'mean_word_length': 5.192958390489255, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5427018', 'n_tokens_mistral': 4805, 'n_tokens_neox': 4719, 'n_words': 610}