Changeset 14739 for branches/PersistenceOverhaul/HeuristicLab.Tests
- Timestamp:
- 03/09/17 13:52:38 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs
r14714 r14739 33 33 using HeuristicLab.Algorithms.GeneticAlgorithm; 34 34 using HeuristicLab.Common; 35 using HeuristicLab.Core; 35 36 using HeuristicLab.Data; 36 37 using HeuristicLab.Persistence; … … 2104 2105 } 2105 2106 2107 2108 #region conversion 2109 2110 [StorableType("F9F51075-490C-48E3-BF64-14514A210149")] 2111 private class OldBaseType { 2112 [Storable] 2113 public ItemCollection<IItem> items; 2114 } 2115 [StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75")] 2116 private class OldType : OldBaseType { 2117 [Storable] 2118 public IntValue a; 2119 2120 [Storable] 2121 public ItemList<IntValue> vals; 2122 2123 [Storable] 2124 public OldType mySelf; 2125 2126 [Storable] 2127 public Tuple<int, int> tup; 2128 2129 [Storable] 2130 public int x; 2131 [Storable] 2132 public int y; 2133 } 2134 2135 2136 [StorableType("B72C58C8-3321-4706-AA94-578F57337070")] 2137 private class NewBaseType { 2138 [Storable] 2139 public DoubleValue[] items; 2140 } 2141 [StorableType("00000000-0000-0000-0000-BADCAFFEE000")] // for testing 2142 private partial class NewType : NewBaseType { 2143 [Storable] 2144 public int a; 2145 2146 [Storable] 2147 public int[] val; 2148 2149 [Storable] 2150 public NewType mySelf; 2151 2152 [Storable] 2153 public int TupItem1; 2154 2155 [Storable] 2156 public int TupItem2; 2157 2158 [Storable] 2159 public Point coords; 2160 } 2161 2162 // conversion part 2163 private partial class NewType : NewBaseType { 2164 //[Storable(AllowOneWay = true, Name = "items")] 2165 //private ItemCollection<IItem> itemsCompat { 2166 // set { items = value.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray(); } 2167 //} 2168 2169 //[Storable(AllowOneWay = true, Name = "a")] 2170 //private IntValue aBackwardsCompat { set { a = value.Value; } } 2171 2172 //[StorableConversion(Name = "a", Version=1)] 2173 //private int ConvertA(IntValue oldValue) { return oldValue.Value; } 2174 //[StorableConversion(Name = "newA", Version=2)] 2175 //private double ConvertA(int oldValue) { return (double)oldValue; } 2176 2177 //[StorableConversion(OldNames= new [] {"x", "y"}, Name = "point", Version=2)] 2178 //public Point ConvertXY(int x, int y) { return new Point(x, y); } 2179 2180 //[StorableConversion(OldNames= new [] {tup}, Names = [] {"TupItem1", "TupItem2"}, Version=2)] 2181 //public IEnumerable<object> ConvertTup(Tuple<int, int> tup) { 2182 // yield return tup.Item1; 2183 // yield return tup.Item2; 2184 //} 2185 // 2186 //[Storable(AllowOneWay = true, Name = "val")] 2187 //private ItemList<IntValue> valCompat { set { val = value.Select(iv => iv.Value).ToArray(); } } 2188 // 2189 //[Storable(AllowOneWay = true, Name = "mySelf")] 2190 //private NewType mySelfCompat { set { mySelf = value; } } 2191 2192 2193 2194 //[StorableConversion(SrcVersion = 1)] 2195 //private Dictionary<string, object> ConvertAll(Dictionary<string, object> values) 2196 //{ 2197 // 2198 //} 2199 } 2200 2201 [TestMethod] 2202 [TestCategory("Persistence4")] 2203 [TestProperty("Time", "short")] 2204 public void TestConversion() { 2205 var test = new Func<OldType>(() => { 2206 var p = new OldType(); 2207 p.a = new IntValue(1); 2208 p.mySelf = p; 2209 p.vals = new ItemList<IntValue>(new IntValue[] { p.a, new IntValue(2), new IntValue(3) }); 2210 var dv = new DoubleValue(1.0); 2211 p.items = new ItemCollection<IItem>(new IItem[] { dv, dv, p.a }); 2212 return p; 2213 }); 2214 2215 ProtoBufSerializer serializer = new ProtoBufSerializer(); 2216 var old = test(); 2217 serializer.Serialize(old, tempFile); 2218 Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(OldType)).Guid); 2219 Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(OldType)).Guid, typeof(NewType)); 2220 2221 object o = serializer.Deserialize(tempFile); 2222 var restored = (NewType)o; 2223 Assert.AreEqual(restored.a, old.a.Value); 2224 Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value))); 2225 Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value))); 2226 Assert.AreSame(restored.items[0], restored.items[1]); 2227 Assert.AreSame(restored, restored.mySelf); 2228 2229 string msg = Profile(test); 2230 Console.WriteLine(msg); 2231 } 2232 2233 #endregion 2234 2106 2235 [TestMethod] 2107 2236 [TestCategory("Persistence4")]
Note: See TracChangeset
for help on using the changeset viewer.