|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/util/iterator.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <optional> |
| 24 | +#include <type_traits> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | +#include "iceberg/test/matchers.h" |
| 30 | + |
| 31 | +namespace iceberg { |
| 32 | +namespace { |
| 33 | + |
| 34 | +class CopyOnly { |
| 35 | + public: |
| 36 | + explicit CopyOnly(int value) : value_(value) {} |
| 37 | + |
| 38 | + CopyOnly(const CopyOnly&) = default; |
| 39 | + CopyOnly& operator=(const CopyOnly&) = default; |
| 40 | + CopyOnly(CopyOnly&&) = delete; |
| 41 | + CopyOnly& operator=(CopyOnly&&) = delete; |
| 42 | + |
| 43 | + int value() const { return value_; } |
| 44 | + |
| 45 | + private: |
| 46 | + int value_; |
| 47 | +}; |
| 48 | + |
| 49 | +static_assert(std::is_copy_constructible_v<CopyOnly>); |
| 50 | +static_assert(!std::is_move_constructible_v<CopyOnly>); |
| 51 | + |
| 52 | +// Exercises ToVector() with values that can be copied but not moved. |
| 53 | +class CopyOnlyIterator final : public Iterator<CopyOnly> { |
| 54 | + private: |
| 55 | + Result<std::optional<CopyOnly>> NextImpl() override { |
| 56 | + if (next_ == 3) { |
| 57 | + return Result<std::optional<CopyOnly>>(std::in_place, std::nullopt); |
| 58 | + } |
| 59 | + return Result<std::optional<CopyOnly>>(std::in_place, std::in_place, next_++); |
| 60 | + } |
| 61 | + |
| 62 | + int next_ = 0; |
| 63 | +}; |
| 64 | + |
| 65 | +// Exercises ToVector() with values that can be moved but not copied. |
| 66 | +class MoveOnlyIterator final : public Iterator<std::unique_ptr<int>> { |
| 67 | + public: |
| 68 | + int calls() const { return calls_; } |
| 69 | + |
| 70 | + private: |
| 71 | + Result<std::optional<std::unique_ptr<int>>> NextImpl() override { |
| 72 | + ++calls_; |
| 73 | + if (next_ == 3) { |
| 74 | + return Result<std::optional<std::unique_ptr<int>>>(std::in_place, std::nullopt); |
| 75 | + } |
| 76 | + return Result<std::optional<std::unique_ptr<int>>>(std::in_place, std::in_place, |
| 77 | + std::make_unique<int>(next_++)); |
| 78 | + } |
| 79 | + |
| 80 | + int next_ = 0; |
| 81 | + int calls_ = 0; |
| 82 | +}; |
| 83 | + |
| 84 | +// Exercises ToVector() error propagation after some values have been consumed. |
| 85 | +class FailingIterator final : public Iterator<int> { |
| 86 | + public: |
| 87 | + int calls() const { return calls_; } |
| 88 | + |
| 89 | + private: |
| 90 | + Result<std::optional<int>> NextImpl() override { |
| 91 | + ++calls_; |
| 92 | + if (next_ < 2) { |
| 93 | + return Result<std::optional<int>>(std::in_place, std::in_place, next_++); |
| 94 | + } |
| 95 | + return Invalid("iteration failed"); |
| 96 | + } |
| 97 | + |
| 98 | + int next_ = 0; |
| 99 | + int calls_ = 0; |
| 100 | +}; |
| 101 | + |
| 102 | +static_assert(std::is_move_constructible_v<CopyOnlyIterator>); |
| 103 | +static_assert(std::is_move_assignable_v<CopyOnlyIterator>); |
| 104 | +static_assert(std::is_move_constructible_v<MoveOnlyIterator>); |
| 105 | +static_assert(std::is_move_assignable_v<MoveOnlyIterator>); |
| 106 | + |
| 107 | +TEST(IteratorTest, ToVectorSupportsCopyOnlyValues) { |
| 108 | + CopyOnlyIterator iterator; |
| 109 | + |
| 110 | + ICEBERG_UNWRAP_OR_FAIL(auto values, iterator.ToVector()); |
| 111 | + |
| 112 | + ASSERT_EQ(values.size(), 3); |
| 113 | + EXPECT_EQ(values[0].value(), 0); |
| 114 | + EXPECT_EQ(values[1].value(), 1); |
| 115 | + EXPECT_EQ(values[2].value(), 2); |
| 116 | +} |
| 117 | + |
| 118 | +TEST(IteratorTest, ToVectorSupportsMoveOnlyValues) { |
| 119 | + MoveOnlyIterator iterator; |
| 120 | + |
| 121 | + ICEBERG_UNWRAP_OR_FAIL(auto values, iterator.ToVector()); |
| 122 | + |
| 123 | + ASSERT_EQ(values.size(), 3); |
| 124 | + EXPECT_EQ(*values[0], 0); |
| 125 | + EXPECT_EQ(*values[1], 1); |
| 126 | + EXPECT_EQ(*values[2], 2); |
| 127 | +} |
| 128 | + |
| 129 | +TEST(IteratorTest, NextRemainsAtEndAfterExhaustion) { |
| 130 | + MoveOnlyIterator iterator; |
| 131 | + ICEBERG_UNWRAP_OR_FAIL(auto values, iterator.ToVector()); |
| 132 | + ASSERT_EQ(values.size(), 3); |
| 133 | + EXPECT_EQ(iterator.calls(), 4); |
| 134 | + |
| 135 | + for (int i = 0; i < 2; ++i) { |
| 136 | + auto result = iterator.Next(); |
| 137 | + ASSERT_TRUE(result.has_value()); |
| 138 | + EXPECT_FALSE(result->has_value()); |
| 139 | + } |
| 140 | + EXPECT_EQ(iterator.calls(), 4); |
| 141 | +} |
| 142 | + |
| 143 | +TEST(IteratorTest, ToVectorPropagatesErrorsAfterPartialConsumption) { |
| 144 | + FailingIterator iterator; |
| 145 | + |
| 146 | + ICEBERG_UNWRAP_OR_FAIL(auto first, iterator.Next()); |
| 147 | + ASSERT_TRUE(first.has_value()); |
| 148 | + EXPECT_EQ(first.value(), 0); |
| 149 | + |
| 150 | + auto result = iterator.ToVector(); |
| 151 | + |
| 152 | + EXPECT_THAT(result, IsError(ErrorKind::kInvalid)); |
| 153 | + EXPECT_THAT(result, HasErrorMessage("iteration failed")); |
| 154 | +} |
| 155 | + |
| 156 | +TEST(IteratorTest, NextRepeatsErrorWithoutAdvancing) { |
| 157 | + FailingIterator iterator; |
| 158 | + auto first_error = iterator.ToVector(); |
| 159 | + EXPECT_THAT(first_error, IsError(ErrorKind::kInvalid)); |
| 160 | + EXPECT_THAT(first_error, HasErrorMessage("iteration failed")); |
| 161 | + EXPECT_EQ(iterator.calls(), 3); |
| 162 | + |
| 163 | + for (int i = 0; i < 2; ++i) { |
| 164 | + auto result = iterator.Next(); |
| 165 | + EXPECT_THAT(result, IsError(ErrorKind::kInvalid)); |
| 166 | + EXPECT_THAT(result, HasErrorMessage("iteration failed")); |
| 167 | + } |
| 168 | + EXPECT_EQ(iterator.calls(), 3); |
| 169 | +} |
| 170 | + |
| 171 | +} // namespace |
| 172 | +} // namespace iceberg |
0 commit comments