| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // 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>
- namespace anki
- {
- static I32 functionAcceptingFunction(const Function<I32(F32)>& f)
- {
- return f(1.0f) + f(2.0f);
- }
- } // end namespace anki
- 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);
- }
- // No templates
- {
- F32 f = 1.1f;
- Function<I32(F32)> func(alloc, [&f](F32 ff) {
- f += 2.0f;
- return I32(f) + I32(ff);
- });
- const I32 o = functionAcceptingFunction(func);
- func.destroy(alloc);
- ANKI_TEST_EXPECT_EQ(o, 11);
- }
- // 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();
- }
- }
|