You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As I, again, was bitten by the fact that variables used in e.g. will_set_contents_of_parameter() will need to be live when the call to the mocked function happens (Ouch, segv...), I first documented the problem. As is the programmers first resort.
Keep a copy
Then I started thinking about the reason for this issue, and if and how it could be avoided. And a natural fix would be to let the constraint make a copy of the value and keep that rather than keeping the pointer to it.
This should then of course also extend to will_return_by_value(). Instead of keeping the pointer in the constraint, forced into the CgreenValue in it, it should make a copy and use that.
Unify all types
Then it dawned on me that we could unify all values possible to return from a C function into the same form, doing away with
(memdup being a short-hand for malloc and memcpy).
Deallocation
But then we'd get the same issue, or transfer of responsibility, that we just introduced for return_by_value, the user must deallocate the memory that the constraint allocated. (Described after the Mock macros section in the guide.
If we can solve this deallocation problem transparently, in the mock() macro, then I think this would be a great improvement.
One idea would be to build on the example user code for this problem from the guide:
Boxretrieve_box() {
Box*box_p= (Box*)mock(); /* Get the pointer */Boxthe_box=*box_p; /* Dereference to get a struct */free(box_p); /* Deallocate the returned area */returnthe_box; /* Finally we can return the struct by value */
}
and push it into mock() which would then need to expand to something like:
returning the last statement of a block is a GNU extension AFAIK after a quick google, but clang also supports this statement expression. "inline functions" are supposedly the way to do this in "proper" C but can't be made to work here, I think.
we can't get <type> since C has no mechanism for getting the name of the type of a variable or expression
For 1) we would have to leave non-GNU/Clang support which would be a pity.
For 2) we could add an extra argument to mock() which will be the type name:
<type> func(...) {
return mock(<type), ...);
}
This would do a way with return_by_value() and the whole "boxing" game for doubles. (Pun intended...)
Help and discussion
At this moment I'm just writing this down, it feels to important to get lost.
I'd appreciate any input from other C programmers. Maybe especially from those who have participated in Cgreen development, like @lastcraft, @matthargett, @gardenia, if you are still into C programming.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
The problem
As I, again, was bitten by the fact that variables used in e.g.
will_set_contents_of_parameter()
will need to be live when the call to the mocked function happens (Ouch, segv...), I first documented the problem. As is the programmers first resort.Keep a copy
Then I started thinking about the reason for this issue, and if and how it could be avoided. And a natural fix would be to let the constraint make a copy of the value and keep that rather than keeping the pointer to it.
This should then of course also extend to
will_return_by_value()
. Instead of keeping the pointer in the constraint, forced into the CgreenValue in it, it should make a copy and use that.Unify all types
Then it dawned on me that we could unify all values possible to return from a C function into the same form, doing away with
and just use
Behind the scene whatever gets thrown at
will_return()
would be(
memdup
being a short-hand formalloc
andmemcpy
).Deallocation
But then we'd get the same issue, or transfer of responsibility, that we just introduced for
return_by_value
, the user must deallocate the memory that the constraint allocated. (Described after the Mock macros section in the guide.If we can solve this deallocation problem transparently, in the
mock()
macro, then I think this would be a great improvement.One idea would be to build on the example user code for this problem from the guide:
and push it into
mock()
which would then need to expand to something like:Issues
There are two issues here
statement expression
. "inline functions" are supposedly the way to do this in "proper" C but can't be made to work here, I think.<type>
since C has no mechanism for getting the name of the type of a variable or expressionFor 1) we would have to leave non-GNU/Clang support which would be a pity.
For 2) we could add an extra argument to
mock()
which will be the type name:This would do a way with
return_by_value()
and the whole "boxing" game for doubles. (Pun intended...)Help and discussion
At this moment I'm just writing this down, it feels to important to get lost.
I'd appreciate any input from other C programmers. Maybe especially from those who have participated in Cgreen development, like @lastcraft, @matthargett, @gardenia, if you are still into C programming.
Beta Was this translation helpful? Give feedback.
All reactions