1 #include <gtest/gtest.h> 2 #include <pthread.h> 3 #include <thread> 4 #include <threads.h> 5 6 using namespace testing::ext; 7 8 class ThreadMutexattrTest : public testing::Test { 9 protected: 10 pthread_mutexattr_t mutexAttr_; 11 SetUp()12 void SetUp() override 13 { 14 EXPECT_EQ(0, pthread_mutexattr_init(&mutexAttr_)); 15 } TearDown()16 void TearDown() override 17 { 18 EXPECT_EQ(0, pthread_mutexattr_destroy(&mutexAttr_)); 19 } 20 }; 21 22 /** 23 * @tc.name: pthread_mutexattr_gettype_001 24 * @tc.desc: Set PTHREAD_MUTEX_NORMAL for mutex, call this function to obtain the properties, and then verify that it 25 * is equal to expected 26 * @tc.type: FUNC 27 * */ 28 HWTEST_F(ThreadMutexattrTest, pthread_mutexattr_gettype_001, TestSize.Level1) 29 { 30 int pthreadAttrType; 31 32 EXPECT_EQ(0, pthread_mutexattr_settype(&mutexAttr_, PTHREAD_MUTEX_NORMAL)); 33 EXPECT_EQ(0, pthread_mutexattr_gettype(&mutexAttr_, &pthreadAttrType)); 34 EXPECT_EQ(PTHREAD_MUTEX_NORMAL, pthreadAttrType); 35 } 36 37 /** 38 * @tc.name: pthread_mutexattr_gettype_002 39 * @tc.desc: Set PTHREAD_MUTEX_ERRORCHECK for mutex, call this function to obtain the properties, and then verify that 40 * it is equal to expected 41 * @tc.type: FUNC 42 * */ 43 HWTEST_F(ThreadMutexattrTest, pthread_mutexattr_gettype_002, TestSize.Level1) 44 { 45 int pthreadAttrType; 46 47 EXPECT_EQ(0, pthread_mutexattr_settype(&mutexAttr_, PTHREAD_MUTEX_ERRORCHECK)); 48 EXPECT_EQ(0, pthread_mutexattr_gettype(&mutexAttr_, &pthreadAttrType)); 49 EXPECT_EQ(PTHREAD_MUTEX_ERRORCHECK, pthreadAttrType); 50 } 51 52 /** 53 * @tc.name: pthread_mutexattr_gettype_003 54 * @tc.desc: Set PTHREAD_MUTEX_RECURSIVE for mutex, call this function to obtain the properties, and then verify that it 55 * is equal to expected 56 * @tc.type: FUNC 57 * */ 58 HWTEST_F(ThreadMutexattrTest, pthread_mutexattr_gettype_003, TestSize.Level1) 59 { 60 int pthreadAttrType; 61 62 EXPECT_EQ(0, pthread_mutexattr_settype(&mutexAttr_, PTHREAD_MUTEX_RECURSIVE)); 63 EXPECT_EQ(0, pthread_mutexattr_gettype(&mutexAttr_, &pthreadAttrType)); 64 EXPECT_EQ(PTHREAD_MUTEX_RECURSIVE, pthreadAttrType); 65 } 66 67 /** 68 * @tc.name: pthread_mutexattr_gettype_004 69 * @tc.desc: Set PTHREAD_MUTEX_DEFAULT for mutex, call this function to obtain the properties, and then verify that it 70 * is equal to expected 71 * @tc.type: FUNC 72 * */ 73 HWTEST_F(ThreadMutexattrTest, pthread_mutexattr_gettype_004, TestSize.Level1) 74 { 75 int pthreadAttrType; 76 77 EXPECT_EQ(0, pthread_mutexattr_settype(&mutexAttr_, PTHREAD_MUTEX_DEFAULT)); 78 EXPECT_EQ(0, pthread_mutexattr_gettype(&mutexAttr_, &pthreadAttrType)); 79 EXPECT_EQ(PTHREAD_MUTEX_DEFAULT, pthreadAttrType); 80 } 81 82 /** 83 * @tc.name: pthread_mutexattr_setprotocol_001 84 * @tc.desc: Verify pthread_mutexattr_t setting and obtaining the Protocol attribute in the t structure works normally 85 * @tc.type: FUNC 86 * */ 87 HWTEST_F(ThreadMutexattrTest, pthread_mutexattr_setprotocol_001, TestSize.Level1) 88 { 89 int destProtocol; 90 EXPECT_EQ(0, pthread_mutexattr_getprotocol(&mutexAttr_, &destProtocol)); 91 EXPECT_EQ(PTHREAD_PRIO_NONE, destProtocol); 92 93 EXPECT_EQ(0, pthread_mutexattr_setprotocol(&mutexAttr_, PTHREAD_PRIO_NONE)); 94 EXPECT_EQ(0, pthread_mutexattr_getprotocol(&mutexAttr_, &destProtocol)); 95 EXPECT_EQ(destProtocol, PTHREAD_PRIO_NONE); 96 97 EXPECT_EQ(0, pthread_mutexattr_setprotocol(&mutexAttr_, PTHREAD_PRIO_INHERIT)); 98 EXPECT_EQ(0, pthread_mutexattr_getprotocol(&mutexAttr_, &destProtocol)); 99 EXPECT_EQ(destProtocol, PTHREAD_PRIO_INHERIT); 100 }