public void Episodes_Should_Not_Duplicate_When_Reshuffling() { List <MediaItem> contents = Episodes(10); // normally returns 10 5 7 4 3 6 2 8 9 1 1 (note duplicate 1 at end) var state = new CollectionEnumeratorState { Seed = 8 }; var groupedMediaItems = contents.Map(mi => new GroupedMediaItem(mi, null)).ToList(); var shuffledContent = new ShuffledMediaCollectionEnumerator(groupedMediaItems, state); var list = new List <int>(); for (var i = 1; i <= 1000; i++) { shuffledContent.Current.IsSome.Should().BeTrue(); shuffledContent.Current.Do(x => list.Add(x.Id)); shuffledContent.MoveNext(); } for (var i = 0; i < list.Count - 1; i++) { if (list[i] == list[i + 1]) { Assert.Fail("List contains duplicate items"); } } }
public void State_Should_Reset_When_Invalid() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState { Index = 10 }; var chronologicalContent = new ChronologicalMediaCollectionEnumerator(contents, state); chronologicalContent.State.Index.Should().Be(0); chronologicalContent.State.Seed.Should().Be(0); }
public void State_Index_Should_Increment() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState(); var chronologicalContent = new ChronologicalMediaCollectionEnumerator(contents, state); for (var i = 0; i < 10; i++) { chronologicalContent.State.Index.Should().Be(i % 10); chronologicalContent.MoveNext(); } }
public void State_Should_Reset_When_Invalid() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState { Index = 10, Seed = MagicSeed }; var groupedMediaItems = contents.Map(mi => new GroupedMediaItem(mi, null)).ToList(); var shuffledContent = new ShuffledMediaCollectionEnumerator(groupedMediaItems, state); shuffledContent.State.Index.Should().Be(0); shuffledContent.State.Seed.Should().NotBe(MagicSeed); }
public void State_Index_Should_Increment() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState(); var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state); for (var i = 0; i < 10; i++) { shuffledContent.State.Index.Should().Be(i); shuffledContent.MoveNext(); } }
public ShuffledMediaCollectionEnumerator(IList <MediaItem> mediaItems, CollectionEnumeratorState state) { _mediaItems = mediaItems; _random = new Random(state.Seed); _shuffled = Shuffle(_mediaItems, _random); State = new CollectionEnumeratorState { Seed = state.Seed }; while (State.Index < state.Index) { MoveNext(); } }
public void Episodes_Should_Sort_By_Aired() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState(); var chronologicalContent = new ChronologicalMediaCollectionEnumerator(contents, state); for (var i = 1; i <= 10; i++) { chronologicalContent.Current.IsSome.Should().BeTrue(); chronologicalContent.Current.Map(x => x.Id).IfNone(-1).Should().Be(i); chronologicalContent.MoveNext(); } }
public void State_Index_Should_Increment() { Collection collection = CreateCollection(10); List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState(); var customOrderContent = new CustomOrderCollectionEnumerator(collection, contents, state); for (var i = 0; i < 10; i++) { customOrderContent.State.Index.Should().Be(i % 10); customOrderContent.MoveNext(); } }
public ChronologicalMediaCollectionEnumerator( IEnumerable <MediaItem> mediaItems, CollectionEnumeratorState state) { _sortedMediaItems = mediaItems.OrderBy(identity, new ChronologicalComparer()).ToList(); State = new CollectionEnumeratorState { Seed = state.Seed }; while (State.Index < state.Index) { MoveNext(); } }
public void MediaItems_Should_Sort_By_CustomOrder() { Collection collection = CreateCollection(10); List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState(); var customOrderContent = new CustomOrderCollectionEnumerator(collection, contents, state); for (var i = 10; i >= 1; i--) { customOrderContent.Current.IsSome.Should().BeTrue(); customOrderContent.Current.Map(x => x.Id).IfNone(-1).Should().Be(i); customOrderContent.MoveNext(); } }
public RandomizedMediaCollectionEnumerator(IList <MediaItem> mediaItems, CollectionEnumeratorState state) { _mediaItems = mediaItems; _random = new Random(state.Seed); State = new CollectionEnumeratorState { Seed = state.Seed }; // we want to move at least once so we start with a random item and not the first // because _index defaults to 0 while (State.Index <= state.Index) { MoveNext(); } }
public void State_Should_Impact_Iterator_Start() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState { Index = 5 }; var chronologicalContent = new ChronologicalMediaCollectionEnumerator(contents, state); for (var i = 6; i <= 10; i++) { chronologicalContent.Current.IsSome.Should().BeTrue(); chronologicalContent.Current.Map(x => x.Id).IfNone(-1).Should().Be(i); chronologicalContent.State.Index.Should().Be(i - 1); chronologicalContent.MoveNext(); } }
public void State_Should_Impact_Iterator_Start() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState { Index = 5, Seed = MagicSeed }; var groupedMediaItems = contents.Map(mi => new GroupedMediaItem(mi, null)).ToList(); var shuffledContent = new ShuffledMediaCollectionEnumerator(groupedMediaItems, state); for (var i = 6; i <= 10; i++) { shuffledContent.Current.IsSome.Should().BeTrue(); shuffledContent.Current.Map(x => x.Id).IfNone(-1).Should().Be(i); shuffledContent.State.Index.Should().Be(i - 1); shuffledContent.MoveNext(); } }
public void State_Should_Impact_Iterator_Start() { Collection collection = CreateCollection(10); List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState { Index = 5 }; var customOrderContent = new CustomOrderCollectionEnumerator(collection, contents, state); for (var i = 5; i >= 1; i--) { customOrderContent.Current.IsSome.Should().BeTrue(); customOrderContent.Current.Map(x => x.Id).IfNone(-1).Should().Be(i); customOrderContent.State.Index.Should().Be(5 - i + 5); // 5 through 10 customOrderContent.MoveNext(); } }
public CustomOrderCollectionEnumerator( Collection collection, List <MediaItem> mediaItems, CollectionEnumeratorState state) { // TODO: this will break if we allow shows and seasons _sortedMediaItems = collection.CollectionItems .OrderBy(ci => ci.CustomIndex) .Map(ci => mediaItems.First(mi => mi.Id == ci.MediaItemId)) .ToList(); State = new CollectionEnumeratorState { Seed = state.Seed }; while (State.Index < state.Index) { MoveNext(); } }
public void Duplicate_Check_Should_Ignore_Single_Item() { List <MediaItem> contents = Episodes(1); var state = new CollectionEnumeratorState(); var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state); var list = new List <int>(); for (var i = 1; i <= 10; i++) { shuffledContent.Current.IsSome.Should().BeTrue(); shuffledContent.Current.Do(x => list.Add(x.Id)); shuffledContent.MoveNext(); } list.Should().Equal(1, 1, 1, 1, 1, 1, 1, 1, 1, 1); }
public void Episodes_Should_Shuffle() { List <MediaItem> contents = Episodes(10); var state = new CollectionEnumeratorState(); var shuffledContent = new ShuffledMediaCollectionEnumerator(contents, state); var list = new List <int>(); for (var i = 1; i <= 10; i++) { shuffledContent.Current.IsSome.Should().BeTrue(); shuffledContent.Current.Do(x => list.Add(x.Id)); shuffledContent.MoveNext(); } list.Should().NotEqual(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); list.Should().BeEquivalentTo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); }
public OrderedScheduleItemsEnumerator( IEnumerable <ProgramScheduleItem> scheduleItems, CollectionEnumeratorState state) { _sortedScheduleItems = scheduleItems.OrderBy(i => i.Index).ToList(); State = new CollectionEnumeratorState { Seed = state.Seed }; if (state.Index >= _sortedScheduleItems.Count) { state.Index = 0; state.Seed = 0; } while (State.Index < state.Index) { MoveNext(); } }
public ShuffledMediaCollectionEnumerator( IList <GroupedMediaItem> mediaItems, CollectionEnumeratorState state) { _mediaItemCount = mediaItems.Sum(i => 1 + Optional(i.Additional).Flatten().Count()); _mediaItems = mediaItems; if (state.Index >= _mediaItems.Count) { state.Index = 0; state.Seed = new Random(state.Seed).Next(); } _random = new CloneableRandom(state.Seed); _shuffled = Shuffle(_mediaItems, _random); State = new CollectionEnumeratorState { Seed = state.Seed }; while (State.Index < state.Index) { MoveNext(); } }
public ShuffledScheduleItemsEnumerator( IList <ProgramScheduleItem> scheduleItems, CollectionEnumeratorState state) { _scheduleItemsCount = scheduleItems.Count; _scheduleItems = scheduleItems; if (state.Index >= _scheduleItems.Count) { state.Index = 0; state.Seed = new Random(state.Seed).Next(); } _random = new CloneableRandom(state.Seed); _shuffled = Shuffle(_scheduleItems, _random); State = new CollectionEnumeratorState { Seed = state.Seed }; while (State.Index < state.Index) { MoveNext(); } }
public ShuffleInOrderCollectionEnumerator( IList <CollectionWithItems> collections, CollectionEnumeratorState state) { _collections = collections; _mediaItemCount = collections.Sum(c => c.MediaItems.Count); if (state.Index >= _mediaItemCount) { state.Index = 0; state.Seed = new Random(state.Seed).Next(); } _random = new Random(state.Seed); _shuffled = Shuffle(_collections, _random); State = new CollectionEnumeratorState { Seed = state.Seed }; while (State.Index < state.Index) { MoveNext(); } }