1- #include < gtest/gtest.h>
21#include < audioapi/utils/FatFunction.hpp>
2+ #include < gtest/gtest.h>
33#include < cstddef>
44#include < memory>
5- #include < utility>
65#include < stdexcept>
76#include < type_traits>
7+ #include < utility>
88
99using namespace audioapi ;
1010
@@ -24,20 +24,24 @@ class FatFunctionTest : public ::testing::Test {
2424};
2525
2626TEST (FatFunctionTest, BasicFunctionality) {
27- FatFunction<64 , IntOp> add = [](int a, int b) { return a + b; };
27+ FatFunction<64 , IntOp> add = [](int a, int b) {
28+ return a + b;
29+ };
2830 EXPECT_EQ (add (2 , 3 ), 5 );
2931 // FatFunction contains both raw storage and function pointers.
3032 // The compiler may add tail padding so each instance still satisfies
3133 // alignof(std::max_align_t), especially when objects are placed in arrays.
3234 // We therefore compare against the aligned size, not just the raw field sum.
33- constexpr size_t dataSize = 64 + sizeof (void *) * 3 ;
35+ constexpr size_t dataSize = 64 + sizeof (void *) * 3 ;
3436 constexpr size_t alignment = alignof (std::max_align_t );
3537 constexpr size_t expectedSize = ((dataSize + alignment - 1 ) / alignment) * alignment;
3638 EXPECT_EQ (sizeof (add), expectedSize);
3739}
3840
3941TEST (FatFunctionTest, MoveSemantics) {
40- FatFunction<64 , IntOp> add = [](int a, int b) { return a + b; };
42+ FatFunction<64 , IntOp> add = [](int a, int b) {
43+ return a + b;
44+ };
4145 FatFunction<64 , IntOp> movedAdd = std::move (add);
4246 EXPECT_EQ (movedAdd (4 , 5 ), 9 );
4347 EXPECT_THROW (add (1 , 2 ), std::bad_function_call); // Original should be empty
@@ -46,25 +50,29 @@ TEST(FatFunctionTest, MoveSemantics) {
4650TEST (FatFunctionTest, Release) {
4751 int destructorCalls = 0 ;
4852 struct Tracked {
49- int * counter;
50- explicit Tracked (int * c) : counter(c) {}
51- int operator ()(int a, int b) const { return a + b; }
52- ~Tracked () { (*counter)++; }
53+ int *counter;
54+ explicit Tracked (int *c) : counter(c) {}
55+ int operator ()(int a, int b) const {
56+ return a + b;
57+ }
58+ ~Tracked () {
59+ (*counter)++;
60+ }
5361 };
5462
5563 {
56- FatFunction<64 , IntOp> add = Tracked (&destructorCalls);
57- // we comment this because compiler can optimize it and do not call destructor here
58- // EXPECT_EQ(destructorCalls, 1); // Destructor called for the temporary Tracked object, but not for the one inside add
59- destructorCalls = 0 ; // Reset counter after construction
60- auto [storage, deleter] = add.release ();
61- EXPECT_GT (storage.size (), 0 );
62- EXPECT_NE (deleter, nullptr );
63-
64- // We can call the deleter to clean up resources if needed
65- if (deleter) {
66- deleter (storage.data ());
67- }
64+ FatFunction<64 , IntOp> add = Tracked (&destructorCalls);
65+ // we comment this because compiler can optimize it and do not call destructor here
66+ // EXPECT_EQ(destructorCalls, 1); // Destructor called for the temporary Tracked object, but not for the one inside add
67+ destructorCalls = 0 ; // Reset counter after construction
68+ auto [storage, deleter] = add.release ();
69+ EXPECT_GT (storage.size (), 0 );
70+ EXPECT_NE (deleter, nullptr );
71+
72+ // We can call the deleter to clean up resources if needed
73+ if (deleter) {
74+ deleter (storage.data ());
75+ }
6876 } // FatFunction goes out of scope here, but it was released so it shouldn't destroy the object again
6977
7078 EXPECT_EQ (destructorCalls, 1 ); // Destructor should have been called exactly once from the deleter
@@ -76,12 +84,16 @@ TEST(FatFunctionTest, EmptyFunctionCall) {
7684}
7785
7886TEST (FatFunctionTest, SwapFunctions) {
79- FatFunction<64 , IntOp> add = [](int a, int b) { return a + b; };
80- FatFunction<64 , IntOp> multiply = [](int a, int b) { return a * b; };
87+ FatFunction<64 , IntOp> add = [](int a, int b) {
88+ return a + b;
89+ };
90+ FatFunction<64 , IntOp> multiply = [](int a, int b) {
91+ return a * b;
92+ };
8193
8294 std::swap (add, multiply);
8395
84- EXPECT_EQ (add (2 , 3 ), 6 ); // Now add should multiply
96+ EXPECT_EQ (add (2 , 3 ), 6 ); // Now add should multiply
8597 EXPECT_EQ (multiply (2 , 3 ), 5 ); // Now multiply should add
8698}
8799
@@ -110,14 +122,18 @@ TEST(FatFunctionTest, TriviallyMoveableCallable) {
110122}
111123
112124TEST (FatFunctionTest, SmallerToLargerMove) {
113- FatFunction<32 , IntOp> smallFunc = [](int a, int b) { return a + b; };
125+ FatFunction<32 , IntOp> smallFunc = [](int a, int b) {
126+ return a + b;
127+ };
114128 FatFunction<64 , IntOp> largeFunc = std::move (smallFunc);
115129 EXPECT_EQ (largeFunc (2 , 3 ), 5 );
116130 EXPECT_THROW (smallFunc (1 , 2 ), std::bad_function_call); // Original should be empty
117131}
118132
119133TEST (FatFunctionTest, LargerToSmallerMove) {
120- FatFunction<64 , IntOp> largeFunc = [](int a, int b) { return a * b; };
134+ FatFunction<64 , IntOp> largeFunc = [](int a, int b) {
135+ return a * b;
136+ };
121137 // This should fail to compile because largeFunc exceeds the storage size of smallFunc
122138 bool isConstructible = std::is_constructible_v<FatFunction<32 , IntOp>, decltype (largeFunc)>;
123139 EXPECT_FALSE (isConstructible);
@@ -128,21 +144,24 @@ TEST(FatFunctionTest, SmallerToLargerMoveWithNonTrivialMoveAndDestruct) {
128144 int moverCalled = 0 ;
129145
130146 struct NonTrivialCallable {
131- int * dCounter;
132- int * mCounter ;
147+ int * dCounter;
148+ int * mCounter ;
133149
134- NonTrivialCallable (int * d, int * m) : dCounter(d), mCounter (m) {}
150+ NonTrivialCallable (int * d, int * m) : dCounter(d), mCounter (m) {}
135151
136152 int operator ()(int a, int b) const {
137153 return a + b;
138154 }
139155 ~NonTrivialCallable () {
140- if (dCounter) (*dCounter)++;
156+ if (dCounter)
157+ (*dCounter)++;
141158 }
142- NonTrivialCallable (NonTrivialCallable&& other) : dCounter(other.dCounter), mCounter (other.mCounter ) {
143- if (mCounter ) (*mCounter )++;
159+ NonTrivialCallable (NonTrivialCallable &&other)
160+ : dCounter(other.dCounter), mCounter (other.mCounter ) {
161+ if (mCounter )
162+ (*mCounter )++;
144163 }
145- NonTrivialCallable (const NonTrivialCallable&) = delete ; // Non-copyable
164+ NonTrivialCallable (const NonTrivialCallable &) = delete ; // Non-copyable
146165 };
147166
148167 {
@@ -172,9 +191,9 @@ TEST(FatFunctionTest, MutableLambdaBasic) {
172191 counter++;
173192 return a + b + counter;
174193 };
175- EXPECT_EQ (func (1 , 2 ), 4 ); // 1 + 2 + 1
176- EXPECT_EQ (func (1 , 2 ), 5 ); // 1 + 2 + 2
177- EXPECT_EQ (func (1 , 2 ), 6 ); // 1 + 2 + 3
194+ EXPECT_EQ (func (1 , 2 ), 4 ); // 1 + 2 + 1
195+ EXPECT_EQ (func (1 , 2 ), 5 ); // 1 + 2 + 2
196+ EXPECT_EQ (func (1 , 2 ), 6 ); // 1 + 2 + 3
178197}
179198
180199TEST (FatFunctionTest, MutableLambdaWithUniquePtr) {
0 commit comments