mirror of https://github.com/ErsatzTV/ErsatzTV.git
4 changed files with 146 additions and 16 deletions
@ -0,0 +1,90 @@ |
|||||||
|
using System.Collections.Generic; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
using ErsatzTV.Core.Scheduling; |
||||||
|
using FluentAssertions; |
||||||
|
using LanguageExt; |
||||||
|
using LanguageExt.UnsafeValueAccess; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Tests.Scheduling |
||||||
|
{ |
||||||
|
[TestFixture] |
||||||
|
public class ShuffledMediaCollectionEnumeratorTests |
||||||
|
{ |
||||||
|
private readonly List<GroupedMediaItem> _mediaItems = new() |
||||||
|
{ |
||||||
|
new GroupedMediaItem(new MediaItem { Id = 1 }, new List<MediaItem>()), |
||||||
|
new GroupedMediaItem(new MediaItem { Id = 2 }, new List<MediaItem>()), |
||||||
|
new GroupedMediaItem(new MediaItem { Id = 3 }, new List<MediaItem>()) |
||||||
|
}; |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void Peek_Zero_Should_Match_Current() |
||||||
|
{ |
||||||
|
var state = new CollectionEnumeratorState { Index = 0, Seed = 0 }; |
||||||
|
var enumerator = new ShuffledMediaCollectionEnumerator(_mediaItems, state); |
||||||
|
|
||||||
|
Option<MediaItem> peek = enumerator.Peek(0); |
||||||
|
Option<MediaItem> current = enumerator.Current; |
||||||
|
|
||||||
|
peek.IsSome.Should().BeTrue(); |
||||||
|
current.IsSome.Should().BeTrue(); |
||||||
|
peek.ValueUnsafe().Id.Should().Be(1); |
||||||
|
current.ValueUnsafe().Id.Should().Be(1); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void Peek_One_Should_Match_Next() |
||||||
|
{ |
||||||
|
var state = new CollectionEnumeratorState { Index = 0, Seed = 0 }; |
||||||
|
var enumerator = new ShuffledMediaCollectionEnumerator(_mediaItems, state); |
||||||
|
|
||||||
|
Option<MediaItem> peek = enumerator.Peek(1); |
||||||
|
|
||||||
|
enumerator.MoveNext(); |
||||||
|
Option<MediaItem> next = enumerator.Current; |
||||||
|
|
||||||
|
peek.IsSome.Should().BeTrue(); |
||||||
|
next.IsSome.Should().BeTrue(); |
||||||
|
peek.ValueUnsafe().Id.Should().Be(2); |
||||||
|
next.ValueUnsafe().Id.Should().Be(2); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void Peek_Two_Should_Match_NextNext() |
||||||
|
{ |
||||||
|
var state = new CollectionEnumeratorState { Index = 0, Seed = 0 }; |
||||||
|
var enumerator = new ShuffledMediaCollectionEnumerator(_mediaItems, state); |
||||||
|
|
||||||
|
Option<MediaItem> peek = enumerator.Peek(2); |
||||||
|
|
||||||
|
enumerator.MoveNext(); |
||||||
|
enumerator.MoveNext(); |
||||||
|
Option<MediaItem> next = enumerator.Current; |
||||||
|
|
||||||
|
peek.IsSome.Should().BeTrue(); |
||||||
|
next.IsSome.Should().BeTrue(); |
||||||
|
peek.ValueUnsafe().Id.Should().Be(3); |
||||||
|
next.ValueUnsafe().Id.Should().Be(3); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void Peek_Three_Should_Match_NextNextNext() |
||||||
|
{ |
||||||
|
var state = new CollectionEnumeratorState { Index = 0, Seed = 0 }; |
||||||
|
var enumerator = new ShuffledMediaCollectionEnumerator(_mediaItems, state); |
||||||
|
|
||||||
|
Option<MediaItem> peek = enumerator.Peek(3); |
||||||
|
|
||||||
|
enumerator.MoveNext(); |
||||||
|
enumerator.MoveNext(); |
||||||
|
enumerator.MoveNext(); |
||||||
|
Option<MediaItem> next = enumerator.Current; |
||||||
|
|
||||||
|
peek.IsSome.Should().BeTrue(); |
||||||
|
next.IsSome.Should().BeTrue(); |
||||||
|
peek.ValueUnsafe().Id.Should().Be(2); |
||||||
|
next.ValueUnsafe().Id.Should().Be(2); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,41 @@ |
|||||||
|
using System; |
||||||
|
|
||||||
|
namespace ErsatzTV.Core.Scheduling |
||||||
|
{ |
||||||
|
public class CloneableRandom |
||||||
|
{ |
||||||
|
private readonly int _seed; |
||||||
|
private readonly Random _random; |
||||||
|
private int _count; |
||||||
|
|
||||||
|
public CloneableRandom(int seed) |
||||||
|
{ |
||||||
|
_seed = seed; |
||||||
|
_random = new Random(_seed); |
||||||
|
} |
||||||
|
|
||||||
|
public CloneableRandom Clone() |
||||||
|
{ |
||||||
|
var clone = new CloneableRandom(_seed); |
||||||
|
|
||||||
|
for (var i = 0; i < _count; i++) |
||||||
|
{ |
||||||
|
clone.Next(); |
||||||
|
} |
||||||
|
|
||||||
|
return clone; |
||||||
|
} |
||||||
|
|
||||||
|
public int Next() |
||||||
|
{ |
||||||
|
_count++; |
||||||
|
return _random.Next(); |
||||||
|
} |
||||||
|
|
||||||
|
public int Next(int maxValue) |
||||||
|
{ |
||||||
|
_count++; |
||||||
|
return _random.Next(maxValue); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue