Changeset 14284
- Timestamp:
- 09/17/16 11:24:10 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.Instances.DataAnalysis-3.3/TableFileParserTest.cs
r14185 r14284 24 24 using System.Globalization; 25 25 using System.IO; 26 using System.Text; 26 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 27 28 … … 47 48 Assert.AreEqual(4, parser.Columns); 48 49 Assert.AreEqual(parser.Values[3][0], 3.14); 49 } finally { 50 } 51 finally { 50 52 File.Delete(tempFileName); 51 53 } … … 71 73 Assert.AreEqual(4, parser.Columns); 72 74 Assert.AreEqual(parser.Values[3][0], 3.14); 73 } finally { 75 } 76 finally { 74 77 File.Delete(tempFileName); 75 78 } … … 94 97 Assert.AreEqual(4, parser.Columns); 95 98 Assert.AreEqual(parser.Values[3][0], 3.14); 96 } finally { 99 } 100 finally { 97 101 File.Delete(tempFileName); 98 102 } … … 118 122 Assert.AreEqual(4, parser.Columns); 119 123 Assert.AreEqual(parser.Values[3][0], 3.14); 120 } finally { 124 } 125 finally { 121 126 File.Delete(tempFileName); 122 127 } … … 141 146 Assert.AreEqual(4, parser.Columns); 142 147 Assert.AreEqual((double)parser.Values[3][0], 3); 143 } finally { 148 } 149 finally { 144 150 File.Delete(tempFileName); 145 151 } … … 165 171 Assert.AreEqual(4, parser.Columns); 166 172 Assert.AreEqual((double)parser.Values[3][0], 3); 167 } finally { 173 } 174 finally { 168 175 File.Delete(tempFileName); 169 176 } … … 188 195 Assert.AreEqual(4, parser.Columns); 189 196 Assert.AreEqual((double)parser.Values[3][0], 3); 190 } finally { 197 } 198 finally { 191 199 File.Delete(tempFileName); 192 200 } … … 211 219 Assert.AreEqual(4, parser.Columns); 212 220 Assert.AreEqual((double)parser.Values[3][0], 3); 213 } finally { 221 } 222 finally { 214 223 File.Delete(tempFileName); 215 224 } … … 235 244 Assert.AreEqual(4, parser.Columns); 236 245 Assert.AreEqual((double)parser.Values[3][0], 3); 237 } finally { 246 } 247 finally { 238 248 File.Delete(tempFileName); 239 249 } … … 259 269 Assert.AreEqual(4, parser.Columns); 260 270 Assert.AreEqual((double)parser.Values[3][0], 3); 261 } finally { 271 } 272 finally { 262 273 File.Delete(tempFileName); 263 274 } … … 283 294 Assert.AreEqual(4, parser.Columns); 284 295 Assert.AreEqual((double)parser.Values[3][0], 3.14); 285 } finally { 296 } 297 finally { 286 298 File.Delete(tempFileName); 287 299 } … … 307 319 Assert.AreEqual(4, parser.Columns); 308 320 Assert.AreEqual((double)parser.Values[3][0], 3.14); 309 } finally { 321 } 322 finally { 310 323 File.Delete(tempFileName); 311 324 } … … 330 343 Assert.AreEqual(4, parser.Columns); 331 344 Assert.AreEqual((double)parser.Values[3][0], 3.14); 332 } finally { 345 } 346 finally { 333 347 File.Delete(tempFileName); 334 348 } … … 354 368 Assert.AreEqual(4, parser.Columns); 355 369 Assert.AreEqual((double)parser.Values[3][0], 3.14); 356 } finally { 370 } 371 finally { 357 372 File.Delete(tempFileName); 358 373 } … … 377 392 Assert.AreEqual(4, parser.Columns); 378 393 Assert.AreEqual((double)parser.Values[3][0], 3); 379 } finally { 394 } 395 finally { 380 396 File.Delete(tempFileName); 381 397 } … … 401 417 Assert.AreEqual(4, parser.Columns); 402 418 Assert.AreEqual((double)parser.Values[3][0], 3); 403 } finally { 419 } 420 finally { 404 421 File.Delete(tempFileName); 405 422 } … … 424 441 Assert.AreEqual(4, parser.Rows); 425 442 Assert.AreEqual(4, parser.Columns); 426 } finally { 443 } 444 finally { 427 445 File.Delete(tempFileName); 428 446 } … … 447 465 Assert.AreEqual(4, parser.Columns); 448 466 Assert.AreEqual((double)parser.Values[3][0], 3.14); 449 } finally { 467 } 468 finally { 450 469 File.Delete(tempFileName); 451 470 } … … 471 490 Assert.AreEqual(4, parser.Columns); 472 491 Assert.AreEqual((double)parser.Values[3][0], 3.14); 473 } finally { 492 } 493 finally { 474 494 File.Delete(tempFileName); 475 495 } … … 494 514 Assert.AreEqual(4, parser.Columns); 495 515 Assert.AreEqual((double)parser.Values[3][0], 3.14); 496 } finally { 516 } 517 finally { 497 518 File.Delete(tempFileName); 498 519 } … … 518 539 Assert.AreEqual(4, parser.Columns); 519 540 Assert.AreEqual((double)parser.Values[3][0], 3.14); 520 } finally { 541 } 542 finally { 521 543 File.Delete(tempFileName); 522 544 } … … 539 561 Assert.AreEqual(3, parser.Rows); 540 562 Assert.AreEqual(4507, parser.Columns); 541 } finally { 563 } 564 finally { 542 565 File.Delete(tempFileName); 543 566 } … … 562 585 Assert.AreEqual(4, parser.Columns); 563 586 Assert.AreEqual((double)parser.Values[3][0], 3); 564 } finally { 587 } 588 finally { 565 589 File.Delete(tempFileName); 566 590 } … … 586 610 Assert.AreEqual(4, parser.Columns); 587 611 Assert.AreEqual((double)parser.Values[3][0], 3); 588 } finally { 612 } 613 finally { 589 614 File.Delete(tempFileName); 590 615 } … … 627 652 Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in DE-DE NumberFormat just "unendlich" is not allowed (compare with InvariantCulture) 628 653 Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4])); 629 } finally { 654 } 655 finally { 630 656 File.Delete(tempFileName); 631 657 } … … 664 690 Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in InvariantCulture +Infinity is not allowed (compare with DE-DE) 665 691 Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4])); 666 } finally { 667 File.Delete(tempFileName); 692 } 693 finally { 694 File.Delete(tempFileName); 695 } 696 } 697 698 699 [TestMethod] 700 [TestCategory("Problems.Instances")] 701 [TestProperty("Time", "short")] 702 public void ParseWithTypeConversion() { 703 // the parser tries to determine the column type (double, datetime, string) by looking at the values in the first few rows 704 // if the values are of a different type then the type of the column is converted 705 { 706 // case 1 707 // default for values is double and therefore a column with all missing values should be List<double> and contain NaN 708 var tmpFileName = Path.GetTempFileName(); 709 WriteToFile(tmpFileName, 710 @"stringCol,note 711 ,missing val 712 3.14,double 713 "); 714 715 TableFileParser parser = new TableFileParser(); 716 try { 717 parser.Parse(tmpFileName, 718 CultureInfo.InvariantCulture.NumberFormat, 719 CultureInfo.InvariantCulture.DateTimeFormat, 720 separator: ',', columnNamesInFirstLine: true); 721 Assert.IsTrue(parser.Values[0] is List<double>); 722 Assert.AreEqual(double.NaN, parser.Values[0][0]); 723 Assert.AreEqual(3.14, parser.Values[0][1]); 724 } 725 finally { 726 File.Delete(tmpFileName); 727 } 728 729 } 730 731 { 732 // case 2 733 // 'The first missing values are replaced with double.NaN while the last ones with string.Empty.' 734 735 var tmpFileName = Path.GetTempFileName(); 736 WriteToFile(tmpFileName, 737 @"stringCol,note 738 ,missing val 739 3.14, 740 ,missing val 741 str,a string --> column is converted to List<string> 742 ,missing val 743 "); 744 745 TableFileParser parser = new TableFileParser(); 746 try { 747 parser.Parse(tmpFileName, 748 CultureInfo.InvariantCulture.NumberFormat, 749 CultureInfo.InvariantCulture.DateTimeFormat, 750 separator: ',', columnNamesInFirstLine: true); 751 Assert.IsTrue(parser.Values[0] is List<string>); 752 Assert.AreEqual(string.Empty, parser.Values[0][0]); 753 Assert.AreEqual("3.14", parser.Values[0][1]); 754 Assert.AreEqual(string.Empty, parser.Values[0][2]); 755 Assert.AreEqual("str", parser.Values[0][3]); 756 Assert.AreEqual(string.Empty, parser.Values[0][4]); 757 } 758 finally { 759 File.Delete(tmpFileName); 760 } 761 } 762 763 { 764 // case 3 765 // DateTime conversion to strings 766 var tmpFileName = Path.GetTempFileName(); 767 WriteToFile(tmpFileName, 768 @"stringCol,note 769 ,missing val 770 3.1.2016, 771 ,missing val 772 str,a string --> column is converted to List<string> 773 ,missing val 774 "); 775 776 TableFileParser parser = new TableFileParser(); 777 try { 778 parser.Parse(tmpFileName, 779 CultureInfo.InvariantCulture.NumberFormat, 780 CultureInfo.InvariantCulture.DateTimeFormat, 781 separator: ',', columnNamesInFirstLine: true); 782 Assert.IsTrue(parser.Values[0] is List<string>); 783 Assert.AreEqual(string.Empty, parser.Values[0][0]); 784 Assert.AreEqual("3.1.2016", parser.Values[0][1]); 785 Assert.AreEqual(string.Empty, parser.Values[0][2]); 786 Assert.AreEqual("str", parser.Values[0][3]); 787 Assert.AreEqual(string.Empty, parser.Values[0][4]); 788 } 789 finally { 790 File.Delete(tmpFileName); 791 } 792 } 793 } 794 795 [TestMethod] 796 [TestCategory("Problems.Instances")] 797 [TestProperty("Time", "short")] 798 public void ParseDateTime() { 799 { 800 // case 1 dates and datetimes should be parsed as datetime column 801 var tmpFileName = Path.GetTempFileName(); 802 WriteToFile(tmpFileName, 803 @"stringCol,note 804 19.6.2016,date 805 19.6.2016 8:15,datetime 806 "); 807 808 TableFileParser parser = new TableFileParser(); 809 try { 810 parser.Parse(tmpFileName, 811 CultureInfo.GetCultureInfo("de-de").NumberFormat, 812 CultureInfo.GetCultureInfo("de-de").DateTimeFormat, 813 separator: ',', columnNamesInFirstLine: true); 814 Assert.IsTrue(parser.Values[0] is List<DateTime>); 815 Assert.AreEqual(new DateTime(2016, 6, 19), parser.Values[0][0]); 816 Assert.AreEqual(new DateTime(2016, 6, 19, 8, 15, 0), parser.Values[0][1]); 817 818 WriteToFile(tmpFileName, 819 @"stringCol,note 820 2016/6/19,date 821 2016/6/19 8:15,datetime 822 "); 823 824 parser.Parse(tmpFileName, 825 CultureInfo.InvariantCulture.NumberFormat, 826 CultureInfo.InvariantCulture.DateTimeFormat, 827 separator: ',', columnNamesInFirstLine: true); 828 Assert.IsTrue(parser.Values[0] is List<DateTime>); 829 Assert.AreEqual(new DateTime(2016, 6, 19), parser.Values[0][0]); 830 Assert.AreEqual(new DateTime(2016, 6, 19, 8, 15, 0), parser.Values[0][1]); 831 } 832 833 finally { 834 File.Delete(tmpFileName); 835 } 836 } 837 838 { 839 // case 2 never parse time values as datetimes 840 var tmpFileName = Path.GetTempFileName(); 841 WriteToFile(tmpFileName, 842 @"stringCol,note 843 8:15,time value 844 9:40,time value 845 "); 846 847 TableFileParser parser = new TableFileParser(); 848 try { 849 parser.Parse(tmpFileName, 850 CultureInfo.InvariantCulture.NumberFormat, 851 CultureInfo.InvariantCulture.DateTimeFormat, 852 separator: ',', columnNamesInFirstLine: true); 853 Assert.IsTrue(parser.Values[0] is List<string>); // time values should be parsed as strings 854 Assert.AreEqual("8:15", parser.Values[0][0]); 855 Assert.AreEqual("9:40", parser.Values[0][1]); 856 } 857 finally { 858 File.Delete(tmpFileName); 859 } 860 } 861 } 862 863 864 865 [TestMethod] 866 [TestCategory("Problems.Instances")] 867 [TestProperty("Time", "short")] 868 public void CheckTypeConversionAndLongFiles() { 869 { 870 // case 1 incorrect input after 500 rows should lead to exceptions 871 var tmpFileName = Path.GetTempFileName(); 872 // create input data 873 var sb = new StringBuilder(); 874 sb.AppendLine("col1,col2"); 875 for (int r = 0; r < 2000; r++) { 876 sb.AppendLine("3.15, 3.15"); 877 } 878 // add a row with only one input value 879 sb.AppendLine("3.15"); 880 881 WriteToFile(tmpFileName, sb.ToString()); 882 883 TableFileParser parser = new TableFileParser(); 884 try { 885 parser.Parse(tmpFileName, 886 CultureInfo.InvariantCulture.NumberFormat, 887 CultureInfo.InvariantCulture.DateTimeFormat, 888 separator: ',', columnNamesInFirstLine: true); 889 // Parse should fail with an exception 890 Assert.Fail("expected exception TableFileParser.DataFormatException"); 891 } 892 catch (TableFileParser.DataFormatException) { 893 // ignore the expected exception 894 } 895 896 finally { 897 File.Delete(tmpFileName); 898 } 899 } 900 { 901 // case 2 902 var tmpFileName = Path.GetTempFileName(); 903 // create input data 904 var sb = new StringBuilder(); 905 sb.AppendLine("doubleCol,stringCol"); 906 for (int r = 0; r < 2000; r++) { 907 sb.AppendLine("3.15, 3.15"); 908 } 909 // add a row with a string value --> the column should be converted to string 910 sb.AppendLine("3.15,str"); 911 912 WriteToFile(tmpFileName, sb.ToString()); 913 914 TableFileParser parser = new TableFileParser(); 915 try { 916 parser.Parse(tmpFileName, 917 CultureInfo.InvariantCulture.NumberFormat, 918 CultureInfo.InvariantCulture.DateTimeFormat, 919 separator: ',', columnNamesInFirstLine: true); 920 Assert.IsTrue(parser.Values[0] is List<double>); 921 Assert.IsTrue(parser.Values[1] is List<string>); 922 Assert.AreEqual(parser.Values[1][0], "3.15"); 923 Assert.AreEqual(parser.Values[1][2000], "str"); 924 } 925 926 finally { 927 File.Delete(tmpFileName); 928 } 668 929 } 669 930 }
Note: See TracChangeset
for help on using the changeset viewer.