|
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.
|
|
|
|
|
Wales Floods
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
|
|
|
|
|
And still no mention on the Met Office website. But they do refer to flood warnings for England. I guess they need to remove the word "United" from our country's name.
|
|
|
|
|
It (remove United) appears to be headed that way Richard
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
|
|
|
|
|
Not close - about 30 miles away, the other side of a mountain I think.
The river at the bottom of the garden has risen about ten foot or so, but it's still got 15 or so to reach the house. And to flood a field on the other side of the river first.
If we got flooded, I wouldn't worry too much - I'd already have used up my worry quota for the town downstream!
"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!
|
|
|
|
|
one of my (now sporadic) attempts to get a C# language issue discussion going on the "C# Language Forum" has been replied to with this in-depth response from Harold Aptroot: Re: the new Math.Generics ? - C# Discussion Boards[^]. for me, Harold's posts have always been very valuable, prescient.
i feel like i just received a wonderful gift, and i want the "world" to know
p.s. as i hoped i expressed in my query that Harold responded to, i am not complaining about what the C# language forum has, imho, evolved into over the years ...
it is very valuable, and i learn* a lot from the often fulsome and astute responses to what i call "QA for grown-ups" posts by such luminaries, as OriginalGriff, MacCutchan, and Deening, and others !
* learn: often ... given my distance (by choice) from what C# and .NET have become in terms of web-centric, server-side, etc., over the last years: "become aware of" is a better choice than "learn."
best wishes from a past-his-use-by date happy camper
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
I buy gaming starter PCs for work machines because I can get good build quality, and a fast CPU/RAM/storage combo sans GPU for a good price because there's a lot of competition out there. The market for gaming starter machines is good for buyers. Although in my current instance I built from scratch so I could match parts for maximum performance and the features I wanted, but I still used things like a gaming motherboard for example - because it supports overclocked RAM. I run my RAM significantly faster than stock DDR5.
I have an all glass case, which means everything is visible. It looks super classy, but not with lights everywhere.
OH THE LIGHTS. I just paid a pretty penny to get rid of them. I had fans that were a carryover from a gaming starter system i bought last year, and they lit up green. It was awful.
It's so hard to find unlit performance parts these days. I finally got everything but my video card unlit, but you almost have to pay extra for that! All the mid shelf gear is lit it seems like so you either go ultra cheap/low build quality on your fans, or you spring for premium noctuas in plain black. There is not an appealing middle ground. The corsairs for example, of any quality, are all RGB these days. Even if you don't light it, they're still white. Ick.
My next GPU will be a founders edition from nvidia because they are low profile, no lights.
How do you feel about lights on your PC?
Am I the only one that hates them? I can't even stand the popularity of them. I am not quite resentful, but almost there because it makes sourcing the right unlit parts difficult.
To err is human. Fortune favors the monsters.
|
|
|
|
|