// Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors. // All rights reserved. // Code licensed under the BSD License. // http://www.anki3d.org/LICENSE #include #include #include ANKI_TEST(Util, Function) { HeapAllocator alloc(allocAligned, nullptr); // Simple { Function f; I32 i = 0; f.init(alloc, [&i](U32 a, F32 b) -> I32 { i += I32(a) + I32(b); return i; }); i = 1; f(2, 10.0f); ANKI_TEST_EXPECT_EQ(i, 13); f.destroy(alloc); } // Allocated { const Vec4 a(1.9f); const Vec4 b(2.2f); Function f(alloc, [a, b](Vec4 c, Vec4 d) mutable -> Vec4 { return a + c * 2.0f + b * d; }); const Vec4 r = f(Vec4(10.0f), Vec4(20.8f)); ANKI_TEST_EXPECT_EQ(r, a + Vec4(10.0f) * 2.0f + b * Vec4(20.8f)); f.destroy(alloc); } // Complex { { Foo foo, bar; Function f(alloc, [foo](Foo& r) { r.x += foo.x; }); Function ff; ff = std::move(f); ff(bar); ANKI_TEST_EXPECT_EQ(bar.x, 666 * 2); ff.destroy(alloc); } ANKI_TEST_EXPECT_EQ(Foo::constructorCallCount, Foo::destructorCallCount); Foo::reset(); } // Copy { { Foo foo, bar; Function f(alloc, [foo](Foo& r) { r.x += foo.x; }); Function ff; ff.copy(f, alloc); ff(bar); ANKI_TEST_EXPECT_EQ(bar.x, 666 * 2); ff.destroy(alloc); f.destroy(alloc); } ANKI_TEST_EXPECT_EQ(Foo::constructorCallCount, Foo::destructorCallCount); Foo::reset(); } }