<commit_before>// LAF Base Library
// Copyright (c) 2001-2016 <NAME>
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.

#include "tests/test.h"

#include "base/rw_lock.h"

using namespace base;

TEST(RWLock, MultipleReaders)
{
  RWLock a;
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
  a.unlock();
  a.unlock();
  a.unlock();
}

TEST(RWLock, OneWriter)
{
  RWLock a;

  EXPECT_TRUE(a.lock(RWLock::WriteLock, 0));
  EXPECT_FALSE(a.lock(RWLock::ReadLock, 0));
  a.unlock();

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
  EXPECT_TRUE(a.lock(RWLock::WriteLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
}

TEST(RWLock, UpgradeToWrite)
{
  RWLock a;

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  EXPECT_TRUE(a.upgradeToWrite(0));
  EXPECT_FALSE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.downgradeToRead();
  a.unlock();
}

TEST(RWLock, WeakLock)
{
  RWLock a;
  RWLock::WeakLock flag = RWLock::WeakUnlocked;

  EXPECT_TRUE(a.weakLock(&flag));
  EXPECT_EQ(RWLock::WeakLocked, flag);
  EXPECT_FALSE(a.lock(RWLock::ReadLock, 0));
  EXPECT_EQ(RWLock::WeakUnlocking, flag);
  a.weakUnlock();
  EXPECT_EQ(RWLock::WeakUnlocked, flag);

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.weakLock(&flag));
  a.unlock();
}<commit_msg>Fix compilation error for rw_lock tests<commit_after>// LAF Base Library
// Copyright (c) 2001-2016 <NAME>
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.

#include <gtest/gtest.h>

#include "base/rw_lock.h"

using namespace base;

TEST(RWLock, MultipleReaders)
{
  RWLock a;
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
  a.unlock();
  a.unlock();
  a.unlock();
}

TEST(RWLock, OneWriter)
{
  RWLock a;

  EXPECT_TRUE(a.lock(RWLock::WriteLock, 0));
  EXPECT_FALSE(a.lock(RWLock::ReadLock, 0));
  a.unlock();

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
  EXPECT_TRUE(a.lock(RWLock::WriteLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.unlock();
}

TEST(RWLock, UpgradeToWrite)
{
  RWLock a;

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  EXPECT_TRUE(a.upgradeToWrite(0));
  EXPECT_FALSE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.lock(RWLock::WriteLock, 0));
  a.downgradeToRead();
  a.unlock();
}

TEST(RWLock, WeakLock)
{
  RWLock a;
  RWLock::WeakLock flag = RWLock::WeakUnlocked;

  EXPECT_TRUE(a.weakLock(&flag));
  EXPECT_EQ(RWLock::WeakLocked, flag);
  EXPECT_FALSE(a.lock(RWLock::ReadLock, 0));
  EXPECT_EQ(RWLock::WeakUnlocking, flag);
  a.weakUnlock();
  EXPECT_EQ(RWLock::WeakUnlocked, flag);

  EXPECT_TRUE(a.lock(RWLock::ReadLock, 0));
  EXPECT_FALSE(a.weakLock(&flag));
  a.unlock();
}

int main(int argc, char** argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
{'fraction_non_alphanumeric': 0.18255208333333334, 'fraction_numerical': 0.015625, 'mean_word_length': 5.510169491525423, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7282359', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1555, 'n_words': 242}