brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · a68c84c Raw
80 lines · cpp
1//===-- Unittests for mutex -----------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/__support/CPP/mutex.h"10#include "test/UnitTest/Test.h"11 12using LIBC_NAMESPACE::cpp::adopt_lock;13using LIBC_NAMESPACE::cpp::lock_guard;14 15// Simple struct for testing cpp::lock_guard. It defines methods 'lock' and16// 'unlock' which are required for the cpp::lock_guard class template.17struct Mutex {18  // Flag to show whether this mutex is locked.19  bool locked = false;20 21  // Flag to show if this mutex has been double locked.22  bool double_locked = false;23 24  // Flag to show if this mutex has been double unlocked.25  bool double_unlocked = false;26 27  Mutex() {}28 29  void lock() {30    if (locked)31      double_locked = true;32 33    locked = true;34  }35 36  void unlock() {37    if (!locked)38      double_unlocked = true;39 40    locked = false;41  }42};43 44TEST(LlvmLibcMutexTest, Basic) {45  Mutex m;46  ASSERT_FALSE(m.locked);47  ASSERT_FALSE(m.double_locked);48  ASSERT_FALSE(m.double_unlocked);49 50  {51    lock_guard lg(m);52    ASSERT_TRUE(m.locked);53    ASSERT_FALSE(m.double_locked);54  }55 56  ASSERT_FALSE(m.locked);57  ASSERT_FALSE(m.double_unlocked);58}59 60TEST(LlvmLibcMutexTest, AcquireLocked) {61  Mutex m;62  ASSERT_FALSE(m.locked);63  ASSERT_FALSE(m.double_locked);64  ASSERT_FALSE(m.double_unlocked);65 66  // Lock the mutex before placing a lock guard on it.67  m.lock();68  ASSERT_TRUE(m.locked);69  ASSERT_FALSE(m.double_locked);70 71  {72    lock_guard lg(m, adopt_lock);73    ASSERT_TRUE(m.locked);74    ASSERT_FALSE(m.double_locked);75  }76 77  ASSERT_FALSE(m.locked);78  ASSERT_FALSE(m.double_unlocked);79}80