Skip to content

Commit

Permalink
Fix empty Deque<T>.GetEnumerator()
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Sep 25, 2023
1 parent 92f31cb commit 22ff364
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Fix empty `Deque<T>.GetEnumerator()`

## [3.1.0] - 2023-09-24
### Added
- Add IModInt interface
Expand Down
6 changes: 5 additions & 1 deletion Source/ac-library-csharp/STL/Deque.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ public Enumerator(Deque<T> deque, bool isReverse)
{
this.deque = deque;
this.isReverse = isReverse;
if (isReverse)
if (deque.head == deque.tail)
{
index = last = 0;
}
else if (isReverse)
{
index = deque.tail;
last = deque.head;
Expand Down
94 changes: 94 additions & 0 deletions Test/ac-library-csharp.Test/STL/DequeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,106 @@
using System.Linq;
using AtCoder.Internal;
using FluentAssertions;
using MersenneTwister;
using Xunit;

namespace AtCoder
{
public class DequeTest
{
[Fact]
public void Empty()
{
Impl(new Deque<int>());
for (int capacity = 0; capacity < 10; capacity++)
{
Impl(new Deque<int>(capacity));

var deque = new Deque<int>(capacity);
deque.AddFirst(0);
deque.PopLast();
Impl(new Deque<int>(capacity));
}

static void Impl(Deque<int> deque)
{
deque.GetEnumerator().MoveNext().Should().BeFalse();
deque.Count.Should().Be(0);
deque.Should().BeEmpty();
deque.Should().Equal(Array.Empty<int>());
}
}

[Fact]
public void Lengths()
{
for (int size = 1; size < 10; size++)
{
var orig = Enumerable.Range(1, size).ToArray();
var deque = new Deque<int>();
foreach (var num in orig)
deque.AddLast(num);
deque.Count.Should().Be(size);
deque.Should().Equal(orig);
}
}

[Fact]
public void Random()
{
var mt = MTRandom.Create();
var deque = new Deque<int>();
var list = new LinkedList<int>();

void AddFirst(int num)
{
deque.AddFirst(num);
list.AddFirst(num);
}

void AddLast(int num)
{
deque.AddLast(num);
list.AddLast(num);
}
void PopFirst()
{
deque.PopFirst();
list.RemoveFirst();
}

void PopLast()
{
deque.PopLast();
list.RemoveLast();
}


for (int q = 0; q < 10000; q++)
{
var type = mt.Next(4);
if (deque.Count == 0) type %= 2;

switch (type)
{
case 0:
AddFirst(mt.Next());
break;
case 1:
AddLast(mt.Next());
break;
case 2:
PopFirst();
break;
case 3:
PopLast();
break;
}

deque.Should().Equal(list);
}
}

[Fact]
public void Simple()
{
Expand Down

0 comments on commit 22ff364

Please sign in to comment.