/**
Copyright (c) 2025 Huawei Technologies Co., Ltd.
This program is free software, you can redistribute it and/or modify it under the terms and conditions of
CANN Open Software License Agreement Version 2.0 (the "License").
Please refer to the License for details. You may not use this file except in compliance with the License.
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
See LICENSE in the root of the software repository for the full text of the License.
*/

#include "test_common.h"
#include "acl/acl.h"
#include <gtest/gtest.h>

using namespace std;
using namespace PtoTestCommon;

namespace TRowExpandAddTest {
template <typename T, uint32_t dstRow, uint32_t dstCol, uint32_t src1Row, uint32_t src1Col, bool src0eqdst>
void launchTRowExpandAdd(T *out, T *src0, T *src1, void *stream);

template <typename T, uint32_t dstRow, uint32_t dstCol, uint32_t src1Row, uint32_t src1Col, bool src0eqdst>
void launchTRowExpandAdd2(T *out, T *src0, T *src1, void *stream);

class TRowExpandAddTest : public testing::Test {
protected:
    void SetUp() override
    {}
    void TearDown() override
    {}
};

std::string GetGoldenDir()
{
    const testing::TestInfo *testInfo = testing::UnitTest::GetInstance()->current_test_info();
    const std::string caseName = testInfo->name();
    std::string suiteName = testInfo->test_suite_name();
    std::string fullPath = "../" + suiteName + "." + caseName;
    return fullPath;
}

template <typename T, uint32_t dstRow, uint32_t dstCol, uint32_t src1Row, uint32_t src1Col, bool src0eqdst,
          bool isRowMajor>
void test_trowexpandadd()
{
    size_t inputFileSize = src1Row * src1Col * sizeof(T);
    size_t outputFileSize = dstRow * dstCol * sizeof(T);

    aclInit(nullptr);
    aclrtSetDevice(0);
    aclrtStream stream;
    aclrtCreateStream(&stream);

    T *dstHost, *src0Host, *src1Host;
    T *dstDevice, *src0Device, *src1Device;

    aclrtMallocHost((void **)(&dstHost), outputFileSize);
    aclrtMallocHost((void **)(&src0Host), outputFileSize);
    aclrtMallocHost((void **)(&src1Host), inputFileSize);

    aclrtMalloc((void **)&dstDevice, outputFileSize, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc((void **)&src0Device, outputFileSize, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc((void **)&src1Device, inputFileSize, ACL_MEM_MALLOC_HUGE_FIRST);

    ReadFile(GetGoldenDir() + "/input0.bin", outputFileSize, src0Host, outputFileSize);
    ReadFile(GetGoldenDir() + "/input1.bin", inputFileSize, src1Host, inputFileSize);

    aclrtMemcpy(src0Device, outputFileSize, src0Host, outputFileSize, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(src1Device, inputFileSize, src1Host, inputFileSize, ACL_MEMCPY_HOST_TO_DEVICE);
    if (isRowMajor) {
        launchTRowExpandAdd2<T, dstRow, dstCol, src1Row, src1Col, src0eqdst>(dstDevice, src0Device, src1Device, stream);
    } else {
        launchTRowExpandAdd<T, dstRow, dstCol, src1Row, src1Col, src0eqdst>(dstDevice, src0Device, src1Device, stream);
    }

    aclrtSynchronizeStream(stream);
    aclrtMemcpy(dstHost, outputFileSize, dstDevice, outputFileSize, ACL_MEMCPY_DEVICE_TO_HOST);

    WriteFile(GetGoldenDir() + "/output.bin", dstHost, outputFileSize);

    aclrtFree(dstDevice);
    aclrtFree(src0Device);
    aclrtFree(src1Device);

    aclrtFreeHost(dstHost);
    aclrtFreeHost(src0Host);
    aclrtFreeHost(src1Host);

    aclrtDestroyStream(stream);
    aclrtResetDevice(0);
    aclFinalize();

    std::vector<float> golden(outputFileSize);
    std::vector<float> devFinal(outputFileSize);
    ReadFile(GetGoldenDir() + "/golden.bin", outputFileSize, golden.data(), outputFileSize);
    ReadFile(GetGoldenDir() + "/output.bin", outputFileSize, devFinal.data(), outputFileSize);
    bool ret = ResultCmp(golden, devFinal, 0.001f);

    EXPECT_TRUE(ret);
}

// 2D_NoPostUpdate
TEST_F(TRowExpandAddTest, case_fp32_16_32)
{
    test_trowexpandadd<float, 16, 32, 16, 1, true, false>();
}
TEST_F(TRowExpandAddTest, case_fp32_56_128)
{
    test_trowexpandadd<float, 56, 128, 56, 1, true, false>();
}
TEST_F(TRowExpandAddTest, case_fp16_48_64)
{
    test_trowexpandadd<aclFloat16, 48, 64, 48, 1, true, false>();
}
TEST_F(TRowExpandAddTest, case_fp16_16_128)
{
    test_trowexpandadd<aclFloat16, 16, 128, 16, 1, true, false>();
}
// 2D_NoPostUpdate2
TEST_F(TRowExpandAddTest, case_fp32_24_64)
{
    test_trowexpandadd<float, 24, 64, 24, 8, true, true>();
}
// 2D_NoPostUpdate
TEST_F(TRowExpandAddTest, case_fp16_32_64)
{
    test_trowexpandadd<aclFloat16, 32, 64, 32, 1, false, false>();
}
// 2D_NoPostUpdate2
TEST_F(TRowExpandAddTest, case_fp32_20_64)
{
    test_trowexpandadd<float, 20, 64, 20, 8, false, true>();
}
// 2D_NoPostUpdate
TEST_F(TRowExpandAddTest, case_int32_16_32)
{
    test_trowexpandadd<int32_t, 16, 32, 16, 1, true, false>();
}
TEST_F(TRowExpandAddTest, case_int16_16_64)
{
    test_trowexpandadd<int16_t, 16, 64, 16, 1, true, false>();
}
} // namespace TRowExpandAddTest