datetime review part 2 [Update 4]

bearophile bearophileHUGS at lycos.com
Tue Nov 9 15:48:30 PST 2010


Yao G.:

> Ugh. The datetime.d file is 1.5 MB? 0_o

It contains too-much-repetitive code like:

        assertEqual(SysTime(DateTime(2010, 8, 1, 23, 59, 59), FracSec.from!"msecs"(999)).dayOfGregorianCal, 733_985);
        assertEqual(SysTime(DateTime(2010, 8, 31, 23, 59, 59), FracSec.from!"msecs"(999)).dayOfGregorianCal, 734_015);
        assertEqual(SysTime(DateTime(2010, 9, 1, 23, 59, 59), FracSec.from!"msecs"(999)).dayOfGregorianCal, 734_016);
        assertEqual(SysTime(DateTime(2010, 9, 30, 23, 59, 59), FracSec.from!"msecs"(999)).dayOfGregorianCal, 734_045);
        assertEqual(SysTime(DateTime(2010, 10, 1, 23, 59, 59), FracSec.from!"msecs"(999)).dayOfGregorianCal, 734_046);
        assertEqual(SysTime(DateTime(2010, 10, 31, 23, 59, 59), FracSec.from!"msecs"(999)).dayOfGregorianCal, 734_076);
        assertEqual(SysTime(DateTime(2010, 11, 1, 23, 59, 59), FracSec.from!"msecs"(999)).dayOfGregorianCal, 734_077);


My suggestions:
- Keep only 5-10% of the tests inside the datetime.d module, and move the other 90-95% in a separated datetime test module.
- Use deterministic (repeatable) random data in a loop to perform some compact fuzzy testing
- Use alias or string mixing to greatly reduce the amount of text contained in the tests. Tuples help a lot. All those lines may be replaced with a single loop that contains something like:
assertEqual(SysTime(DateTime(z0, x1, x2, x3, x4, x5), FracSec.from!"msecs"(x6)).dayOfGregorianCal, x7)
Where the xn data comes from an array of typecons.Tuples like:
Data5(2010, 11, 1, 23, 59, 59, 999, 734_077)

Bye,
bearophile


More information about the Digitalmars-d mailing list