/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "it_pthread_test.h" // 引入测试相关的头文件
 
/* ***************************************************************************
 * Public Functions
 * ************************************************************************** */
 
// 定义全局变量用于存储当前线程的优先级、策略和测试线程计数
static int g_currThreadPri, g_currThreadPolicy;
static int g_testPthredCount;
 
// 线程函数，用于测试线程调度策略和优先级
static void *ThreadFuncTest2(void *arg)
{
    (void)arg; // 未使用参数，避免编译器警告
    int ret; // 存放函数返回值
    int policy; // 存放调度策略
    struct sched_param param = { 0 }; // 初始化调度参数结构体
    pthread_t pthread = pthread_self(); // 获取当前线程ID
 
    g_testPthredCount++; // 测试线程计数加1
 
    // 获取当前线程的调度策略和参数
    ret = pthread_getschedparam(pthread, &policy, &param);
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); // 检查返回值，如果不为0则跳转到EXIT标签
 
    // 验证当前线程的优先级和策略是否与全局变量一致
    ICUNIT_GOTO_EQUAL(g_currThreadPri, param.sched_priority, param.sched_priority, EXIT);
    ICUNIT_GOTO_EQUAL(g_currThreadPolicy, policy, policy, EXIT);
    // 验证是否创建了2个测试线程
    ICUNIT_GOTO_EQUAL(g_testPthredCount, 2, g_testPthredCount, EXIT);
 
EXIT: // 错误处理或正常退出的标签
    return NULL;
}
 
// 另一个线程函数，用于测试SCHED_FIFO策略
static void *ThreadFuncTest3(void *arg)
{
    (void)arg; // 未使用参数，避免编译器警告
    int ret; // 存放函数返回值
    int policy; // 存放调度策略
    struct sched_param param = { 0 }; // 初始化调度参数结构体
    pthread_t pthread = pthread_self(); // 获取当前线程ID
 
    g_testPthredCount++; // 测试线程计数加1
 
    // 获取当前线程的调度策略和参数
    ret = pthread_getschedparam(pthread, &policy, &param);
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT); // 检查返回值，如果不为0则跳转到EXIT标签
 
    // 验证当前线程的优先级和策略是否与预期一致
    ICUNIT_GOTO_EQUAL(g_currThreadPri, param.sched_priority, param.sched_priority, EXIT);
    ICUNIT_GOTO_EQUAL(policy, SCHED_FIFO, policy, EXIT);
    // 验证是否创建了4个测试线程
    ICUNIT_GOTO_EQUAL(g_testPthredCount, 4, g_testPthredCount, EXIT);
 
EXIT: // 错误处理或正常退出的标签
    return NULL;
}
 
// 测试用例函数
static int Testcase()
{
    struct sched_param param = { 0 }; // 初始化调度参数结构体
    int ret; // 存放函数返回值
    void *res = NULL; // 用于存放线程函数的返回值
    pthread_attr_t a = { 0 }; // 初始化线程属性结构体（此处未使用）
    pthread_t newPthread, newPthread1; // 定义线程ID变量（注意：newPthread1未使用）
 
    g_testPthredCount = 0; // 初始化测试线程计数
 
    // 获取当前线程的调度策略和参数
    ret = pthread_getschedparam(pthread_self(), &g_currThreadPolicy, &param);
    ICUNIT_ASSERT_EQUAL(ret, 0, -ret); // 检查返回值
 
    g_currThreadPri = param.sched_priority; // 保存当前线程的优先级
 
    g_testPthredCount++; // 测试线程计数加1（此处应为初始化后的第一个操作，但计数逻辑稍显混乱）
    // 创建第一个测试线程
    ret = pthread_create(&newPthread, NULL, ThreadFuncTest2, 0);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查返回值
 
    // 等待第一个测试线程结束
    ret = pthread_join(newPthread, &res);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查返回值
 
    // 验证是否创建了2个测试线程（逻辑上应在此处验证，但前面已有验证）
    ICUNIT_ASSERT_EQUAL(g_testPthredCount, 2, g_testPthredCount);
    g_testPthredCount++; // 测试线程计数再加1，准备创建下一个线程
 
    // 设置当前线程的调度策略为SCHED_FIFO，并设置优先级
    param.sched_priority = g_currThreadPri;
    ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查返回值
 
    // 尝试重新创建并使用已定义的newPthread变量（这里应使用不同的变量名以避免混淆）
    ret = pthread_create(&newPthread, NULL, ThreadFuncTest3, 0);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查返回值
 
    // 等待第二个测试线程结束
    ret = pthread_join(newPthread, &res);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查返回值
 
    // 验证是否创建了4个测试线程（实际只创建了2个，此处逻辑有误）
    ICUNIT_ASSERT_EQUAL(g_testPthredCount, 4, g_testPthredCount);
 
    // 恢复当前线程的调度策略为SCHED_RR，并设置优先级
    param.sched_priority = g_currThreadPri;
    ret = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
    ICUNIT_ASSERT_EQUAL(ret, 0, ret); // 检查返回值
 
    return 0; // 测试用例结束
}
 
// 注册测试用例到测试框架中
void ItTestPthread003(void)
{
    TEST_ADD_CASE("IT_POSIX_PTHREAD_003", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
    // 注册一个名为"IT_POSIX_PTHREAD_003"的测试用例，指定测试函数为Testcase，并设置相关测试属性
}
