/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <iostream>
#include <string>

#include <gtest/gtest.h>
#include "disassembler.h"

using namespace panda::disasm;

#cmakedefine DISASM_BIN_DIR "@DISASM_BIN_DIR@/"

TEST(label_test, test1)
{
    Disassembler d {};

    std::stringstream ss {};
    d.Disassemble(std::string(DISASM_BIN_DIR) + "labels1.bc");
    d.Serialize(ss, false);

    size_t beg_g = ss.str().find("u1 g() <static> {\n");
    size_t end_g = ss.str().find('}', beg_g);
    size_t beg_gg = ss.str().find("u1 gg() <static> {\n");
    size_t end_gg = ss.str().find('}', beg_gg);

    ASSERT_TRUE(beg_g != std::string::npos && end_g != std::string::npos) << "function g not found";
    ASSERT_TRUE(beg_gg != std::string::npos && end_gg != std::string::npos) << "function gg not found";

    std::string body_g =
        ss.str().substr(beg_g + strlen("u1 g() <static> {\n"), end_g - (beg_g + strlen("u1 g() <static> {\n")));
    std::string body_gg =
        ss.str().substr(beg_gg + strlen("u1 gg() <static> {\n"), end_gg - (beg_gg + strlen("u1 gg() <static> {\n")));

    EXPECT_EQ(body_g, "jump_label_0:\n\tjmp jump_label_0\n\treturn\n");
    EXPECT_EQ(body_gg, "\tjmp jump_label_0\njump_label_0:\n\treturn\n");
}

TEST(label_test, test2)
{
    Disassembler d {};

    std::stringstream ss {};
    d.Disassemble(std::string(DISASM_BIN_DIR) + "labels2.bc");
    d.Serialize(ss);

    size_t beg_g = ss.str().find("g() <static> {");
    size_t end_g = ss.str().find('}', beg_g);

    ASSERT_TRUE(beg_g != std::string::npos && end_g != std::string::npos) << "function g not found";

    std::string body_g = ss.str().substr(beg_g + strlen("g() {"), end_g - (beg_g + strlen("g() {")));

    EXPECT_TRUE(body_g.find("jump_label_0:\n\tmovi v0, 0x0") != std::string::npos) << "jump_label_0 not found";
    EXPECT_TRUE(body_g.find("jump_label_2:\n\tmovi v0, 0x1") != std::string::npos) << "jump_label_1 not found";
    EXPECT_TRUE(body_g.find("jump_label_4:\n\tmovi v0, 0x2") != std::string::npos) << "jump_label_2 not found";
    EXPECT_TRUE(body_g.find("jump_label_6:\n\tmovi v0, 0x3") != std::string::npos) << "jump_label_3 not found";
    EXPECT_TRUE(body_g.find("jump_label_7:\n\tmovi v0, 0x4") != std::string::npos) << "jump_label_4 not found";
    EXPECT_TRUE(body_g.find("jump_label_5:\n\tmovi v0, 0x5") != std::string::npos) << "jump_label_5 not found";
    EXPECT_TRUE(body_g.find("jump_label_3:\n\tmovi v0, 0x6") != std::string::npos) << "jump_label_6 not found";
    EXPECT_TRUE(body_g.find("jump_label_1:\n\tmovi v0, 0x7") != std::string::npos) << "jump_label_7 not found";

    EXPECT_TRUE(body_g.find("\tjmp jump_label_0\n"
                            "\tjmp jump_label_1\n"
                            "\tjmp jump_label_2\n"
                            "\tjmp jump_label_3\n"
                            "\tjmp jump_label_4\n"
                            "\tjmp jump_label_5\n"
                            "\tjmp jump_label_6\n"
                            "\tjmp jump_label_7\n") != std::string::npos)
        << "label sequence is broken";
}

TEST(label_test, test_exceptions)
{
    Disassembler d {};

    std::stringstream ss {};
    d.Disassemble(std::string(DISASM_BIN_DIR) + "exceptions.bc");
    d.Serialize(ss);

    std::string res = ss.str();

    EXPECT_TRUE(res.find("try_begin_label_0:\n\tldai 0x1") != std::string::npos);
    EXPECT_TRUE(res.find("try_end_label_0:\n\tldai 0x3") != std::string::npos);
    EXPECT_TRUE(res.find("handler_begin_label_0_0:\n\tcall.virt.short A_exception.getMessage:(A_exception), v0") !=
                std::string::npos);
    EXPECT_TRUE(res.find("handler_end_label_0_0:\n\tldai 0x6") != std::string::npos);
    EXPECT_TRUE(res.find("handler_begin_label_0_1:\n\tldai 0x7") != std::string::npos);
    EXPECT_TRUE(
        res.find(
            ".catch A_exception, try_begin_label_0, try_end_label_0, handler_begin_label_0_0, handler_end_label_0_0") !=
        std::string::npos);
    EXPECT_TRUE(res.find(".catchall try_begin_label_0, try_end_label_0, handler_begin_label_0_1") != std::string::npos);
}

#undef DISASM_BIN_DIR