/
GitObjectIdTests.cs
134 lines (110 loc) · 4.73 KB
/
GitObjectIdTests.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
131
132
133
134
using System;
using System.Runtime.InteropServices;
using System.Text;
using Nerdbank.GitVersioning.ManagedGit;
using Xunit;
using Xunit.Abstractions;
namespace ManagedGit
{
public class GitObjectIdTests
{
private readonly byte[] shaAsByteArray = new byte[] { 0x4e, 0x91, 0x27, 0x36, 0xc2, 0x7e, 0x40, 0xb3, 0x89, 0x90, 0x4d, 0x04, 0x6d, 0xc6, 0x3d, 0xc9, 0xf5, 0x78, 0x11, 0x7f };
private const string shaAsHexString = "4e912736c27e40b389904d046dc63dc9f578117f";
private readonly byte[] shaAsHexAsciiByteArray = Encoding.ASCII.GetBytes(shaAsHexString);
[Fact]
public void ParseByteArrayTest()
{
var objectId = GitObjectId.Parse(this.shaAsByteArray);
Span<byte> value = stackalloc byte[20];
objectId.CopyTo(value);
Assert.True(value.SequenceEqual(this.shaAsByteArray.AsSpan()));
}
[Fact]
public void ParseStringTest()
{
var objectId = GitObjectId.Parse(shaAsHexString);
Span<byte> value = stackalloc byte[20];
objectId.CopyTo(value);
Assert.True(value.SequenceEqual(this.shaAsByteArray.AsSpan()));
}
[Fact]
public void ParseHexArrayTest()
{
var objectId = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
Span<byte> value = stackalloc byte[20];
objectId.CopyTo(value);
Assert.True(value.SequenceEqual(this.shaAsByteArray.AsSpan()));
}
[Fact]
public void EqualsObjectTest()
{
var objectId = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
var objectId2 = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
// Must be equal to itself
Assert.True(objectId.Equals((object)objectId));
Assert.True(objectId.Equals((object)objectId2));
// Not equal to null
Assert.False(objectId.Equals(null));
// Not equal to other representations of the object id
Assert.False(objectId.Equals(this.shaAsHexAsciiByteArray));
Assert.False(objectId.Equals(this.shaAsByteArray));
Assert.False(objectId.Equals(shaAsHexString));
// Not equal to other object ids
Assert.False(objectId.Equals((object)GitObjectId.Empty));
}
[Fact]
public void EqualsObjectIdTest()
{
var objectId = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
var objectId2 = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
// Must be equal to itself
Assert.True(objectId.Equals(objectId));
Assert.True(objectId.Equals(objectId2));
// Not equal to other object ids
Assert.False(objectId.Equals(GitObjectId.Empty));
}
[Fact]
public void GetHashCodeTest()
{
// The hash code is the int32 representation of the first 4 bytes
var objectId = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
Assert.Equal(0x3627914e, objectId.GetHashCode());
Assert.Equal(0, GitObjectId.Empty.GetHashCode());
}
[Fact]
public void AsUInt16Test()
{
// The hash code is the int32 representation of the first 4 bytes
var objectId = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
Assert.Equal(0x4e91, objectId.AsUInt16());
Assert.Equal(0, GitObjectId.Empty.GetHashCode());
}
[Fact]
public void ToStringTest()
{
var objectId = GitObjectId.Parse(this.shaAsByteArray);
Assert.Equal(shaAsHexString, objectId.ToString());
}
[Fact]
public void CopyToUtf16StringTest()
{
// Common use case: create the path to the object in the Git object store,
// e.g. git/objects/[byte 0]/[bytes 1 - 19]
byte[] valueAsBytes = Encoding.Unicode.GetBytes("git/objects/00/01020304050607080910111213141516171819");
Span<char> valueAsChars = MemoryMarshal.Cast<byte, char>(valueAsBytes);
var objectId = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
objectId.CopyAsHex(0, 1, valueAsChars.Slice(12, 1 * 2));
objectId.CopyAsHex(1, 19, valueAsChars.Slice(15, 19 * 2));
var path = Encoding.Unicode.GetString(valueAsBytes);
Assert.Equal("git/objects/4e/912736c27e40b389904d046dc63dc9f578117f", path);
}
[Fact]
public void CopyToTest()
{
var objectId = GitObjectId.Parse(this.shaAsByteArray);
byte[] actual = new byte[20];
objectId.CopyTo(actual);
Assert.Equal(this.shaAsByteArray, actual);
}
}
}