|
Wordle 575 3/6*
β¬π¨π©π¨π¨
π¨β¬π©π¨π©
π©π©π©π©π©
Happiness will never come to those who fail to appreciate what they already have. -Anon
|
|
|
|
|
Wordle 575 3/6
β¬β¬π¨β¬π©
π©β¬β¬π©π©
π©π©π©π©π©
Yes
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
I don't do TDD, but maybe you do?
If you do, do you include a test to ensure that calling a function with the same input a second time returns the same result, e.g. f(x) == f(x) ?
Doing so would have saved me a bunch of time and anxiety this morning when I found that last summer I had introduced a bug into a function which I very rarely call a second time in any of my utilities.
As it turns out, the function uses a static buffer area for temporary storage and I had forgotten to clear it.
And it remained undetected until today, when I rebuilt probably the only utility I have which calls the function twice.
modified 14-Jan-23 20:13pm.
|
|
|
|
|
I don't.
What's the purpose of the static buffer, if you have to clear it on exit?
BTW: go functional!
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Not having to reallocate it on each call. Depending on the input, it can get large. And I clear it on enter (unless I forget to).
The use of the buffer is to improve the performance of worst-case data.
|
|
|
|
|
PIEBALDconsult wrote: Not having to reallocate it on each call. This begs the question, why would that matter since you call the function only once in most of your utilities?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Because it may also get called millions of times in other applications.
|
|
|
|
|
Agree with CPallini on functional style where possible.
If you know the method you are unit testing has static storage or side effects, then I think you are on the right path to invoke it multiple times in the same test.
For true TDD, you should not know if there are statics,etc.
So use the βSeparationβ (per Apple TV) process when switching from Dev role to tester role.
|
|
|
|
|
englebart wrote: you should not know if there are statics
While that is probably true, it still remains that I had a bug in the function which would have been caught by calling the function twice with the same input and comparing the results. A test of the function would have caught it so that higher-level callers don't need to know.
|
|
|
|
|
PIEBALDconsult wrote: I don't do TDD, but maybe you do?
No but I still use unit tests. I did that before unit test frameworks existed and before TDD existed.
PIEBALDconsult wrote: calling a function with the same input a second time
No.
PIEBALDconsult wrote: As it turns out, the function uses a static buffer area for temporary storage and I had forgotten to clear
Except of course if I wrote a method like that then yes I would write appropriate unit tests to test that.
|
|
|
|
|
Just so you know, I'm not hardcore TDD or BDD evangelist. I think it's ok to not write your tests first as you figure stuff out and get a feel for the app. But, make no mistake... I 100% believe there should be at least unit tests in a project. You wanna make the argument that a functional test may or may not be required... cool ok. But, nowhere under the sun should a project not be covered with unit tests.
Ahem, now with that out the way. To actually address your question...
It depends. Is your function a pure function? If it's using temp storage or an external dependency (resource, not lib), it's not. Typically, I design my unit tests to say f(x) == f(x) for pure functions. In your case I would not. I'd still make sure the tests always pass and are about to allocate resources (mocked or otherwise), but I wouldn't assume X in always equals X out. I would make sure the unit tests around that function handle all error scenarios however.
If you have "pure" logic that you must test inside that routine, then and only then would I mock the external resource inside the routine and test just the logic itself with f(x) == f(x) in mind. For the rest of the test coverage on that routine you can do it the non-pure way.
It's dancing around it though since technically it's not a pure function, but at may at least give you the best of both worlds in this instance.
Jeremy Falcon
modified 15-Jan-23 15:25pm.
|
|
|
|
|
Hmm, well, as to "purity"... not technically, but in the spirit of it.
I could write the function so that it allocates a new local buffer on each call -- then it would be pure.
The issue I have with that is when a large buffer is required many times. I'd rather use a static buffer so that repeated calls (if any) don't have to keep allocating large buffers; they can re-use a buffer which has already been allocated for the largest value so far encountered.
Although my original post involves a scenario in which this function gets called once (or twice, rarely) with small values (short strings), the function might get called millions of times with strings of thousands of characters in the worst-case scenario.
So I have the buffer static. And I clear it upon entering the function (unless I forget to) rather than at the end.
If I clear it at the end, that might be seen as "more pure", in that it leaves the buffer the way it found it.
But I see no point in clearing it before the end of the function; clearing it at the beginning makes more sense to me.
Clearing the buffer takes hardly any effort, so it's not like there is a performance difference in when it gets cleared.
Having said that, in some other situations, clearing a full buffer may require more effort than clearing an empty buffer, so maybe needlessly clearing an empty buffer on the first call is better than needlessly clearing a full buffer on the last. :shrug:
At any rate, I'm not about to change the function just so that it fully qualifies as a pure function; there are bigger fish to fry.
|
|
|
|
|
PIEBALDconsult wrote: The issue I have with that is when a large buffer is required many times. I'd rather use a static buffer so that repeated calls (if any) don't have to keep allocating large buffers; they can re-use a buffer which has already been allocated for the largest value so far encountered. Oh, my point wasn't so much about not using a static buffer. There are plenty of use cases where a static buffer on the heap and/or local static variable on the heap make total sense. Just saying what I do in each circumstance.
PIEBALDconsult wrote: Although my original post involves a scenario in which this function gets called once (or twice, rarely) with small values (short strings), the function might get called millions of times with strings of thousands of characters in the worst-case scenario. Sure, so in this case the routine wouldn't be pure as you can't guarantee that memory will be allocated initially... especially if it's a large chunk. I mean, we could get pedantic and say the same thing about the stack too, but you get the idea. Usually if the stack is corrupt that program is gonna crash anyway. Not saying don't do it... saying I would test it as a non-pure function.
PIEBALDconsult wrote: At any rate, I'm not about to change the function just so that it fully qualifies as a pure function; there are bigger fish to fry. Yeah dude, you completely misread my entire post. Try again.
Jeremy Falcon
|
|
|
|
|
#Worldle #358 4/6 (100%)
β¬β¬β¬β¬β¬β¬
οΈ
π©π©β¬β¬β¬βοΈ
π©π©β¬β¬β¬βοΈ
π©π©π©π©π©π
ββ
https://worldle.teuteuf.fr
after 3 very bad guesses I had to use map
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
Unfortunately it will be beyond my current lifetime when QM is added to AI . What an incredible world that will be . Will they be conscious ? friendly ? Will they be willing to do the laundry ? Can't wait for "The Orville" next season to begin . My favorite character is Isaac . Incidentally my favorite all time film characters are Hal 9000 and Dr. Strangelove .
|
|
|
|
|
|
nevermind
"A little time, a little trouble, your better day"
Badfinger
|
|
|
|
|
QM == Quasimodo Metaphysics ?
Β«The mind is not a vessel to be filled but a fire to be kindledΒ» Plutarch
|
|
|
|
|
|
... a fun way to describe identity theft?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
BT:DT
Letter from IRS, "dear sir your SSN has been compromised. Go directly to jail, do not pass Go, do not collect $200"
Doesn't seem as funny when it is you. I, however, can now laugh about it. Didn't cost me any money, just anxiety.
>64
Some days the dragon wins. Suck it up.
|
|
|
|
|
Are the floods anywhere near you Paul ?
Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming βWow! What a Ride!" - Hunter S Thompson - RIP
|
|
|
|
|
Apart from a puddle on my drive, nothing much up here at the posh end. 
|
|
|
|
|
Not much in Addlestone either(no change there then ) I'm seeing bad flooding in Wales is why I asked
Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming βWow! What a Ride!" - Hunter S Thompson - RIP
|
|
|
|
|
Nothing about floods in Wales on the Met Office website.
|
|
|
|