Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Unified Diff: test/unittests/atomic-utils-unittest.cc

Issue 1324153003: Add template parameter and unittests to atomic utils. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: fighting c++ Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/unittests/atomic-utils-unittest.cc
diff --git a/test/unittests/atomic-utils-unittest.cc b/test/unittests/atomic-utils-unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..060973630179aca5174f2d34c9e9cf7131da2d8b
--- /dev/null
+++ b/test/unittests/atomic-utils-unittest.cc
@@ -0,0 +1,217 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <limits.h>
+
+#include "src/atomic-utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace v8 {
+namespace internal {
+
+TEST(AtomicNumber, Constructor) {
+ // Test some common types.
+ AtomicNumber<int> zero_int;
+ AtomicNumber<size_t> zero_size_t;
+ AtomicNumber<intptr_t> zero_intptr_t;
+ EXPECT_EQ(0, zero_int.Value());
+ EXPECT_EQ(0, zero_size_t.Value());
+ EXPECT_EQ(0, zero_intptr_t.Value());
+}
+
+
+TEST(AtomicNumber, Value) {
+ AtomicNumber<int> a(1);
+ EXPECT_EQ(1, a.Value());
+ AtomicNumber<int> b(-1);
+ EXPECT_EQ(-1, b.Value());
+ AtomicNumber<size_t> c(1);
+ EXPECT_EQ(1, c.Value());
+ AtomicNumber<size_t> d(static_cast<size_t>(-1));
+ EXPECT_EQ(std::numeric_limits<size_t>::max(), d.Value());
+}
+
+
+TEST(AtomicNumber, SetValue) {
+ AtomicNumber<int> a(1);
+ a.SetValue(-1);
+ EXPECT_EQ(-1, a.Value());
+}
+
+
+TEST(AtomicNumber, Increment) {
+ AtomicNumber<int> a(std::numeric_limits<int>::max());
+ a.Increment(1);
+ EXPECT_EQ(std::numeric_limits<int>::min(), a.Value());
+ // Check that potential signed-ness of the underlying storage has no impact
+ // on unsigned types.
+ AtomicNumber<size_t> b(std::numeric_limits<intptr_t>::max());
+ b.Increment(1);
+ EXPECT_EQ(static_cast<size_t>(std::numeric_limits<intptr_t>::max()) + 1,
+ b.Value());
+ // Should work as decrement as well.
+ AtomicNumber<size_t> c(1);
+ c.Increment(-1);
+ EXPECT_EQ(0, c.Value());
+ c.Increment(-1);
+ EXPECT_EQ(std::numeric_limits<size_t>::max(), c.Value());
+}
+
+
+namespace {
+
+enum TestFlag {
+ kA,
+ kB,
+ kC,
+};
+
+} // namespace
+
+
+TEST(AtomicValue, Initial) {
+ AtomicValue<TestFlag> a(kA);
+ EXPECT_EQ(TestFlag::kA, a.Value());
+}
+
+
+TEST(AtomicValue, TrySetValue) {
+ AtomicValue<TestFlag> a(kA);
+ EXPECT_FALSE(a.TrySetValue(kB, kC));
+ EXPECT_TRUE(a.TrySetValue(kA, kC));
+ EXPECT_EQ(TestFlag::kC, a.Value());
+}
+
+
+TEST(AtomicValue, SetValue) {
+ AtomicValue<TestFlag> a(kB);
+ EXPECT_EQ(kB, a.SetValue(kC));
+ EXPECT_EQ(TestFlag::kC, a.Value());
+}
+
+
+TEST(AtomicValue, WithVoidStar) {
+ AtomicValue<void*> a(nullptr);
+ AtomicValue<void*> dummy(nullptr);
+ EXPECT_EQ(nullptr, a.Value());
+ EXPECT_EQ(nullptr, a.SetValue(&a));
+ EXPECT_EQ(&a, a.Value());
+ EXPECT_FALSE(a.TrySetValue(nullptr, &dummy));
+ EXPECT_TRUE(a.TrySetValue(&a, &dummy));
+ EXPECT_EQ(&dummy, a.Value());
+}
+
+
+namespace {
+
+enum TestSetValue { kAA, kBB, kCC, kLastValue = kCC };
+
+} // namespace
+
+
+TEST(AtomicEnumSet, Constructor) {
+ AtomicEnumSet<TestSetValue> a;
+ EXPECT_TRUE(a.IsEmpty());
+ EXPECT_FALSE(a.Contains(kAA));
+}
+
+
+TEST(AtomicEnumSet, AddSingle) {
+ AtomicEnumSet<TestSetValue> a;
+ a.Add(kAA);
+ EXPECT_FALSE(a.IsEmpty());
+ EXPECT_TRUE(a.Contains(kAA));
+ EXPECT_FALSE(a.Contains(kBB));
+ EXPECT_FALSE(a.Contains(kCC));
+}
+
+
+TEST(AtomicEnumSet, AddOtherSet) {
+ AtomicEnumSet<TestSetValue> a;
+ AtomicEnumSet<TestSetValue> b;
+ a.Add(kAA);
+ EXPECT_FALSE(a.IsEmpty());
+ EXPECT_TRUE(b.IsEmpty());
+ b.Add(a);
+ EXPECT_FALSE(b.IsEmpty());
+ EXPECT_TRUE(a.Contains(kAA));
+ EXPECT_TRUE(b.Contains(kAA));
+}
+
+
+TEST(AtomicEnumSet, RemoveSingle) {
+ AtomicEnumSet<TestSetValue> a;
+ a.Add(kAA);
+ a.Add(kBB);
+ EXPECT_TRUE(a.Contains(kAA));
+ EXPECT_TRUE(a.Contains(kBB));
+ a.Remove(kAA);
+ EXPECT_FALSE(a.Contains(kAA));
+ EXPECT_TRUE(a.Contains(kBB));
+}
+
+
+TEST(AtomicEnumSet, RemoveOtherSet) {
+ AtomicEnumSet<TestSetValue> a;
+ AtomicEnumSet<TestSetValue> b;
+ a.Add(kAA);
+ a.Add(kBB);
+ b.Add(kBB);
+ a.Remove(b);
+ EXPECT_TRUE(a.Contains(kAA));
+ EXPECT_FALSE(a.Contains(kBB));
+ EXPECT_FALSE(a.Contains(kCC));
+}
+
+
+TEST(AtomicEnumSet, RemoveEmptySet) {
+ AtomicEnumSet<TestSetValue> a;
+ AtomicEnumSet<TestSetValue> b;
+ a.Add(kAA);
+ a.Add(kBB);
+ EXPECT_TRUE(a.Contains(kAA));
+ EXPECT_TRUE(a.Contains(kBB));
+ EXPECT_FALSE(a.Contains(kCC));
+ EXPECT_TRUE(b.IsEmpty());
+ a.Remove(b);
+ EXPECT_TRUE(a.Contains(kAA));
+ EXPECT_TRUE(a.Contains(kBB));
+ EXPECT_FALSE(a.Contains(kCC));
+}
+
+
+TEST(AtomicEnumSet, Intersect) {
+ AtomicEnumSet<TestSetValue> a;
+ AtomicEnumSet<TestSetValue> b;
+ a.Add(kAA);
+ b.Add(kCC);
+ a.Intersect(b);
+ EXPECT_TRUE(a.IsEmpty());
+}
+
+
+TEST(AtomicEnumSet, ContainsAnyOf) {
+ AtomicEnumSet<TestSetValue> a;
+ AtomicEnumSet<TestSetValue> b;
+ a.Add(kAA);
+ b.Add(kCC);
+ EXPECT_FALSE(a.ContainsAnyOf(b));
+ b.Add(kAA);
+ EXPECT_TRUE(a.ContainsAnyOf(b));
+}
+
+
+TEST(AtomicEnumSet, Equality) {
+ AtomicEnumSet<TestSetValue> a;
+ AtomicEnumSet<TestSetValue> b;
+ a.Add(kAA);
+ EXPECT_FALSE(a == b);
+ EXPECT_TRUE(a != b);
+ b.Add(kAA);
+ EXPECT_TRUE(a == b);
+ EXPECT_FALSE(a != b);
+}
+
+} // namespace internal
+} // namespace v8
« src/atomic-utils.h ('K') | « src/heap/heap.h ('k') | test/unittests/unittests.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698