gflags now likes namespace gflags, not google
[folly.git] / folly / test / AtomicStructTest.cpp
1 /*
2  * Copyright 2014 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 <folly/AtomicStruct.h>
18
19 #include <gflags/gflags.h>
20 #include <gtest/gtest.h>
21
22 using namespace folly;
23
24 struct TwoBy32 {
25   uint32_t left;
26   uint32_t right;
27 };
28
29 TEST(AtomicStruct, two_by_32) {
30   AtomicStruct<TwoBy32> a(TwoBy32{ 10, 20 });
31   TwoBy32 av = a;
32   EXPECT_EQ(av.left, 10);
33   EXPECT_EQ(av.right, 20);
34   EXPECT_TRUE(a.compare_exchange_strong(av, TwoBy32{ 30, 40 }));
35   EXPECT_FALSE(a.compare_exchange_weak(av, TwoBy32{ 31, 41 }));
36   EXPECT_EQ(av.left, 30);
37   EXPECT_TRUE(a.is_lock_free());
38   auto b = a.exchange(TwoBy32{ 50, 60 });
39   EXPECT_EQ(b.left, 30);
40   EXPECT_EQ(b.right, 40);
41   EXPECT_EQ(a.load().left, 50);
42   a = TwoBy32{ 70, 80 };
43   EXPECT_EQ(a.load().right, 80);
44   a.store(TwoBy32{ 90, 100 });
45   av = a;
46   EXPECT_EQ(av.left, 90);
47   AtomicStruct<TwoBy32> c;
48   c = b;
49   EXPECT_EQ(c.load().right, 40);
50 }
51
52 TEST(AtomicStruct, size_selection) {
53   struct S1 { char x[1]; };
54   struct S2 { char x[2]; };
55   struct S3 { char x[3]; };
56   struct S4 { char x[4]; };
57   struct S5 { char x[5]; };
58   struct S6 { char x[6]; };
59   struct S7 { char x[7]; };
60   struct S8 { char x[8]; };
61
62   EXPECT_EQ(sizeof(AtomicStruct<S1>), 1);
63   EXPECT_EQ(sizeof(AtomicStruct<S2>), 2);
64   EXPECT_EQ(sizeof(AtomicStruct<S3>), 4);
65   EXPECT_EQ(sizeof(AtomicStruct<S4>), 4);
66   EXPECT_EQ(sizeof(AtomicStruct<S5>), 8);
67   EXPECT_EQ(sizeof(AtomicStruct<S6>), 8);
68   EXPECT_EQ(sizeof(AtomicStruct<S7>), 8);
69   EXPECT_EQ(sizeof(AtomicStruct<S8>), 8);
70 }
71
72 int main(int argc, char** argv) {
73   testing::InitGoogleTest(&argc, argv);
74   gflags::ParseCommandLineFlags(&argc, &argv, true);
75   return RUN_ALL_TESTS();
76 }