LazyTest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #include <folly/Lazy.h>
  17. #include <functional>
  18. #include <iostream>
  19. #include <map>
  20. #include <folly/portability/GTest.h>
  21. namespace folly {
  22. TEST(Lazy, Simple) {
  23. int computeCount = 0;
  24. auto const val = folly::lazy([&]() -> int {
  25. ++computeCount;
  26. EXPECT_EQ(computeCount, 1);
  27. return 12;
  28. });
  29. EXPECT_EQ(computeCount, 0);
  30. for (int i = 0; i < 100; ++i) {
  31. if (i > 50) {
  32. EXPECT_EQ(val(), 12);
  33. EXPECT_EQ(computeCount, 1);
  34. } else {
  35. EXPECT_EQ(computeCount, 0);
  36. }
  37. }
  38. EXPECT_EQ(val(), 12);
  39. EXPECT_EQ(computeCount, 1);
  40. }
  41. auto globalCount = folly::lazy([] { return 0; });
  42. auto const foo = folly::lazy([]() -> std::string {
  43. ++globalCount();
  44. EXPECT_EQ(globalCount(), 1);
  45. return std::string("YEP");
  46. });
  47. TEST(Lazy, Global) {
  48. EXPECT_EQ(globalCount(), 0);
  49. EXPECT_EQ(foo(), "YEP");
  50. EXPECT_EQ(globalCount(), 1);
  51. }
  52. TEST(Lazy, Map) {
  53. auto lazyMap = folly::lazy([]() -> std::map<std::string, std::string> {
  54. return {
  55. {"foo", "bar"},
  56. {"baz", "quux"},
  57. };
  58. });
  59. EXPECT_EQ(lazyMap().size(), 2);
  60. lazyMap()["blah"] = "asd";
  61. EXPECT_EQ(lazyMap().size(), 3);
  62. }
  63. struct CopyCount {
  64. CopyCount() {}
  65. CopyCount(const CopyCount&) {
  66. ++count;
  67. }
  68. CopyCount(CopyCount&&) noexcept {}
  69. static int count;
  70. bool operator()() const {
  71. return true;
  72. }
  73. };
  74. int CopyCount::count = 0;
  75. TEST(Lazy, NonLambda) {
  76. auto const rval = folly::lazy(CopyCount());
  77. EXPECT_EQ(CopyCount::count, 0);
  78. EXPECT_EQ(rval(), true);
  79. EXPECT_EQ(CopyCount::count, 0);
  80. CopyCount cpy;
  81. auto const lval = folly::lazy(cpy);
  82. EXPECT_EQ(CopyCount::count, 1);
  83. EXPECT_EQ(lval(), true);
  84. EXPECT_EQ(CopyCount::count, 1);
  85. std::function<bool()> f = [&] { return 12; };
  86. auto const lazyF = folly::lazy(f);
  87. EXPECT_EQ(lazyF(), true);
  88. }
  89. TEST(Lazy, Consty) {
  90. std::function<int()> const f = [&] { return 12; };
  91. auto lz = folly::lazy(f);
  92. EXPECT_EQ(lz(), 12);
  93. }
  94. } // namespace folly