Skip to content

Commit ba6398e

Browse files
authored
Add tests for E format specifier (#203)
***NO_CI***
1 parent 6dd90a8 commit ba6398e

File tree

1 file changed

+93
-34
lines changed

1 file changed

+93
-34
lines changed

Tests/NFUnitTestArithmetic/UnitTestFormat.cs

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
using nanoFramework.TestFramework;
88
using System;
99
using System.Collections;
10-
using System.Diagnostics;
11-
using System.Globalization;
12-
10+
using static NFUnitTestArithmetic.SampleDisplay;
1311

1412
namespace NFUnitTestArithmetic
1513
{
@@ -81,34 +79,38 @@ public void StringFormat_03(string formatString, double value, string outcomeMes
8179
public void DecimalFormat()
8280
{
8381
sampleDisplay = new SampleDisplay();
84-
85-
TestFormat(" 123", "D", "123");
86-
TestFormat(" 129", "D", "129");
87-
TestFormat(" -129", "D", "-129");
88-
TestFormat(" 128", "D", "128");
89-
TestFormat(" -128", "D", "-128");
90-
TestFormat(" -128", "D2", "-128");
91-
TestFormat(" 1234", "D2", "1234");
92-
TestFormat("-1234", "D", "-1234");
82+
83+
TestFormat(" 123", "D", "123");
84+
TestFormat(" 129", "D", "129");
85+
TestFormat(" -129", "D", "-129");
86+
TestFormat(" 128", "D", "128");
87+
TestFormat(" -128", "D", "-128");
88+
TestFormat(" -128", "D2", "-128");
89+
TestFormat(" 1234", "D2", "1234");
90+
TestFormat("-1234", "D", "-1234");
9391
TestFormat(" 1234", "D6", "001234");
94-
TestFormat("-1234", "D6","-001234");
92+
TestFormat("-1234", "D6", "-001234");
9593

9694
sampleDisplay.WriteOutput();
9795

9896
}
9997

100-
98+
10199
[TestMethod]
102100
// the F format can be used with all number types
103101
public void FixedFormat()
104102
{
105103
sampleDisplay = new SampleDisplay();
106104

107-
TestFormat("123", "F", "123.00"); // default for CultureInvariant is 2 decimal places
105+
// default for CultureInvariant is 2 decimal places
106+
107+
TestFormat("0", "F", "0.00");
108+
TestFormat("0", "F4", "0.0000");
109+
TestFormat("123", "F", "123.00");
108110
TestFormat("129", "F", "129.00");
109111
TestFormat("-129", "F", "-129.00");
110112
TestFormat("128", "F", "128.00");
111-
TestFormat("128", "F4", "128.0000"); // bug - int gets different value than float/double
113+
TestFormat("128", "F4", "128.0000");
112114
TestFormat("-128", "F", "-128.00");
113115
TestFormat("-128", "F2", "-128.00");
114116
TestFormat("1234", "F2", "1234.00");
@@ -117,18 +119,69 @@ public void FixedFormat()
117119
TestFormat("-1234", "F6", "-1234.000000");
118120
TestFormat("123.78", "F3", "123.780");
119121
TestFormat("123.78", "F1", "123.8");
120-
TestFormat("1234.8999", "F3", "1234.900");
122+
TestFormat("1234.8999", "F3", "1234.900");
121123

122124
sampleDisplay.WriteOutput();
123125

124126
}
125127

128+
// the E format can be used with all number types
129+
[DataRow("0", "E", "0.000000E+000", true, true, true)]
130+
[DataRow("0", "E4", "0.0000E+000", true, true, true)]
131+
[DataRow("12345.6789", "E", "1.234567E+004", true, true, false)]
132+
[DataRow("12345.678", "E6", "1.234567E+004", true, true, false)]
133+
[DataRow("12345.6789", "e4", "1.2345e+004", true, true, false)]
134+
[DataRow("123", "E", "1.230000E+002", true, true, true)]
135+
[DataRow("-123", "E", "-1.230000E+002", true, true, true)]
136+
[DataRow("1.2345e-9", "E", "1.234500E-009", true, false, false)]
137+
[DataRow("1.2345e-9", "E5", "1.23450E-009", true, false, false)]
138+
[TestMethod]
139+
public void ExponentialFormat(string valueStr, string formatString, string expectedResult, bool testDouble, bool testSingle, bool testIntegers)
140+
{
141+
double value = double.Parse(valueStr);
142+
143+
if (testDouble)
144+
{
145+
CheckValue(double.Parse(valueStr), valueStr, formatString, expectedResult, ColumnType.Single, null);
146+
Assert.IsTrue(double.TryParse(valueStr, out double result), $"TryParse failed for double {valueStr}");
147+
CheckValue(result, valueStr, formatString, expectedResult, ColumnType.Double, null);
148+
}
149+
150+
if (testSingle)
151+
{
152+
CheckValue(float.Parse(valueStr), valueStr, formatString, expectedResult, ColumnType.Single, null);
153+
Assert.IsTrue(float.TryParse(valueStr, out float result), $"TryParse failed for float {valueStr}");
154+
CheckValue(result, valueStr, formatString, expectedResult, ColumnType.Single, null);
155+
}
156+
157+
if (testIntegers)
158+
{
159+
// can't test negative values with UInt64
160+
if (value > 0)
161+
{
162+
CheckValue(ulong.Parse(valueStr), valueStr, formatString, expectedResult, ColumnType.UInt64, null);
163+
Assert.IsTrue(ulong.TryParse(valueStr, out ulong result), $"TryParse failed for ulong {valueStr}");
164+
CheckValue(result, valueStr, formatString, expectedResult, ColumnType.UInt64, null);
165+
}
166+
167+
CheckValue(long.Parse(valueStr), valueStr, formatString, expectedResult, ColumnType.Int64, null);
168+
Assert.IsTrue(long.TryParse(valueStr, out long result1), $"TryParse failed for long {valueStr}");
169+
CheckValue(result1, valueStr, formatString, expectedResult, ColumnType.Int64, null);
170+
}
171+
172+
//;
173+
174+
sampleDisplay.WriteOutput();
175+
}
176+
126177
[TestMethod]
127178
// the G format can be used with all number types
128179
public void GeneralFormat()
129180
{
130181
sampleDisplay = new SampleDisplay();
131182

183+
TestFormat("0", "G", "0");
184+
TestFormat("0", "G4", "0");
132185
TestFormat("123", "G", "123");
133186
TestFormat("129", "G", "129");
134187
TestFormat("-129", "G", "-129");
@@ -149,10 +202,12 @@ public void GeneralFormat()
149202
TestFormat("1234.8999", "G6", "1234.9");
150203
TestFormat("1234.8999", "G7", "1234.9");
151204
TestFormat("-1234.901", "G7", "-1234.901");
205+
TestFormat("1.2345E-9", "G", "1.2345E-09");
152206

153207
sampleDisplay.WriteOutput();
154208

155209
}
210+
156211
[TestMethod]
157212
// the N format can be used with all number types
158213
public void NumberFormat()
@@ -163,12 +218,12 @@ public void NumberFormat()
163218
TestFormat("129", "N", "129.00");
164219
TestFormat("-129", "N", "-129.00");
165220
TestFormat("128", "N", "128.00");
166-
TestFormat("128", "N4", "128.0000");
221+
TestFormat("128", "N4", "128.0000");
167222
TestFormat("-128", "N", "-128.00");
168223
TestFormat("-128", "N2", "-128.00");
169224
TestFormat("1234", "N2", "1,234.00");
170225
TestFormat("-1234", "N", "-1,234.00");
171-
TestFormat("1234", "N6", "1,234.000000");
226+
TestFormat("1234", "N6", "1,234.000000");
172227
TestFormat("-1234", "N6", "-1,234.000000");
173228
TestFormat("1234.567", "N2", "1,234.57");
174229
TestFormat("-1234.567", "N2", "-1,234.57");
@@ -208,7 +263,7 @@ public void HexFormat()
208263

209264

210265
#region Helper functions
211-
private void TestFormat(string valueStr, string formatString, string expectedResult, Case formatCase=Case.Both)
266+
private void TestFormat(string valueStr, string formatString, string expectedResult, Case formatCase = Case.Both)
212267
{
213268
double value = Double.Parse(valueStr); // !!! does not return a negative number when parsed if string value does not contain a decimal point
214269
bool isNegative = false;
@@ -371,7 +426,7 @@ private void TestFormatInner(string valueStr, string formatString, string expect
371426

372427
private void CheckValue(object value, string valueStr, string formatString, string expectedResult, SampleDisplay.ColumnType columnType, SampleDisplay.RowData rowData)
373428
{
374-
string result = String.Format($"{{0:{formatString}}}", new object[] { value });
429+
string result = string.Format($"{{0:{formatString}}}", new object[] { value });
375430
// for format of X if the number is negative there will be extra F's in the front depending on integer size.
376431
// because of this we will only check the ending characters for format X types
377432
if (formatString.ToUpper()[0] == 'X')
@@ -382,7 +437,11 @@ private void CheckValue(object value, string valueStr, string formatString, stri
382437
{
383438
Assert.AreEqual(result, expectedResult, $"The expected result for '{formatString}' on value {valueStr} for type {value.GetType().Name} is '{expectedResult}'");
384439
}
385-
rowData.SetResult(result, columnType);
440+
441+
if (rowData != null)
442+
{
443+
rowData.SetResult(result, columnType);
444+
}
386445
}
387446

388447
#endregion
@@ -470,19 +529,19 @@ public RowData(string value, string formatString, ArrayList columnInfos)
470529

471530
public void SetResult(string result, ColumnType column)
472531
{
473-
int i = (int)column;
474-
ColumnData[i] = result;
475-
if (((ColumnInfo)ColumnInfos[i]).LargestLength < result.Length)
476-
{
477-
((ColumnInfo)ColumnInfos[i]).LargestLength = result.Length;
478-
}
479-
if (result != NotApplicable)
480-
{
481-
((ColumnInfo)ColumnInfos[i]).DefaultOnly = false;
532+
int i = (int)column;
533+
ColumnData[i] = result;
534+
if (((ColumnInfo)ColumnInfos[i]).LargestLength < result.Length)
535+
{
536+
((ColumnInfo)ColumnInfos[i]).LargestLength = result.Length;
537+
}
538+
if (result != NotApplicable)
539+
{
540+
((ColumnInfo)ColumnInfos[i]).DefaultOnly = false;
482541

542+
}
483543
}
484544
}
485-
}
486545

487546
public RowData AddRow(string value, string formatString)
488547
{
@@ -510,7 +569,7 @@ public void WriteOutput()
510569
for (int j = 0; j < (int)ColumnType.MaxColumns; j++)
511570
{
512571
// don't print columns that only have default (n/a) in them
513-
if (GetColumnInfo((ColumnType)j).DefaultOnly)
572+
if (GetColumnInfo((ColumnType)j).DefaultOnly)
514573
{
515574
continue;
516575
}
@@ -530,5 +589,5 @@ public void WriteOutput()
530589
}
531590

532591
}
533-
#endregion
592+
#endregion
534593
}

0 commit comments

Comments
 (0)