Changeset 13525


Ignore:
Timestamp:
01/16/16 15:21:38 (5 years ago)
Author:
gkronber
Message:

#2071 added unit test for type conversion of columns

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.Instances.DataAnalysis-3.3/TableFileParserTest.cs

    r12012 r13525  
    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      WriteToFile(tempFileName,
     604      "str\tdbl\tdbl\tdbl" + Environment.NewLine +
     605      "1,3\t1,3\t0\t3" + Environment.NewLine +
     606      "1,3\t\t0\t0" + Environment.NewLine +
     607      "s\tn. def.\t0\t0" + Environment.NewLine +
     608      "s\t+unendlich\t0\t0" + Environment.NewLine +
     609      "s\t-unendlich\t0\t0" + Environment.NewLine +
     610      "s\t0\t0\t0");
     611      TableFileParser parser = new TableFileParser();
     612      try {
     613        parser.Parse(tempFileName,
     614          CultureInfo.GetCultureInfo("DE-DE").NumberFormat,
     615          CultureInfo.GetCultureInfo("DE-DE").DateTimeFormat,
     616          '\t',
     617          parser.AreColumnNamesInFirstLine(tempFileName));
     618        Assert.AreEqual(6, parser.Rows);
     619        Assert.AreEqual(4, parser.Columns);
     620        Assert.IsTrue(parser.Values[0] is List<string>);
     621        Assert.IsTrue(parser.Values[1] is List<double>);
     622        Assert.IsTrue(parser.Values[2] is List<double>);
     623        Assert.IsTrue(parser.Values[3] is List<double>);
     624        Assert.IsTrue(double.IsNaN((double)parser.Values[1][1])); // missing value
     625        Assert.IsTrue(double.IsNaN((double)parser.Values[1][2]));
     626        Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in DE-DE NumberFormat just "unendlich" is not allowed (compare with InvariantCulture)
     627        Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4]));
     628      } finally {
     629        File.Delete(tempFileName);
     630      }
     631    }
     632
     633    [TestMethod]
     634    [TestCategory("Problems.Instances")]
     635    [TestProperty("Time", "short")]
     636    public void ParseWithColumnTypeConversionInvariant() {
     637      // see ParseWithColumnTypeConversionDE above
     638      // same routine only using invariant culture
     639      string tempFileName = Path.GetTempFileName();
     640      WriteToFile(tempFileName,
     641      @"str,dbl,dbl,dbl
     6421.3,1.3,0,3
     6431.3,,0,0
     644s,NaN,0,0
     645s,Infinity,0,0
     646s,-Infinity,0,0
     647s,0,0,0");
     648      TableFileParser parser = new TableFileParser();
     649      try {
     650        parser.Parse(tempFileName,
     651          CultureInfo.InvariantCulture.NumberFormat,
     652          CultureInfo.InvariantCulture.DateTimeFormat,
     653          ',',
     654          parser.AreColumnNamesInFirstLine(tempFileName));
     655        Assert.AreEqual(6, parser.Rows);
     656        Assert.AreEqual(4, parser.Columns);
     657        Assert.IsTrue(parser.Values[0] is List<string>);
     658        Assert.IsTrue(parser.Values[1] is List<double>);
     659        Assert.IsTrue(parser.Values[2] is List<double>);
     660        Assert.IsTrue(parser.Values[3] is List<double>);
     661        Assert.IsTrue(double.IsNaN((double)parser.Values[1][1])); // missing value
     662        Assert.IsTrue(double.IsNaN((double)parser.Values[1][2]));
     663        Assert.IsTrue(double.IsPositiveInfinity((double)parser.Values[1][3])); // NOTE: in InvariantCulture +Infinity is not allowed (compare with DE-DE)
     664        Assert.IsTrue(double.IsNegativeInfinity((double)parser.Values[1][4]));
     665      } finally {
     666        File.Delete(tempFileName);
     667      }
     668    }
     669
    591670    private void WriteToFile(string fileName, string content) {
    592671      using (StreamWriter writer = new StreamWriter(fileName)) {
Note: See TracChangeset for help on using the changeset viewer.