Even though decimal digit are correct, assert are getting failed - nunit

I have those 2 values :
Expected Value (fResult)= 103393.431493782901937514
Actual Value (output) = 103393.431493782901937514
When I assert on the value, my expected result was consider as 103393.431493783m
Because of that my Assert was fails. Can anyone help in this regard.
Assert.That(output, Is.EqualTo(fResult));
More Information: both Actual and Expected values are Decimal data type,
[TestCase("value1", "value2", 5, 103393.431493782901937514)]
public void converFormulaforPressure(String Fromunit, String toUnit, decimal Avalue, decimal fResult) {
var output = ut1.Convert(ut1.GetUnit("Pressure", Fromunit), ut1.GetUnit("Pressure", toUnit), Avalue).Val;
Assert.That(output, Is.EqualTo(fResult));
}
ut1.Convert is a method which converts the value and give the Actual result .

You need to provide a tolerance level and you cannot pass a decimal as a TestCase parameter. The code you provided is passing the last value as a double, hence the rounding when you run the assert. You can solve this by using TestCaseSource instead.
The test below passes:
private static readonly object[] TestCases = {
new object[] {"value1", "value2", 5m, 103393.431493782901937514m}
};
[Test, TestCaseSource("TestCases")]
public void TestExample(string fromUnit, string toUnit, decimal value, decimal fResult) {
//Replace the line below with your convert method using values from testCase
var output = 103393.431493782901937514m;
Assert.That(output, Is.EqualTo(fResult).Within(0.00000000000001));
}

Related

Dart Flutter: Syntax confusion in setting default value for class properties

Here are two examples of class constructors:
class A {
final String name;
final String sex;
A({
this.name = 'Unknown',
this.sex = 'Unknown',
});
}
Now consider the second class:
class B {
final String name;
final String sex;
B({
this.name = 'Unknown',
sex,
}) : sex = name == 'Unknown' ? 'Unknown' : 'Other';
}
I am confused as to if the second example is considered as an alternative to the first example's syntax or is it intended for a whole different purpose?
The first example allows "sex" to be any value given to the constructor.
The second example however only accepts the default ("Unknown", and sets "sex" to "Other" for any value given other than "Unknown".
So a = A(name:"Jon", sex:"male") result in a.sex == "male", whereas b = B(name:"Jon", sex:"male") result in b.sex == "Other".
If defaults are used, as in x = A(name:"Joe"), or x = B(name:"Joe") the resulting objects will both have x.sex == "Unknown"
From what I could gather and since I received no satisfying answer/comment, here is what I ended up accepting as a difference.
The first example's syntax allows for assigning a default value to a property when that value is a single non-conditional value.
However, if one needs to assign a value based on a condition -like in the case of the second example, a ternary operator- it won't be possible to do that following the first syntax because it will result in an error, so the variable must be initialized according to the second syntax.
Please correct me if I am wrong.

Using a string comparison in a boolean that was defined as a command line argument

I am writing a program to determine the cost of gasoline, given the price-per-gallon and the amount of gallons purchased, but also given the payment method (cash or credit). To read the payment method, I have defined a String named paymentMethod as the third argument input in the command line, as shown below:
public class gas{
public static void main(String[] args){
double pricePerGallon=Double.parseDouble(args[0]);
double gallonsPurchased=Double.parseDouble(args[1]);
String paymentMethod=args[2];
String cash="cash";
String credit="credit";
if (pricePerGallon>0 && gallonsPurchased>0){
**if (paymentMethod==cash)**
{
System.out.println(pricePerGallon*gallonsPurchased);
}
else if (paymentMethod==credit)
{
System.out.println(1.1*pricePerGallon*gallonsPurchased);
}
}
else{
System.out.println("Error");
}
}
}
I'm seeing if the third argument (which is a string) is the string "cash", in which case the cost of the gas is calculated and printed. If it is credit, there's an additional 10% charge.
Problems are arising with the boolean " (paymentMethod==cash) ". My program is compiling and I can run it but when I hit enter, nothing is returned. Not even the computation of the price.
You need to use the equals method. The == operator does not work on Strings in Java as you would want it to.
Change:
if (paymentMethod==cash)
to
if (paymentMethod.equals(cash))
And similarly for the credit conditional also.

How to convert BeamRecord into String in BeamSql

I'm converting a beamRecord into String . Below is the code snippet :
PCollection<BeamRecord> output_A = apps.apply(BeamSql.query("select Outlet from PCOLLECTION"));
output_A.apply(TextIO.write().to("gs://google_bucket/output/sbc.txt"));
output_A is in BeamRecord format and needs to get converted into String so that it can be written to output file .
Below is the error on output_A.apply:
The method apply(PTransform,OutputT>) in the type PCollection is not applicable for the arguments (TextIO.Write)
So my question is how to convert BeamRecord into String format.
You need to apply a transform to the pipeline which converts a BeamRecord to a String before applying the TextIO.Write transform. There are a number of ways to do this. You can convert the BeamRecord to a String in any way you want.
Simple toString().
public void processElement(ProcessContext c) {
// c.element() returns a BeamRecord
c.output(c.element().toString());
}
Or get specific field value(s) from the BeamRecord.
public void processElement(ProcessContext c) {
// c.element() returns a BeamRecord
c.output(c.element().getString("fieldName"));
}
Reference: BeamRecord

get double value from bigdecimal without exponential java

is there a way to get double from BigDecimal without exponential?
Eg: I have an new BigDecimal("123456797676.897")
if i print new BigDecimal("123456797676.897").toString() it prints properly as 123456797676.897.
Now suppose if I try to print new BigDecimal("123456797676.897").doubleValue(), it prints with exponential like 1.23456797676897E11.
Is there any way I can get doublevalue with out exponential.
Thanks :)
The following program demonstrates that these are all exactly the same double:
new BigDecimal("123456797676.897").doubleValue()
123456797676.897
1.23456797676897E11
12.3456797676897E10
The Double toString method has to pick one representation for each value. For numbers greater than 107 it uses exponential notation with a single digit before the decimal point. That is a generally reasonable choice, but it is not always the right choice. If you want it displayed without the exponent use DecimalFormat. If you are just using the number, it makes no difference whether Double's toString would have displayed it with an exponent or not, and you don't need to do anything about it.
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
double doubleValue = new BigDecimal("123456797676.897").doubleValue();
double simpleLiteral = 123456797676.897;
double exponent11Literal = 1.23456797676897E11;
double exponent10Literal = 12.3456797676897E10;
System.out.println(doubleValue == simpleLiteral);
System.out.println(doubleValue == exponent11Literal);
System.out.println(doubleValue == exponent10Literal);
}
}
output:
true
true
true

Contract.Ensures for an OverFlowException

I have a simple Method that returns the exponential value from a given number:
public int Exp(int num)
{
return Convert.ToInt32(System.Math.Exp(num));
}
When running Pex I get an OverFlowException in the Summary/Exception field for a certain large number: 1969057606.
How could I create a Post Condition using Contract.Ensure()?
I tried the following but it does not do anything:
Contract.Ensures(Contract.Result<int>() < 2147483647);
// This is because the max value an int variable can hold is 2147483647
Contract.Ensures is used to assert something about the state of your class after the function is run or, in this case, the result of the function regardless of what the input was. You need to add a Contract.Requires such that e^num <= int.MaxValue, e.g.
Contract.Requires<ArgumentOutOfRangeException>(num <= Math.Floor(Math.Log(int.MaxValue)))
Although you'd probably want to pull the max value calculation out into a constant.
public static readonly int MAX_EXPONENT = Math.Floor(Math.Log(int.MaxValue));
...
Contract.Requires<ArgumentOutOfRangeException>(num <= MAX_EXPONENT)