tuple: update to make use of C++11
[chromium-blink-merge.git] / mojo / edk / system / channel_endpoint_id_unittest.cc
bloba5bd14fa73c28bd5bb6a3baca5bc3c6165a42435
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.
5 #include "mojo/edk/system/channel_endpoint_id.h"
7 #include <sstream>
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace mojo {
12 namespace system {
13 namespace {
15 TEST(ChannelEndpointIdTest, Basic) {
16 ChannelEndpointId invalid;
17 ChannelEndpointId bootstrap(ChannelEndpointId::GetBootstrap());
19 EXPECT_EQ(invalid, invalid);
20 EXPECT_EQ(bootstrap, bootstrap);
21 EXPECT_FALSE(invalid == bootstrap);
23 EXPECT_FALSE(invalid != invalid);
24 EXPECT_FALSE(bootstrap != bootstrap);
25 EXPECT_NE(invalid, bootstrap);
27 EXPECT_FALSE(invalid < invalid);
28 EXPECT_LT(invalid, bootstrap);
30 EXPECT_FALSE(invalid.is_valid());
31 EXPECT_TRUE(bootstrap.is_valid());
33 EXPECT_FALSE(invalid.is_remote());
34 EXPECT_FALSE(bootstrap.is_remote());
36 // Test assignment.
37 ChannelEndpointId copy;
38 copy = bootstrap;
39 EXPECT_EQ(copy, bootstrap);
40 copy = invalid;
41 EXPECT_EQ(copy, invalid);
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());
51 // Tests ostream output. (This tests implementation details.)
52 TEST(ChannelEndpointIdTest, Ostream) {
54 std::ostringstream stream;
55 stream << ChannelEndpointId();
56 EXPECT_EQ("0", stream.str());
59 std::ostringstream stream;
60 stream << ChannelEndpointId::GetBootstrap();
61 EXPECT_EQ("1", stream.str());
65 TEST(LocalChannelEndpointIdGeneratorTest, Basic) {
66 LocalChannelEndpointIdGenerator gen;
68 ChannelEndpointId id1;
69 EXPECT_FALSE(id1.is_valid()); // Check sanity.
71 id1 = gen.GetNext();
72 EXPECT_TRUE(id1.is_valid());
73 EXPECT_FALSE(id1.is_remote());
75 EXPECT_EQ(ChannelEndpointId::GetBootstrap().value(), id1.value());
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());
87 // Note: LocalChannelEndpointIdGeneratorTest.WrapAround is defined further
88 // below, outside the anonymous namespace.
90 TEST(RemoteChannelEndpointIdGeneratorTest, Basic) {
91 RemoteChannelEndpointIdGenerator gen;
93 ChannelEndpointId id1;
94 EXPECT_FALSE(id1.is_valid()); // Check sanity.
96 id1 = gen.GetNext();
97 EXPECT_TRUE(id1.is_valid());
98 EXPECT_TRUE(id1.is_remote());
100 // This tests an implementation detail.
101 EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id1.value());
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());
113 // Note: RemoteChannelEndpointIdGeneratorTest.WrapAround is defined further
114 // below, outside the anonymous namespace.
116 } // namespace
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);
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());
130 id = gen.GetNext();
131 EXPECT_TRUE(id.is_valid());
132 EXPECT_FALSE(id.is_remote());
133 EXPECT_EQ(1u, id.value());
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);
143 ChannelEndpointId id = gen.GetNext();
144 EXPECT_TRUE(id.is_valid());
145 EXPECT_TRUE(id.is_remote());
146 EXPECT_EQ(~0u, id.value());
148 id = gen.GetNext();
149 EXPECT_TRUE(id.is_valid());
150 EXPECT_TRUE(id.is_remote());
151 EXPECT_EQ(ChannelEndpointId::kRemoteFlag, id.value());
154 } // namespace system
155 } // namespace mojo