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"
14 using namespace mozilla
;
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
> {
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(
41 [](const int32_t value
) -> Result
<int64_t, nsresult
> {
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
> {
58 return Err(NS_ERROR_FAILURE
);
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; },
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
);