Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into refactor/37-circularAr…
Browse files Browse the repository at this point in the history
…ray-to-exposedQueue
  • Loading branch information
JoenvandeVorle committed May 27, 2024
2 parents 8da8085 + 390d295 commit 94836cc
Show file tree
Hide file tree
Showing 53 changed files with 1,073 additions and 361 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
<ProjectReference Include="..\Aplib.Core\Aplib.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Agents\"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Aplib.Core.Belief;
using Aplib.Core.Desire;
using Aplib.Core.Agents;
using Aplib.Core.Belief.BeliefSets;
using Aplib.Core.Desire.DesireSets;
using Aplib.Core.Desire.Goals;
using Aplib.Core.Intent.Actions;
using Aplib.Core.Intent.Tactics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Aplib.Core.Belief;
using Aplib.Core.Belief.Beliefs;
using Aplib.Core.Belief.BeliefSets;

namespace Aplib.Core.Tests.Belief;

Expand Down Expand Up @@ -98,7 +99,6 @@ private class TestBeliefSetProperties : BeliefSet
/// Belief that sets Updated to true when UpdateBelief is called.
/// </summary>
public SimpleBelief Belief2 { get; } = new();

}


Expand All @@ -122,7 +122,6 @@ private class TestBeliefSetPrivate : BeliefSet

/// <inheritdoc cref="_belief2"/>
public SimpleBelief Belief2 => _belief2;

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Aplib.Core.Belief;
using Aplib.Core.Belief.Beliefs;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -7,10 +7,29 @@
namespace Aplib.Core.Tests.Belief;

/// <summary>
/// Describes a set of tests for the <see cref="Belief{TReference,TObservation}"/> class.
/// Describes a set of tests for the <see cref="Belief{TReference,TObservation}" /> class.
/// </summary>
public class BeliefTests
{
private struct MyEnumerable : IEnumerable<int>
{
private readonly int _number;

private const int _max = 3;

public MyEnumerable(int number) => _number = number;

public IEnumerator<int> GetEnumerator()
{
for (int i = 0; i < _max; i++)
{
yield return _number;
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

/// <summary>
/// Given a Belief instance,
/// When it is assigned to a variable of its observation type,
Expand All @@ -22,6 +41,7 @@ public void Belief_AssignedToObservationType_IsCorrectlyImplicitlyConvertedToObs
// Arrange
// ReSharper disable once ConvertToConstant.Local
string def = "def";

// Observation: Get the first letter.
Belief<string, char> belief = new(def, reference => reference[0]);

Expand All @@ -30,45 +50,45 @@ public void Belief_AssignedToObservationType_IsCorrectlyImplicitlyConvertedToObs
}

/// <summary>
/// Given a Belief instance with an shouldUpdate condition that is satisfied,
/// When UpdateBelief is called,
/// Then the observation is updated.
/// Given a reference that is actually a value type, hidden behind an interface,
/// When a new Belief is constructed from this reference,
/// The constructor throws an ArgumentException.
/// </summary>
[Fact]
public void UpdateBelief_ShouldUpdateConditionIsSatisfied_UpdatesObservation()
public void Belief_ConstructedWithAValueTypeViaAnInterface_IsRejected()
{
// Arrange
List<int> list = [];
Belief<List<int>, int> belief = new(list, reference => reference.Count, () => true);
MyEnumerable value = new(1);
const string paramName = "reference";

// Act
list.Add(69);
belief.UpdateBelief();
// ReSharper disable once ConvertToLocalFunction
Action construction = () =>
{
// The bug is the fact that we can get around the constraint that `TReference` should be a reference type.
_ = new Belief<IEnumerable<int>, List<int>>(value, values => values.ToList());
};

// Assert
Assert.Equal(list.Count, belief);
Assert.Equal(list.Count, belief.Observation);
// Act, Assert
Assert.Throws<ArgumentException>(paramName, construction);
}

/// <summary>
/// Given a Belief instance with an shouldUpdate condition that is not satisfied,
/// When UpdateBelief is called,
/// Then the observation is not updated.
/// Given a reference,
/// When a new Belief is constructed,
/// Then the observation is also initialized.
/// </summary>
[Fact]
public void UpdateBelief_ShouldUpdateConditionIsNotSatisfied_DoesNotUpdateObservation()
public void Belief_DuringConstruction_UpdatesTheObservation()
{
// Arrange
List<int> list = [];
Belief<List<int>, int> belief = new(list, reference => reference.Count, () => false);
// ReSharper disable once ConvertToConstant.Local
string def = "def";

// Act
list.Add(420);
belief.UpdateBelief();
Belief<string, string> belief = new(def, str => str);

// Assert
Assert.NotEqual(list.Count, belief);
Assert.NotEqual(list.Count, belief.Observation);
Assert.Equal(def, belief.Observation);
}

/// <summary>
Expand All @@ -92,59 +112,44 @@ public void UpdateBelief_ReferenceIsAssignedTo_DoesNotUpdateObservation()
}

/// <summary>
/// Given a reference,
/// When a new Belief is constructed,
/// Then the observation is also initialized.
/// Given a Belief instance with an shouldUpdate condition that is not satisfied,
/// When UpdateBelief is called,
/// Then the observation is not updated.
/// </summary>
[Fact]
public void Belief_DuringConstruction_UpdatesTheObservation()
public void UpdateBelief_ShouldUpdateConditionIsNotSatisfied_DoesNotUpdateObservation()
{
// Arrange
// ReSharper disable once ConvertToConstant.Local
string def = "def";
List<int> list = [];
Belief<List<int>, int> belief = new(list, reference => reference.Count, () => false);

// Act
Belief<string, string> belief = new(def, str => str);
list.Add(420);
belief.UpdateBelief();

// Assert
Assert.Equal(def, belief.Observation);
Assert.NotEqual(list.Count, belief);
Assert.NotEqual(list.Count, belief.Observation);
}

/// <summary>
/// Given a reference that is actually a value type, hidden behind an interface,
/// When a new Belief is constructed from this reference,
/// The constructor throws an ArgumentException.
/// Given a Belief instance with an shouldUpdate condition that is satisfied,
/// When UpdateBelief is called,
/// Then the observation is updated.
/// </summary>
[Fact]
public void Belief_ConstructedWithAValueTypeViaAnInterface_IsRejected()
public void UpdateBelief_ShouldUpdateConditionIsSatisfied_UpdatesObservation()
{
// Arrange
MyEnumerable value = new(1);
const string paramName = "reference";
// ReSharper disable once ConvertToLocalFunction
Action construction = () =>
{
// The bug is the fact that we can get around the constraint that `TReference` should be a reference type.
Belief<IEnumerable<int>, List<int>> _ = new(value, values => values.ToList());
};

// Act, Assert
Assert.Throws<ArgumentException>(paramName, construction);
}

private struct MyEnumerable : IEnumerable<int>
{
private readonly int _number;

private const int MAX = 3;

public MyEnumerable(int number) => _number = number;
List<int> list = [];
Belief<List<int>, int> belief = new(list, reference => reference.Count, () => true);

public IEnumerator<int> GetEnumerator()
{
for (int i = 0; i < MAX; i++) yield return _number;
}
// Act
list.Add(69);
belief.UpdateBelief();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
// Assert
Assert.Equal(list.Count, belief);
Assert.Equal(list.Count, belief.Observation);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Aplib.Core.Belief;
using Aplib.Core.Belief.Beliefs;
using System.Collections.Generic;

namespace Aplib.Core.Tests.Belief;
Expand All @@ -18,6 +18,7 @@ public void ListBelief_WithoutShouldUpdate_UpdatesObservation()
{
// Arrange
int[] numbers = [1, 1, 2, 3, 5, 8];

// Observation: Is the number even?
ListBelief<int, bool> belief = new(numbers, i => i % 2 == 0);

Expand All @@ -40,6 +41,7 @@ public void ListBelief_ShouldUpdateConditionIsNotSatisfied_DoesNotUpdateObservat
{
// Arrange
List<string> strings = ["foo", "bar"];

// Observation: What is the last character?
ListBelief<string, char> belief = new(strings, str => str[^1], () => false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Aplib.Core.Belief;
using System;
using Aplib.Core.Belief.Beliefs;
using System.Collections.Generic;

namespace Aplib.Core.Tests.Belief;
Expand Down Expand Up @@ -55,22 +54,22 @@ public void GetMemoryAt_WhenObservationIsUpdated_ShouldReturnObservationAtSpecif
/// <summary>
/// Given a MemoryBelief instance with an observation,
/// When asking for an index that is out of bounds,
/// Then an exception should be thrown.
/// Then the closest element that is in bounds is returned.
/// </summary>
[Fact]
public void GetMemoryAt_IndexOutOfBounds_ShouldThrowException()
public void GetMemoryAt_IndexOutOfBounds_ShouldReturnClosestElement()
{
// Arrange
List<int> list = [1, 2, 3];
MemoryBelief<List<int>, int> belief = new(list, reference => reference.Count, 3);

// Act
void GetMemoryAtNegativeIndex() => belief.GetMemoryAt(-1);
void GetMemoryAtIndexGreaterThanCount() => belief.GetMemoryAt(3);
list.Add(4);
belief.UpdateBelief();

// Assert
Assert.Throws<ArgumentOutOfRangeException>(GetMemoryAtNegativeIndex);
Assert.Throws<ArgumentOutOfRangeException>(GetMemoryAtIndexGreaterThanCount);
Assert.Equal(3, belief.GetMemoryAt(-1, true));
Assert.Equal(0, belief.GetMemoryAt(5, true));
}

/// <summary>
Expand All @@ -90,6 +89,6 @@ public void GetAllMemories_ReturnsAllMemories()
belief.UpdateBelief();

// Assert
Assert.Equal([3], belief.GetAllMemories());
Assert.Equal([3, 0, 0], belief.GetAllMemories());
}
}
Loading

0 comments on commit 94836cc

Please sign in to comment.