Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11373


Ignore:
Timestamp:
09/16/14 16:49:37 (10 years ago)
Author:
mkommend
Message:

#1802: Merged r10896 and r11352 to stable.

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Persistence

  • stable/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs

    r11170 r11373  
    7878    /// </returns>
    7979    public bool CanSerialize(Type type) {
    80       return numberSerializerMap.ContainsKey(type);
     80      return numberSerializerMap.ContainsKey(Nullable.GetUnderlyingType(type) ?? type);
    8181    }
    8282
     
    9090    /// </returns>
    9191    public string JustifyRejection(Type type) {
    92       return string.Format("not a number type (one of {0})",
     92      return string.Format("not a (nullable) number type (one of {0})",
    9393        string.Join(", ", numberSerializers.Select(n => n.SourceType.Name).ToArray()));
    9494    }
     
    100100    /// <returns></returns>
    101101    public string Format(object obj) {
    102       return ((XmlString)numberSerializerMap[obj.GetType()].Format(obj)).Data;
     102      if (obj == null) return "null";
     103      Type type = obj.GetType();
     104      return ((XmlString)numberSerializerMap[Nullable.GetUnderlyingType(type) ?? type].Format(obj)).Data;
    103105    }
    104106
     
    110112    /// <returns></returns>
    111113    public object Parse(string stringValue, Type type) {
     114      if (stringValue == "null") return null;
    112115      try {
    113         return numberSerializerMap[type].Parse(new XmlString(stringValue));
     116        return numberSerializerMap[Nullable.GetUnderlyingType(type) ?? type].Parse(new XmlString(stringValue));
    114117      }
    115118      catch (FormatException e) {
  • stable/HeuristicLab.Tests

  • stable/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCases.cs

    r11170 r11373  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2424using System.Collections.Generic;
    2525using System.Drawing;
     26using System.Globalization;
    2627using System.IO;
    2728using System.Linq;
     
    14411442    }
    14421443
     1444    [TestMethod]
     1445    [TestCategory("Persistence")]
     1446    [TestProperty("Time", "short")]
     1447    public void TestSpecialCharacters() {
     1448      var s = "abc" + "\x15" + "def";
     1449      XmlGenerator.Serialize(s, tempFile);
     1450      var newS = XmlParser.Deserialize(tempFile);
     1451      Assert.AreEqual(s, newS);
     1452    }
     1453
     1454    [TestMethod]
     1455    [TestCategory("Persistence")]
     1456    [TestProperty("Time", "short")]
     1457    public void TestByteArray() {
     1458      var b = new byte[3];
     1459      b[0] = 0;
     1460      b[1] = 200;
     1461      b[2] = byte.MaxValue;
     1462      XmlGenerator.Serialize(b, tempFile);
     1463      var newB = (byte[]) XmlParser.Deserialize(tempFile);
     1464      CollectionAssert.AreEqual(b, newB);
     1465    }
     1466
     1467    [TestMethod]
     1468    [TestCategory("Persistence")]
     1469    [TestProperty("Time", "short")]
     1470    public void TestOptionalNumberEnumerable() {
     1471      var values = new List<double?> {0, null, double.NaN, double.PositiveInfinity, double.MaxValue, 1};
     1472      XmlGenerator.Serialize(values, tempFile);
     1473      var newValues = (List<double?>) XmlParser.Deserialize(tempFile);
     1474      CollectionAssert.AreEqual(values, newValues);
     1475    }
     1476
     1477    [TestMethod]
     1478    [TestCategory("Persistence")]
     1479    [TestProperty("Time", "short")]
     1480    public void TestOptionalDateTimeEnumerable() {
     1481      var values = new List<DateTime?> { DateTime.MinValue, null, DateTime.Now, DateTime.Now.Add(TimeSpan.FromDays(1)),
     1482        DateTime.ParseExact("10.09.2014 12:21", "dd.MM.yyyy hh:mm", CultureInfo.InvariantCulture), DateTime.MaxValue};
     1483      XmlGenerator.Serialize(values, tempFile);
     1484      var newValues = (List<DateTime?>) XmlParser.Deserialize(tempFile);
     1485      CollectionAssert.AreEqual(values, newValues);
     1486    }
     1487
     1488    [TestMethod]
     1489    [TestCategory("Persistence")]
     1490    [TestProperty("Time", "short")]
     1491    public void TestStringEnumerable() {
     1492      var values = new List<string> {"", null, "s", "string", string.Empty, "123", "<![CDATA[nice]]>", "<![CDATA[nasty unterminated"};
     1493      XmlGenerator.Serialize(values, tempFile);
     1494      var newValues = (List<String>) XmlParser.Deserialize(tempFile);
     1495      CollectionAssert.AreEqual(values, newValues);
     1496    }
     1497
     1498    [TestMethod]
     1499    [TestCategory("Persistence")]
     1500    [TestProperty("Time", "short")]
     1501    public void TestUnicodeCharArray() {
     1502      var s = Encoding.UTF8.GetChars(new byte[] {0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb});
     1503      XmlGenerator.Serialize(s, tempFile);
     1504      var newS = (char[])XmlParser.Deserialize(tempFile);
     1505      CollectionAssert.AreEqual(s, newS);
     1506    }
     1507
     1508    [TestMethod]
     1509    [TestCategory("Persistence")]
     1510    [TestProperty("Time", "short")]
     1511    public void TestUnicode() {
     1512      var s = Encoding.UTF8.GetString(new byte[] {0, 1, 2, 03, 04, 05, 06, 07, 08, 09, 0xa, 0xb});
     1513      XmlGenerator.Serialize(s, tempFile);
     1514      var newS = XmlParser.Deserialize(tempFile);
     1515      Assert.AreEqual(s, newS);
     1516    }
     1517
     1518
    14431519    [ClassInitialize]
    14441520    public static void Initialize(TestContext testContext) {
Note: See TracChangeset for help on using the changeset viewer.