| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <Tests/Framework/Framework.h>
- #include <Tests/Util/Foo.h>
- #include <AnKi/Util/Function.h>
- ANKI_TEST(Util, Function)
- {
- HeapAllocator<U8> alloc(allocAligned, nullptr);
- // Simple
- {
- Function<I32(I32, F32), 8> 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<Vec4(Vec4, Vec4), 8> 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<void(Foo&)> f(alloc, [foo](Foo& r) { r.x += foo.x; });
- Function<void(Foo&)> 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<void(Foo&)> f(alloc, [foo](Foo& r) { r.x += foo.x; });
- Function<void(Foo&)> 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();
- }
- }
|