|
19 | 19 | using FluentAssertions;
|
20 | 20 | using Hyperion.Extensions;
|
21 | 21 | using Xunit;
|
| 22 | +using Xunit.Abstractions; |
22 | 23 |
|
23 | 24 | namespace Hyperion.Tests
|
24 | 25 | {
|
25 | 26 |
|
26 | 27 | public class Bugs
|
27 | 28 | {
|
| 29 | + private readonly ITestOutputHelper _output; |
| 30 | + |
| 31 | + public Bugs(ITestOutputHelper output) |
| 32 | + { |
| 33 | + _output = output; |
| 34 | + } |
| 35 | + |
28 | 36 | #region issue 58
|
29 | 37 |
|
30 | 38 | public enum TrustLevel { Unknown, Suspicious, Partial, Fully }
|
@@ -249,25 +257,223 @@ public Recover(SnapshotSelectionCriteria fromSnapshot, long toSequenceNr = long.
|
249 | 257 | public long ReplayMax { get; private set; }
|
250 | 258 | }
|
251 | 259 |
|
252 |
| - class Temp |
| 260 | + delegate int TestDelegate(int x, int y); |
| 261 | + |
| 262 | + class Temp : IEquatable<Temp> |
253 | 263 | {
|
254 | 264 | public object[] SubArray { get; set; }
|
255 |
| - public string aa { get; set; } |
256 |
| - public Dictionary<string,string> dc { get; set; } |
| 265 | + public int[] IntArray { get; set; } |
| 266 | + public int[,] IntIntArray { get; set; } |
| 267 | + public Poco Poco { get; set; } |
| 268 | + public string String { get; set; } |
| 269 | + public Dictionary<int,string> Dictionary { get; set; } |
| 270 | + public TestDelegate Delegate { get; set; } |
| 271 | + public IEnumerable<int> TestEnum { get; set; } |
| 272 | + public Exception Exception { get; set; } |
| 273 | + public ImmutableList<int> ImmutableList { get; set; } |
| 274 | + public ImmutableDictionary<int, string> ImmutableDictionary { get; set; } |
| 275 | + |
| 276 | + public bool Equals(Temp other) |
| 277 | + { |
| 278 | + if (other == null) |
| 279 | + throw new Exception("Equals failed."); |
| 280 | + if (ReferenceEquals(this, other)) |
| 281 | + throw new Exception("Equals failed."); |
| 282 | + if (IntIntArray.Rank != other.IntIntArray.Rank) |
| 283 | + throw new Exception("Equals failed."); |
| 284 | + |
| 285 | + for (var i = 0; i < IntIntArray.Rank; ++i) |
| 286 | + { |
| 287 | + for (var j = 0; j < IntIntArray.GetLength(i); ++j) |
| 288 | + { |
| 289 | + if (IntIntArray[j, i] != other.IntIntArray[j, i]) |
| 290 | + throw new Exception("Equals failed."); |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + if (Exception.GetType() != other.Exception.GetType()) |
| 295 | + throw new Exception("Equals failed."); |
| 296 | + if (Exception.Message != other.Exception.Message) |
| 297 | + throw new Exception("Equals failed."); |
| 298 | + if(Exception.InnerException != null |
| 299 | + && Exception.InnerException.GetType() != other.Exception.InnerException.GetType()) |
| 300 | + throw new Exception("Equals failed."); |
| 301 | + |
| 302 | + for (var i = 0; i < SubArray.Length; i++) |
| 303 | + { |
| 304 | + if (SubArray[i].GetType() != other.SubArray[i].GetType()) |
| 305 | + throw new Exception("Equals failed."); |
| 306 | + |
| 307 | + if (SubArray[i] is Array arr) |
| 308 | + { |
| 309 | + var oArr = (Array)other.SubArray[i]; |
| 310 | + for (var j = 0; j < arr.Length; ++j) |
| 311 | + { |
| 312 | + if (!arr.GetValue(j).Equals(oArr.GetValue(j))) |
| 313 | + throw new Exception("Equals failed."); |
| 314 | + } |
| 315 | + } else if (!SubArray[i].Equals(other.SubArray[i])) |
| 316 | + throw new Exception("Equals failed."); |
| 317 | + } |
| 318 | + |
| 319 | + foreach (var key in Dictionary.Keys) |
| 320 | + { |
| 321 | + if (!Dictionary[key].Equals(other.Dictionary[key])) |
| 322 | + throw new Exception("Equals failed."); |
| 323 | + } |
| 324 | + |
| 325 | + foreach (var key in ImmutableDictionary.Keys) |
| 326 | + { |
| 327 | + if (!ImmutableDictionary[key].Equals(other.ImmutableDictionary[key])) |
| 328 | + throw new Exception("Equals failed."); |
| 329 | + } |
| 330 | + |
| 331 | + if (other.Delegate(2, 2) != 4) |
| 332 | + throw new Exception("Equals failed."); |
| 333 | + |
| 334 | + if(!IntArray.SequenceEqual(other.IntArray)) |
| 335 | + throw new Exception("Equals failed."); |
| 336 | + if(!Equals(Poco, other.Poco)) |
| 337 | + throw new Exception("Equals failed."); |
| 338 | + if (String != other.String) |
| 339 | + throw new Exception("Equals failed."); |
| 340 | + if(!TestEnum.SequenceEqual(other.TestEnum)) |
| 341 | + throw new Exception("Equals failed."); |
| 342 | + if(!ImmutableList.SequenceEqual(other.ImmutableList)) |
| 343 | + throw new Exception("Equals failed."); |
| 344 | + |
| 345 | + return true; |
| 346 | + } |
| 347 | + |
| 348 | + public override bool Equals(object obj) |
| 349 | + { |
| 350 | + if (obj == null) throw new Exception("Equals failed."); |
| 351 | + if (ReferenceEquals(this, obj)) return true; |
| 352 | + if (obj.GetType() != this.GetType()) throw new Exception("Equals failed."); |
| 353 | + return Equals((Temp) obj); |
| 354 | + } |
| 355 | + |
| 356 | + public override int GetHashCode() |
| 357 | + { |
| 358 | + unchecked |
| 359 | + { |
| 360 | + var hashCode = (SubArray != null ? SubArray.GetHashCode() : 0); |
| 361 | + hashCode = (hashCode * 397) ^ (IntArray != null ? IntArray.GetHashCode() : 0); |
| 362 | + hashCode = (hashCode * 397) ^ (IntIntArray != null ? IntIntArray.GetHashCode() : 0); |
| 363 | + hashCode = (hashCode * 397) ^ (Poco != null ? Poco.GetHashCode() : 0); |
| 364 | + hashCode = (hashCode * 397) ^ (String != null ? String.GetHashCode() : 0); |
| 365 | + hashCode = (hashCode * 397) ^ (Dictionary != null ? Dictionary.GetHashCode() : 0); |
| 366 | + hashCode = (hashCode * 397) ^ (Delegate != null ? Delegate.GetHashCode() : 0); |
| 367 | + hashCode = (hashCode * 397) ^ (TestEnum != null ? TestEnum.GetHashCode() : 0); |
| 368 | + hashCode = (hashCode * 397) ^ (Exception != null ? Exception.GetHashCode() : 0); |
| 369 | + hashCode = (hashCode * 397) ^ (ImmutableList != null ? ImmutableList.GetHashCode() : 0); |
| 370 | + return hashCode; |
| 371 | + } |
| 372 | + } |
| 373 | + } |
| 374 | + |
| 375 | + class Poco : IEquatable<Poco> |
| 376 | + { |
| 377 | + public Poco() |
| 378 | + { } |
| 379 | + |
| 380 | + public Poco(int intValue, string stringValue) |
| 381 | + { |
| 382 | + Int = intValue; |
| 383 | + String = stringValue; |
| 384 | + } |
| 385 | + |
| 386 | + public int Int { get; set; } |
| 387 | + public string String { get; set; } |
| 388 | + |
| 389 | + public bool Equals(Poco other) |
| 390 | + { |
| 391 | + if (ReferenceEquals(null, other)) |
| 392 | + throw new Exception("Equals failed."); |
| 393 | + if (ReferenceEquals(this, other)) |
| 394 | + throw new Exception("Equals failed."); |
| 395 | + if(Int != other.Int) |
| 396 | + throw new Exception("Equals failed."); |
| 397 | + if(String != other.String) |
| 398 | + throw new Exception("Equals failed."); |
| 399 | + return true; |
| 400 | + } |
| 401 | + |
| 402 | + public override bool Equals(object obj) |
| 403 | + { |
| 404 | + if (ReferenceEquals(null, obj)) throw new Exception("Equals failed."); |
| 405 | + if (ReferenceEquals(this, obj)) return true; |
| 406 | + if (obj.GetType() != this.GetType()) throw new Exception("Equals failed."); |
| 407 | + return Equals((Poco) obj); |
| 408 | + } |
| 409 | + |
| 410 | + public override int GetHashCode() |
| 411 | + { |
| 412 | + unchecked |
| 413 | + { |
| 414 | + return (Int * 397) ^ (String != null ? String.GetHashCode() : 0); |
| 415 | + } |
| 416 | + } |
257 | 417 | }
|
258 | 418 |
|
259 | 419 | [Fact]
|
260 |
| - public void WritesManifestEvenIfKnown1() |
| 420 | + public void WritesManifestEvenIfKnown() |
261 | 421 | {
|
262 | 422 | var stream = new MemoryStream();
|
263 |
| - var msg = new Temp() { aa = "huhu", dc = new Dictionary<string, string>() { { "a", "b" } }, SubArray = new object[] { 1, (byte)2, new object[] { 3 } } }; |
264 |
| - var serializer = new Serializer(new SerializerOptions(knownTypes: new[] { typeof(DictionaryEntry), typeof(Dictionary<string, string>), typeof(Temp), typeof(object[]), })); |
| 423 | + var msg = new Temp |
| 424 | + { |
| 425 | + SubArray = new object[] { 1, (byte)2, new object[] { 3 } }, |
| 426 | + IntArray = new [] {1, 2, 3, 4, 5}, |
| 427 | + IntIntArray = new [,] {{1, 2}, {3,4}, {5,6}, {7,8}}, |
| 428 | + Poco = new Poco(999, "666"), |
| 429 | + String = "huhu", |
| 430 | + Dictionary = new Dictionary<int, string> |
| 431 | + { |
| 432 | + { 666, "b" }, |
| 433 | + { 999, "testString" }, |
| 434 | + { 42, "iMaGiNe" } |
| 435 | + }, |
| 436 | + Delegate = (x, y) => x * y, |
| 437 | + TestEnum = new[]{4,8,9,3,2}, |
| 438 | + Exception = new ArgumentException("Test Exception", new IndexOutOfRangeException("-999")), |
| 439 | + ImmutableList = new [] {9, 4, 6, 2, 5}.ToImmutableList(), |
| 440 | + ImmutableDictionary = new Dictionary<int, string> |
| 441 | + { |
| 442 | + { 666, "b" }, |
| 443 | + { 999, "testString" }, |
| 444 | + { 42, "iMaGiNe" } |
| 445 | + }.ToImmutableDictionary(), |
| 446 | + }; |
| 447 | + var serializer = new Serializer(new SerializerOptions(knownTypes: new[] |
| 448 | + { |
| 449 | + typeof(object[]), |
| 450 | + typeof(int[]), |
| 451 | + typeof(int[,]), |
| 452 | + typeof(Dictionary<int, string>), |
| 453 | + typeof(DictionaryEntry), |
| 454 | + typeof(KeyValuePair<int,string>), |
| 455 | + typeof(Temp), |
| 456 | + typeof(TestDelegate), |
| 457 | + typeof(Enumerable), |
| 458 | + typeof(IEnumerable<int>), |
| 459 | + typeof(Exception), |
| 460 | + typeof(ArgumentException), |
| 461 | + typeof(IndexOutOfRangeException), |
| 462 | + typeof(FieldInfo), |
| 463 | + typeof(ImmutableList), |
| 464 | + typeof(ImmutableList<int>), |
| 465 | + typeof(ImmutableDictionary<int, string>), |
| 466 | + typeof(MethodInfo), |
| 467 | + typeof(PropertyInfo), |
| 468 | + })); |
265 | 469 | serializer.Serialize(msg, stream);
|
266 | 470 | stream.Position = 0;
|
267 | 471 | var a = stream.ToArray();
|
268 |
| - var text = string.Join("", a.Select(x => ((char)x).ToString())); |
269 |
| - var res = serializer.Deserialize(stream); |
| 472 | + var text = string.Join("", a.Select(x => x < 32 || x > 126 ? "" : ((char)x).ToString())); |
| 473 | + _output.WriteLine(text); |
| 474 | + var res = (Temp)serializer.Deserialize(stream); |
270 | 475 | Assert.DoesNotContain("System.Collections.Generic.Dictionary", text);
|
| 476 | + Assert.Equal(msg, res); |
271 | 477 | }
|
272 | 478 | }
|
273 | 479 | }
|
0 commit comments