Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x
dotnet-version: 9.x
dotnet-quality: 'preview'
- name: Restore dependencies
run: dotnet restore
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x
dotnet-version: 9.x
dotnet-quality: 'preview'
- name: Restore dependencies
run: dotnet restore
Expand Down
155 changes: 150 additions & 5 deletions CardinalityEstimation.Test/BiasCorrectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// /*
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Xunit;

// /*
// See https://github.com/saguiitay/CardinalityEstimation.
// The MIT License (MIT)
//
Expand All @@ -22,11 +25,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// */

namespace CardinalityEstimation.Test
{
using Xunit;

public class BiasCorrectionTests
{
[Fact]
Expand Down Expand Up @@ -72,11 +72,156 @@ public void RawEstimateArraysAndBiasDataArraysHaveSameLengths()
{
Assert.True(BiasCorrection.RawEstimate.Length >= 14);
Assert.Equal(BiasCorrection.RawEstimate.Length, BiasCorrection.BiasData.Length);

for (var bits = 0; bits < BiasCorrection.RawEstimate.Length; bits++)
{
Assert.Equal(BiasCorrection.RawEstimate[bits].Length, BiasCorrection.BiasData[bits].Length);
}
}

/// <summary>
/// Verifies that when the calculated bias exceeds the raw estimate,
/// the corrected result is clamped to zero.
/// </summary>
[Theory]
[InlineData(4, 1.0)]
[InlineData(5, 5.0)]
[InlineData(6, 10.0)]
[Trait("Owner", "AI Testing Agent v0.1.0-alpha.25310.44+8471bbd")]
[Trait("Category", "auto-generated")]
public void CorrectBias_BiasExceedsRawEstimate_ReturnsZero(int bits, double rawEstimate)
{
// Arrange
// Act
double corrected = BiasCorrection.CorrectBias(rawEstimate, bits);
// Assert
Assert.Equal(0.0, corrected);
}

/// <summary>
/// Verifies that special floating-point inputs are handled in accordance with System.Math rules.
/// For PositiveInfinity, the result remains infinity; for NaN, the result is NaN;
/// for NegativeInfinity, the result is clamped to zero.
/// </summary>
/// <param name = "rawEstimate">The special raw estimate value.</param>
/// <param name = "isPositiveInfinityExpected">Indicates whether the result should be positive infinity.</param>
/// <param name = "isNaNExpected">Indicates whether the result should be NaN.</param>
[Theory]
[InlineData(double.PositiveInfinity, true, false)]
[InlineData(double.NegativeInfinity, false, false)]
[InlineData(double.NaN, false, true)]
[Trait("Owner", "AI Testing Agent v0.1.0-alpha.25310.44+8471bbd")]
[Trait("Category", "auto-generated")]
public void CorrectBias_SpecialFloatingPointInputs_PropagatesAccordingToMathRules(double rawEstimate, bool isPositiveInfinityExpected, bool isNaNExpected)
{
// Arrange
int bits = 4;
// Act
double corrected = BiasCorrection.CorrectBias(rawEstimate, bits);
// Assert
if (isNaNExpected)
{
Assert.True(double.IsNaN(corrected));
}
else if (isPositiveInfinityExpected)
{
Assert.True(double.IsPositiveInfinity(corrected));
}
else
{
Assert.Equal(0.0, corrected);
}
}

}

/// <summary>
/// Contains additional edge-case tests for <see cref = "BiasCorrection.CorrectBias(double, int)"/>.
/// The original happy-path scenarios already exist; here we focus on
/// numeric extremes, invalid estimator sizes and exact-match integrity
/// across multiple precision levels.
/// </summary>
public class BiasCorrectionEdgeTests
{
#region Exact-match scenarios
/// <summary>
/// Verifies that when the raw estimate exactly matches the first element
/// of the corresponding RawEstimate array, the bias from the matching
/// index is used without interpolation.
/// </summary>
/// <param name = "bits">Estimator precision being exercised.</param>
/// <param name = "rawEstimate">Exact raw estimate value located at index 0.</param>
/// <param name = "expectedBias">Bias located at index 0 for the same precision.</param>
[Theory]
[InlineData(4, 11.0, 10.0)]
[InlineData(5, 23.0, 22.0)]
[InlineData(6, 46.0, 45.0)]
[Trait("Owner", "AI Testing Agent v0.1.0-alpha.25306.18+1df180b")]
[Trait("Category", "auto-generated")]
public void CorrectBias_RawEstimateMatchesFirstArrayElement_ReturnsValueMinusBias(int bits, double rawEstimate, double expectedBias)
{
// Act
double corrected = BiasCorrection.CorrectBias(rawEstimate, bits);
// Assert
Assert.Equal(rawEstimate - expectedBias, corrected);
}

#endregion
#region Extreme double values
/// <summary>
/// Ensures that special floating-point inputs are propagated as defined
/// by System.Math.Max and arithmetic rules.
/// </summary>
/// <param name = "rawEstimate">Special double value being tested.</param>
/// <param name = "isPositiveInfinityExpected">True when the result should be positive infinity.</param>
/// <param name = "isNaNExpected">True when the result should be NaN.</param>
[Theory]
[InlineData(double.PositiveInfinity, true, false)]
[InlineData(double.NegativeInfinity, false, false)]
[InlineData(double.NaN, false, true)]
[Trait("Owner", "AI Testing Agent v0.1.0-alpha.25306.18+1df180b")]
[Trait("Category", "auto-generated")]
public void CorrectBias_SpecialFloatingPointInputs_PropagatesAccordingToMathRules(double rawEstimate, bool isPositiveInfinityExpected, bool isNaNExpected)
{
// Act
double result = BiasCorrection.CorrectBias(rawEstimate, 4);
// Assert
if (isNaNExpected)
{
Assert.True(double.IsNaN(result));
}
else if (isPositiveInfinityExpected)
{
Assert.True(double.IsPositiveInfinity(result));
}
else
{
Assert.Equal(0, result); // Negative infinity or very small values clamp to zero.
}
}

#endregion
#region Invalid estimator sizes
#endregion
#region Bias larger than raw estimate
/// <summary>
/// Confirms that when the calculated bias exceeds the raw estimate,
/// the corrected value is never negative and is instead clamped to zero.
/// </summary>
/// <param name = "bits">Estimator precision.</param>
/// <param name = "rawEstimate">Intentionally low raw estimate.</param>
[Theory]
[InlineData(4, 1.0)] // Bias ~10
[InlineData(5, 5.0)] // Bias ~22
[InlineData(6, 10.0)] // Bias ~45
[Trait("Owner", "AI Testing Agent v0.1.0-alpha.25306.18+1df180b")] // Bias ~45
[Trait("Category", "auto-generated")] // Bias ~45
public void CorrectBias_BiasExceedsRawEstimate_ReturnsZero(int bits, double rawEstimate)
{
// Act
double corrected = BiasCorrection.CorrectBias(rawEstimate, bits);
// Assert
Assert.Equal(0, corrected);
}
#endregion
}
}
6 changes: 3 additions & 3 deletions CardinalityEstimation.Test/CardinalityEstimation.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// /*
// /*
// See https://github.com/saguiitay/CardinalityEstimation.
// The MIT License (MIT)
//
Expand Down
Loading
Loading