1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#include <algorithm>
19#include <cstddef>
20#include <cstdint>
21#include <vector>
22
23#include <gtest/gtest.h>
24
25#include "arrow/test-util.h"
26#include "arrow/util/lazy.h"
27
28namespace arrow {
29
30class TestLazyIter : public ::testing::Test {
31 public:
32 int64_t kSize = 1000;
33 void SetUp() {
34 randint(kSize, 0, 1000000, &source_);
35 target_.resize(kSize);
36 }
37
38 protected:
39 std::vector<int> source_;
40 std::vector<int> target_;
41};
42
43TEST_F(TestLazyIter, TestIncrementCopy) {
44 auto add_one = [this](int64_t index) { return source_[index] + 1; };
45 auto lazy_range = internal::MakeLazyRange(add_one, kSize);
46 std::copy(lazy_range.begin(), lazy_range.end(), target_.begin());
47
48 for (int64_t index = 0; index < kSize; ++index) {
49 ASSERT_EQ(source_[index] + 1, target_[index]);
50 }
51}
52
53TEST_F(TestLazyIter, TestPostIncrementCopy) {
54 auto add_one = [this](int64_t index) { return source_[index] + 1; };
55 auto lazy_range = internal::MakeLazyRange(add_one, kSize);
56 auto iter = lazy_range.begin();
57 auto end = lazy_range.end();
58 auto target_iter = target_.begin();
59
60 while (iter != end) {
61 *(target_iter++) = *(iter++);
62 }
63
64 for (size_t index = 0, limit = source_.size(); index != limit; ++index) {
65 ASSERT_EQ(source_[index] + 1, target_[index]);
66 }
67}
68} // namespace arrow
69