-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadonlyBytesTests.cs
130 lines (114 loc) · 4.64 KB
/
ReadonlyBytesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Linq;
using NUnit.Framework;
namespace hashes
{
[TestFixture]
public class ReadonlyBytesTests
{
[Test]
public void Creation()
{
new ReadonlyBytes(1, 2, 3);
}
[Test]
public void CreationException()
{
Assert.Throws<ArgumentNullException>(() => { new ReadonlyBytes(null); });
}
[Test]
public void Length()
{
var items = new ReadonlyBytes(1, 2, 3);
Assert.AreEqual(3, items.Length);
}
[Test]
public void Indexer()
{
var items = new ReadonlyBytes(4, 1, 2);
Assert.AreEqual(4, items[0]);
Assert.AreEqual(1, items[1]);
Assert.AreEqual(2, items[2]);
}
[Test]
public void IndexOutOfRangeException()
{
var items = new ReadonlyBytes(4, 1, 2);
// Обращение к индексу за границами исходного массива должно приводить
// к исключению типа IndexOutOfRangeException
Assert.Throws<IndexOutOfRangeException>(() =>
{
var z = items[100500];
});
}
[Test]
public void Enumeration()
{
var data = new ReadonlyBytes(1, 2, 3);
var list = data.ToList();
Assert.AreEqual(new byte[] { 1, 2, 3 }, list);
}
[Test]
public void EqualOnSameBytes()
{
// ReSharper disable EqualExpressionComparison
Assert.IsTrue(new ReadonlyBytes(Array.Empty<byte>()).Equals(new ReadonlyBytes(Array.Empty<byte>())));
Assert.IsTrue(new ReadonlyBytes(100).Equals(new ReadonlyBytes(100)));
Assert.IsTrue(new ReadonlyBytes(1, 2, 3).Equals(new ReadonlyBytes(1, 2, 3)));
var items = new ReadonlyBytes(4, 2, 67, 1);
Assert.IsTrue(items.Equals(items));
// ReSharper restore EqualExpressionComparison
}
[Test]
public void NotEqualIfBytesDiffer()
{
Assert.IsFalse(new ReadonlyBytes(1, 2, 3).Equals(new ReadonlyBytes(1, 2, 4)));
Assert.IsFalse(new ReadonlyBytes(1, 2, 3).Equals(new ReadonlyBytes(1, 2, 3, 4)));
Assert.IsFalse(new ReadonlyBytes(1, 2, 3).Equals(new ReadonlyBytes(1, 2)));
Assert.IsFalse(new ReadonlyBytes(1, 2, 3).Equals(new ReadonlyBytes(3, 2, 1)));
}
[TestCase(new byte[] { })]
[TestCase(new byte[] { 1, 2, 4 })]
[TestCase(new byte[] { 0 })]
public void NotEqualWithOtherTypes(byte[] bytes)
{
Assert.IsFalse(new ReadonlyBytes(bytes).Equals(null));
Assert.IsFalse(new ReadonlyBytes(bytes).Equals("string"));
Assert.IsFalse(new ReadonlyBytes(bytes).Equals(new DerivedFromReadonlyBytes()));
}
[Test]
public void HashCode()
{
Assert.AreEqual(new ReadonlyBytes().GetHashCode(), new ReadonlyBytes().GetHashCode());
Assert.AreEqual(new ReadonlyBytes(1, 2, 3).GetHashCode(), new ReadonlyBytes(1, 2, 3).GetHashCode());
Assert.AreNotEqual(new ReadonlyBytes(1, 2, 3).GetHashCode(), new ReadonlyBytes(1, 2, 3, 4).GetHashCode());
Assert.AreNotEqual(new ReadonlyBytes(1, 2, 3).GetHashCode(), new ReadonlyBytes(1, 2, 4).GetHashCode());
Assert.AreNotEqual(new ReadonlyBytes(1, 2, 3).GetHashCode(), new ReadonlyBytes(1, 2).GetHashCode());
Assert.AreNotEqual(new ReadonlyBytes(1, 2, 3).GetHashCode(), new ReadonlyBytes(3, 2, 1).GetHashCode());
Assert.AreNotEqual(new ReadonlyBytes(1, 0).GetHashCode(), new ReadonlyBytes(0, 31).GetHashCode());
var items = new ReadonlyBytes(4, 2, 67, 1);
Assert.AreEqual(items.GetHashCode(), items.GetHashCode());
}
[Test]
public void ToStringIsOverridden()
{
Assert.AreEqual("[2]", new ReadonlyBytes(2).ToString());
Assert.AreEqual("[]", new ReadonlyBytes().ToString());
Assert.AreEqual("[2, 3, 5]", new ReadonlyBytes(2, 3, 5).ToString());
}
[Test]
public void MassiveCallToGetHashCodeOfLargeBytesArray()
{
var items = new ReadonlyBytes(Enumerable.Repeat((byte)6, 100000).ToArray());
var hash = items.GetHashCode();
for (var i = 0; i < 100000; i++)
Assert.AreEqual(hash, items.GetHashCode());
}
public class DerivedFromReadonlyBytes : ReadonlyBytes
{
public DerivedFromReadonlyBytes() : base()
{
}
}
}
}