Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/07/17 16:44:47 (7 years ago)
Author:
gkronber
Message:

#2661: merged r14284:14286,r14288,r14296,r14298,r14408 from trunk to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Tests

  • stable/HeuristicLab.Tests/HeuristicLab.Problems.Instances.DataAnalysis-3.3/TableFileParserTest.cs

    r14186 r15170  
    2424using System.Globalization;
    2525using System.IO;
     26using System.Text;
    2627using Microsoft.VisualStudio.TestTools.UnitTesting;
    2728
     
    4748        Assert.AreEqual(4, parser.Columns);
    4849        Assert.AreEqual(parser.Values[3][0], 3.14);
    49       } finally {
     50      }
     51      finally {
    5052        File.Delete(tempFileName);
    5153      }
     
    7173        Assert.AreEqual(4, parser.Columns);
    7274        Assert.AreEqual(parser.Values[3][0], 3.14);
    73       } finally {
     75      }
     76      finally {
    7477        File.Delete(tempFileName);
    7578      }
     
    9497        Assert.AreEqual(4, parser.Columns);
    9598        Assert.AreEqual(parser.Values[3][0], 3.14);
    96       } finally {
     99      }
     100      finally {
    97101        File.Delete(tempFileName);
    98102      }
     
    118122        Assert.AreEqual(4, parser.Columns);
    119123        Assert.AreEqual(parser.Values[3][0], 3.14);
    120       } finally {
     124      }
     125      finally {
    121126        File.Delete(tempFileName);
    122127      }
     
    141146        Assert.AreEqual(4, parser.Columns);
    142147        Assert.AreEqual((double)parser.Values[3][0], 3);
    143       } finally {
     148      }
     149      finally {
    144150        File.Delete(tempFileName);
    145151      }
     
    165171        Assert.AreEqual(4, parser.Columns);
    166172        Assert.AreEqual((double)parser.Values[3][0], 3);
    167       } finally {
     173      }
     174      finally {
    168175        File.Delete(tempFileName);
    169176      }
     
    188195        Assert.AreEqual(4, parser.Columns);
    189196        Assert.AreEqual((double)parser.Values[3][0], 3);
    190       } finally {
     197      }
     198      finally {
    191199        File.Delete(tempFileName);
    192200      }
     
    211219        Assert.AreEqual(4, parser.Columns);
    212220        Assert.AreEqual((double)parser.Values[3][0], 3);
    213       } finally {
     221      }
     222      finally {
    214223        File.Delete(tempFileName);
    215224      }
     
    235244        Assert.AreEqual(4, parser.Columns);
    236245        Assert.AreEqual((double)parser.Values[3][0], 3);
    237       } finally {
     246      }
     247      finally {
    238248        File.Delete(tempFileName);
    239249      }
     
    259269        Assert.AreEqual(4, parser.Columns);
    260270        Assert.AreEqual((double)parser.Values[3][0], 3);
    261       } finally {
     271      }
     272      finally {
    262273        File.Delete(tempFileName);
    263274      }
     
    283294        Assert.AreEqual(4, parser.Columns);
    284295        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    285       } finally {
     296      }
     297      finally {
    286298        File.Delete(tempFileName);
    287299      }
     
    307319        Assert.AreEqual(4, parser.Columns);
    308320        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    309       } finally {
     321      }
     322      finally {
    310323        File.Delete(tempFileName);
    311324      }
     
    330343        Assert.AreEqual(4, parser.Columns);
    331344        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    332       } finally {
     345      }
     346      finally {
    333347        File.Delete(tempFileName);
    334348      }
     
    354368        Assert.AreEqual(4, parser.Columns);
    355369        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    356       } finally {
     370      }
     371      finally {
    357372        File.Delete(tempFileName);
    358373      }
     
    377392        Assert.AreEqual(4, parser.Columns);
    378393        Assert.AreEqual((double)parser.Values[3][0], 3);
    379       } finally {
     394      }
     395      finally {
    380396        File.Delete(tempFileName);
    381397      }
     
    401417        Assert.AreEqual(4, parser.Columns);
    402418        Assert.AreEqual((double)parser.Values[3][0], 3);
    403       } finally {
     419      }
     420      finally {
    404421        File.Delete(tempFileName);
    405422      }
     
    424441        Assert.AreEqual(4, parser.Rows);
    425442        Assert.AreEqual(4, parser.Columns);
    426       } finally {
     443      }
     444      finally {
    427445        File.Delete(tempFileName);
    428446      }
     
    447465        Assert.AreEqual(4, parser.Columns);
    448466        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    449       } finally {
     467      }
     468      finally {
    450469        File.Delete(tempFileName);
    451470      }
     
    471490        Assert.AreEqual(4, parser.Columns);
    472491        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    473       } finally {
     492      }
     493      finally {
    474494        File.Delete(tempFileName);
    475495      }
     
    494514        Assert.AreEqual(4, parser.Columns);
    495515        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    496       } finally {
     516      }
     517      finally {
    497518        File.Delete(tempFileName);
    498519      }
     
    518539        Assert.AreEqual(4, parser.Columns);
    519540        Assert.AreEqual((double)parser.Values[3][0], 3.14);
    520       } finally {
     541      }
     542      finally {
    521543        File.Delete(tempFileName);
    522544      }
     
    539561        Assert.AreEqual(3, parser.Rows);
    540562        Assert.AreEqual(4507, parser.Columns);
    541       } finally {
     563      }
     564      finally {
    542565        File.Delete(tempFileName);
    543566      }
     
    562585        Assert.AreEqual(4, parser.Columns);
    563586        Assert.AreEqual((double)parser.Values[3][0], 3);
    564       } finally {
     587      }
     588      finally {
    565589        File.Delete(tempFileName);
    566590      }
     
    586610        Assert.AreEqual(4, parser.Columns);
    587611        Assert.AreEqual((double)parser.Values[3][0], 3);
    588       } finally {
     612      }
     613      finally {
    589614        File.Delete(tempFileName);
    590615      }
     
    627652        Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in DE-DE NumberFormat just "unendlich" is not allowed (compare with InvariantCulture)
    628653        Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4]));
    629       } finally {
     654      }
     655      finally {
    630656        File.Delete(tempFileName);
    631657      }
     
    664690        Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in InvariantCulture +Infinity is not allowed (compare with DE-DE)
    665691        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
     7123.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
     7393.14,
     740,missing val
     741str,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
     7703.1.2016,
     771,missing val
     772str,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
     80419.6.2016,date
     80519.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
     8202016/6/19,date
     8212016/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
     8438:15,time value
     8449: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 (IOException) {
     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        }
    668929      }
    669930    }
Note: See TracChangeset for help on using the changeset viewer.