-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathFormatTests.cs
More file actions
177 lines (162 loc) · 8.6 KB
/
FormatTests.cs
File metadata and controls
177 lines (162 loc) · 8.6 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Xunit;
namespace StackExchange.Redis.Tests;
public class FormatTests(ITestOutputHelper output) : TestBase(output)
{
public static IEnumerable<object?[]> EndpointData()
{
// note: the 3rd arg is for formatting; null means "expect the original string"
// DNS
yield return new object?[] { "localhost", new DnsEndPoint("localhost", 0), null };
yield return new object?[] { "localhost:6390", new DnsEndPoint("localhost", 6390), null };
yield return new object?[] { "bob.the.builder.com", new DnsEndPoint("bob.the.builder.com", 0), null };
yield return new object?[] { "bob.the.builder.com:6390", new DnsEndPoint("bob.the.builder.com", 6390), null };
// IPv4
yield return new object?[] { "0.0.0.0", new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0), null };
yield return new object?[] { "127.0.0.1", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0), null };
yield return new object?[] { "127.1", new IPEndPoint(IPAddress.Parse("127.1"), 0), "127.0.0.1" };
yield return new object?[] { "127.1:6389", new IPEndPoint(IPAddress.Parse("127.1"), 6389), "127.0.0.1:6389" };
yield return new object?[] { "127.0.0.1:6389", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6389), null };
yield return new object?[] { "127.0.0.1:1", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1), null };
yield return new object?[] { "127.0.0.1:2", new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2), null };
yield return new object?[] { "10.10.9.18:2", new IPEndPoint(IPAddress.Parse("10.10.9.18"), 2), null };
// IPv6
yield return new object?[] { "::1", new IPEndPoint(IPAddress.Parse("::1"), 0), null };
yield return new object?[] { "::1:6379", new IPEndPoint(IPAddress.Parse("::0.1.99.121"), 0), "::0.1.99.121" }; // remember your brackets!
yield return new object?[] { "[::1]:6379", new IPEndPoint(IPAddress.Parse("::1"), 6379), null };
yield return new object?[] { "[::1]", new IPEndPoint(IPAddress.Parse("::1"), 0), "::1" };
yield return new object?[] { "[::1]:1000", new IPEndPoint(IPAddress.Parse("::1"), 1000), null };
yield return new object?[] { "2001:db7:85a3:8d2:1319:8a2e:370:7348", new IPEndPoint(IPAddress.Parse("2001:db7:85a3:8d2:1319:8a2e:370:7348"), 0), null };
yield return new object?[] { "[2001:db7:85a3:8d2:1319:8a2e:370:7348]", new IPEndPoint(IPAddress.Parse("2001:db7:85a3:8d2:1319:8a2e:370:7348"), 0), "2001:db7:85a3:8d2:1319:8a2e:370:7348" };
yield return new object?[] { "[2001:db7:85a3:8d2:1319:8a2e:370:7348]:1000", new IPEndPoint(IPAddress.Parse("2001:db7:85a3:8d2:1319:8a2e:370:7348"), 1000), null };
}
[Theory]
[MemberData(nameof(EndpointData))]
public void ParseEndPoint(string data, EndPoint expected, string? expectedFormat)
{
Assert.True(Format.TryParseEndPoint(data, out var result));
Assert.Equal(expected, result);
// and write again
var s = Format.ToString(result);
expectedFormat ??= data;
Assert.Equal(expectedFormat, s);
}
[Theory]
[InlineData(CommandFlags.None, "None")]
#if NETFRAMEWORK
[InlineData(CommandFlags.PreferReplica, "PreferMaster, PreferReplica")] // 2-bit flag is hit-and-miss
[InlineData(CommandFlags.DemandReplica, "PreferMaster, DemandReplica")] // 2-bit flag is hit-and-miss
#else
[InlineData(CommandFlags.PreferReplica, "PreferReplica")] // 2-bit flag is hit-and-miss
[InlineData(CommandFlags.DemandReplica, "DemandReplica")] // 2-bit flag is hit-and-miss
#endif
#if NET8_0_OR_GREATER
[InlineData(CommandFlags.PreferReplica | CommandFlags.FireAndForget, "FireAndForget, PreferReplica")] // 2-bit flag is hit-and-miss
[InlineData(CommandFlags.DemandReplica | CommandFlags.FireAndForget, "FireAndForget, DemandReplica")] // 2-bit flag is hit-and-miss
#else
[InlineData(CommandFlags.PreferReplica | CommandFlags.FireAndForget, "PreferMaster, FireAndForget, PreferReplica")] // 2-bit flag is hit-and-miss
[InlineData(CommandFlags.DemandReplica | CommandFlags.FireAndForget, "PreferMaster, FireAndForget, DemandReplica")] // 2-bit flag is hit-and-miss
#endif
public void CommandFlagsFormatting(CommandFlags value, string expected)
{
Assert.SkipWhen(Runtime.IsMono, "Mono has different enum flag behavior");
Assert.Equal(expected, value.ToString());
}
[Theory]
[InlineData(ClientType.Normal, "Normal")]
[InlineData(ClientType.Replica, "Replica")]
[InlineData(ClientType.PubSub, "PubSub")]
public void ClientTypeFormatting(ClientType value, string expected)
=> Assert.Equal(expected, value.ToString());
[Theory]
[InlineData(ClientFlags.None, "None")]
[InlineData(ClientFlags.Replica | ClientFlags.Transaction, "Replica, Transaction")]
[InlineData(ClientFlags.Transaction | ClientFlags.ReplicaMonitor | ClientFlags.UnixDomainSocket, "ReplicaMonitor, Transaction, UnixDomainSocket")]
public void ClientFlagsFormatting(ClientFlags value, string expected)
=> Assert.Equal(expected, value.ToString());
[Theory]
[InlineData(ReplicationChangeOptions.None, "None")]
[InlineData(ReplicationChangeOptions.ReplicateToOtherEndpoints, "ReplicateToOtherEndpoints")]
[InlineData(ReplicationChangeOptions.SetTiebreaker | ReplicationChangeOptions.ReplicateToOtherEndpoints, "SetTiebreaker, ReplicateToOtherEndpoints")]
[InlineData(ReplicationChangeOptions.Broadcast | ReplicationChangeOptions.SetTiebreaker | ReplicationChangeOptions.ReplicateToOtherEndpoints, "All")]
public void ReplicationChangeOptionsFormatting(ReplicationChangeOptions value, string expected)
=> Assert.Equal(expected, value.ToString());
[Theory]
[InlineData(0, "0")]
[InlineData(1, "1")]
[InlineData(-1, "-1")]
[InlineData(100, "100")]
[InlineData(-100, "-100")]
[InlineData(int.MaxValue, "2147483647")]
[InlineData(int.MinValue, "-2147483648")]
public unsafe void FormatInt32(int value, string expectedValue)
{
Span<byte> dest = stackalloc byte[expectedValue.Length];
Assert.Equal(expectedValue.Length, Format.FormatInt32(value, dest));
fixed (byte* s = dest)
{
Assert.Equal(expectedValue, Encoding.ASCII.GetString(s, expectedValue.Length));
}
}
[Theory]
[InlineData(0, "0")]
[InlineData(1, "1")]
[InlineData(-1, "-1")]
[InlineData(100, "100")]
[InlineData(-100, "-100")]
[InlineData(long.MaxValue, "9223372036854775807")]
[InlineData(long.MinValue, "-9223372036854775808")]
public unsafe void FormatInt64(long value, string expectedValue)
{
Assert.Equal(expectedValue.Length, Format.MeasureInt64(value));
Span<byte> dest = stackalloc byte[expectedValue.Length];
Assert.Equal(expectedValue.Length, Format.FormatInt64(value, dest));
fixed (byte* s = dest)
{
Assert.Equal(expectedValue, Encoding.ASCII.GetString(s, expectedValue.Length));
}
}
[Theory]
[InlineData(0, "0")]
[InlineData(1, "1")]
[InlineData(100, "100")]
[InlineData(ulong.MaxValue, "18446744073709551615")]
public unsafe void FormatUInt64(ulong value, string expectedValue)
{
Assert.Equal(expectedValue.Length, Format.MeasureUInt64(value));
Span<byte> dest = stackalloc byte[expectedValue.Length];
Assert.Equal(expectedValue.Length, Format.FormatUInt64(value, dest));
fixed (byte* s = dest)
{
Assert.Equal(expectedValue, Encoding.ASCII.GetString(s, expectedValue.Length));
}
}
[Theory]
[InlineData(0, "0")]
[InlineData(1, "1")]
[InlineData(-1, "-1")]
[InlineData(0.5, "0.5")]
[InlineData(0.50001, "0.50000999999999995")]
[InlineData(Math.PI, "3.1415926535897931")]
[InlineData(100, "100")]
[InlineData(-100, "-100")]
[InlineData(double.MaxValue, "1.7976931348623157E+308")]
[InlineData(double.MinValue, "-1.7976931348623157E+308")]
[InlineData(double.Epsilon, "4.9406564584124654E-324")]
[InlineData(double.PositiveInfinity, "+inf")]
[InlineData(double.NegativeInfinity, "-inf")]
[InlineData(double.NaN, "NaN")] // never used in normal code
public unsafe void FormatDouble(double value, string expectedValue)
{
Assert.Equal(expectedValue.Length, Format.MeasureDouble(value));
Span<byte> dest = stackalloc byte[expectedValue.Length];
Assert.Equal(expectedValue.Length, Format.FormatDouble(value, dest));
fixed (byte* s = dest)
{
Assert.Equal(expectedValue, Encoding.ASCII.GetString(s, expectedValue.Length));
}
}
}