Multi-producer multi-consumer queue with optional blocking
[folly.git] / folly / test / DeterministicScheduleTest.cpp
diff --git a/folly/test/DeterministicScheduleTest.cpp b/folly/test/DeterministicScheduleTest.cpp
new file mode 100644 (file)
index 0000000..43e02a8
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2013 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "DeterministicSchedule.h"
+
+#include <gflags/gflags.h>
+#include <gtest/gtest.h>
+
+using namespace folly::test;
+
+TEST(DeterministicSchedule, uniform) {
+  auto p = DeterministicSchedule::uniform(0);
+  int buckets[10] = {};
+  for (int i = 0; i < 100000; ++i) {
+    buckets[p(10)]++;
+  }
+  for (int i = 0; i < 10; ++i) {
+    EXPECT_TRUE(buckets[i] > 9000);
+  }
+}
+
+TEST(DeterministicSchedule, uniformSubset) {
+  auto ps = DeterministicSchedule::uniformSubset(0, 3, 100);
+  int buckets[10] = {};
+  std::set<int> seen;
+  for (int i = 0; i < 100000; ++i) {
+    if (i > 0 && (i % 100) == 0) {
+      EXPECT_EQ(seen.size(), 3);
+      seen.clear();
+    }
+    int x = ps(10);
+    seen.insert(x);
+    EXPECT_TRUE(seen.size() <= 3);
+    buckets[x]++;
+  }
+  for (int i = 0; i < 10; ++i) {
+    EXPECT_TRUE(buckets[i] > 9000);
+  }
+}
+
+int main(int argc, char** argv) {
+  testing::InitGoogleTest(&argc, argv);
+  google::ParseCommandLineFlags(&argc, &argv, true);
+  return RUN_ALL_TESTS();
+}