7
7
using nanoFramework . TestFramework ;
8
8
using System ;
9
9
using System . Collections ;
10
- using System . Diagnostics ;
11
- using System . Globalization ;
12
-
10
+ using static NFUnitTestArithmetic . SampleDisplay ;
13
11
14
12
namespace NFUnitTestArithmetic
15
13
{
@@ -81,34 +79,38 @@ public void StringFormat_03(string formatString, double value, string outcomeMes
81
79
public void DecimalFormat ( )
82
80
{
83
81
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" ) ;
93
91
TestFormat ( " 1234" , "D6" , "001234" ) ;
94
- TestFormat ( "-1234" , "D6" , "-001234" ) ;
92
+ TestFormat ( "-1234" , "D6" , "-001234" ) ;
95
93
96
94
sampleDisplay . WriteOutput ( ) ;
97
95
98
96
}
99
97
100
-
98
+
101
99
[ TestMethod ]
102
100
// the F format can be used with all number types
103
101
public void FixedFormat ( )
104
102
{
105
103
sampleDisplay = new SampleDisplay ( ) ;
106
104
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" ) ;
108
110
TestFormat ( "129" , "F" , "129.00" ) ;
109
111
TestFormat ( "-129" , "F" , "-129.00" ) ;
110
112
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" ) ;
112
114
TestFormat ( "-128" , "F" , "-128.00" ) ;
113
115
TestFormat ( "-128" , "F2" , "-128.00" ) ;
114
116
TestFormat ( "1234" , "F2" , "1234.00" ) ;
@@ -117,18 +119,69 @@ public void FixedFormat()
117
119
TestFormat ( "-1234" , "F6" , "-1234.000000" ) ;
118
120
TestFormat ( "123.78" , "F3" , "123.780" ) ;
119
121
TestFormat ( "123.78" , "F1" , "123.8" ) ;
120
- TestFormat ( "1234.8999" , "F3" , "1234.900" ) ;
122
+ TestFormat ( "1234.8999" , "F3" , "1234.900" ) ;
121
123
122
124
sampleDisplay . WriteOutput ( ) ;
123
125
124
126
}
125
127
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
+
126
177
[ TestMethod ]
127
178
// the G format can be used with all number types
128
179
public void GeneralFormat ( )
129
180
{
130
181
sampleDisplay = new SampleDisplay ( ) ;
131
182
183
+ TestFormat ( "0" , "G" , "0" ) ;
184
+ TestFormat ( "0" , "G4" , "0" ) ;
132
185
TestFormat ( "123" , "G" , "123" ) ;
133
186
TestFormat ( "129" , "G" , "129" ) ;
134
187
TestFormat ( "-129" , "G" , "-129" ) ;
@@ -149,10 +202,12 @@ public void GeneralFormat()
149
202
TestFormat ( "1234.8999" , "G6" , "1234.9" ) ;
150
203
TestFormat ( "1234.8999" , "G7" , "1234.9" ) ;
151
204
TestFormat ( "-1234.901" , "G7" , "-1234.901" ) ;
205
+ TestFormat ( "1.2345E-9" , "G" , "1.2345E-09" ) ;
152
206
153
207
sampleDisplay . WriteOutput ( ) ;
154
208
155
209
}
210
+
156
211
[ TestMethod ]
157
212
// the N format can be used with all number types
158
213
public void NumberFormat ( )
@@ -163,12 +218,12 @@ public void NumberFormat()
163
218
TestFormat ( "129" , "N" , "129.00" ) ;
164
219
TestFormat ( "-129" , "N" , "-129.00" ) ;
165
220
TestFormat ( "128" , "N" , "128.00" ) ;
166
- TestFormat ( "128" , "N4" , "128.0000" ) ;
221
+ TestFormat ( "128" , "N4" , "128.0000" ) ;
167
222
TestFormat ( "-128" , "N" , "-128.00" ) ;
168
223
TestFormat ( "-128" , "N2" , "-128.00" ) ;
169
224
TestFormat ( "1234" , "N2" , "1,234.00" ) ;
170
225
TestFormat ( "-1234" , "N" , "-1,234.00" ) ;
171
- TestFormat ( "1234" , "N6" , "1,234.000000" ) ;
226
+ TestFormat ( "1234" , "N6" , "1,234.000000" ) ;
172
227
TestFormat ( "-1234" , "N6" , "-1,234.000000" ) ;
173
228
TestFormat ( "1234.567" , "N2" , "1,234.57" ) ;
174
229
TestFormat ( "-1234.567" , "N2" , "-1,234.57" ) ;
@@ -208,7 +263,7 @@ public void HexFormat()
208
263
209
264
210
265
#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 )
212
267
{
213
268
double value = Double . Parse ( valueStr ) ; // !!! does not return a negative number when parsed if string value does not contain a decimal point
214
269
bool isNegative = false ;
@@ -371,7 +426,7 @@ private void TestFormatInner(string valueStr, string formatString, string expect
371
426
372
427
private void CheckValue ( object value , string valueStr , string formatString , string expectedResult , SampleDisplay . ColumnType columnType , SampleDisplay . RowData rowData )
373
428
{
374
- string result = String . Format ( $ "{{0:{ formatString } }}", new object [ ] { value } ) ;
429
+ string result = string . Format ( $ "{{0:{ formatString } }}", new object [ ] { value } ) ;
375
430
// for format of X if the number is negative there will be extra F's in the front depending on integer size.
376
431
// because of this we will only check the ending characters for format X types
377
432
if ( formatString . ToUpper ( ) [ 0 ] == 'X' )
@@ -382,7 +437,11 @@ private void CheckValue(object value, string valueStr, string formatString, stri
382
437
{
383
438
Assert . AreEqual ( result , expectedResult , $ "The expected result for '{ formatString } ' on value { valueStr } for type { value . GetType ( ) . Name } is '{ expectedResult } '") ;
384
439
}
385
- rowData . SetResult ( result , columnType ) ;
440
+
441
+ if ( rowData != null )
442
+ {
443
+ rowData . SetResult ( result , columnType ) ;
444
+ }
386
445
}
387
446
388
447
#endregion
@@ -470,19 +529,19 @@ public RowData(string value, string formatString, ArrayList columnInfos)
470
529
471
530
public void SetResult ( string result , ColumnType column )
472
531
{
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 ;
482
541
542
+ }
483
543
}
484
544
}
485
- }
486
545
487
546
public RowData AddRow ( string value , string formatString )
488
547
{
@@ -510,7 +569,7 @@ public void WriteOutput()
510
569
for ( int j = 0 ; j < ( int ) ColumnType . MaxColumns ; j ++ )
511
570
{
512
571
// don't print columns that only have default (n/a) in them
513
- if ( GetColumnInfo ( ( ColumnType ) j ) . DefaultOnly )
572
+ if ( GetColumnInfo ( ( ColumnType ) j ) . DefaultOnly )
514
573
{
515
574
continue ;
516
575
}
@@ -530,5 +589,5 @@ public void WriteOutput()
530
589
}
531
590
532
591
}
533
- #endregion
592
+ #endregion
534
593
}
0 commit comments