Commit 8f1bc558 authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

[base] Add tests for base::AsAtomicWord::SetBits.

TBR: mlippautz@chromium.org
Change-Id: I1d0c3c84e4483287aa599c7d3a0c0d1c5a4d154a
Reviewed-on: https://chromium-review.googlesource.com/612177
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47379}
parent cc29092d
......@@ -207,5 +207,80 @@ TEST(AsAtomic8, CompareAndSwap_Concurrent) {
}
}
TEST(AsAtomicWord, SetBits_Sequential) {
uintptr_t word = 0;
// Fill the word with a repeated 0xF0 pattern.
for (unsigned i = 0; i < sizeof(word); i++) {
word = (word << 8) | 0xF0;
}
// Check the pattern.
for (unsigned i = 0; i < sizeof(word); i++) {
EXPECT_EQ(0xF0u, (word >> (i * 8) & 0xFFu));
}
// Set the i-th byte value to i.
uintptr_t mask = 0xFF;
for (unsigned i = 0; i < sizeof(word); i++) {
uintptr_t byte = static_cast<uintptr_t>(i) << (i * 8);
AsAtomicWord::SetBits(&word, byte, mask);
mask <<= 8;
}
for (unsigned i = 0; i < sizeof(word); i++) {
EXPECT_EQ(i, (word >> (i * 8) & 0xFFu));
}
}
namespace {
class BitSettingThread final : public Thread {
public:
BitSettingThread()
: Thread(Options("BitSettingThread")),
word_addr_(nullptr),
bit_index_(0) {}
void Initialize(uintptr_t* word_addr, int bit_index) {
word_addr_ = word_addr;
bit_index_ = bit_index;
}
void Run() override {
uintptr_t bit = 1;
bit = bit << bit_index_;
AsAtomicWord::SetBits(word_addr_, bit, bit);
}
private:
uintptr_t* word_addr_;
int bit_index_;
};
} // namespace.
TEST(AsAtomicWord, SetBits_Concurrent) {
const int kBitCount = sizeof(uintptr_t) * 8;
const int kThreadCount = kBitCount / 2;
BitSettingThread threads[kThreadCount];
uintptr_t word;
AsAtomicWord::Relaxed_Store(&word, 0);
for (int i = 0; i < kThreadCount; i++) {
// Thread i sets bit number i * 2.
threads[i].Initialize(&word, i * 2);
}
for (int i = 0; i < kThreadCount; i++) {
threads[i].Start();
}
for (int i = 0; i < kThreadCount; i++) {
threads[i].Join();
}
uintptr_t actual_word = AsAtomicWord::Relaxed_Load(&word);
for (int i = 0; i < kBitCount; i++) {
// Every second bit must be set.
uintptr_t expected = (i % 2 == 0);
EXPECT_EQ(expected, actual_word & 1u);
actual_word >>= 1;
}
}
} // namespace base
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment