Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A dynamic unit test framework in C

Reply
Thread Tools

A dynamic unit test framework in C

 
 
ImpalerCore
Guest
Posts: n/a
 
      09-23-2011
On Sep 23, 12:03*pm, ImpalerCore <(E-Mail Removed)> wrote:
> On Sep 22, 11:19*pm, MC Andre <(E-Mail Removed)> wrote:
>

[snip]
>
> \code
> int c_rand_int( int n )
> {
> * int limit = RAND_MAX - RAND_MAX % n;
> * int r;
>
> * do {
> * * r = rand();
> * } while ( r >= limit );
>
> * return r % n;
> }
>
> \endcode
>


[snip]

I made a subtle mistake ... in the use of the c_rand_int() function,
the modulus goes from [0...N).

\code
void rndset( void* p, size_t n )
{
unsigned char* ucp = (unsigned char*)p;

while ( n-- != 0 ) {
*ucp++ = (unsigned char)c_rand_int( UCHAR_MAX + 1 );
^^^
}
}
\endcode

UCHAR_MAX for 8-bit characters is 255, which implies that the value
from 'r % 255' will be from 0...254. Replace UCHAR_MAX with UCHAR_MAX
+ 1 and CHAR_MAX with CHAR_MAX + 1.
 
Reply With Quote
 
 
 
 
ImpalerCore
Guest
Posts: n/a
 
      09-23-2011
On Sep 23, 12:29*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> ImpalerCore <(E-Mail Removed)> writes:
>


[snip]
>
> To be really picky, it's problematic even for integer types. *You
> probably don't want to generate trap representations with a probability
> determined by the number of padding bits. *In fact, you may want to avoid
> them altogether. *Also, for integer systems that permit two (non-trap)
> zero representations, the result won't be "value" uniform since 0 will
> be (ever so slightly) more common. *Of course, having both zeros
> represented might be seen as an advantage when testing.


I just discovered that my 'c_rand_int( 1000000 + 1 )' was infinite
looping because my 'limit' was negative. It appears that it needs
some adjustment for when INT_MAX > RAND_MAX, in my case RAND_MAX =
32767. Is there an alternative to filling bytes that doesn't tread
into risky territory?
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-24-2011
ImpalerCore <(E-Mail Removed)> writes:

> On Sep 23, 12:29¬*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
>> ImpalerCore <(E-Mail Removed)> writes:
>>

>
> [snip]
>>
>> To be really picky, it's problematic even for integer types. ¬*You
>> probably don't want to generate trap representations with a probability
>> determined by the number of padding bits. ¬*In fact, you may want to avoid
>> them altogether. ¬*Also, for integer systems that permit two (non-trap)
>> zero representations, the result won't be "value" uniform since 0 will
>> be (ever so slightly) more common. ¬*Of course, having both zeros
>> represented might be seen as an advantage when testing.

>
> I just discovered that my 'c_rand_int( 1000000 + 1 )' was infinite
> looping because my 'limit' was negative. It appears that it needs
> some adjustment for when INT_MAX > RAND_MAX, in my case RAND_MAX =
> 32767. Is there an alternative to filling bytes that doesn't tread
> into risky territory?


If the risky territory is the possibility of padding bits and so on,
then I think you need to take a value-based approach. For example, one
could start with uniform double in [0, 1) and use that to get a uniform
int in [0. INT_MAX]. There's risky territory here also, because INT_MAX
might exceed the range of exact integers that double can represent.

Most good random number generators produce random bits (but if
RAND_MAX+1 is not a power of 2 this will probably not be true) so you
can often just take bytes from rand() and put them where you need them.
This is equivalent to what you were doing with % (UCHAR_MAX + 1). The
point about the bits being random is simply that you don't need to worry
about bias when reducing the range to some smaller number of bits.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
contradicting unit test regarding blocks pass, bug in unit/test? timr Ruby 2 11-20-2010 06:30 AM
Test::Unit - Ruby Unit Testing Framework Questions Bill Mosteller Ruby 0 10-22-2009 02:02 PM
All of us know about unit test. But what's a unit? Bill David Java 2 06-18-2008 12:40 AM
unit--, a unit test framework for C++ VvanN C++ 5 04-28-2006 10:01 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments