Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/02/16 08:15:07 (8 years ago)
Author:
gkronber
Message:

#2071: merged r13411,r13413,r13414,r13415,r13419,r13440,r13441,r13442,r13445,r13447,r13525,r13526,r13529,r13584,r13901,r13925 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

    r12009 r13974  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Globalization;
    2325using System.IO;
    2426using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    589591    }
    590592
     593
     594    [TestMethod]
     595    [TestCategory("Problems.Instances")]
     596    [TestProperty("Time", "short")]
     597    public void ParseWithColumnTypeConversionDE() {
     598      // If first entry of a column can be parsed as a double we assume all values are doubles.
     599      // However, if any of the following entries cannot be parsed as a double we convert the whole column to a string column.
     600      // Special care needs to be taken with missing values, NaN (n.def.) and infinity values.
     601      // We only support DE-DE and InvariantCulture number formats
     602      string tempFileName = Path.GetTempFileName();
     603      var deCultureInfo = CultureInfo.GetCultureInfo("DE-DE");
     604      WriteToFile(tempFileName,
     605      "str\tdbl\tdbl\tdbl" + Environment.NewLine +
     606      "1,3\t1,3\t0\t3" + Environment.NewLine +
     607      "1,3\t\t0\t0" + Environment.NewLine +
     608      "s\t" + double.NaN.ToString(deCultureInfo) + "\t0\t0" + Environment.NewLine + // double.NaN might have a different string representation on different systems (even when using the same CultureInfo)
     609      "s\t" + double.PositiveInfinity.ToString(deCultureInfo) + "\t0\t0" + Environment.NewLine +
     610      "s\t" + double.NegativeInfinity.ToString(deCultureInfo) + "\t0\t0" + Environment.NewLine +
     611      "s\t0\t0\t0");
     612      TableFileParser parser = new TableFileParser();
     613      try {
     614        parser.Parse(tempFileName,
     615          deCultureInfo.NumberFormat,
     616          deCultureInfo.DateTimeFormat,
     617          '\t',
     618          true);
     619        Assert.AreEqual(6, parser.Rows);
     620        Assert.AreEqual(4, parser.Columns);
     621        Assert.IsTrue(parser.Values[0] is List<string>);
     622        Assert.IsTrue(parser.Values[1] is List<double>);
     623        Assert.IsTrue(parser.Values[2] is List<double>);
     624        Assert.IsTrue(parser.Values[3] is List<double>);
     625        Assert.IsTrue(double.IsNaN((double)parser.Values[1][1])); // missing value
     626        Assert.IsTrue(double.IsNaN((double)parser.Values[1][2]));
     627        Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in DE-DE NumberFormat just "unendlich" is not allowed (compare with InvariantCulture)
     628        Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4]));
     629      } finally {
     630        File.Delete(tempFileName);
     631      }
     632    }
     633
     634    [TestMethod]
     635    [TestCategory("Problems.Instances")]
     636    [TestProperty("Time", "short")]
     637    public void ParseWithColumnTypeConversionInvariant() {
     638      // see ParseWithColumnTypeConversionDE above
     639      // same routine only using invariant culture
     640      string tempFileName = Path.GetTempFileName();
     641      WriteToFile(tempFileName,
     642      @"str,dbl,dbl,dbl
     6431.3,1.3,0,3
     6441.3,,0,0
     645s,NaN,0,0
     646s,Infinity,0,0
     647s,-Infinity,0,0
     648s,0,0,0");
     649      TableFileParser parser = new TableFileParser();
     650      try {
     651        parser.Parse(tempFileName,
     652          CultureInfo.InvariantCulture.NumberFormat,
     653          CultureInfo.InvariantCulture.DateTimeFormat,
     654          ',',
     655          parser.AreColumnNamesInFirstLine(tempFileName));
     656        Assert.AreEqual(6, parser.Rows);
     657        Assert.AreEqual(4, parser.Columns);
     658        Assert.IsTrue(parser.Values[0] is List<string>);
     659        Assert.IsTrue(parser.Values[1] is List<double>);
     660        Assert.IsTrue(parser.Values[2] is List<double>);
     661        Assert.IsTrue(parser.Values[3] is List<double>);
     662        Assert.IsTrue(double.IsNaN((double)parser.Values[1][1])); // missing value
     663        Assert.IsTrue(double.IsNaN((double)parser.Values[1][2]));
     664        Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in InvariantCulture +Infinity is not allowed (compare with DE-DE)
     665        Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4]));
     666      } finally {
     667        File.Delete(tempFileName);
     668      }
     669    }
     670
    591671    private void WriteToFile(string fileName, string content) {
    592672      using (StreamWriter writer = new StreamWriter(fileName)) {
Note: See TracChangeset for help on using the changeset viewer.