Commit f923aead authored by Marc Gravell's avatar Marc Gravell

add redisvalue equivalency tests ... and fix the gaps :)

parent a8cbbf69
......@@ -62,19 +62,19 @@ public void TestValues()
CheckNotSame(bool1, bool2);
}
private void CheckSame(RedisValue x, RedisValue y)
internal static void CheckSame(RedisValue x, RedisValue y)
{
Assert.True(Equals(x, y));
Assert.True(Equals(y, x));
Assert.True(EqualityComparer<RedisValue>.Default.Equals(x, y));
Assert.True(EqualityComparer<RedisValue>.Default.Equals(y, x));
Assert.True(x == y);
Assert.True(y == x);
Assert.False(x != y);
Assert.False(y != x);
Assert.True(x.Equals(y));
Assert.True(y.Equals(x));
Assert.True(x.GetHashCode() == y.GetHashCode());
Assert.True(Equals(x, y), "Equals(x, y)");
Assert.True(Equals(y, x), "Equals(y, x)");
Assert.True(EqualityComparer<RedisValue>.Default.Equals(x, y), "EQ(x,y)");
Assert.True(EqualityComparer<RedisValue>.Default.Equals(y, x), "EQ(y,x)");
Assert.True(x == y, "x==y");
Assert.True(y == x, "y==x");
Assert.False(x != y, "x!=y");
Assert.False(y != x, "y!=x");
Assert.True(x.Equals(y),"x.EQ(y)");
Assert.True(y.Equals(x), "y.EQ(x)");
Assert.True(x.GetHashCode() == y.GetHashCode(), "GetHashCode");
}
private void CheckNotSame(RedisValue x, RedisValue y)
......@@ -108,7 +108,7 @@ private void CheckNotNull(RedisValue value)
CheckNotSame(value, (byte[])null);
}
private void CheckNull(RedisValue value)
internal static void CheckNull(RedisValue value)
{
Assert.True(value.IsNull);
Assert.True(value.IsNullOrEmpty);
......@@ -172,42 +172,5 @@ public void CanBeDynamic()
Assert.Equal((byte)'b', blob[1]);
Assert.Equal((byte)'c', blob[2]);
}
[Fact]
public void TryParse()
{
{
RedisValue val = "1";
Assert.True(val.TryParse(out int i));
Assert.Equal(1, i);
Assert.True(val.TryParse(out long l));
Assert.Equal(1L, l);
Assert.True(val.TryParse(out double d));
Assert.Equal(1.0, l);
}
{
RedisValue val = "8675309";
Assert.True(val.TryParse(out int i));
Assert.Equal(8675309, i);
Assert.True(val.TryParse(out long l));
Assert.Equal(8675309L, l);
Assert.True(val.TryParse(out double d));
Assert.Equal(8675309.0, l);
}
{
RedisValue val = "3.14159";
Assert.True(val.TryParse(out double d));
Assert.Equal(3.14159, d);
}
{
RedisValue val = "not a real number";
Assert.False(val.TryParse(out int i));
Assert.False(val.TryParse(out long l));
Assert.False(val.TryParse(out double d));
}
}
}
}
using System.Text;
using Xunit;
namespace StackExchange.Redis.Tests
{
public class RedisValueEquivalency
{
// internal storage types: null, integer, double, string, raw
// public perceived types: int, long, double, bool, memory / byte[]
[Fact]
public void Int32_Matrix()
{
void Check(RedisValue known, RedisValue test)
{
KeysAndValues.CheckSame(known, test);
if (known.IsNull)
{
Assert.True(test.IsNull);
Assert.False(((int?)test).HasValue);
}
else
{
Assert.False(test.IsNull);
Assert.Equal((int)known, ((int?)test).Value);
Assert.Equal((int)known, (int)test);
}
Assert.Equal((int)known, (int)test);
}
Check(42, 42);
Check(42, 42.0);
Check(42, "42");
Check(42, "42.0");
Check(42, Bytes("42"));
Check(42, Bytes("42.0"));
CheckString(42, "42");
Check(-42, -42);
Check(-42, -42.0);
Check(-42, "-42");
Check(-42, "-42.0");
Check(-42, Bytes("-42"));
Check(-42, Bytes("-42.0"));
CheckString(-42, "-42");
Check(1, true);
Check(0, false);
}
[Fact]
public void Int64_Matrix()
{
void Check(RedisValue known, RedisValue test)
{
KeysAndValues.CheckSame(known, test);
if (known.IsNull)
{
Assert.True(test.IsNull);
Assert.False(((long?)test).HasValue);
}
else
{
Assert.False(test.IsNull);
Assert.Equal((long)known, ((long?)test).Value);
Assert.Equal((long)known, (long)test);
}
Assert.Equal((long)known, (long)test);
}
Check(1099511627848, 1099511627848);
Check(1099511627848, 1099511627848.0);
Check(1099511627848, "1099511627848");
Check(1099511627848, "1099511627848.0");
Check(1099511627848, Bytes("1099511627848"));
Check(1099511627848, Bytes("1099511627848.0"));
CheckString(1099511627848, "1099511627848");
Check(-1099511627848, -1099511627848);
Check(-1099511627848, -1099511627848);
Check(-1099511627848, "-1099511627848");
Check(-1099511627848, "-1099511627848.0");
Check(-1099511627848, Bytes("-1099511627848"));
Check(-1099511627848, Bytes("-1099511627848.0"));
CheckString(-1099511627848, "-1099511627848");
Check(1L, true);
Check(0L, false);
}
[Fact]
public void Double_Matrix()
{
void Check(RedisValue known, RedisValue test)
{
KeysAndValues.CheckSame(known, test);
if (known.IsNull)
{
Assert.True(test.IsNull);
Assert.False(((double?)test).HasValue);
}
else
{
Assert.False(test.IsNull);
Assert.Equal((double)known, ((double?)test).Value);
Assert.Equal((double)known, (double)test);
}
Assert.Equal((double)known, (double)test);
}
Check(1099511627848.0, 1099511627848);
Check(1099511627848.0, 1099511627848.0);
Check(1099511627848.0, "1099511627848");
Check(1099511627848.0, "1099511627848.0");
Check(1099511627848.0, Bytes("1099511627848"));
Check(1099511627848.0, Bytes("1099511627848.0"));
CheckString(1099511627848.0, "1099511627848");
Check(-1099511627848.0, -1099511627848);
Check(-1099511627848.0, -1099511627848);
Check(-1099511627848.0, "-1099511627848");
Check(-1099511627848.0, "-1099511627848.0");
Check(-1099511627848.0, Bytes("-1099511627848"));
Check(-1099511627848.0, Bytes("-1099511627848.0"));
CheckString(-1099511627848.0, "-1099511627848");
Check(1.0, true);
Check(0.0, false);
Check(1099511627848.6001, 1099511627848.6001);
Check(1099511627848.6001, "1099511627848.6001");
Check(1099511627848.6001, Bytes("1099511627848.6001"));
CheckString(1099511627848.6001, "1099511627848.6001");
Check(-1099511627848.6001, -1099511627848.6001);
Check(-1099511627848.6001, "-1099511627848.6001");
Check(-1099511627848.6001, Bytes("-1099511627848.6001"));
CheckString(-1099511627848.6001, "-1099511627848.6001");
Check(double.NegativeInfinity, double.NegativeInfinity);
Check(double.NegativeInfinity, "-inf");
CheckString(double.NegativeInfinity, "-inf");
Check(double.PositiveInfinity, double.PositiveInfinity);
Check(double.PositiveInfinity, "+inf");
CheckString(double.PositiveInfinity, "+inf");
}
static void CheckString(RedisValue value, string expected)
{
var s = value.ToString();
Assert.True(s == expected, $"'{s}' vs '{expected}'");
}
static byte[] Bytes(string s) => s == null ? null : Encoding.UTF8.GetBytes(s);
}
}
......@@ -62,7 +62,16 @@ internal static string ToString(double value)
return value.ToString("G17", NumberFormatInfo.InvariantInfo);
}
internal static string ToString(object value) => Convert.ToString(value, CultureInfo.InvariantCulture);
internal static string ToString(object value)
{
if (value == null) return "";
if (value is long l) return ToString(l);
if (value is int i) return ToString(i);
if (value is float f) return ToString(f);
if (value is double d) return ToString(d);
if (value is EndPoint e) return ToString(e);
return Convert.ToString(value, CultureInfo.InvariantCulture);
}
internal static string ToString(EndPoint endpoint)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment