folly copyright 2015 -> copyright 2016
[folly.git] / folly / test / SparseByteSetTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cstdint>
18 #include <limits>
19 #include <random>
20 #include <set>
21 #include <folly/SparseByteSet.h>
22 #include <gtest/gtest.h>
23
24 using namespace std;
25 using namespace folly;
26
27 namespace {
28
29 class SparseByteSetTest : public testing::Test {
30  protected:
31   using lims = numeric_limits<uint8_t>;
32   SparseByteSet s;
33 };
34
35 }
36
37 TEST_F(SparseByteSetTest, empty) {
38   for (auto c = lims::min(); c < lims::max(); ++c) {
39     EXPECT_FALSE(s.contains(c));
40   }
41 }
42
43 TEST_F(SparseByteSetTest, each) {
44   for (auto c = lims::min(); c < lims::max(); ++c) {
45     EXPECT_TRUE(s.add(c));
46     EXPECT_TRUE(s.contains(c));
47   }
48   for (auto c = lims::min(); c < lims::max(); ++c) {
49     EXPECT_FALSE(s.add(c));
50     EXPECT_TRUE(s.contains(c));
51   }
52 }
53
54 TEST_F(SparseByteSetTest, each_random) {
55   mt19937 rng;
56   uniform_int_distribution<uint8_t> dist;
57   set<uint8_t> added;
58   while (added.size() <= lims::max()) {
59     auto c = dist(rng);
60     EXPECT_EQ(added.count(c), s.contains(c));
61     EXPECT_EQ(!added.count(c), s.add(c));
62     added.insert(c);
63     EXPECT_TRUE(added.count(c)); // sanity
64     EXPECT_TRUE(s.contains(c));
65   }
66 }