From 72231250ed81e10d66bfe70701e64fa5fe50f712 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Wed, 15 Jun 2016 23:09:44 +0200 Subject: Publish --- vespalib/src/tests/explore_modern_cpp/.gitignore | 1 + .../src/tests/explore_modern_cpp/CMakeLists.txt | 8 ++++++ vespalib/src/tests/explore_modern_cpp/FILES | 1 + .../explore_modern_cpp/explore_modern_cpp_test.cpp | 31 ++++++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 vespalib/src/tests/explore_modern_cpp/.gitignore create mode 100644 vespalib/src/tests/explore_modern_cpp/CMakeLists.txt create mode 100644 vespalib/src/tests/explore_modern_cpp/FILES create mode 100644 vespalib/src/tests/explore_modern_cpp/explore_modern_cpp_test.cpp (limited to 'vespalib/src/tests/explore_modern_cpp') 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 + +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 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 fun2 = closure_ref; + fun2(); + EXPECT_EQUAL(13u, value); // +4 + closure(); + EXPECT_EQUAL(17u, value); // +4 (fun2 had a copy of count) + std::function 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(); } -- cgit v1.2.3