Bug 1883861 - Part 1: Move visitMemoryBarrier into the common CodeGenerator file...
[gecko.git] / xpcom / tests / gtest / TestArrayAlgorithm.cpp
blobfbaf9a6a241df232b30395376ec0f192e30291c4
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "gtest/gtest.h"
9 #include "mozilla/ArrayAlgorithm.h"
10 #include "mozilla/Result.h"
11 #include "mozilla/ResultExtensions.h"
12 #include "nsTArray.h"
14 using namespace mozilla;
15 using std::begin;
16 using std::end;
18 namespace {
19 constexpr static int32_t arr1[3] = {1, 2, 3};
22 TEST(nsAlgorithm_TransformIntoNewArrayAbortOnErr, NoError_Fallible)
24 auto res = TransformIntoNewArrayAbortOnErr(
25 begin(arr1), end(arr1),
26 [](const int32_t value) -> Result<int64_t, nsresult> {
27 return value * 10;
29 fallible);
30 ASSERT_TRUE(res.isOk());
31 const nsTArray<int64_t>& out = res.inspect();
33 const nsTArray<int64_t> expected = {10, 20, 30};
34 ASSERT_EQ(expected, out);
37 TEST(nsAlgorithm_TransformIntoNewArrayAbortOnErr, NoError_Fallible_Range)
39 auto res = TransformIntoNewArrayAbortOnErr(
40 arr1,
41 [](const int32_t value) -> Result<int64_t, nsresult> {
42 return value * 10;
44 fallible);
45 ASSERT_TRUE(res.isOk());
46 const nsTArray<int64_t>& out = res.inspect();
48 const nsTArray<int64_t> expected = {10, 20, 30};
49 ASSERT_EQ(expected, out);
52 TEST(nsAlgorithm_TransformIntoNewArrayAbortOnErr, ErrorOnOther_Fallible)
54 auto res = TransformIntoNewArrayAbortOnErr(
55 begin(arr1), end(arr1),
56 [](const int32_t value) -> Result<int64_t, nsresult> {
57 if (value > 1) {
58 return Err(NS_ERROR_FAILURE);
60 return value * 10;
62 fallible);
63 ASSERT_TRUE(res.isErr());
64 ASSERT_EQ(NS_ERROR_FAILURE, res.inspectErr());
67 TEST(nsAlgorithm_TransformIntoNewArray, NoError)
69 auto res = TransformIntoNewArray(
70 begin(arr1), end(arr1),
71 [](const int32_t value) -> int64_t { return value * 10; });
73 const nsTArray<int64_t> expected = {10, 20, 30};
74 ASSERT_EQ(expected, res);
77 TEST(nsAlgorithm_TransformIntoNewArray, NoError_Range)
79 auto res = TransformIntoNewArray(
80 arr1, [](const int32_t value) -> int64_t { return value * 10; });
82 const nsTArray<int64_t> expected = {10, 20, 30};
83 ASSERT_EQ(expected, res);
86 TEST(nsAlgorithm_TransformIntoNewArray, NoError_Fallible)
88 auto res = TransformIntoNewArray(
89 begin(arr1), end(arr1),
90 [](const int32_t value) -> int64_t { return value * 10; }, fallible);
91 ASSERT_TRUE(res.isOk());
92 const nsTArray<int64_t>& out = res.inspect();
94 const nsTArray<int64_t> expected = {10, 20, 30};
95 ASSERT_EQ(expected, out);
98 TEST(nsAlgorithm_TransformIntoNewArray, NoError_Fallible_Range)
100 auto res = TransformIntoNewArray(
101 arr1, [](const int32_t value) -> int64_t { return value * 10; },
102 fallible);
103 ASSERT_TRUE(res.isOk());
104 const nsTArray<int64_t>& out = res.inspect();
106 const nsTArray<int64_t> expected = {10, 20, 30};
107 ASSERT_EQ(expected, out);