1 module quantities.runtime;
2 
3 public import quantities.runtime.qvariant;
4 public import quantities.runtime.parsing;
5 
6 ///
7 unittest
8 {
9     import quantities.runtime;
10     import quantities.si;
11 
12     // Note: the types of the predefined SI units (gram, mole, liter...)
13     // are Quantity instances, not QVariant instance.
14 
15     // Introductory example
16     {
17         // I have to make a new solution at the concentration of 5 mmol/L
18         QVariant!double concentration = 5.0 * milli(mole)/liter;
19 
20         // The final volume is 100 ml.
21         QVariant!double volume = 100.0 * milli(liter);
22 
23         // The molar mass of my compound is 118.9 g/mol
24         QVariant!double molarMass = 118.9 * gram/mole;
25 
26         // What mass should I weigh?
27         QVariant!double mass = concentration * volume * molarMass;
28         writefln("Weigh %s of substance", mass); 
29         // prints: Weigh 5.945e-05 [M] of substance
30         // Wait! That's not really useful!
31         writefln("Weigh %s of substance", siFormat("%.1f mg", mass));
32         // prints: Weigh 59.5 mg of substance
33     }
34 
35     // Working with predefined units
36     {
37         QVariant!double distance = 384_400 * kilo(meter);
38         QVariant!double speed = 299_792_458  * meter/second;
39         QVariant!double time = distance / speed;
40         writefln("Travel time of light from the moon: %s", siFormat("%.3f s", time));
41     }
42 
43     // Dimensional correctness
44     {
45         import std.exception : assertThrown;
46         QVariant!double mass = 4 * kilogram;
47         assertThrown!DimensionException(mass + meter);
48         assertThrown!DimensionException(mass == 1.2);
49     }
50 
51     // Create a new unit from the predefined ones
52     {
53         QVariant!double inch = 2.54 * centi(meter);
54         QVariant!double mile = 1609 * meter;
55         writefln("There are %s inches in a mile", mile.value(inch));
56         // NB. Cannot use siFormat, because inches are not SI units
57     }
58 
59     // Create a new unit with new dimensions
60     {
61         // Create a new base unit of currency
62         QVariant!double euro = unit!double("C"); // C is the chosen dimension symol (for currency...)
63 
64         QVariant!double dollar = euro / 1.35;
65         QVariant!double price = 2000 * dollar;
66         writefln("This computer costs €%.2f", price.value(euro));
67     }
68 
69     // Run-time parsing
70     {
71         auto data = [
72             "distance-to-the-moon": "384_400 km",
73             "speed-of-light": "299_792_458 m/s"
74         ];
75         QVariant!double distance = parseSI(data["distance-to-the-moon"]);
76         QVariant!double speed = parseSI(data["speed-of-light"]);
77         QVariant!double time = distance / speed;
78         writefln("Travel time of light from the moon: %s", siFormat("%.3f s", time));
79     }
80 }
81 
82 version (unittest)
83 {
84     version (QuantitiesPrintTests)
85         import std.stdio : writefln;
86     else
87         private void writefln(T...)(T args) {}
88 }