¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Re: [TDD] Testing a class which utilizes randomness

 

¿ªÔÆÌåÓý

Hi Nikola,

On Apr 4, 2014, at 6:53 PM, Nikola Novak <reasonsharp@...> wrote:

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.

Sounds like a good start. I¡¯ve not built this but my intuition is telling me that the design would like to have a PuttingAway object that holds strings, and a MakingStrings object that builds random strings, and some structure, perhaps a PuttingAwayUnique object but perhaps not.

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:
?

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:
?

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.

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.


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: 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:
?

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] How to TDD JavaScript so it thinks it is in a browser.

 

¿ªÔÆÌåÓý

Have you tried webdriver + phantomjs?

---
Nayan Hajratwala ... @nhajratw ... 734.658.6032 ...

On Apr 4, 2014, at 5:35 PM, Ian <hobson42@...> wrote:

?

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


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:
?

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.

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.


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

 

¿ªÔÆÌåÓý

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.

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.


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:
?

Jarod,


On Apr 4, 2014, at 1:55 PM, Jarod Eells <jarod+yahoo@...> wrote:

Testing 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.

I¡¯d advise a little research on this topic. There are issues of runs, alternation, patterns, and many others. It¡¯s quite a subject.

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

 

¿ªÔÆÌåÓý

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.

I think not. OP said:

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.

To test duplicate removal, one need not test the RNG at all. It¡¯s the STRINGS that have to not be duplicated. It will take many rolls of the RNG to get one string. So at first it seems that what one must do is generate the random string and then test whether it is duplicated.

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


On Fri, Apr 4, 2014 at 1:55 PM, Jarod Eells <jarod+yahoo@...> wrote:

?

On Fri, Apr 04, 2014 at 12:23:49PM -0400, Mark Levison wrote:
> Desirable? - by definition - randomness can and should occasionally
> duplicate something. Testing a random number generator is very hard.
>
> Cheers
> Mark

Testing 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




--
Mark Levison?| 1 (877) 248-8277 |??|??|?
Certified ScrumMaster Training:??|??|??|??|?
?|?
Proud Sponsor of??and?


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.
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.

I¡¯d advise a little research on this topic. There are issues of runs, alternation, patterns, and many others. It¡¯s quite a subject.

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 occasionally
duplicate something. Testing a random number generator is very hard.

Cheers
Mark
Testing 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.?

Don¡¯t test the RNG. Test the string store.

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:

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?

One could:

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
 




On Fri, Apr 4, 2014 at 2:29 AM, Nikola Novak <reasonsharp@...> wrote:
?

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.

Ever? ?Is that even possible?

Desirable? - by definition - randomness can and should occasionally duplicate something. Testing a random number generator is very hard.?

Cheers
Mark
Mark Levison?| 1 (877) 248-8277 |??|??|?
Certified ScrumMaster Training:??|??|??|??|?
?|?
Proud Sponsor of??and?


Re: [TDD] Testing a class which utilizes randomness

 




On Fri, Apr 4, 2014 at 2:29 AM, Nikola Novak <reasonsharp@...> wrote:
?

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.

Ever? ?Is that even possible?
?
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?


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".

?
Kind regards,
Nikola



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

[Test]
public void GeneratesRandomStrings()
{
? var list = new List
?var generator = new RandomStringGenerator();
? for(int i=0; i< [reasonableLength]; i++)
??? list.add(generator.Next());

? // verify uniqueness of each item of the list
? // e.g. create a Set with the elements and verify set.Count == list.Count;

}


On Fri, Apr 4, 2014 at 7:58 PM, David Rosenstrauch <darose@...> wrote:
?

On 04/04/2014 05:29 AM, Nikola Novak wrote:
> 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

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



Re: [TDD] Testing a class which utilizes randomness

 

On 04/04/2014 05:29 AM, Nikola Novak wrote:
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

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