Keyboard Shortcuts
Likes
- Testdrivendevelopment
- Messages
Search
Re: [TDD] Testing a class which utilizes randomness
¿ªÔÆÌåÓýHi Nikola,On Apr 4, 2014, at 6:53 PM, Nikola Novak <reasonsharp@...> wrote:
If I¡¯m right, there will be no need to create random strings for purposes of putting away uniquely: you can work with strings like ¡°Hello¡±, ¡°Goodbye¡±, and ¡°Hello¡±. If I¡¯m right, that will make the PuttingAway tests simpler. And that will be better. So I recommend pushing in that direction, trying a few ways, and seeing what you come up with. Regards, Ron Jeffries I know we always like to say it'll be easier to do it now than it will be to do it later. Not likely. I plan to be smarter later than I am now, so I think it'll be just as easy later, maybe even easier. Why pay now when we can pay later? |
||
Re: [TDD] Testing a class which utilizes randomness
Nikola Novak
Hello! Thanks everyone for your replies. Like I said, I'm not testing the RNG (or RSG in this case), but rather the algorithm which deals with duplicates. I'll go with what David and John suggested, writing a class to generate unique random strings and injecting a random string generator.
Interesting how a TDD approach resulted in code being organized properly. If I hadn't used it, I'd probably have written a method that used an embedded random string generator and would never have given the problem a second thought.
Thanks for the help, Nikola On Fri, Apr 4, 2014 at 8:52 PM, Steven Gordon <sgordonphd@...> wrote:
|
||
Re: Testing a class which utilizes randomness
Hello!
Thanks everyone for your replies. Like I said, I'm not testing the RNG (or RSG in this case), but rather the algorithm which deals with duplicates. I'll go with what David and John suggested, writing a class to generate unique random strings and injecting a random string generator. Interesting how a TDD approach resulted in code being organized properly. If I hadn't used it, I'd probably have written a method that used an embedded random string generator and would never have given the problem a second thought. Thanks for the help, Nikola |
||
Re: [TDD] How to TDD JavaScript so it thinks it is in a browser.
There are a bunch of different ways to do it. I recommend Jasmine as a test framework. It has a fluent, RSpec-like syntax and is decoupled from its runner enough to run almost everywhere (There are dozens of other frameworks and tools that play nice with it.)?
You can use something like jsdom or one of a number of alternatives to stub out a dom and verify code that runs against it. Those run ridiculously fast on Node.js and are a good first pass for basic correctness. However, you are still going to want to have a few integration tests to make sure that critical things do run in the various browsers.?
You also want to decouple your domain logic from your dom manipulation. This is exactly the same as in any other OO language. The language in your code should be about what the user is trying to accomplish and details about the dom manipulation should be in small composed methods underneath that.? On Fri, Apr 4, 2014 at 2:35 PM, Ian <hobson42@...> wrote:
|
||
Re: [TDD] How to TDD JavaScript so it thinks it is in a browser.
toggle quoted message
Show quoted text
On Apr 4, 2014, at 5:35 PM, Ian <hobson42@...> wrote:
|
||
How to TDD JavaScript so it thinks it is in a browser.
Hi all,
How can I set up some programmed tests to test JavaScript, so it thinks its in a browser? The JavaScript code modifies the style objects and the DOM. Have tried various utilities that claim to test JavaScript in the browser, and they have proved impractically slow. (A paste of a 25 character string took over 3 minutes!) So I guess the JavaScript has to be run elsewhere (WSH?) and made to think it is in a browser, but I have no ideas how to do this. I can use Windows or Linux - (No mac :( ). Thanks Ian |
||
Re: [TDD] Testing a class which utilizes randomness
Agreed. Already suggested that the" history" object is what needs to be TDD-ed. The fact that randomness is not a good candidate for TDD-ing is just an interesting side-note.
On Fri, Apr 4, 2014 at 11:50 AM, Ron Jeffries <ronjeffries@...> wrote:
|
||
Re: [TDD] Testing a class which utilizes randomness
¿ªÔÆÌåÓýI¡¯m pretty sure OP is trying to learn how to TDD, not how to create really great random strings.:) R On Apr 4, 2014, at 2:48 PM, Steven Gordon <sgordonphd@...> wrote: This project is presumably using an RNG rather than coding one. Ron Jeffries I know we always like to say it'll be easier to do it now than it will be to do it later. Not likely. I plan to be smarter later than I am now, so I think it'll be just as easy later, maybe even easier. Why pay now when we can pay later? |
||
Re: [TDD] Testing a class which utilizes randomness
This project is presumably using an RNG rather than coding one. The criteria that matters would be whether the strings being generated (using an RNG) is sufficiently random, not whether the sequence of numbers that are being used to generate those strings are sufficiently random.
It is quite possible for the RNG to be random enough, but the strings to not be. ?For example, the long term behavior of the random number generator could always create a nice random distribution on bins, but that subsequences are not as random. If each string would be created by a subsequence rather than individual number, then they would not be random enough.
It could make a difference whether or not the size of the character set is prime. On Fri, Apr 4, 2014 at 11:36 AM, Ron Jeffries <ronjeffries@...> wrote:
|
||
Re: [TDD] Testing a class which utilizes randomness
¿ªÔÆÌåÓýMark,On Apr 4, 2014, at 2:39 PM, Mark Levison <mark@...> wrote: All you've done is test with you've approximately built a random number generator. My underlying point was the OP might want to choose a slightly saner problem. What I want to test is exactly this behavior - the code which discards the duplicate and forces the string to be regenerated. To test that, I need to force the random string generator to return a duplicate at least once (but finite number of times). To create the test which would generate strings until there's at least one duplicate is unrealistic, because such a test could take a very long time to complete. But no. The code partitions into getting a string and seeing whether it is duplicated and putting it away if it is not. The source of the string is irrelevant to the other two. You can test those two, however they are partitioned, by storing Pig, Dog, Chicken, Dog and making sure you only get Pig Dog and Chicken.? In fact, I¡¯d just check that I had three, if the code shaped up like I expect it would. Or even just test isThisAlreadyThere, depending on how it flows.? In no case is it necessary to look at the RNG. The issue isn¡¯t in not creating a duplicate, it¡¯s in not storing it. Offhand, I can¡¯t think of a good way to prevent creating a duplicate, since AAAAAB != AAAAAC. Ron Jeffries I'm not bad, I'm just drawn that way. ?-- Jessica Rabbit
|
||
Re: [TDD] Testing a class which utilizes randomness
Mark Levison
All you've done is test with you've approximately built a random number generator. My underlying point was the OP might want to choose a slightly saner problem.
Cheers Mark
--
|
||
Re: [TDD] Testing a class which utilizes randomness
¿ªÔÆÌåÓýJarod,On Apr 4, 2014, at 1:55 PM, Jarod Eells <jarod+yahoo@...> wrote: Testing a RNG doesn't have to be hard. Ron Jeffries Sometimes I give myself admirable advice, but I am incapable of taking it. -- Mary Wortley Montagu |
||
Re: [TDD] Testing a class which utilizes randomness
On Fri, Apr 04, 2014 at 12:23:49PM -0400, Mark Levison wrote:
Desirable? - by definition - randomness can and should occasionallyTesting a RNG doesn't have to be hard. Just have to roll the dice a sufficient number of times and bin out the results. Then check that the bins are filled according to the expected statistical distribution. If you want a higher accuracy for your test then make more bins and/or roll more numbers. Jarod Eells |
||
Re: [TDD] Testing a class which utilizes randomness
¿ªÔÆÌåÓýAll ¡On Apr 4, 2014, at 12:23 PM, Mark Levison <mark@...> wrote: Desirable? - by definition - randomness can and should occasionally duplicate something. Testing a random number generator is very hard.? Ron Jeffries You never know what is enough unless you know what is more than enough. -- William Blake
|
||
Re: [TDD] Testing a class which utilizes randomness
¿ªÔÆÌåÓýHi Nikola,On Apr 4, 2014, at 5:29 AM, Nikola Novak <reasonsharp@...> wrote:
Have a method boolean containsString(aString) and test that. Have a method putIfAbsent(aString) and test that (by putting the same string twice and observing that I still only have one string. Or something approximately like that. I generally try to avoid something like boolean putString(aString) that returns whether it accepted the string or not, because I try to avoid methods that return a value and do something. I might put up with it but probably not. There are more ways ... Ron Jeffries I have two cats, and a big house full of cat stuff.? The cats fight and divide up the house, messing up their own lives.? Nice work cats.? Meow. |
||
Re: [TDD] Testing a class which utilizes randomness
Mark Levison
Desirable? - by definition - randomness can and should occasionally duplicate something. Testing a random number generator is very hard.?
Cheers Mark
|
||
Re: [TDD] Testing a class which utilizes randomness
On Fri, Apr 4, 2014 at 2:29 AM, Nikola Novak <reasonsharp@...> wrote:
Ever? ?Is that even possible? ?
There must be some part of the code that "remembers" what it has previously generated and checks whether the string just generated is among them. ?So, shouldn't this part of the code be put into a separate function with its own unit tests. ?This function must have the candidate string as a parameter and the history as the class or parameter, so you can TDD this logic with whatever inputs your unit tests needs, right?
That tests whether the function does what it is supposed to. ?You would also have to TDD that the history structure is being correctly built. ?If you then want to also test whether all the code works together, if you can inject the length of the strings and the character set they are generated from, then you can be sure that for a very small length and character set, duplicates will get generated quickly enough to validate that it really works.
More interesting is how you could test whether the strings generated are sufficiently "random". ?
|
||
Re: [TDD] Testing a class which utilizes randomness
Donaldson, John
¿ªÔÆÌåÓýNikola, ? In this case, you have two things happening: random strings, and unique strings. ? Let¡¯s suppose your unique random string generator is called UniqueStringGenerator. You need to inject the random generator ¨C don¡¯t embed it in the class itself ¨C in this way you can control what is generated when needed (in your test). ? For example your constructor might say: public UniqueStringGenerator ( IRandomGenerator randomGenerator). Then you need to create the interface IRandomGenerator. There will be a method something like getRandom() or something similar. ? In your duplicate name test, create a DuplicateTestRandomGenerator object which implements IRandomGenerator. In DuplicateRandomTestGenerator just return the same random string from getRandom(). Inject it into the UniqueStringGenerator. Run your test. ? Of course, when you want to really use UniqueStringGenerator, you instantiate it with an object which implements IRandomGenerator ¨C but which really generates random strings. The real RandomGenerator gets tested in other tests. ? So, two principals: -????????? Inject things which you need to control them for testing (random number generators or clocks for example) -????????? Separate responsibilities: the UniqueStringGenerator is only responsible for handing out a unique string (it will check against previously generated strings). The RandomGenerator is only responsible for generating a random string. ? Hmm! More writing than I expected. I hope it¡¯s clear. ? John D. ? From: testdrivendevelopment@... [mailto:testdrivendevelopment@...]
On Behalf Of Nikola Novak
Sent: 04 April 2014 11:29 To: testdrivendevelopment@... Subject: [TDD] Testing a class which utilizes randomness ?
Hello! ? I'm new to TDD and this group. For practice, I started a simple project and now I want to create a list of random strings, but such that no two strings are ever the same. In a very unlikely, yet possible case, when two randomly generated strings are the same, the method that does this should discard the duplicate and generate a new string before it returns. ? What I want to test is exactly this behavior - the code which discards the duplicate and forces the string to be regenerated. To test that, I need to force the random string generator to return a duplicate at least once (but finite number of times). To create the test which would generate strings until there's at least one duplicate is unrealistic, because such a test could take a very long time to complete. ? How would I write such a test? ? Kind regards, Nikola
|
||
Re: [TDD] Testing a class which utilizes randomness
DR I think Nikola wants to test the real implementation of the random string generator off the top of my head{ ? var list = new List ?var generator = new RandomStringGenerator(); ? for(int i=0; i< [reasonableLength]; i++)} On Fri, Apr 4, 2014 at 7:58 PM, David Rosenstrauch <darose@...> wrote:
|
||
Re: [TDD] Testing a class which utilizes randomness
On 04/04/2014 05:29 AM, Nikola Novak wrote:
Hello! Trying abstracting away the notion of the random string generator, into an interface/parent class just called "string generator". Then make 2 subclasses of that: your existing random string generator, and a "test string generator" that can return a predictable, deterministic sequence of values. For your unit tests, you would pass in an instance of the test string generator, while in the production code you would use the random string generator. Your code would work the same no matter which string generator it gets passed. But by using the test string generator in your unit tests you could control exactly the values being generated, and in that way make sure your program is handling all the edge cases correctly. HTH, DR |