|
3 | 3 | using System.Linq; |
4 | 4 | using ProtoBuf; |
5 | 5 |
|
6 | | -namespace CacheTower.Tests |
| 6 | +namespace CacheTower.Tests; |
| 7 | + |
| 8 | +internal static class SequenceComparison |
7 | 9 | { |
8 | | - [ProtoContract] |
9 | | - public class ComplexTypeCaching_TypeOne : IEquatable<ComplexTypeCaching_TypeOne> |
| 10 | + public static bool Compare<T>(IEnumerable<T> x, IEnumerable<T> y) |
10 | 11 | { |
11 | | - [ProtoMember(1)] |
12 | | - public int ExampleNumber { get; set; } |
13 | | - [ProtoMember(2)] |
14 | | - public string ExampleString { get; set; } |
15 | | - [ProtoMember(3)] |
16 | | - public List<int> ListOfNumbers { get; set; } |
17 | | - |
18 | | - public bool Equals(ComplexTypeCaching_TypeOne other) |
| 12 | + if (x is not null && y is not null) |
19 | 13 | { |
20 | | - if (other == null) |
21 | | - { |
22 | | - return false; |
23 | | - } |
24 | | - |
25 | | - return ExampleNumber == other.ExampleNumber && |
26 | | - ExampleString == other.ExampleString && |
27 | | - ListOfNumbers.SequenceEqual(other.ListOfNumbers); |
| 14 | + return x.SequenceEqual(y); |
28 | 15 | } |
| 16 | + return x is null && y is null; |
| 17 | + } |
| 18 | +} |
29 | 19 |
|
30 | | - public override bool Equals(object obj) |
31 | | - { |
32 | | - if (obj is ComplexTypeCaching_TypeOne complexType) |
33 | | - { |
34 | | - return Equals(complexType); |
35 | | - } |
| 20 | +[ProtoContract] |
| 21 | +public record BasicTypeCaching_TypeOne |
| 22 | +{ |
| 23 | + [ProtoMember(1)] |
| 24 | + public string ExampleString { get; set; } |
| 25 | +} |
36 | 26 |
|
| 27 | +[ProtoContract] |
| 28 | +public class ComplexTypeCaching_TypeOne : IEquatable<ComplexTypeCaching_TypeOne> |
| 29 | +{ |
| 30 | + [ProtoMember(1)] |
| 31 | + public int ExampleNumber { get; set; } |
| 32 | + [ProtoMember(2)] |
| 33 | + public string ExampleString { get; set; } |
| 34 | + [ProtoMember(3)] |
| 35 | + public List<int> ListOfNumbers { get; set; } |
| 36 | + |
| 37 | + public bool Equals(ComplexTypeCaching_TypeOne other) |
| 38 | + { |
| 39 | + if (other == null) |
| 40 | + { |
37 | 41 | return false; |
38 | 42 | } |
39 | 43 |
|
40 | | - public override int GetHashCode() |
| 44 | + return ExampleNumber == other.ExampleNumber && |
| 45 | + ExampleString == other.ExampleString && |
| 46 | + SequenceComparison.Compare(ListOfNumbers, other.ListOfNumbers); |
| 47 | + } |
| 48 | + |
| 49 | + public override bool Equals(object obj) |
| 50 | + { |
| 51 | + if (obj is ComplexTypeCaching_TypeOne complexType) |
41 | 52 | { |
42 | | - return ExampleNumber.GetHashCode() ^ |
43 | | - ExampleString.GetHashCode() ^ |
44 | | - ListOfNumbers.GetHashCode(); |
| 53 | + return Equals(complexType); |
45 | 54 | } |
| 55 | + |
| 56 | + return false; |
46 | 57 | } |
47 | 58 |
|
48 | | - [ProtoContract] |
49 | | - public class ComplexTypeCaching_TypeTwo : IEquatable<ComplexTypeCaching_TypeTwo> |
| 59 | + public override int GetHashCode() |
50 | 60 | { |
51 | | - [ProtoMember(1)] |
52 | | - public string ExampleString { get; set; } |
53 | | - [ProtoMember(2)] |
54 | | - public ComplexTypeCaching_TypeOne[] ArrayOfObjects { get; set; } |
55 | | - [ProtoMember(3)] |
56 | | - public Dictionary<string, int> DictionaryOfNumbers { get; set; } |
57 | | - |
58 | | - public bool Equals(ComplexTypeCaching_TypeTwo other) |
59 | | - { |
60 | | - if (other == null) |
61 | | - { |
62 | | - return false; |
63 | | - } |
| 61 | + return ExampleNumber.GetHashCode() ^ |
| 62 | + ExampleString.GetHashCode() ^ |
| 63 | + ListOfNumbers.GetHashCode(); |
| 64 | + } |
| 65 | +} |
64 | 66 |
|
65 | | - return ExampleString == other.ExampleString && |
66 | | - ArrayOfObjects.SequenceEqual(other.ArrayOfObjects) && |
67 | | - DictionaryOfNumbers.SequenceEqual(other.DictionaryOfNumbers); |
68 | | - } |
| 67 | +[ProtoContract] |
| 68 | +public class ComplexTypeCaching_TypeTwo : IEquatable<ComplexTypeCaching_TypeTwo> |
| 69 | +{ |
| 70 | + [ProtoMember(1)] |
| 71 | + public string ExampleString { get; set; } |
| 72 | + [ProtoMember(2)] |
| 73 | + public ComplexTypeCaching_TypeOne[] ArrayOfObjects { get; set; } |
| 74 | + [ProtoMember(3)] |
| 75 | + public Dictionary<string, int> DictionaryOfNumbers { get; set; } |
69 | 76 |
|
70 | | - public override bool Equals(object obj) |
| 77 | + public bool Equals(ComplexTypeCaching_TypeTwo other) |
| 78 | + { |
| 79 | + if (other == null) |
71 | 80 | { |
72 | | - if (obj is ComplexTypeCaching_TypeTwo complexType) |
73 | | - { |
74 | | - return Equals(complexType); |
75 | | - } |
76 | | - |
77 | 81 | return false; |
78 | 82 | } |
79 | 83 |
|
80 | | - public override int GetHashCode() |
| 84 | + return ExampleString == other.ExampleString && |
| 85 | + SequenceComparison.Compare(ArrayOfObjects, other.ArrayOfObjects) && |
| 86 | + SequenceComparison.Compare(DictionaryOfNumbers, other.DictionaryOfNumbers); |
| 87 | + } |
| 88 | + |
| 89 | + public override bool Equals(object obj) |
| 90 | + { |
| 91 | + if (obj is ComplexTypeCaching_TypeTwo complexType) |
81 | 92 | { |
82 | | - return ExampleString.GetHashCode() ^ |
83 | | - ArrayOfObjects.GetHashCode() ^ |
84 | | - DictionaryOfNumbers.GetHashCode(); |
| 93 | + return Equals(complexType); |
85 | 94 | } |
| 95 | + |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + public override int GetHashCode() |
| 100 | + { |
| 101 | + return ExampleString.GetHashCode() ^ |
| 102 | + ArrayOfObjects.GetHashCode() ^ |
| 103 | + DictionaryOfNumbers.GetHashCode(); |
86 | 104 | } |
87 | 105 | } |
0 commit comments