diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespalib/src/tests/explore_modern_cpp |
Publish
Diffstat (limited to 'vespalib/src/tests/explore_modern_cpp')
4 files changed, 41 insertions, 0 deletions
diff --git a/vespalib/src/tests/explore_modern_cpp/.gitignore b/vespalib/src/tests/explore_modern_cpp/.gitignore new file mode 100644 index 00000000000..202178fb850 --- /dev/null +++ b/vespalib/src/tests/explore_modern_cpp/.gitignore @@ -0,0 +1 @@ +vespalib_explore_modern_cpp_test_app diff --git a/vespalib/src/tests/explore_modern_cpp/CMakeLists.txt b/vespalib/src/tests/explore_modern_cpp/CMakeLists.txt new file mode 100644 index 00000000000..d2406224a05 --- /dev/null +++ b/vespalib/src/tests/explore_modern_cpp/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_explore_modern_cpp_test_app + SOURCES + explore_modern_cpp_test.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_explore_modern_cpp_test_app COMMAND vespalib_explore_modern_cpp_test_app) diff --git a/vespalib/src/tests/explore_modern_cpp/FILES b/vespalib/src/tests/explore_modern_cpp/FILES new file mode 100644 index 00000000000..e5522821b16 --- /dev/null +++ b/vespalib/src/tests/explore_modern_cpp/FILES @@ -0,0 +1 @@ +explore_modern_cpp_test.cpp diff --git a/vespalib/src/tests/explore_modern_cpp/explore_modern_cpp_test.cpp b/vespalib/src/tests/explore_modern_cpp/explore_modern_cpp_test.cpp new file mode 100644 index 00000000000..05703b15641 --- /dev/null +++ b/vespalib/src/tests/explore_modern_cpp/explore_modern_cpp_test.cpp @@ -0,0 +1,31 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/vespalib/testkit/test_kit.h> + +TEST("verify how std::function copies lambda closures") { + size_t count = 0; + size_t value = 0; + auto closure = [count,&value]()mutable{ ++count; value += count; }; + closure(); + EXPECT_EQUAL(0u, count); + EXPECT_EQUAL(1u, value); // +1 + closure(); + EXPECT_EQUAL(3u, value); // +2 + std::function<void()> fun = closure; + fun(); + EXPECT_EQUAL(6u, value); // +3 + closure(); + EXPECT_EQUAL(9u, value); // +3 (fun had a copy of count) + auto &closure_ref = closure; + std::function<void()> fun2 = closure_ref; + fun2(); + EXPECT_EQUAL(13u, value); // +4 + closure(); + EXPECT_EQUAL(17u, value); // +4 (fun2 had a copy of count) + std::function<void()> fun3 = std::ref(closure); + fun3(); + EXPECT_EQUAL(22u, value); // +5 + closure(); + EXPECT_EQUAL(28u, value); // +6 (fun only had a copy of the wrapper) +} + +TEST_MAIN() { TEST_RUN_ALL(); } |