Originally Posted by
iMalc
There is more to the solution to issue 1 than you'd think though. If you use random numbers equally distributed over the entire range of an int then you'll get very few numbers close to 0 and as such might not test any one or two digit cases for example.
You can either test say -9999 to 9999 explicitly first (along with other specific cases like min and max), or you can generate the values from a different distribution such as normal distribution.
With number 3, in the real world it could be handled by documentation perhaps in the form of comments that the buffer must always have room for 15 characters (1 minus sign, 10 digits, 3 commas, 1 null terminator).
Probably tests based on some range between -n to +n, plus the particular cases
like zero, int_min, int_max, plus a random serie of numbers in 3-4 different ranges
could be exaustive enough.
With number 3 I used the "always room for 15 characters" option.
@Elysia
So x64 cut it down to 265 ms? Nice.
Yes, X64 option is a bit faster even on my IA32 machine. :-)
Your real X64 machine should give an impressive 80-90 ms for
iMalc version. Did you test it?
And a last thing. Your version can spare some ms getting rid of
the bool neg altogheter. Without you have:
Code:
Testing version : Elysia
------------------------------
Testing on Intel Core 2 Duo E6600 2.4 Ghz IA32
OS = Windows 7 Ultimate 64 bit -- Compiler = Pelles C 6.00.4
The value of num is: -1234567890
The formatted value of num is: -1,234,567,890
Elapsed time: 897 ms to perform 10,000,000 cycles
-------------------------------------------------------
handling 0 ---> 0
handling 12 ---> 12
handling 256 ---> 256
handling 1000 --> 1,000
handling 1000000 ---> 1,000,000
handling 1000000000 ---> 1,000,000,000
handling 2147483647 -> 2,147,483,647
handling -2147483648 -> -2,147,483,648
Changing the appropriate code:
Code:
if (num < 0)
{
// neg = true;
copy_to++;
buffer[0] = '-';
newnum = -num; // transform number to positive if negative
}