| 1 |
// Copyright (c) 2025 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "source/util/index_range.h" |
| 16 |
|
| 17 |
#include "gmock/gmock.h" |
| 18 |
|
| 19 |
namespace spvtools { |
| 20 |
namespace utils { |
| 21 |
namespace { |
| 22 |
|
| 23 |
using IndexRangeTest = ::testing::Test; |
| 24 |
|
| 25 |
using ushort = unsigned short; |
| 26 |
|
| 27 |
TEST(IndexRangeTest, Initialize_Default) { |
| 28 |
double sentinel_a = 0.0; |
| 29 |
double sentinel_b = 1.0; |
| 30 |
const IndexRange<double, unsigned, ushort> ir; |
| 31 |
|
| 32 |
EXPECT_EQ(ir.first(), unsigned(0)); |
| 33 |
EXPECT_EQ(ir.count(), ushort(0)); |
| 34 |
EXPECT_TRUE(ir.empty()); |
| 35 |
|
| 36 |
auto span_null = ir.apply(nullptr); |
| 37 |
EXPECT_EQ(span_null.data(), nullptr); |
| 38 |
EXPECT_EQ(span_null.size(), 0); |
| 39 |
EXPECT_TRUE(span_null.empty()); |
| 40 |
|
| 41 |
auto span_a = ir.apply(&sentinel_a); |
| 42 |
EXPECT_EQ(span_a.data(), &sentinel_a); |
| 43 |
EXPECT_EQ(span_a.size(), 0); |
| 44 |
EXPECT_TRUE(span_a.empty()); |
| 45 |
|
| 46 |
auto span_b = ir.apply(&sentinel_b); |
| 47 |
EXPECT_EQ(span_b.data(), &sentinel_b); |
| 48 |
EXPECT_EQ(span_b.size(), 0); |
| 49 |
EXPECT_TRUE(span_b.empty()); |
| 50 |
} |
| 51 |
|
| 52 |
TEST(IndexRangeTest, Initialize_NonEmpty) { |
| 53 |
const IndexRange<double, unsigned, ushort> ir(1, 2); |
| 54 |
|
| 55 |
EXPECT_EQ(ir.first(), unsigned(1)); |
| 56 |
EXPECT_EQ(ir.count(), ushort(2)); |
| 57 |
EXPECT_FALSE(ir.empty()); |
| 58 |
|
| 59 |
auto span_null = ir.apply(nullptr); |
| 60 |
EXPECT_EQ(span_null.data(), nullptr); |
| 61 |
EXPECT_EQ(span_null.size(), 0); |
| 62 |
EXPECT_TRUE(span_null.empty()); |
| 63 |
|
| 64 |
double arr[] = {0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0}; |
| 65 |
|
| 66 |
auto span_a = ir.apply(arr); |
| 67 |
EXPECT_EQ(span_a.begin(), arr + 1); |
| 68 |
EXPECT_EQ(span_a.end(), arr + 3); |
| 69 |
EXPECT_FALSE(span_a.empty()); |
| 70 |
|
| 71 |
auto span_b = ir.apply(arr + 3); |
| 72 |
EXPECT_EQ(span_b.begin(), arr + 4); |
| 73 |
EXPECT_EQ(span_b.end(), arr + 6); |
| 74 |
EXPECT_FALSE(span_a.empty()); |
| 75 |
} |
| 76 |
|
| 77 |
} // namespace |
| 78 |
} // namespace utils |
| 79 |
} // namespace spvtools |