#!perl -w

use Units;

# Initialise the units processors
if (!CFX5::Units::unitsInit()) {
  die "Error initialising units";
}

#Get common units for velocity
my $unitsListRef = CFX5::Units::getCommonUnits("Velocity");
if (!defined($unitsListRef)) {
  die "Error getting common units";
}

#Add an error for testing.
push (@$unitsListRef,"foo");

#Get the dimensions for each one.
foreach $unitString (@$unitsListRef) {
  print "$unitString: ";
  my $dimArrRef = CFX5::Units::dimFromUnit($unitString);
  if (defined($dimArrRef)) {
    foreach $dim (@$dimArrRef) {
      print "$dim ";
    }
  } else {
  print "Dimensions not defined for '$unitString'";
  }
  print "\n";
}


print "\n\n";
# Test physical quantity parsing and resulting dimension strings
@PQtests = ("10 [m s^-1]",
            "10.5 m s^-1",
            "(u/1 [m]) [s]",
            "myExp",
            "foo",
            "5.6 * bar",
            "10 [m/s]",
            "10 [K] * 15.4 [W m^-2]",
            "15 [W / m^2]",
            "15 [W m^-2]",
            "1e-3 [kg /m  /  s^2    mm^4]",
            "12",
            "13.4 * 17",
            "13.4*17",
            " 1e5 [Pa]", 
            "1e-3 [ knot]",
            "[m/s]",
            "15.4 [fortnight]",
            "12.5 []",
            "1.E-3 [Pa]",
            "2. [m/s]",
            "1 [bar]",
            "1 [MBTU]",
            "1 [bar / Pa]",
            "1 [day / BTU]",
            "1 [in Wg]");

foreach $PQString (@PQtests) {
  print "$PQString: ";
  (my $value,my $units) = CFX5::Units::parseQuantity($PQString);
  if (!defined($value)) { 
   $value = "UNDEF";
  }
  print "($value,";
  if (defined($units)) {
   print "$units): ";
   my $dimArr = CFX5::Units::dimFromUnit($units);
   if (defined($dimArr)) {
     foreach $dim (@$dimArr) {
       print "$dim ";
     }
   } else {
    print "NO DIMS"
   }
  } else {
   print "UNDEF): ";
  }
  print "\n";
}

 print "---- Testing Conversion to SI -----\n";
 foreach my $quantity (@PQtests) {
  my ($val, $unit) = CFX5::Units::convertToSI($quantity); 
  if (!defined($val)) { $val = "UNDEF";}
  if (!defined($unit)) { $unit = "UNDEF";}
  print "$quantity = $val $unit\n";
 }
