1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
//! Types and constants for handling volumes (that is, three-dimensional space, not loudness).

use super::measurement::*;
#[cfg(feature = "from_str")]
use regex::Regex;
#[cfg(feature = "from_str")]
use std::str::FromStr;

/// The `Volume` struct can be used to deal with volumes in a common way.
///
/// #Example
///
/// ```
/// use measurements::Volume;
///
/// let gallon = Volume::from_gallons(1.0);
/// let pint = Volume::from_pints(1.0);
/// let beers = gallon / pint;
/// println!("A gallon of beer will pour {} pints!", beers);
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug)]
pub struct Volume {
    liters: f64,
}

/// Number of Milliliters in a litre
pub const LITER_MILLILITERS_FACTOR: f64 = 1000.0;
/// Number of Cubic Centimeters in a litre
pub const LITER_CUBIC_CENTIMETER_FACTOR: f64 = 1000.0;
/// Number of Cubic Meters in a litre
pub const LITER_CUBIC_METER_FACTOR: f64 = 1.0 / 1000.0;
/// Number of Drops in a litre
pub const LITER_DROP_FACTOR: f64 = 15419.6298055;
/// Number of (US) Fluid Drams in a litre
pub const LITER_DRAM_FACTOR: f64 = 270.510351863;
/// Number of Teaspoons in a litre
pub const LITER_TEASPOONS_FACTOR: f64 = 202.8841362;
/// Number of Tablespoons in a litre
pub const LITER_TABLESPOONS_FACTOR: f64 = 67.6280454;
/// Number of Cubic Inches in a litre
pub const LITER_CUBIC_INCHES_FACTOR: f64 = 61.0237440947;
/// Number of Fluid Ounces (UK) in a litre
pub const LITER_FLUID_OUNCES_UK_FACTOR: f64 = 35.19506424;
/// Number of Fluid Ounces (US) in a litre
pub const LITER_FLUID_OUNCES_FACTOR: f64 = 33.8140227;
/// Number of Cups in a litre
pub const LITER_CUP_FACTOR: f64 = 4.226752838;
/// Number of Pints in a litre
pub const LITER_PINTS_FACTOR: f64 = 2.11337641887;
/// Number of Pints (UK) in a litre
pub const LITER_PINTS_UK_FACTOR: f64 = 1.75975398639;
/// Number of Quarts in a litre
pub const LITER_QUARTS_FACTOR: f64 = 1.05668820943;
/// Number of Gallons in a litre
pub const LITER_GALLONS_FACTOR: f64 = 0.264172052358;
/// Number of Gallons (UK) in a litre
pub const LITER_GALLONS_UK_FACTOR: f64 = 0.219969248299;
/// Number of Cubic Feet in a litre
pub const LITER_CUBIC_FEET_FACTOR: f64 = 0.0353146667215;
/// Number of Cubic Yards in a litre
pub const LITER_CUBIC_YARD_FACTOR: f64 = 0.0013079506193;

impl Volume {
    /// Create a new Volume from a floating point value in Liters (l)
    pub fn from_liters(liters: f64) -> Self {
        Volume { liters }
    }

    /// Create a new Volume from a floating point value in Litres (l)
    pub fn from_litres(liters: f64) -> Self {
        Self::from_liters(liters)
    }

    /// Create a new Volume from a floating point value in Cubic Centimeters (cc or cm³)
    pub fn from_cubic_centimeters(cubic_centimeters: f64) -> Self {
        Self::from_liters(cubic_centimeters / LITER_CUBIC_CENTIMETER_FACTOR)
    }

    /// Create a new Volume from a floating point value in Cubic Centimetres (cc or cm³)
    pub fn from_cubic_centimetres(cubic_centimeters: f64) -> Self {
        Self::from_cubic_centimeters(cubic_centimeters)
    }

    /// Create a new Volume from a floating point value in Milliliters (ml)
    pub fn from_milliliters(milliliters: f64) -> Self {
        Self::from_liters(milliliters / LITER_MILLILITERS_FACTOR)
    }

    /// Create a new Volume from a floating point value in Millilitres (ml)
    pub fn from_millilitres(milliliters: f64) -> Self {
        Self::from_milliliters(milliliters)
    }

    /// Create a new Volume from a floating point value in Cubic Meters (m³)
    pub fn from_cubic_meters(cubic_meters: f64) -> Self {
        Self::from_liters(cubic_meters / LITER_CUBIC_METER_FACTOR)
    }

    /// Create a new Volume from a floating point value in Cubic Metres (m³)
    pub fn from_cubic_metres(cubic_meters: f64) -> Self {
        Self::from_cubic_meters(cubic_meters)
    }

    /// Create a new Volume from a floating point value in Drops
    pub fn from_drops(drops: f64) -> Self {
        Self::from_liters(drops / LITER_DROP_FACTOR)
    }

    /// Create a new Volume from a floating point value in US Fluid Drams
    pub fn from_drams(drams: f64) -> Self {
        Self::from_liters(drams / LITER_DRAM_FACTOR)
    }

    /// Create a new Volume from a floating point value in Teaspoons (tsp)
    pub fn from_teaspoons(teaspoons: f64) -> Self {
        Self::from_liters(teaspoons / LITER_TEASPOONS_FACTOR)
    }

    /// Create a new Volume from a floating point value in Tablespoons (tbsp)
    pub fn from_tablespoons(tablespoons: f64) -> Self {
        Self::from_liters(tablespoons / LITER_TABLESPOONS_FACTOR)
    }

    /// Create a new Volume from a floating point value in UK Fluid Ounces (fl oz)
    pub fn from_fluid_ounces_uk(fluid_ounces_uk: f64) -> Self {
        Self::from_liters(fluid_ounces_uk / LITER_FLUID_OUNCES_UK_FACTOR)
    }

    /// Create a new Volume from a floating point value in US Fluid Ounces (fl oz)
    pub fn from_fluid_ounces(fluid_ounces: f64) -> Self {
        Self::from_liters(fluid_ounces / LITER_FLUID_OUNCES_FACTOR)
    }

    /// Create a new Volume from a floating point value in Cubic Inches (cu in or in³)
    pub fn from_cubic_inches(cubic_inches: f64) -> Self {
        Self::from_liters(cubic_inches / LITER_CUBIC_INCHES_FACTOR)
    }

    /// Create a new Volume from a floating point value in Cups
    pub fn from_cups(cups: f64) -> Self {
        Self::from_liters(cups / LITER_CUP_FACTOR)
    }

    /// Create a new Volume from a floating point value in US Pints
    pub fn from_pints(pints: f64) -> Self {
        Self::from_liters(pints / LITER_PINTS_FACTOR)
    }

    /// Create a new Volume from a floating point value in UK Pints
    pub fn from_pints_uk(pints_uk: f64) -> Self {
        Self::from_liters(pints_uk / LITER_PINTS_UK_FACTOR)
    }

    /// Create a new Volume from a floating point value in Quarts
    pub fn from_quarts(quarts: f64) -> Self {
        Self::from_liters(quarts / LITER_QUARTS_FACTOR)
    }

    /// Create a new Volume from a floating point value in US Gallons (gal US)
    pub fn from_gallons(gallons: f64) -> Self {
        Self::from_liters(gallons / LITER_GALLONS_FACTOR)
    }

    /// Create a new Volume from a floating point value in UK/Imperial Gallons (gal)
    pub fn from_gallons_uk(gallons_uk: f64) -> Self {
        Self::from_liters(gallons_uk / LITER_GALLONS_UK_FACTOR)
    }

    /// Create a new Volume from a floating point value in Cubic Feet (ft³)
    pub fn from_cubic_feet(cubic_feet: f64) -> Self {
        Self::from_liters(cubic_feet / LITER_CUBIC_FEET_FACTOR)
    }

    /// Create a new Volume from a floating point value in Cubic Yards (yd³)
    pub fn from_cubic_yards(cubic_yards: f64) -> Self {
        Self::from_liters(cubic_yards / LITER_CUBIC_YARD_FACTOR)
    }

    /// Convert Volume to a floating point value in Cubic Centimeters (cc or cm³)
    pub fn as_cubic_centimeters(&self) -> f64 {
        self.liters * LITER_CUBIC_CENTIMETER_FACTOR
    }

    /// Convert Volume to a floating point value in Cubic Centimetres (cc or cm³)
    pub fn as_cubic_centimetres(&self) -> f64 {
        self.as_cubic_centimeters()
    }

    /// Convert Volume to a floating point value in Milliliters (ml)
    pub fn as_milliliters(&self) -> f64 {
        self.liters * LITER_MILLILITERS_FACTOR
    }

    /// Convert Volume to a floating point value in Millilitres (ml)
    pub fn as_millilitres(&self) -> f64 {
        self.as_milliliters()
    }

    /// Convert Volume to a floating point value in Liters (l)
    pub fn as_liters(&self) -> f64 {
        self.liters
    }

    /// Convert Volume to a floating point value in Litres (l)
    pub fn as_litres(&self) -> f64 {
        self.as_liters()
    }

    /// Convert Volume to a floating point value in Cubic Meters (m³)
    pub fn as_cubic_meters(&self) -> f64 {
        self.liters * LITER_CUBIC_METER_FACTOR
    }

    /// Convert Volume to a floating point value in Cubic Metres (m³)
    pub fn as_cubic_metres(&self) -> f64 {
        self.as_cubic_meters()
    }

    /// Convert Volume to a floating point value in Drops
    pub fn as_drops(&self) -> f64 {
        self.liters * LITER_DROP_FACTOR
    }

    /// Convert Volume to a floating point value in US Fluid Drams
    pub fn as_drams(&self) -> f64 {
        self.liters * LITER_DRAM_FACTOR
    }

    /// Convert Volume to a floating point value in Teaspoons (tsp)
    pub fn as_teaspoons(&self) -> f64 {
        self.liters * LITER_TEASPOONS_FACTOR
    }

    /// Convert Volume to a floating point value in Tablespoons (tbsp)
    pub fn as_tablespoons(&self) -> f64 {
        self.liters * LITER_TABLESPOONS_FACTOR
    }

    /// Convert Volume to a floating point value in Cubic Inches (cu in or in³)
    pub fn as_cubic_inches(&self) -> f64 {
        self.liters * LITER_CUBIC_INCHES_FACTOR
    }

    /// Convert Volume to a floating point value in UK Fluid Ounces (fl oz)
    pub fn as_fluid_ounces_uk(&self) -> f64 {
        self.liters * LITER_FLUID_OUNCES_UK_FACTOR
    }

    /// Convert Volume to a floating point value in US Fluid Ounces (fl oz)
    pub fn as_fluid_ounces(&self) -> f64 {
        self.liters * LITER_FLUID_OUNCES_FACTOR
    }

    /// Convert Volume to a floating point value in Cups
    pub fn as_cups(&self) -> f64 {
        self.liters * LITER_CUP_FACTOR
    }

    /// Convert Volume to a floating point value in US Pints
    pub fn as_pints(&self) -> f64 {
        self.liters * LITER_PINTS_FACTOR
    }

    /// Convert Volume to a floating point value in UK Pints
    pub fn as_pints_uk(&self) -> f64 {
        self.liters * LITER_PINTS_UK_FACTOR
    }

    /// Convert Volume to a floating point value in Quarts
    pub fn as_quarts(&self) -> f64 {
        self.liters * LITER_QUARTS_FACTOR
    }

    /// Convert Volume to a floating point value in US Gallons (gal us)
    pub fn as_gallons(&self) -> f64 {
        self.liters * LITER_GALLONS_FACTOR
    }

    /// Convert Volume to a floating point value in UK/Imperial Gallons (gal)
    pub fn as_gallons_uk(&self) -> f64 {
        self.liters * LITER_GALLONS_UK_FACTOR
    }

    /// Convert Volume to a floating point value in Cubic Feet (ft³)
    pub fn as_cubic_feet(&self) -> f64 {
        self.liters * LITER_CUBIC_FEET_FACTOR
    }

    /// Convert Volume to a floating point value in Cubic Yards (yd³)
    pub fn as_cubic_yards(&self) -> f64 {
        self.liters * LITER_CUBIC_YARD_FACTOR
    }
}

impl Measurement for Volume {
    fn as_base_units(&self) -> f64 {
        self.liters
    }

    fn from_base_units(units: f64) -> Self {
        Self::from_liters(units)
    }

    fn get_base_units_name(&self) -> &'static str {
        "l"
    }

    fn get_appropriate_units(&self) -> (&'static str, f64) {
        // Smallest to largest
        let list = [
            ("pl", 1e-12),
            ("nl", 1e-9),
            ("\u{00B5}l", 1e-6),
            ("ml", 1e-3),
            ("l", 1e0),
            ("m\u{00B3}", 1e3),
            ("km\u{00B3}", 1e12),
        ];
        self.pick_appropriate_units(&list)
    }
}

#[cfg(feature = "from_str")]
impl FromStr for Volume {
    type Err = std::num::ParseFloatError;

    /// Create a new Volume from a string
    /// Plain numbers in string are considered to be Liters. Defaults for units with US
    /// and UK variants are considered to be in US without specific "imp" prefix.
    fn from_str(val: &str) -> Result<Self, Self::Err> {
        if val.is_empty() {
            return Ok(Volume::from_liters(0.0));
        }

        let re = Regex::new(r"(?i)\s*([0-9.]*)\s?([a-z .]{1,10}[0-9³]{0,1})\s*$").unwrap();
        if let Some(caps) = re.captures(val) {
            let float_val = caps.get(1).unwrap().as_str();
            return Ok(
                match caps.get(2).unwrap().as_str().to_lowercase().as_str() {
                    "cm3" | "cm\u{00b3}" => {
                        Volume::from_cubic_centimeters(float_val.parse::<f64>()?)
                    }
                    "ft3" | "ft\u{00b3}" => Volume::from_cubic_feet(float_val.parse::<f64>()?),
                    "yd3" | "yd\u{00b3}" => Volume::from_cubic_yards(float_val.parse::<f64>()?),
                    "in3" | "in\u{00b3}" => Volume::from_cubic_inches(float_val.parse::<f64>()?),
                    "gal" | "us gal" => Volume::from_gallons(float_val.parse::<f64>()?),
                    "imp gal" => Volume::from_gallons_uk(float_val.parse::<f64>()?),
                    "cup" => Volume::from_cups(float_val.parse::<f64>()?),
                    "tsp" => Volume::from_teaspoons(float_val.parse::<f64>()?),
                    "tbsp" | "t." => Volume::from_tablespoons(float_val.parse::<f64>()?),
                    "ml" => Volume::from_milliliters(float_val.parse::<f64>()?),
                    "us fl oz" | "fl oz" => Volume::from_fluid_ounces(float_val.parse::<f64>()?),
                    "imp fl oz" => Volume::from_fluid_ounces_uk(float_val.parse::<f64>()?),
                    "m3" | "m\u{00b3}" => Volume::from_cubic_meters(float_val.parse::<f64>()?),
                    "gt" | "gtt" => Volume::from_drops(float_val.parse::<f64>()?),
                    "dr" => Volume::from_drams(float_val.parse::<f64>()?),
                    "l" => Volume::from_litres(float_val.parse::<f64>()?),
                    "qt" => Volume::from_quarts(float_val.parse::<f64>()?),
                    "us pt" | "us p" | "p" | "pt" => Volume::from_pints(float_val.parse::<f64>()?),
                    "imp pt" | "imp p" => Volume::from_pints_uk(float_val.parse::<f64>()?),
                    _ => Volume::from_litres(val.parse::<f64>()?),
                },
            );
        }

        Ok(Volume::from_liters(val.parse::<f64>()?))
    }
}

implement_measurement! { Volume }

#[cfg(test)]
mod test {
    use test_utils::assert_almost_eq;
    use volume::*;

    // Volume Units
    // Metric
    #[test]
    fn litres() {
        let t = Volume::from_litres(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 100.0);
    }

    #[test]
    fn cubic_centimeters() {
        let t = Volume::from_litres(1.0);
        let o = t.as_cubic_centimeters();
        assert_almost_eq(o, 1000.0);

        let t = Volume::from_cubic_centimeters(1000.0);
        let o = t.as_litres();
        assert_almost_eq(o, 1.0);
    }

    #[test]
    fn milliliters() {
        let t = Volume::from_litres(1.0);
        let o = t.as_milliliters();
        assert_almost_eq(o, 1000.0);

        let t = Volume::from_milliliters(1000.0);
        let o = t.as_litres();
        assert_almost_eq(o, 1.0);
    }

    #[test]
    fn cubic_meters() {
        let t = Volume::from_litres(100.0);
        let o = t.as_cubic_meters();
        assert_almost_eq(o, 0.1);

        let t = Volume::from_cubic_meters(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 100000.0);
    }

    // Imperial
    #[test]
    fn drops() {
        let t = Volume::from_litres(100.0);
        let o = t.as_drops();
        assert_almost_eq(o, 1541962.98055);

        let t = Volume::from_drops(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 0.00648524);
    }

    #[test]
    fn drams() {
        let t = Volume::from_litres(100.0);
        let o = t.as_drams();
        assert_almost_eq(o, 27051.0351863);

        let t = Volume::from_drams(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 0.36967162);
    }

    #[test]
    fn teaspoons() {
        let t = Volume::from_litres(100.0);
        let o = t.as_teaspoons();
        assert_almost_eq(o, 20288.41362);

        let t = Volume::from_teaspoons(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 0.492892159402);
    }

    #[test]
    fn tablespoons() {
        let t = Volume::from_litres(100.0);
        let o = t.as_tablespoons();
        assert_almost_eq(o, 6762.80454);

        let t = Volume::from_tablespoons(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 1.47867647821);
    }

    #[test]
    fn cubic_inches() {
        let t = Volume::from_litres(100.0);
        let o = t.as_cubic_inches();
        assert_almost_eq(o, 6102.37440947);

        let t = Volume::from_cubic_inches(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 1.6387064);
    }

    #[test]
    fn fluid_ounces_uk() {
        let t = Volume::from_litres(100.0);
        let o = t.as_fluid_ounces_uk();
        assert_almost_eq(o, 3519.506424);

        let t = Volume::from_fluid_ounces_uk(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 2.84130750034);
    }

    #[test]
    fn fluid_ounces() {
        let t = Volume::from_litres(100.0);
        let o = t.as_fluid_ounces();
        assert_almost_eq(o, 3381.40227);

        let t = Volume::from_fluid_ounces(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 2.95735295641);
    }

    #[test]
    fn cups() {
        let t = Volume::from_litres(100.0);
        let o = t.as_cups();
        assert_almost_eq(o, 422.6752838);

        let t = Volume::from_cups(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 23.6588236485);
    }

    #[test]
    fn pints() {
        let t = Volume::from_litres(100.0);
        let o = t.as_pints();
        assert_almost_eq(o, 211.337641887);

        let t = Volume::from_pints(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 47.3176473);
    }

    #[test]
    fn pints_uk() {
        let t = Volume::from_litres(100.0);
        let o = t.as_pints_uk();
        assert_almost_eq(o, 175.975398639);

        let t = Volume::from_pints_uk(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 56.826125);
    }

    #[test]
    fn quarts() {
        let t = Volume::from_litres(100.0);
        let o = t.as_quarts();
        assert_almost_eq(o, 105.668820943);

        let t = Volume::from_quarts(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 94.6352946);
    }

    #[test]
    fn gallons() {
        let t = Volume::from_litres(100.0);
        let o = t.as_gallons();
        assert_almost_eq(o, 26.4172052358);

        let t = Volume::from_gallons(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 378.5411784);
    }

    #[test]
    fn gallons_uk() {
        let t = Volume::from_litres(100.0);
        let o = t.as_gallons_uk();
        assert_almost_eq(o, 21.9969248299);

        let t = Volume::from_gallons_uk(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 454.609);
    }

    #[test]
    fn cubic_feet() {
        let t = Volume::from_litres(100.0);
        let o = t.as_cubic_feet();
        assert_almost_eq(o, 3.53146667215);

        let t = Volume::from_cubic_feet(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 2831.6846592);
    }

    #[test]
    fn cubic_yards() {
        let t = Volume::from_litres(100.0);
        let o = t.as_cubic_yards();
        assert_almost_eq(o, 0.13079506193);

        let t = Volume::from_cubic_yards(100.0);
        let o = t.as_litres();
        assert_almost_eq(o, 76455.4857992);
    }

    // Traits
    #[test]
    fn add() {
        let a = Volume::from_litres(2.0);
        let b = Volume::from_litres(4.0);
        let c = a + b;
        let d = b + a;
        assert_almost_eq(c.as_litres(), 6.0);
        assert_eq!(c, d);
    }

    #[test]
    fn sub() {
        let a = Volume::from_litres(2.0);
        let b = Volume::from_litres(4.0);
        let c = a - b;
        assert_almost_eq(c.as_litres(), -2.0);
    }

    #[test]
    fn mul() {
        let a = Volume::from_litres(3.0);
        let b = a * 2.0;
        let c = 2.0 * a;
        assert_almost_eq(b.as_litres(), 6.0);
        assert_eq!(b, c);
    }

    #[test]
    fn div() {
        let a = Volume::from_litres(2.0);
        let b = Volume::from_litres(4.0);
        let c = a / b;
        let d = a / 2.0;
        assert_almost_eq(c, 0.5);
        assert_almost_eq(d.as_litres(), 1.0);
    }

    #[test]
    fn eq() {
        let a = Volume::from_litres(2.0);
        let b = Volume::from_litres(2.0);
        assert_eq!(a == b, true);
    }

    #[test]
    fn neq() {
        let a = Volume::from_litres(2.0);
        let b = Volume::from_litres(4.0);
        assert_eq!(a == b, false);
    }

    #[test]
    fn cmp() {
        let a = Volume::from_litres(2.0);
        let b = Volume::from_litres(4.0);
        assert_eq!(a < b, true);
        assert_eq!(a <= b, true);
        assert_eq!(a > b, false);
        assert_eq!(a >= b, false);
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn empty_val_from_str() {
        let v = Volume::from_str("");
        assert!(v.is_ok());
        assert_eq!(0.0, v.unwrap().as_litres());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn cubic_centimeters_from_str() {
        let v = Volume::from_str(" 10cm3");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_cubic_centimeters());

        let v2 = Volume::from_str("10 cm³ ");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_cubic_centimetres());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn cubic_feet_from_str() {
        let v = Volume::from_str(" 10 ft3");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_cubic_feet());

        let v2 = Volume::from_str(" 10ft³ ");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_cubic_feet());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn cubic_yard_from_str() {
        let v = Volume::from_str(" 10 yd3");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_cubic_yards());

        let v2 = Volume::from_str(" 10yd³ ");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_cubic_yards());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn cubic_inches_from_str() {
        let v = Volume::from_str(" 10 in3");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_cubic_inches());

        let v2 = Volume::from_str("10in³ ");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_cubic_inches());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn gallons_from_str() {
        let v = Volume::from_str(" 10 gal");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_gallons());

        let v2 = Volume::from_str("10 US gal");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_gallons());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn uk_gallons_from_str() {
        let v = Volume::from_str(" 10 imp gal");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_gallons_uk());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn cups_from_str() {
        let v = Volume::from_str("10cup");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_cups());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn teaspoons_from_str() {
        let v = Volume::from_str("10tsp");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_teaspoons());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn tablespoons_from_str() {
        let v = Volume::from_str("10 tbsp");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_tablespoons());

        let v2 = Volume::from_str("10T.");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_tablespoons());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn milliliters_from_str() {
        let v = Volume::from_str("10ml");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_milliliters());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn fluid_ounces_from_str() {
        let v = Volume::from_str("10 US fl oz");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_fluid_ounces());

        let v2 = Volume::from_str("10 fl oz");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_fluid_ounces());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn fluid_ounces_uk_from_str() {
        let v = Volume::from_str("10imp fl oz");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_fluid_ounces_uk());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn cubic_meters_from_str() {
        let v = Volume::from_str("10 m3");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_cubic_meters());

        let v2 = Volume::from_str("10m³");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_cubic_meters());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn drops_from_str() {
        let v = Volume::from_str("10 gt");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_drops());

        let v2 = Volume::from_str("10gtt");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_drops());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn drams_from_str() {
        let v = Volume::from_str("10dr");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_drams());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn liters_from_str() {
        let v = Volume::from_str("10L");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_liters());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn quarts_from_str() {
        let v = Volume::from_str("10qt");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_quarts());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn pints_from_str() {
        let v = Volume::from_str("10 US pt");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_pints());

        let v2 = Volume::from_str("10 US p");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_pints());

        let v3 = Volume::from_str("10p");
        assert!(v3.is_ok());
        assert_almost_eq(10.0, v3.unwrap().as_pints());

        let v4 = Volume::from_str("10pt");
        assert!(v4.is_ok());
        assert_almost_eq(10.0, v4.unwrap().as_pints());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn pints_uk_from_str() {
        let v = Volume::from_str("10 imp pt");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_pints_uk());

        let v2 = Volume::from_str("10 imp p");
        assert!(v2.is_ok());
        assert_almost_eq(10.0, v2.unwrap().as_pints_uk());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn default_from_str() {
        let v = Volume::from_str("10");
        assert!(v.is_ok());
        assert_almost_eq(10.0, v.unwrap().as_liters());
    }

    #[test]
    #[cfg(feature = "from_str")]
    fn invalid_from_str() {
        let v = Volume::from_str("10abcd");
        assert_eq!(false, v.is_ok());
    }
}