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 {
31 EXPECT_EQ(computeCount, 1);
34 EXPECT_EQ(computeCount, 0);
36 for (int i = 0; i < 100; ++i) {
39 EXPECT_EQ(computeCount, 1);
41 EXPECT_EQ(computeCount, 0);
45 EXPECT_EQ(computeCount, 1);
48 auto globalCount = folly::lazy([]{ return 0; });
49 auto const foo = folly::lazy([]() -> std::string {
51 EXPECT_EQ(globalCount(), 1);
52 return std::string("YEP");
56 EXPECT_EQ(globalCount(), 0);
57 EXPECT_EQ(foo(), "YEP");
58 EXPECT_EQ(globalCount(), 1);
62 auto lazyMap = folly::lazy([]() -> std::map<std::string,std::string> {
69 EXPECT_EQ(lazyMap().size(), 2);
70 lazyMap()["blah"] = "asd";
71 EXPECT_EQ(lazyMap().size(), 3);
76 CopyCount(const CopyCount&) { ++count; }
77 CopyCount(CopyCount&&) noexcept {}
81 bool operator()() const { return true ; }
84 int CopyCount::count = 0;
86 TEST(Lazy, NonLambda) {
87 auto const rval = folly::lazy(CopyCount());
88 EXPECT_EQ(CopyCount::count, 0);
89 EXPECT_EQ(rval(), true);
90 EXPECT_EQ(CopyCount::count, 0);
93 auto const lval = folly::lazy(cpy);
94 EXPECT_EQ(CopyCount::count, 1);
95 EXPECT_EQ(lval(), true);
96 EXPECT_EQ(CopyCount::count, 1);
98 std::function<bool()> f = [&]{ return 12; };
99 auto const lazyF = folly::lazy(f);
100 EXPECT_EQ(lazyF(), true);
104 std::function<int ()> const f = [&] { return 12; };
105 auto lz = folly::lazy(f);