Music never falls silent (TrackPicker); mod unit tests

This commit is contained in:
cthangover committed 2026-10-21 12:31:46 +02:00
1 parent eefa7058c6
commit 41a854ef36
4 files changed
+60 -14

No files matched your search

+1
View File
@@ -8,6 +8,7 @@
"sources": [ "sources": [
"src/**/*.cs" "src/**/*.cs"
], ],
"test_sources": ["tests/*.cs"],
"depends": ["shell2d_base>=1", "ui_base>=1"], "depends": ["shell2d_base>=1", "ui_base>=1"],
"ui": [] "ui": []
} }
+1 -14
View File
@@ -276,24 +276,11 @@ namespace Cthangover.Mods.Audio
if (dict == null || !dict.TryGetValue(playlistContext.LastMusicType, out var list) || Lists.IsEmpty(list)) if (dict == null || !dict.TryGetValue(playlistContext.LastMusicType, out var list) || Lists.IsEmpty(list))
return; return;
int iteration = 0; var nextMusic = TrackPicker.Next(list, playlistContext.LastMusicName, System.Random.Shared);
for (;;)
{
int index = (int)(GD.Randi() % (uint)list.Count);
var nextMusic = list[index];
if (list.Count == 1 || playlistContext.LastMusicName != nextMusic)
{
GameLogger.Log("AUDIO", $"NextSound -> '{nextMusic}' (type={playlistContext.LastMusicType}, scene={playlistContext.Playlist?.Scene})"); GameLogger.Log("AUDIO", $"NextSound -> '{nextMusic}' (type={playlistContext.LastMusicType}, scene={playlistContext.Playlist?.Scene})");
StopMusic(); StopMusic();
PlayMusic(nextMusic); PlayMusic(nextMusic);
break;
}
if (iteration++ > 10)
break;
}
} }
/// <summary> /// <summary>
+31
View File
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
namespace Cthangover.Mods.Audio
{
/// <summary>
/// Picks the next track of a music bucket: a random one different from the
/// last played when the bucket offers another, otherwise the last one
/// again. Choosing among the other tracks directly — instead of rerolling
/// until the pick differs — cannot give up and leave the player silent
/// (a bucket holding copies of one track exhausted every reroll).
/// </summary>
public static class TrackPicker
{
/// <summary>The next track of <paramref name="tracks"/>, or null for an empty bucket.</summary>
public static string Next(IReadOnlyList<string> tracks, string last, Random random)
{
if (tracks == null || tracks.Count == 0)
return null;
var others = new List<string>();
foreach (var track in tracks)
{
if (track != last)
others.Add(track);
}
return others.Count > 0 ? others[random.Next(others.Count)] : tracks[0];
}
}
}
+27
View File
@@ -0,0 +1,27 @@
namespace Cthangover.Mods.Audio.Tests
{
using System;
using System.Collections.Generic;
using Cthangover.Core.Mods.Testing;
/// <summary>
/// Choosing the next track never fails: it avoids repeating the last one
/// when anything else is available, and a bucket made of one track (or of
/// copies of it) still plays instead of falling silent.
/// </summary>
public class TrackPickerTests
{
[ModTest("music/next_track_avoids_repeat_and_never_gives_up")]
static void NextTrackAvoidsRepeat(ModTestContext ctx)
{
var random = new Random(3);
var list = new List<string> { "a", "b" };
for (var i = 0; i < 200; i++)
ModAssert.Equal("b", TrackPicker.Next(list, "a", random), "the other track");
ModAssert.Equal("a", TrackPicker.Next(new List<string> { "a", "a", "a" }, "a", random), "copies of one track still play");
ModAssert.Equal("a", TrackPicker.Next(new List<string> { "a" }, "a", random), "a single track repeats");
ModAssert.Null(TrackPicker.Next(new List<string>(), "a", random), "an empty bucket gives nothing");
}
}
}