OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/edk/system/channel_endpoint_id.h" | |
6 | |
7 #include <sstream> | |
8 | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace mojo { | |
12 namespace system { | |
13 namespace { | |
14 | |
15 TEST(ChannelEndpointIdTest, Basic) { | |
16 ChannelEndpointId invalid; | |
17 ChannelEndpointId bootstrap(ChannelEndpointId::GetBootstrap()); | |
18 | |
19 EXPECT_EQ(invalid, invalid); | |
20 EXPECT_EQ(bootstrap, bootstrap); | |
21 EXPECT_FALSE(invalid == bootstrap); | |
22 | |
23 EXPECT_FALSE(invalid != invalid); | |
24 EXPECT_FALSE(bootstrap != bootstrap); | |
25 EXPECT_NE(invalid, bootstrap); | |
26 | |
27 EXPECT_FALSE(invalid < invalid); | |
28 EXPECT_LT(invalid, bootstrap); | |
29 | |
30 EXPECT_FALSE(invalid.is_valid()); | |
31 EXPECT_TRUE(bootstrap.is_valid()); | |
32 | |
33 EXPECT_FALSE(invalid.is_remote()); | |
34 EXPECT_FALSE(bootstrap.is_remote()); | |
35 | |
36 // Test assignment. | |
37 ChannelEndpointId copy; | |
38 copy = bootstrap; | |
39 EXPECT_EQ(copy, bootstrap); | |
40 copy = invalid; | |
41 EXPECT_EQ(copy, invalid); | |
42 } | |
43 | |
44 // Tests values of invalid and bootstrap IDs. (This tests implementation | |
45 // details.) | |
46 TEST(ChannelEndpointIdTest, Value) { | |
47 EXPECT_EQ(0u, ChannelEndpointId().value()); | |
48 EXPECT_EQ(1u, ChannelEndpointId::GetBootstrap().value()); | |
49 } | |
50 | |
51 // Tests ostream output. (This tests implementation details.) | |
52 TEST(ChannelEndpointIdTest, Ostream) { | |
53 { | |
54 std::ostringstream stream; | |
55 stream << ChannelEndpointId(); | |
56 EXPECT_EQ("0", stream.str()); | |
57 } | |
58 { | |
59 std::ostringstream stream; | |
60 stream << ChannelEndpointId::GetBootstrap(); | |
61 EXPECT_EQ("1", stream.str()); | |
62 } | |
63 } | |
64 | |
65 TEST(LocalChannelEndpointIdGeneratorTest, Basic) { | |
66 LocalChannelEndpointIdGenerator gen; | |
67 | |
68 ChannelEndpointId id1; | |
69 EXPECT_FALSE(id1.is_valid()); // Check sanity. | |
70 | |
71 id1 = gen.GetNext(); | |
72 EXPECT_TRUE(id1.is_valid()); | |
73 EXPECT_FALSE(id1.is_remote()); | |
74 | |
75 EXPECT_EQ(ChannelEndpointId::GetBootstrap().value(), id1.value()); | |
76 | |
77 ChannelEndpointId id2 = gen.GetNext(); | |
78 EXPECT_TRUE(id2.is_valid()); | |
79 EXPECT_FALSE(id2.is_remote()); | |
80 // Technically, nonequality here is an implementation detail, since, e.g., | |
81 // random generation of IDs would be a valid implementation. | |
82 EXPECT_NE(id2, id1); | |
83 // ... but right now we just increment to generate IDs. | |
84 EXPECT_EQ(2u, id2.value()); | |
85 } | |
86 | |
87 // Note: LocalChannelEndpointIdGeneratorTest.WrapAround is defined further | |
88 // below, outside the anonymous namespace. | |
89 | |
90 TEST(RemoteChannelEndpointIdGeneratorTest, Basic) { | |
91 RemoteChannelEndpointIdGenerator gen; | |
92 | |
93 ChannelEndpointId id1; | |
94 EXPECT_FALSE(id1.is_valid()); // Check sanity. | |
95 | |
96 id1 = gen.GetNext(); | |
97 EXPECT_TRUE(id1.is_valid()); | |
98 EXPECT_TRUE(id1.is_remote()); | |
99 | |
100 // This tests an implementation detail. | |
101 EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id1.value()); | |
102 | |
103 ChannelEndpointId id2 = gen.GetNext(); | |
104 EXPECT_TRUE(id2.is_valid()); | |
105 EXPECT_TRUE(id2.is_remote()); | |
106 // Technically, nonequality here is an implementation detail, since, e.g., | |
107 // random generation of IDs would be a valid implementation. | |
108 EXPECT_NE(id2, id1); | |
109 // ... but right now we just increment to generate IDs. | |
110 EXPECT_EQ(ChannelEndpointId::kRemoteFlag + 1, id2.value()); | |
111 } | |
112 | |
113 // Note: RemoteChannelEndpointIdGeneratorTest.WrapAround is defined further | |
114 // below, outside the anonymous namespace. | |
115 | |
116 } // namespace | |
117 | |
118 // Tests that |LocalChannelEndpointIdGenerator| handles wrap-around correctly. | |
119 // (This tests implementation details.) This test isn't in an anonymous | |
120 // namespace, since it needs to be friended. | |
121 TEST(LocalChannelEndpointIdGeneratorTest, WrapAround) { | |
122 LocalChannelEndpointIdGenerator gen; | |
123 gen.next_ = ChannelEndpointId(ChannelEndpointId::kRemoteFlag - 1); | |
124 | |
125 ChannelEndpointId id = gen.GetNext(); | |
126 EXPECT_TRUE(id.is_valid()); | |
127 EXPECT_FALSE(id.is_remote()); | |
128 EXPECT_EQ(ChannelEndpointId::kRemoteFlag - 1, id.value()); | |
129 | |
130 id = gen.GetNext(); | |
131 EXPECT_TRUE(id.is_valid()); | |
132 EXPECT_FALSE(id.is_remote()); | |
133 EXPECT_EQ(1u, id.value()); | |
134 } | |
135 | |
136 // Tests that |RemoteChannelEndpointIdGenerator| handles wrap-around correctly. | |
137 // (This tests implementation details.) This test isn't in an anonymous | |
138 // namespace, since it needs to be friended. | |
139 TEST(RemoteChannelEndpointIdGeneratorTest, WrapAround) { | |
140 RemoteChannelEndpointIdGenerator gen; | |
141 gen.next_ = ChannelEndpointId(~0u); | |
142 | |
143 ChannelEndpointId id = gen.GetNext(); | |
144 EXPECT_TRUE(id.is_valid()); | |
145 EXPECT_TRUE(id.is_remote()); | |
146 EXPECT_EQ(~0u, id.value()); | |
147 | |
148 id = gen.GetNext(); | |
149 EXPECT_TRUE(id.is_valid()); | |
150 EXPECT_TRUE(id.is_remote()); | |
151 EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id.value()); | |
152 } | |
153 | |
154 } // namespace system | |
155 } // namespace mojo | |
OLD | NEW |