1 /**
2 * Copyright (c) 2021-2022 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 panda::verifier::test {
24
TEST(Verifier,Lazy)25 TEST(Verifier, Lazy)
26 {
27 std::vector<int> test_data {1, 2, 3, -1, -2, -3, 5};
28
29 auto stream1 = LazyFetch(test_data);
30 auto calc_func = [](int acc, int val) { return acc + val; };
31 auto result1 = FoldLeft(stream1, -4, calc_func);
32
33 EXPECT_EQ(result1, 1);
34
35 EXPECT_EQ(FoldLeft(ConstLazyFetch(test_data), -3, calc_func), 2);
36
37 auto result2 = FoldLeft(Transform(ConstLazyFetch(test_data), [](int val) { return val * 10; }), -49, calc_func);
38 EXPECT_EQ(result2, 1);
39
40 auto result3 = FoldLeft(Filter(ConstLazyFetch(test_data), [](int val) { return val > 0; }), -1, calc_func);
41 EXPECT_EQ(result3, 10);
42
43 auto result4 =
44 FoldLeft(Enumerate(ConstLazyFetch(test_data)), 0, [](int acc, auto val) { return acc + std::get<0>(val); });
45 EXPECT_EQ(result4, 21);
46
47 auto result5 = FoldLeft(IndicesOf(test_data), 0, calc_func);
48 EXPECT_EQ(result5, 21);
49
50 int sum = 0;
51 ForEach(ConstLazyFetch(test_data), [&sum](int val) { sum += val; });
52 EXPECT_EQ(sum, 5);
53
54 int num = 0;
55 for (const auto val : Iterable(Filter(ConstLazyFetch(test_data), [](int val) { return val > 0; }))) {
56 EXPECT_TRUE(val > 0);
57 ++num;
58 }
59 EXPECT_EQ(num, 4);
60
61 auto result6 = FoldLeft(ConstLazyFetch(test_data) + ConstLazyFetch(test_data), 0, calc_func);
62 EXPECT_EQ(result6, 10);
63
64 auto result7 = ContainerOf<std::set<int>>(ConstLazyFetch(test_data));
65 EXPECT_EQ(result7, (std::set<int> {1, 2, 3, -1, -2, -3, 5}));
66
67 auto result8 = ContainerOf<std::vector<int>>(ConstLazyFetch(test_data));
68 EXPECT_EQ(result8, (std::vector<int> {1, 2, 3, -1, -2, -3, 5}));
69 }
70
71 } // namespace panda::verifier::test
72