[PATCH] D47940: [SmallSet] Add some simple unit tests.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 8 06:12:19 PDT 2018


fhahn created this revision.
fhahn added reviewers: craig.topper, dblaikie.
Herald added a subscriber: mgorny.

https://reviews.llvm.org/D47940

Files:
  unittests/ADT/CMakeLists.txt
  unittests/ADT/SmallSetTest.cpp


Index: unittests/ADT/SmallSetTest.cpp
===================================================================
--- /dev/null
+++ unittests/ADT/SmallSetTest.cpp
@@ -0,0 +1,73 @@
+//===- llvm/unittest/ADT/SmallSetTest.cpp ------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// SmallSet unit tests.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/SmallSet.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+TEST(SmallSetTest, Insert) {
+
+  SmallSet<int, 4> s1;
+
+  for (int i = 0; i < 4; i++)
+    s1.insert(i);
+
+  for (int i = 0; i < 4; i++)
+    s1.insert(i);
+
+  EXPECT_EQ(4u, s1.size());
+
+  for (int i = 0; i < 4; i++)
+    EXPECT_EQ(1u, s1.count(i));
+
+  EXPECT_EQ(0u, s1.count(99));
+  EXPECT_EQ(0u, s1.count(4));
+}
+
+TEST(SmallSetTest, Grow) {
+  SmallSet<int, 4> s1;
+
+  for (int i = 0; i < 8; i++)
+    s1.insert(i);
+
+  EXPECT_EQ(8u, s1.size());
+
+  for (int i = 0; i < 8; i++)
+    EXPECT_EQ(1u, s1.count(i));
+
+  EXPECT_EQ(0u, s1.count(99));
+  EXPECT_EQ(0u, s1.count(10));
+}
+
+TEST(SmallSetTest, Erase) {
+  SmallSet<int, 4> s1;
+
+  for (int i = 0; i < 8; i++)
+    s1.insert(i);
+
+  EXPECT_EQ(8u, s1.size());
+
+  // Remove elements one by one and check if all other elements are still there.
+  for (int i = 0; i < 8; i++) {
+    EXPECT_EQ(1u, s1.count(i));
+    EXPECT_TRUE(s1.erase(i));
+    EXPECT_EQ(0u, s1.count(i));
+    EXPECT_EQ(8u - i - 1, s1.size());
+    for (int j = i + 1; j < 8; j++)
+      EXPECT_EQ(1u, s1.count(j));
+  }
+
+  EXPECT_EQ(0u, s1.count(99));
+  EXPECT_EQ(0u, s1.count(10));
+}
Index: unittests/ADT/CMakeLists.txt
===================================================================
--- unittests/ADT/CMakeLists.txt
+++ unittests/ADT/CMakeLists.txt
@@ -51,6 +51,7 @@
   SetVectorTest.cpp
   SimpleIListTest.cpp
   SmallPtrSetTest.cpp
+  SmallSetTest.cpp
   SmallStringTest.cpp
   SmallVectorTest.cpp
   SparseBitVectorTest.cpp


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47940.150501.patch
Type: text/x-patch
Size: 2202 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180608/1800ead9/attachment.bin>


More information about the llvm-commits mailing list