2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <folly/Lazy.h>
22 #include <gtest/gtest.h>
29 auto const val = folly::lazy([&]() -> int {
30 EXPECT_EQ(++computeCount, 1);
33 EXPECT_EQ(computeCount, 0);
35 for (int i = 0; i < 100; ++i) {
38 EXPECT_EQ(computeCount, 1);
40 EXPECT_EQ(computeCount, 0);
44 EXPECT_EQ(computeCount, 1);
47 auto globalCount = folly::lazy([]{ return 0; });
48 auto const foo = folly::lazy([]() -> std::string {
49 EXPECT_EQ(++globalCount(), 1);
50 return std::string("YEP");
54 EXPECT_EQ(globalCount(), 0);
55 EXPECT_EQ(foo(), "YEP");
56 EXPECT_EQ(globalCount(), 1);
60 auto lazyMap = folly::lazy([]() -> std::map<std::string,std::string> {
67 EXPECT_EQ(lazyMap().size(), 2);
68 lazyMap()["blah"] = "asd";
69 EXPECT_EQ(lazyMap().size(), 3);
74 CopyCount(const CopyCount&) { ++count; }
75 CopyCount(CopyCount&&) {}
79 bool operator()() const { return true ; }
82 int CopyCount::count = 0;
84 TEST(Lazy, NonLambda) {
85 auto const rval = folly::lazy(CopyCount());
86 EXPECT_EQ(CopyCount::count, 0);
87 EXPECT_EQ(rval(), true);
88 EXPECT_EQ(CopyCount::count, 0);
91 auto const lval = folly::lazy(cpy);
92 EXPECT_EQ(CopyCount::count, 1);
93 EXPECT_EQ(lval(), true);
94 EXPECT_EQ(CopyCount::count, 1);
96 std::function<bool()> f = [&]{ return 12; };
97 auto const lazyF = folly::lazy(f);
98 EXPECT_EQ(lazyF(), true);
102 std::function<int ()> const f = [&] { return 12; };
103 auto lz = folly::lazy(f);