1 /**
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
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
16 #include "util/lazy.h"
17
18 #include <vector>
19 #include <unordered_set>
20
21 #include <gtest/gtest.h>
22
23 namespace ark::verifier::test {
24
TEST(Verifier,Lazy)25 TEST(Verifier, Lazy)
26 {
27 std::vector<int> testData {1, 2, 3, -1, -2, -3, 5};
28
29 auto stream1 = LazyFetch(testData);
30 auto calcFunc = [](int acc, int val) { return acc + val; };
31 auto result1 = FoldLeft(stream1, -4, calcFunc);
32
33 EXPECT_EQ(result1, 1);
34
35 EXPECT_EQ(FoldLeft(ConstLazyFetch(testData), -3, calcFunc), 2);
36
37 // NOLINTNEXTLINE(readability-magic-numbers)
38 auto result2 = FoldLeft(Transform(ConstLazyFetch(testData), [](int val) { return val * 10; }), -49, calcFunc);
39 EXPECT_EQ(result2, 1);
40
41 auto result3 = FoldLeft(Filter(ConstLazyFetch(testData), [](int val) { return val > 0; }), -1, calcFunc);
42 EXPECT_EQ(result3, 10);
43
44 auto result4 =
45 FoldLeft(Enumerate(ConstLazyFetch(testData)), 0, [](int acc, auto val) { return acc + std::get<0>(val); });
46 EXPECT_EQ(result4, 21);
47
48 auto result5 = FoldLeft(IndicesOf(testData), 0, calcFunc);
49 EXPECT_EQ(result5, 21);
50
51 int sum = 0;
52 ForEach(ConstLazyFetch(testData), [&sum](int val) { sum += val; });
53 EXPECT_EQ(sum, 5);
54
55 int num = 0;
56 for (const auto val : Iterable(Filter(ConstLazyFetch(testData), [](int val) { return val > 0; }))) {
57 EXPECT_TRUE(val > 0);
58 ++num;
59 }
60 EXPECT_EQ(num, 4);
61
62 auto result6 = FoldLeft(ConstLazyFetch(testData) + ConstLazyFetch(testData), 0, calcFunc);
63 EXPECT_EQ(result6, 10);
64
65 auto result7 = ContainerOf<std::set<int>>(ConstLazyFetch(testData));
66 EXPECT_EQ(result7, (std::set<int> {1, 2, 3, -1, -2, -3, 5}));
67
68 auto result8 = ContainerOf<std::vector<int>>(ConstLazyFetch(testData));
69 EXPECT_EQ(result8, (std::vector<int> {1, 2, 3, -1, -2, -3, 5}));
70 }
71
72 } // namespace ark::verifier::test
73