This commit is contained in:
2026-07-23 08:59:10 -04:00
commit fc970fa3d8
30 changed files with 1376 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
+36
View File
@@ -0,0 +1,36 @@
using RhythiaLib.Rhr;
namespace RhythiaLib.Tests.Rhr
{
public sealed class RhrFileReadTests
{
[Fact]
public void Decode_ValidReplay_ReturnsExpectedValues()
{
RhrReplay original = RhrTestData.CreateCurrentReplay();
byte[] bytes = RhrFile.Encode(original);
File.WriteAllBytes("test.rhr", bytes);
RhrReplay decoded = RhrFile.Decode(bytes);
Assert.Equal(original.Version, decoded.Version);
Assert.Equal(original.ScoreData.TimestampTicks, decoded.ScoreData.TimestampTicks);
Assert.Equal("TestPlayerName", decoded.ScoreData.PlayerName);
Assert.Equal("test_legacy_map", decoded.ScoreData.LegacyMapId);
Assert.Equal(1234, decoded.ScoreData.MapId);
Assert.Equal(1f, decoded.ScoreData.Accuracy);
Assert.Equal(2, decoded.Frames.Count);
Assert.Equal(16, decoded.Frames[1].Time);
Assert.Equal(-0.5f, decoded.Frames[1].PositionY);
}
[Fact]
public void Read_LeavesCallerStreamOpen()
{
byte[] bytes = RhrFile.Encode(RhrTestData.CreateCurrentReplay());
using var stream = new MemoryStream(bytes);
_ = RhrFile.Read(stream);
Assert.True(stream.CanRead);
}
}
}
+33
View File
@@ -0,0 +1,33 @@
using RhythiaLib.Rhr;
namespace RhythiaLib.Tests.Rhr
{
public sealed class RhrRoundTripTests
{
[Fact]
public void SyntheticReplay_RoundTripsExactly()
{
RhrReplay original = RhrTestData.CreateCurrentReplay();
byte[] firstEncoding = RhrFile.Encode(original);
RhrReplay decoded = RhrFile.Decode(firstEncoding);
byte[] secondEncoding = RhrFile.Encode(original);
Assert.Equal(firstEncoding, secondEncoding);
}
[Theory]
[MemberData(nameof(RhrTestData.FixturePaths), MemberType = typeof(RhrTestData))]
public void RealFixture_RoundTripsExactly(string path)
{
byte[] originalBytes = File.ReadAllBytes(path);
RhrReplay replay = RhrFile.Decode(originalBytes);
byte[] encodedBytes = RhrFile.Encode(replay);
Assert.Equal(originalBytes, encodedBytes);
}
}
}
+80
View File
@@ -0,0 +1,80 @@
using RhythiaLib.Rhr;
namespace RhythiaLib.Tests.Rhr
{
internal static class RhrTestData
{
public static RhrReplay CreateCurrentReplay()
{
return new RhrReplay
{
Version = RhrVersions.BeatmapHash,
ScoreData = new RhrScoreData
{
TimestampTicks = new DateTime(
2026,
7,
23,
12,
0,
0,
DateTimeKind.Unspecified
).Ticks,
PlayerName = "TestPlayerName",
LegacyMapId = "test_legacy_map",
MapId = 1234,
StartFrom = 0,
Mode = "online_profile",
Passed = true,
ModsJson = "[]",
Spin = false,
Speed = 1f,
TotalScore = 1_000_000,
Accuracy = 1f,
Hits = 100,
Misses = 0,
Points = 100f,
Failed = false,
FailTime = -1,
BeatmapHash =
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
},
Frames =
[
new RhrReplayFrame
{
Time = 0,
PositionX = 0f,
PositionY = 0f,
Health = 1f,
IsImportantFrame = 0,
},
new RhrReplayFrame
{
Time = 16,
PositionX = 0.7f,
PositionY = -0.5f,
Health = 0.85f,
IsImportantFrame = 1,
},
],
};
}
public static TheoryData<string> FixturePaths()
{
var data = new TheoryData<string>();
string directory = Path.Combine(AppContext.BaseDirectory, "Rhr", "Fixtures");
if (!Directory.Exists(directory))
return data;
foreach (string path in Directory.EnumerateFiles(directory, "*.rhr"))
{
data.Add(path);
}
return data;
}
}
}
+71
View File
@@ -0,0 +1,71 @@
using RhythiaLib.Rhr;
namespace RhythiaLib.Tests.Rhr
{
public sealed class RhrVersionTests
{
[Fact]
public void LegacyTimeVersion_RoundTripsFrameTime()
{
RhrReplay replay = RhrTestData.CreateCurrentReplay();
replay.Version = RhrVersions.Int32Time - 1;
replay.Frames =
[
new RhrReplayFrame
{
Time = 1234,
PositionX = 0.5f,
PositionY = 0.2f,
Health = 1f,
IsImportantFrame = 0,
},
];
byte[] bytes = RhrFile.Encode(replay);
RhrReplay decoded = RhrFile.Decode(bytes);
Assert.Equal(1234, decoded.Frames[0].Time);
}
[Fact]
public void LegacyYVersion_PreservesY()
{
RhrReplay replay = RhrTestData.CreateCurrentReplay();
replay.Version = RhrVersions.NegateY - 1;
replay.Frames =
[
new RhrReplayFrame
{
Time = 1234,
PositionX = 0.5f,
PositionY = 0.2f,
Health = 1f,
IsImportantFrame = 0,
},
];
byte[] bytes = RhrFile.Encode(replay);
RhrReplay decoded = RhrFile.Decode(bytes);
Assert.Equal(0.2f, decoded.Frames[0].PositionY);
}
[Fact]
public void VersionBeforeExtendedFields_UsesDefaults()
{
RhrReplay replay = RhrTestData.CreateCurrentReplay();
replay.Version = RhrVersions.ExtendedFields - 1;
byte[] bytes = RhrFile.Encode(replay);
RhrReplay decoded = RhrFile.Decode(bytes);
Assert.True(decoded.ScoreData.Passed);
Assert.Equal("[]", decoded.ScoreData.ModsJson);
Assert.False(decoded.ScoreData.Spin);
Assert.Equal(0f, decoded.ScoreData.Speed);
Assert.Equal(0, decoded.ScoreData.TotalScore);
}
}
}
+41
View File
@@ -0,0 +1,41 @@
using RhythiaLib.Rhr;
namespace RhythiaLib.Tests.Rhr
{
public sealed class RhrWriteTests
{
[Fact]
public void Write_LeavesCallerStreamOpen()
{
RhrReplay replay = RhrTestData.CreateCurrentReplay();
using var stream = new MemoryStream();
RhrFile.Write(stream, replay);
Assert.True(stream.CanWrite);
Assert.NotEqual(0, stream.Length);
}
[Fact]
public void Encode_WritesFrameCount()
{
RhrReplay replay = RhrTestData.CreateCurrentReplay();
byte[] data = RhrFile.Encode(replay);
RhrReplay decoded = RhrFile.Decode(data);
Assert.Equal(replay.Frames.Count, decoded.Frames.Count);
}
[Fact]
public void Write_NonFiniteCoordinate_Throws()
{
RhrReplay replay = RhrTestData.CreateCurrentReplay();
replay.Frames[0].PositionX = float.NaN;
using var stream = new MemoryStream();
Assert.Throws<ArgumentException>(() => RhrFile.Write(stream, replay));
}
}
}