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 "mozilla/Assertions.h"
8 #include "mozilla/IntegerRange.h"
12 using mozilla::IntegerRange
;
13 using mozilla::Reversed
;
15 const size_t kMaxNumber
= 50;
16 const size_t kArraySize
= 256;
18 template <typename IntType
>
19 static IntType
GenerateNumber() {
20 return static_cast<IntType
>(rand() % kMaxNumber
+ 1);
23 template <typename IntType
>
24 static void TestSingleParamRange(const IntType aN
) {
25 IntType array
[kArraySize
];
27 for (auto i
: IntegerRange(aN
)) {
28 static_assert(std::is_same_v
<decltype(i
), IntType
>,
29 "type of the loop var and the param should be the same");
33 MOZ_RELEASE_ASSERT(ptr
- array
== static_cast<ptrdiff_t>(aN
),
34 "Should iterates N items");
35 for (size_t i
= 0; i
< static_cast<size_t>(aN
); i
++) {
36 MOZ_RELEASE_ASSERT(array
[i
] == static_cast<IntType
>(i
),
37 "Values should equal to the index");
41 template <typename IntType
>
42 static void TestSingleParamReverseRange(const IntType aN
) {
43 IntType array
[kArraySize
];
45 for (auto i
: Reversed(IntegerRange(aN
))) {
46 static_assert(std::is_same_v
<decltype(i
), IntType
>,
47 "type of the loop var and the param should be the same");
51 MOZ_RELEASE_ASSERT(ptr
- array
== static_cast<ptrdiff_t>(aN
),
52 "Should iterates N items");
53 for (size_t i
= 0; i
< static_cast<size_t>(aN
); i
++) {
54 MOZ_RELEASE_ASSERT(array
[i
] == static_cast<IntType
>(aN
- i
- 1),
55 "Values should be the reverse of their index");
59 template <typename IntType
>
60 static void TestSingleParamIntegerRange() {
61 const auto kN
= GenerateNumber
<IntType
>();
62 TestSingleParamRange
<IntType
>(0);
63 TestSingleParamReverseRange
<IntType
>(0);
64 TestSingleParamRange
<IntType
>(kN
);
65 TestSingleParamReverseRange
<IntType
>(kN
);
68 template <typename IntType1
, typename IntType2
>
69 static void TestDoubleParamRange(const IntType1 aBegin
, const IntType2 aEnd
) {
70 IntType2 array
[kArraySize
];
71 IntType2
* ptr
= array
;
72 for (auto i
: IntegerRange(aBegin
, aEnd
)) {
73 static_assert(std::is_same_v
<decltype(i
), IntType2
>,
74 "type of the loop var "
75 "should be same as that of the second param");
79 MOZ_RELEASE_ASSERT(ptr
- array
== static_cast<ptrdiff_t>(aEnd
- aBegin
),
80 "Should iterates (aEnd - aBegin) times");
81 for (size_t i
= 0; i
< static_cast<size_t>(aEnd
- aBegin
); i
++) {
82 MOZ_RELEASE_ASSERT(array
[i
] == static_cast<IntType2
>(aBegin
+ i
),
83 "Should iterate integers in [aBegin, aEnd) in order");
87 template <typename IntType1
, typename IntType2
>
88 static void TestDoubleParamReverseRange(const IntType1 aBegin
,
89 const IntType2 aEnd
) {
90 IntType2 array
[kArraySize
];
91 IntType2
* ptr
= array
;
92 for (auto i
: Reversed(IntegerRange(aBegin
, aEnd
))) {
93 static_assert(std::is_same_v
<decltype(i
), IntType2
>,
94 "type of the loop var "
95 "should be same as that of the second param");
99 MOZ_RELEASE_ASSERT(ptr
- array
== static_cast<ptrdiff_t>(aEnd
- aBegin
),
100 "Should iterates (aEnd - aBegin) times");
101 for (size_t i
= 0; i
< static_cast<size_t>(aEnd
- aBegin
); i
++) {
103 array
[i
] == static_cast<IntType2
>(aEnd
- i
- 1),
104 "Should iterate integers in [aBegin, aEnd) in reverse order");
108 template <typename IntType1
, typename IntType2
>
109 static void TestDoubleParamIntegerRange() {
110 const auto kStart
= GenerateNumber
<IntType1
>();
111 const auto kEnd
= static_cast<IntType2
>(kStart
+ GenerateNumber
<IntType2
>());
112 TestDoubleParamRange(kStart
, static_cast<IntType2
>(kStart
));
113 TestDoubleParamReverseRange(kStart
, static_cast<IntType2
>(kStart
));
114 TestDoubleParamRange(kStart
, kEnd
);
115 TestDoubleParamReverseRange(kStart
, kEnd
);
119 TestSingleParamIntegerRange
<int8_t>();
120 TestSingleParamIntegerRange
<int16_t>();
121 TestSingleParamIntegerRange
<int32_t>();
122 TestSingleParamIntegerRange
<int64_t>();
124 TestSingleParamIntegerRange
<uint8_t>();
125 TestSingleParamIntegerRange
<uint16_t>();
126 TestSingleParamIntegerRange
<uint32_t>();
127 TestSingleParamIntegerRange
<uint64_t>();
129 TestDoubleParamIntegerRange
<int8_t, int8_t>();
130 TestDoubleParamIntegerRange
<int16_t, int16_t>();
131 TestDoubleParamIntegerRange
<int32_t, int32_t>();
132 TestDoubleParamIntegerRange
<int64_t, int64_t>();
134 TestDoubleParamIntegerRange
<uint8_t, uint8_t>();
135 TestDoubleParamIntegerRange
<uint16_t, uint16_t>();
136 TestDoubleParamIntegerRange
<uint32_t, uint32_t>();
137 TestDoubleParamIntegerRange
<uint64_t, uint64_t>();
139 TestDoubleParamIntegerRange
<int8_t, int16_t>();
140 TestDoubleParamIntegerRange
<int16_t, int32_t>();
141 TestDoubleParamIntegerRange
<int32_t, int64_t>();
142 TestDoubleParamIntegerRange
<int64_t, int8_t>();
144 TestDoubleParamIntegerRange
<uint8_t, uint64_t>();
145 TestDoubleParamIntegerRange
<uint16_t, uint8_t>();
146 TestDoubleParamIntegerRange
<uint32_t, uint16_t>();
147 TestDoubleParamIntegerRange
<uint64_t, uint32_t>();