Bug 1833854 - Part 6: Round requested nursery before checking range when changing...
[gecko.git] / third_party / libwebrtc / api / rtc_error_unittest.cc
blobba307d8f71641f25313fbe5e6382c88ab5a2eb95
1 /*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
11 #include "api/rtc_error.h"
13 #include <utility>
15 #include "test/gtest.h"
17 namespace {
19 const int kDefaultMoveOnlyIntValue = 0xbadf00d;
21 // Class that has no copy constructor, ensuring that RTCErrorOr can
22 struct MoveOnlyInt {
23 MoveOnlyInt() {}
24 explicit MoveOnlyInt(int value) : value(value) {}
25 MoveOnlyInt(const MoveOnlyInt& other) = delete;
26 MoveOnlyInt& operator=(const MoveOnlyInt& other) = delete;
27 MoveOnlyInt(MoveOnlyInt&& other) : value(other.value) {}
28 MoveOnlyInt& operator=(MoveOnlyInt&& other) {
29 value = other.value;
30 return *this;
33 int value = kDefaultMoveOnlyIntValue;
36 // Same as above. Used to test conversion from RTCErrorOr<A> to RTCErrorOr<B>
37 // when A can be converted to B.
38 struct MoveOnlyInt2 {
39 MoveOnlyInt2() {}
40 explicit MoveOnlyInt2(int value) : value(value) {}
41 MoveOnlyInt2(const MoveOnlyInt2& other) = delete;
42 MoveOnlyInt2& operator=(const MoveOnlyInt2& other) = delete;
43 MoveOnlyInt2(MoveOnlyInt2&& other) : value(other.value) {}
44 MoveOnlyInt2& operator=(MoveOnlyInt2&& other) {
45 value = other.value;
46 return *this;
49 explicit MoveOnlyInt2(MoveOnlyInt&& other) : value(other.value) {}
50 MoveOnlyInt2& operator=(MoveOnlyInt&& other) {
51 value = other.value;
52 return *this;
55 int value = kDefaultMoveOnlyIntValue;
58 } // namespace
60 namespace webrtc {
62 // Test that the default constructor creates a "no error" error.
63 TEST(RTCErrorTest, DefaultConstructor) {
64 RTCError e;
65 EXPECT_EQ(RTCErrorType::NONE, e.type());
66 EXPECT_EQ(std::string(), e.message());
67 EXPECT_TRUE(e.ok());
70 TEST(RTCErrorTest, NormalConstructors) {
71 RTCError a(RTCErrorType::INVALID_PARAMETER);
72 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, a.type());
73 EXPECT_EQ(std::string(), a.message());
75 // Constructor that takes const char* message.
76 RTCError b(RTCErrorType::UNSUPPORTED_PARAMETER, "foobar");
77 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, b.type());
78 EXPECT_EQ(std::string("foobar"), b.message());
80 // Constructor that takes std::string message.
81 RTCError c(RTCErrorType::INVALID_RANGE, std::string("new"));
82 EXPECT_EQ(RTCErrorType::INVALID_RANGE, c.type());
83 EXPECT_EQ(std::string("new"), c.message());
86 TEST(RTCErrorTest, MoveConstructor) {
87 // Static string.
88 RTCError a(RTCErrorType::INVALID_PARAMETER, "foo");
89 RTCError b(std::move(a));
90 EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, b.type());
91 EXPECT_EQ(std::string("foo"), b.message());
93 // Non-static string.
94 RTCError c(RTCErrorType::UNSUPPORTED_PARAMETER, std::string("bar"));
95 RTCError d(std::move(c));
96 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, d.type());
97 EXPECT_EQ(std::string("bar"), d.message());
100 TEST(RTCErrorTest, MoveAssignment) {
101 // Try all combinations of "is static string"/"is non-static string" moves.
102 RTCError e(RTCErrorType::INVALID_PARAMETER, "foo");
104 e = RTCError(RTCErrorType::UNSUPPORTED_PARAMETER, "bar");
105 EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, e.type());
106 EXPECT_EQ(std::string("bar"), e.message());
108 e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("baz"));
109 EXPECT_EQ(std::string("baz"), e.message());
111 e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("another"));
112 EXPECT_EQ(std::string("another"), e.message());
114 e = RTCError(RTCErrorType::SYNTAX_ERROR, "last");
115 EXPECT_EQ(std::string("last"), e.message());
118 // Test that the error returned by RTCError::OK() is a "no error" error.
119 TEST(RTCErrorTest, OKConstant) {
120 RTCError ok = RTCError::OK();
121 EXPECT_EQ(RTCErrorType::NONE, ok.type());
122 EXPECT_EQ(std::string(), ok.message());
123 EXPECT_TRUE(ok.ok());
126 // Test that "error.ok()" behaves as expected.
127 TEST(RTCErrorTest, OkMethod) {
128 RTCError success;
129 RTCError failure(RTCErrorType::INTERNAL_ERROR);
130 EXPECT_TRUE(success.ok());
131 EXPECT_FALSE(failure.ok());
134 // Test that a message can be set using either static const strings or
135 // std::strings.
136 TEST(RTCErrorTest, SetMessage) {
137 RTCError e;
138 // Try all combinations of "is static string"/"is non-static string" calls.
139 e.set_message("foo");
140 EXPECT_EQ(std::string("foo"), e.message());
142 e.set_message("bar");
143 EXPECT_EQ(std::string("bar"), e.message());
145 e.set_message(std::string("string"));
146 EXPECT_EQ(std::string("string"), e.message());
148 e.set_message(std::string("more"));
149 EXPECT_EQ(std::string("more"), e.message());
151 e.set_message("love to test");
152 EXPECT_EQ(std::string("love to test"), e.message());
155 // Test that the default constructor creates an "INTERNAL_ERROR".
156 TEST(RTCErrorOrTest, DefaultConstructor) {
157 RTCErrorOr<MoveOnlyInt> e;
158 EXPECT_EQ(RTCErrorType::INTERNAL_ERROR, e.error().type());
161 // Test that an RTCErrorOr can be implicitly constructed from a value.
162 TEST(RTCErrorOrTest, ImplicitValueConstructor) {
163 RTCErrorOr<MoveOnlyInt> e = [] { return MoveOnlyInt(100); }();
164 EXPECT_EQ(100, e.value().value);
167 // Test that an RTCErrorOr can be implicitly constructed from an RTCError.
168 TEST(RTCErrorOrTest, ImplicitErrorConstructor) {
169 RTCErrorOr<MoveOnlyInt> e = [] {
170 return RTCError(RTCErrorType::SYNTAX_ERROR);
171 }();
172 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, e.error().type());
175 TEST(RTCErrorOrTest, MoveConstructor) {
176 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
177 RTCErrorOr<MoveOnlyInt> b(std::move(a));
178 EXPECT_EQ(5, b.value().value);
181 TEST(RTCErrorOrTest, MoveAssignment) {
182 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
183 RTCErrorOr<MoveOnlyInt> b(MoveOnlyInt(10));
184 a = std::move(b);
185 EXPECT_EQ(10, a.value().value);
188 TEST(RTCErrorOrTest, ConversionConstructor) {
189 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(1));
190 RTCErrorOr<MoveOnlyInt2> b(std::move(a));
193 TEST(RTCErrorOrTest, ConversionAssignment) {
194 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
195 RTCErrorOr<MoveOnlyInt2> b(MoveOnlyInt2(10));
196 b = std::move(a);
197 EXPECT_EQ(5, b.value().value);
200 TEST(RTCErrorOrTest, OkMethod) {
201 RTCErrorOr<int> success(1337);
202 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
203 EXPECT_TRUE(success.ok());
204 EXPECT_FALSE(error.ok());
207 TEST(RTCErrorOrTest, MoveError) {
208 RTCErrorOr<int> e({RTCErrorType::SYNTAX_ERROR, "message"});
209 RTCError err = e.MoveError();
210 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, err.type());
211 EXPECT_EQ(std::string("message"), err.message());
214 TEST(RTCErrorOrTest, MoveValue) {
215 RTCErrorOr<MoveOnlyInt> e(MoveOnlyInt(88));
216 MoveOnlyInt value = e.MoveValue();
217 EXPECT_EQ(88, value.value);
220 // Death tests.
221 // Disabled on Android because death tests misbehave on Android, see
222 // base/test/gtest_util.h.
223 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
225 TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
226 RTCErrorOr<int> err;
227 EXPECT_DEATH(err = RTCError::OK(), "");
230 TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {
231 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
232 EXPECT_DEATH(error.value(), "");
235 TEST(RTCErrorOrDeathTest, MoveErrorValue) {
236 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
237 EXPECT_DEATH(error.MoveValue(), "");
240 #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
242 } // namespace webrtc