summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/TestResult.cpp
blob: 20680e41c832ca6511906b0dd898de2163c3fe09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <string.h>
#include "mozilla/ResultVariant.h"
#include "mozilla/UniquePtr.h"

using mozilla::Err;
using mozilla::GenericErrorResult;
using mozilla::Ok;
using mozilla::Result;
using mozilla::UniquePtr;

#define MOZ_STATIC_AND_RELEASE_ASSERT(expr) \
  static_assert(expr);                      \
  MOZ_RELEASE_ASSERT(expr)

enum struct TestUnusedZeroEnum : int16_t { Ok = 0, NotOk = 1 };

namespace mozilla::detail {
template <>
struct UnusedZero<TestUnusedZeroEnum> : UnusedZeroEnum<TestUnusedZeroEnum> {};
}  // namespace mozilla::detail

struct Failed {};

namespace mozilla::detail {
template <>
struct UnusedZero<Failed> {
  using StorageType = uintptr_t;

  static constexpr bool value = true;
  static constexpr StorageType nullValue = 0;
  static constexpr StorageType GetDefaultValue() { return 2; }

  static constexpr void AssertValid(StorageType aValue) {}
  static constexpr Failed Inspect(const StorageType& aValue) {
    return Failed{};
  }
  static constexpr Failed Unwrap(StorageType aValue) { return Failed{}; }
  static constexpr StorageType Store(Failed aValue) {
    return GetDefaultValue();
  }
};

}  // namespace mozilla::detail

// V is trivially default-constructible, and E has UnusedZero<E>::value == true,
// for a reference type and for a non-reference type
static_assert(mozilla::detail::SelectResultImpl<uintptr_t, Failed>::value ==
              mozilla::detail::PackingStrategy::NullIsOk);
static_assert(
    mozilla::detail::SelectResultImpl<Ok, TestUnusedZeroEnum>::value ==
    mozilla::detail::PackingStrategy::NullIsOk);
static_assert(mozilla::detail::SelectResultImpl<Ok, Failed>::value ==
              mozilla::detail::PackingStrategy::LowBitTagIsError);

static_assert(std::is_trivially_destructible_v<Result<uintptr_t, Failed>>);
static_assert(std::is_trivially_destructible_v<Result<Ok, TestUnusedZeroEnum>>);
static_assert(std::is_trivially_destructible_v<Result<Ok, Failed>>);

static_assert(
    sizeof(Result<bool, TestUnusedZeroEnum>) <= sizeof(uintptr_t),
    "Result with bool value type should not be larger than pointer-sized");
static_assert(sizeof(Result<Ok, Failed>) == sizeof(uint8_t),
              "Result with empty value type should be size 1");
static_assert(sizeof(Result<int*, Failed>) == sizeof(uintptr_t),
              "Result with two aligned pointer types should be pointer-sized");
static_assert(
    sizeof(Result<char*, Failed*>) > sizeof(char*),
    "Result with unaligned success type `char*` must not be pointer-sized");
static_assert(
    sizeof(Result<int*, char*>) > sizeof(char*),
    "Result with unaligned error type `char*` must not be pointer-sized");

enum Foo8 : uint8_t {};
enum Foo16 : uint16_t {};
enum Foo32 : uint32_t {};
static_assert(sizeof(Result<Ok, Foo8>) <= sizeof(uintptr_t),
              "Result with small types should be pointer-sized");
static_assert(sizeof(Result<Ok, Foo16>) <= sizeof(uintptr_t),
              "Result with small types should be pointer-sized");
static_assert(sizeof(Foo32) >= sizeof(uintptr_t) ||
                  sizeof(Result<Ok, Foo32>) <= sizeof(uintptr_t),
              "Result with small types should be pointer-sized");

static_assert(sizeof(Result<Foo16, Foo8>) <= sizeof(uintptr_t),
              "Result with small types should be pointer-sized");
static_assert(sizeof(Result<Foo8, Foo16>) <= sizeof(uintptr_t),
              "Result with small types should be pointer-sized");
static_assert(sizeof(Foo32) >= sizeof(uintptr_t) ||
                  sizeof(Result<Foo32, Foo16>) <= sizeof(uintptr_t),
              "Result with small types should be pointer-sized");
static_assert(sizeof(Foo32) >= sizeof(uintptr_t) ||
                  sizeof(Result<Foo16, Foo32>) <= sizeof(uintptr_t),
              "Result with small types should be pointer-sized");

#if __cplusplus < 202002L
static_assert(std::is_literal_type_v<Result<int*, Failed>>);
static_assert(std::is_literal_type_v<Result<Ok, Failed>>);
static_assert(std::is_literal_type_v<Result<Ok, Foo8>>);
static_assert(std::is_literal_type_v<Result<Foo8, Foo16>>);
static_assert(!std::is_literal_type_v<Result<Ok, UniquePtr<int>>>);
#endif

static constexpr GenericErrorResult<Failed> Fail() { return Err(Failed{}); }

static constexpr GenericErrorResult<TestUnusedZeroEnum>
FailTestUnusedZeroEnum() {
  return Err(TestUnusedZeroEnum::NotOk);
}

static constexpr Result<Ok, Failed> Task1(bool pass) {
  if (!pass) {
    return Fail();  // implicit conversion from GenericErrorResult to Result
  }
  return Ok();
}

static constexpr Result<Ok, TestUnusedZeroEnum> Task1UnusedZeroEnumErr(
    bool pass) {
  if (!pass) {
    return FailTestUnusedZeroEnum();  // implicit conversion from
                                      // GenericErrorResult to Result
  }
  return Ok();
}

static constexpr Result<int, Failed> Task2(bool pass, int value) {
  MOZ_TRY(
      Task1(pass));  // converts one type of result to another in the error case
  return value;      // implicit conversion from T to Result<T, E>
}

static constexpr Result<int, TestUnusedZeroEnum> Task2UnusedZeroEnumErr(
    bool pass, int value) {
  MOZ_TRY(Task1UnusedZeroEnumErr(
      pass));    // converts one type of result to another in the error case
  return value;  // implicit conversion from T to Result<T, E>
}

static Result<int, Failed> Task3(bool pass1, bool pass2, int value) {
  int x, y;
  MOZ_TRY_VAR(x, Task2(pass1, value));
  MOZ_TRY_VAR(y, Task2(pass2, value));
  return x + y;
}

static void BasicTests() {
  MOZ_STATIC_AND_RELEASE_ASSERT(Task1(true).isOk());
  MOZ_STATIC_AND_RELEASE_ASSERT(!Task1(true).isErr());
  MOZ_STATIC_AND_RELEASE_ASSERT(!Task1(false).isOk());
  MOZ_STATIC_AND_RELEASE_ASSERT(Task1(false).isErr());

  MOZ_STATIC_AND_RELEASE_ASSERT(Task1UnusedZeroEnumErr(true).isOk());
  MOZ_STATIC_AND_RELEASE_ASSERT(!Task1UnusedZeroEnumErr(true).isErr());
  MOZ_STATIC_AND_RELEASE_ASSERT(!Task1UnusedZeroEnumErr(false).isOk());
  MOZ_STATIC_AND_RELEASE_ASSERT(Task1UnusedZeroEnumErr(false).isErr());
  MOZ_STATIC_AND_RELEASE_ASSERT(TestUnusedZeroEnum::NotOk ==
                                Task1UnusedZeroEnumErr(false).inspectErr());
  MOZ_STATIC_AND_RELEASE_ASSERT(TestUnusedZeroEnum::NotOk ==
                                Task1UnusedZeroEnumErr(false).unwrapErr());

  // MOZ_TRY works.
  MOZ_STATIC_AND_RELEASE_ASSERT(Task2(true, 3).isOk());
  MOZ_STATIC_AND_RELEASE_ASSERT(Task2(true, 3).unwrap() == 3);
  MOZ_STATIC_AND_RELEASE_ASSERT(Task2(true, 3).unwrapOr(6) == 3);
  MOZ_RELEASE_ASSERT(Task2(false, 3).isErr());
  MOZ_RELEASE_ASSERT(Task2(false, 3).unwrapOr(6) == 6);

  MOZ_STATIC_AND_RELEASE_ASSERT(Task2UnusedZeroEnumErr(true, 3).isOk());
  MOZ_STATIC_AND_RELEASE_ASSERT(Task2UnusedZeroEnumErr(true, 3).unwrap() == 3);
  MOZ_STATIC_AND_RELEASE_ASSERT(Task2UnusedZeroEnumErr(true, 3).unwrapOr(6) ==
                                3);
  MOZ_STATIC_AND_RELEASE_ASSERT(Task2UnusedZeroEnumErr(false, 3).isErr());
  MOZ_STATIC_AND_RELEASE_ASSERT(Task2UnusedZeroEnumErr(false, 3).unwrapOr(6) ==
                                6);

  // MOZ_TRY_VAR works.
  MOZ_RELEASE_ASSERT(Task3(true, true, 3).isOk());
  MOZ_RELEASE_ASSERT(Task3(true, true, 3).unwrap() == 6);
  MOZ_RELEASE_ASSERT(Task3(true, false, 3).isErr());
  MOZ_RELEASE_ASSERT(Task3(false, true, 3).isErr());
  MOZ_RELEASE_ASSERT(Task3(false, true, 3).unwrapOr(6) == 6);

  // Lvalues should work too.
  {
    constexpr Result<Ok, Failed> res1 = Task1(true);
    MOZ_STATIC_AND_RELEASE_ASSERT(res1.isOk());
    MOZ_STATIC_AND_RELEASE_ASSERT(!res1.isErr());

    constexpr Result<Ok, Failed> res2 = Task1(false);
    MOZ_STATIC_AND_RELEASE_ASSERT(!res2.isOk());
    MOZ_STATIC_AND_RELEASE_ASSERT(res2.isErr());
  }

  {
    Result<int, Failed> res = Task2(true, 3);
    MOZ_RELEASE_ASSERT(res.isOk());
    MOZ_RELEASE_ASSERT(res.unwrap() == 3);

    res = Task2(false, 4);
    MOZ_RELEASE_ASSERT(res.isErr());
  }

  // Some tests for pointer tagging.
  {
    int i = 123;

    Result<int*, Failed> res = &i;
    static_assert(sizeof(res) == sizeof(uintptr_t),
                  "should use pointer tagging to fit in a word");

    MOZ_RELEASE_ASSERT(res.isOk());
    MOZ_RELEASE_ASSERT(*res.unwrap() == 123);

    res = Err(Failed());
    MOZ_RELEASE_ASSERT(res.isErr());
  }
}

struct NonCopyableNonMovable {
  explicit constexpr NonCopyableNonMovable(uint32_t aValue) : mValue(aValue) {}

  NonCopyableNonMovable(const NonCopyableNonMovable&) = delete;
  NonCopyableNonMovable(NonCopyableNonMovable&&) = delete;
  NonCopyableNonMovable& operator=(const NonCopyableNonMovable&) = delete;
  NonCopyableNonMovable& operator=(NonCopyableNonMovable&&) = delete;

  uint32_t mValue;
};

static void InPlaceConstructionTests() {
  {
    // PackingStrategy == NullIsOk
    static_assert(mozilla::detail::SelectResultImpl<NonCopyableNonMovable,
                                                    Failed>::value ==
                  mozilla::detail::PackingStrategy::NullIsOk);
    constexpr Result<NonCopyableNonMovable, Failed> result{std::in_place, 42u};
    MOZ_STATIC_AND_RELEASE_ASSERT(42 == result.inspect().mValue);
  }

  {
    // PackingStrategy == Variant
    static_assert(
        mozilla::detail::SelectResultImpl<NonCopyableNonMovable, int>::value ==
        mozilla::detail::PackingStrategy::Variant);
    const Result<NonCopyableNonMovable, int> result{std::in_place, 42};
    MOZ_RELEASE_ASSERT(42 == result.inspect().mValue);
  }
}

/* * */

struct Snafu : Failed {};

static Result<Ok, Snafu*> Explode() {
  static Snafu snafu;
  return Err(&snafu);
}

static Result<Ok, Failed*> ErrorGeneralization() {
  MOZ_TRY(Explode());  // change error type from Snafu* to more general Failed*
  return Ok();
}

static void TypeConversionTests() {
  MOZ_RELEASE_ASSERT(ErrorGeneralization().isErr());

  {
    const Result<Ok, Failed*> res = Explode();
    MOZ_RELEASE_ASSERT(res.isErr());
  }

  {
    const Result<Ok, Failed*> res = Result<Ok, Snafu*>{Ok{}};
    MOZ_RELEASE_ASSERT(res.isOk());
  }
}

static void EmptyValueTest() {
  struct Fine {};
  mozilla::Result<Fine, Failed> res((Fine()));
  res.unwrap();
  MOZ_RELEASE_ASSERT(res.isOk());
  static_assert(sizeof(res) == sizeof(uint8_t),
                "Result with empty value and error types should be size 1");
}

static void MapTest() {
  struct MyError {
    int x;

    explicit MyError(int y) : x(y) {}
  };

  // Mapping over success values, to the same success type.
  {
    Result<int, MyError> res(5);
    bool invoked = false;
    auto res2 = res.map([&invoked](int x) {
      MOZ_RELEASE_ASSERT(x == 5);
      invoked = true;
      return 6;
    });
    MOZ_RELEASE_ASSERT(res2.isOk());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(res2.unwrap() == 6);
  }

  // Mapping over success values, to a different success type.
  {
    Result<int, MyError> res(5);
    bool invoked = false;
    auto res2 = res.map([&invoked](int x) {
      MOZ_RELEASE_ASSERT(x == 5);
      invoked = true;
      return "hello";
    });
    MOZ_RELEASE_ASSERT(res2.isOk());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(strcmp(res2.unwrap(), "hello") == 0);
  }

  // Mapping over success values (constexpr).
  {
    constexpr uint64_t kValue = 42u;
    constexpr auto res2a = Result<int32_t, Failed>{5}.map([](int32_t x) {
      MOZ_RELEASE_ASSERT(x == 5);
      return kValue;
    });
    MOZ_STATIC_AND_RELEASE_ASSERT(res2a.isOk());
    MOZ_STATIC_AND_RELEASE_ASSERT(kValue == res2a.inspect());
  }

  // Mapping over error values.
  {
    MyError err(1);
    Result<char, MyError> res(err);
    MOZ_RELEASE_ASSERT(res.isErr());
    Result<char, MyError> res2 = res.map([](int x) {
      MOZ_RELEASE_ASSERT(false);
      return 'a';
    });
    MOZ_RELEASE_ASSERT(res2.isErr());
    MOZ_RELEASE_ASSERT(res2.unwrapErr().x == err.x);
  }

  // Function pointers instead of lambdas as the mapping function.
  {
    Result<const char*, MyError> res("hello");
    auto res2 = res.map(strlen);
    MOZ_RELEASE_ASSERT(res2.isOk());
    MOZ_RELEASE_ASSERT(res2.unwrap() == 5);
  }
}

static void MapErrTest() {
  struct MyError {
    int x;

    explicit MyError(int y) : x(y) {}
  };

  struct MyError2 {
    int a;

    explicit MyError2(int b) : a(b) {}
  };

  // Mapping over error values, to the same error type.
  {
    MyError err(1);
    Result<char, MyError> res(err);
    MOZ_RELEASE_ASSERT(res.isErr());
    bool invoked = false;
    auto res2 = res.mapErr([&invoked](const auto err) {
      MOZ_RELEASE_ASSERT(err.x == 1);
      invoked = true;
      return MyError(2);
    });
    MOZ_RELEASE_ASSERT(res2.isErr());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(res2.unwrapErr().x == 2);
  }

  // Mapping over error values, to a different error type.
  {
    MyError err(1);
    Result<char, MyError> res(err);
    MOZ_RELEASE_ASSERT(res.isErr());
    bool invoked = false;
    auto res2 = res.mapErr([&invoked](const auto err) {
      MOZ_RELEASE_ASSERT(err.x == 1);
      invoked = true;
      return MyError2(2);
    });
    MOZ_RELEASE_ASSERT(res2.isErr());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(res2.unwrapErr().a == 2);
  }

  // Mapping over success values.
  {
    Result<int, MyError> res(5);
    auto res2 = res.mapErr([](const auto err) {
      MOZ_RELEASE_ASSERT(false);
      return MyError(1);
    });
    MOZ_RELEASE_ASSERT(res2.isOk());
    MOZ_RELEASE_ASSERT(res2.unwrap() == 5);
  }

  // Function pointers instead of lambdas as the mapping function.
  {
    Result<Ok, const char*> res("hello");
    auto res2 = res.mapErr(strlen);
    MOZ_RELEASE_ASSERT(res2.isErr());
    MOZ_RELEASE_ASSERT(res2.unwrapErr() == 5);
  }
}

static Result<Ok, size_t> strlen_ResultWrapper(const char* aValue) {
  return Err(strlen(aValue));
}

static void OrElseTest() {
  struct MyError {
    int x;

    explicit constexpr MyError(int y) : x(y) {}
  };

  struct MyError2 {
    int a;

    explicit constexpr MyError2(int b) : a(b) {}
  };

  // `orElse`ing over error values, to Result<V, E> (the same error type) error
  // variant.
  {
    MyError err(1);
    Result<char, MyError> res(err);
    MOZ_RELEASE_ASSERT(res.isErr());
    bool invoked = false;
    auto res2 = res.orElse([&invoked](const auto err) -> Result<char, MyError> {
      MOZ_RELEASE_ASSERT(err.x == 1);
      invoked = true;
      if (err.x != 42) {
        return Err(MyError(2));
      }
      return 'a';
    });
    MOZ_RELEASE_ASSERT(res2.isErr());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(res2.unwrapErr().x == 2);
  }

  // `orElse`ing over error values, to Result<V, E> (the same error type)
  // success variant.
  {
    MyError err(42);
    Result<char, MyError> res(err);
    MOZ_RELEASE_ASSERT(res.isErr());
    bool invoked = false;
    auto res2 = res.orElse([&invoked](const auto err) -> Result<char, MyError> {
      MOZ_RELEASE_ASSERT(err.x == 42);
      invoked = true;
      if (err.x != 42) {
        return Err(MyError(2));
      }
      return 'a';
    });
    MOZ_RELEASE_ASSERT(res2.isOk());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(res2.unwrap() == 'a');
  }

  // `orElse`ing over error values, to Result<V, E2> (a different error type)
  // error variant.
  {
    MyError err(1);
    Result<char, MyError> res(err);
    MOZ_RELEASE_ASSERT(res.isErr());
    bool invoked = false;
    auto res2 =
        res.orElse([&invoked](const auto err) -> Result<char, MyError2> {
          MOZ_RELEASE_ASSERT(err.x == 1);
          invoked = true;
          if (err.x != 42) {
            return Err(MyError2(2));
          }
          return 'a';
        });
    MOZ_RELEASE_ASSERT(res2.isErr());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(res2.unwrapErr().a == 2);
  }

  // `orElse`ing over error values, to Result<V, E2> (a different error type)
  // success variant.
  {
    MyError err(42);
    Result<char, MyError> res(err);
    MOZ_RELEASE_ASSERT(res.isErr());
    bool invoked = false;
    auto res2 =
        res.orElse([&invoked](const auto err) -> Result<char, MyError2> {
          MOZ_RELEASE_ASSERT(err.x == 42);
          invoked = true;
          if (err.x != 42) {
            return Err(MyError2(2));
          }
          return 'a';
        });
    MOZ_RELEASE_ASSERT(res2.isOk());
    MOZ_RELEASE_ASSERT(invoked);
    MOZ_RELEASE_ASSERT(res2.unwrap() == 'a');
  }

  // `orElse`ing over success values.
  {
    Result<int, MyError> res(5);
    auto res2 = res.orElse([](const auto err) -> Result<int, MyError> {
      MOZ_RELEASE_ASSERT(false);
      return Err(MyError(1));
    });
    MOZ_RELEASE_ASSERT(res2.isOk());
    MOZ_RELEASE_ASSERT(res2.unwrap() == 5);
  }

  // Function pointers instead of lambdas as the `orElse`ing function.
  {
    Result<Ok, const char*> res("hello");
    auto res2 = res.orElse(strlen_ResultWrapper);
    MOZ_RELEASE_ASSERT(res2.isErr());
    MOZ_RELEASE_ASSERT(res2.unwrapErr() == 5);
  }
}

static void AndThenTest() {
  // `andThen`ing over success results.
  {
    Result<int, const char*> r1(10);
    Result<int, const char*> r2 =
        r1.andThen([](int x) { return Result<int, const char*>(x + 1); });
    MOZ_RELEASE_ASSERT(r2.isOk());
    MOZ_RELEASE_ASSERT(r2.unwrap() == 11);
  }

  // `andThen`ing over success results (constexpr).
  {
    constexpr Result<int, Failed> r2a = Result<int, Failed>{10}.andThen(
        [](int x) { return Result<int, Failed>(x + 1); });
    MOZ_STATIC_AND_RELEASE_ASSERT(r2a.isOk());
    MOZ_STATIC_AND_RELEASE_ASSERT(r2a.inspect() == 11);
  }

  // `andThen`ing over error results.
  {
    Result<int, const char*> r3("error");
    Result<int, const char*> r4 = r3.andThen([](int x) {
      MOZ_RELEASE_ASSERT(false);
      return Result<int, const char*>(1);
    });
    MOZ_RELEASE_ASSERT(r4.isErr());
    MOZ_RELEASE_ASSERT(r3.unwrapErr() == r4.unwrapErr());
  }

  // andThen with a function accepting an rvalue
  {
    Result<int, const char*> r1(10);
    Result<int, const char*> r2 =
        r1.andThen([](int&& x) { return Result<int, const char*>(x + 1); });
    MOZ_RELEASE_ASSERT(r2.isOk());
    MOZ_RELEASE_ASSERT(r2.unwrap() == 11);
  }

  // `andThen`ing over error results (constexpr).
  {
    constexpr Result<int, Failed> r4a =
        Result<int, Failed>{Failed{}}.andThen([](int x) {
          MOZ_RELEASE_ASSERT(false);
          return Result<int, Failed>(1);
        });
    MOZ_STATIC_AND_RELEASE_ASSERT(r4a.isErr());
  }
}

using UniqueResult = Result<UniquePtr<int>, const char*>;

static UniqueResult UniqueTask() { return mozilla::MakeUnique<int>(3); }
static UniqueResult UniqueTaskError() { return Err("bad"); }

using UniqueErrorResult = Result<int, UniquePtr<int>>;
static UniqueErrorResult UniqueError() {
  return Err(mozilla::MakeUnique<int>(4));
}

static Result<Ok, UniquePtr<int>> TryUniqueErrorResult() {
  MOZ_TRY(UniqueError());
  return Ok();
}

static void UniquePtrTest() {
  {
    auto result = UniqueTask();
    MOZ_RELEASE_ASSERT(result.isOk());
    auto ptr = result.unwrap();
    MOZ_RELEASE_ASSERT(ptr);
    MOZ_RELEASE_ASSERT(*ptr == 3);
    auto moved = result.unwrap();
    MOZ_RELEASE_ASSERT(!moved);
  }

  {
    auto err = UniqueTaskError();
    MOZ_RELEASE_ASSERT(err.isErr());
    auto ptr = err.unwrapOr(mozilla::MakeUnique<int>(4));
    MOZ_RELEASE_ASSERT(ptr);
    MOZ_RELEASE_ASSERT(*ptr == 4);
  }

  {
    auto result = UniqueTaskError();
    result = UniqueResult(mozilla::MakeUnique<int>(6));
    MOZ_RELEASE_ASSERT(result.isOk());
    MOZ_RELEASE_ASSERT(result.inspect() && *result.inspect() == 6);
  }

  {
    auto result = UniqueError();
    MOZ_RELEASE_ASSERT(result.isErr());
    MOZ_RELEASE_ASSERT(result.inspectErr());
    MOZ_RELEASE_ASSERT(*result.inspectErr() == 4);
    auto err = result.unwrapErr();
    MOZ_RELEASE_ASSERT(!result.inspectErr());
    MOZ_RELEASE_ASSERT(err);
    MOZ_RELEASE_ASSERT(*err == 4);

    result = UniqueErrorResult(0);
    MOZ_RELEASE_ASSERT(result.isOk() && result.unwrap() == 0);
  }

  {
    auto result = TryUniqueErrorResult();
    MOZ_RELEASE_ASSERT(result.isErr());
    auto err = result.unwrapErr();
    MOZ_RELEASE_ASSERT(err && *err == 4);
    MOZ_RELEASE_ASSERT(!result.inspectErr());
  }
}

/* * */

int main() {
  BasicTests();
  InPlaceConstructionTests();
  TypeConversionTests();
  EmptyValueTest();
  MapTest();
  MapErrTest();
  OrElseTest();
  AndThenTest();
  UniquePtrTest();
  return 0;
}