Skip to content

Commit 4dd9de1

Browse files
[patch] Cover ContiguousMap.Entry equality and exclude benchmarks from Sonar
The SonarCloud quality gate failed on new_coverage (0% vs 80% required), from exactly two uncovered new lines: - Containers/ContiguousMap.cs - ContiguousMap<TKey,TValue>.Entry is a public struct with a full equality surface (Equals(Entry), Equals(object?), GetHashCode, == and !=) and had no tests at all. Added four tests covering equal/unequal entries, comparison against null and an unrelated type, and the Key/Value properties. - Containers.Benchmarks - benchmark code is never executed by the test suite, so it can only ever report 0% coverage. Set SonarQubeExclude on the benchmark project. Excluding the benchmark project also retires the 33 benchmark-only findings that are artifacts of benchmark style rather than defects: 29x S3267 (identical foreach/counter loops kept uniform across competing collections so the comparisons stay fair), 3x S4158 (operations on intentionally-empty collections in EmptyCollectionOperations, plus a foreach immediately after PushBack) and 1x S2583. Build clean on all 8 TFMs, 355/355 tests pass.
1 parent 4b449ae commit 4dd9de1

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

Containers.Benchmarks/Containers.Benchmarks.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
<Nullable>enable</Nullable>
77
<AssemblyName>Containers.Benchmarks</AssemblyName>
88
<RootNamespace>ktsu.Containers.Benchmarks</RootNamespace>
9+
<!--
10+
Exclude the benchmark harness from SonarQube/SonarCloud analysis. Benchmarks are
11+
deliberately written in a non-idiomatic style: explicit foreach/counter loops are kept
12+
identical across competing collections so the comparisons stay fair, which Sonar reports
13+
as S3267 ("use Where"), and operations on intentionally-empty collections read as S4158.
14+
Benchmark code is also never executed by the test suite, so it can only ever drag the
15+
new-code coverage gate to 0%. It is not shipped in the package.
16+
-->
17+
<SonarQubeExclude>true</SonarQubeExclude>
918
</PropertyGroup>
1019

1120
<ItemGroup>

Containers.Test/ContiguousMapTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,56 @@ public void HandlesNullValues()
525525
Assert.IsTrue(map.TryGetValue(1, out string? value), "TryGetValue should return true for key with null value");
526526
Assert.IsNull(value);
527527
}
528+
529+
[TestMethod]
530+
public void Entry_Equals_SameKeyAndValue_ReturnsTrue()
531+
{
532+
// Arrange
533+
ContiguousMap<int, string>.Entry left = new(1, "one");
534+
ContiguousMap<int, string>.Entry right = new(1, "one");
535+
536+
// Act & Assert
537+
Assert.IsTrue(left.Equals(right), "Entries with the same key and value should be equal");
538+
Assert.IsTrue(left.Equals((object)right), "Equals(object) should agree with Equals(Entry)");
539+
Assert.IsTrue(left == right, "operator == should report equality");
540+
Assert.IsFalse(left != right, "operator != should report equality");
541+
Assert.AreEqual(left.GetHashCode(), right.GetHashCode());
542+
}
543+
544+
[TestMethod]
545+
public void Entry_Equals_DifferentKeyOrValue_ReturnsFalse()
546+
{
547+
// Arrange
548+
ContiguousMap<int, string>.Entry entry = new(1, "one");
549+
ContiguousMap<int, string>.Entry differentKey = new(2, "one");
550+
ContiguousMap<int, string>.Entry differentValue = new(1, "two");
551+
552+
// Act & Assert
553+
Assert.IsFalse(entry.Equals(differentKey), "Entries with different keys should not be equal");
554+
Assert.IsFalse(entry.Equals(differentValue), "Entries with different values should not be equal");
555+
Assert.IsTrue(entry != differentKey, "operator != should report inequality");
556+
Assert.IsFalse(entry == differentValue, "operator == should report inequality");
557+
}
558+
559+
[TestMethod]
560+
public void Entry_Equals_NonEntryObject_ReturnsFalse()
561+
{
562+
// Arrange
563+
ContiguousMap<int, string>.Entry entry = new(1, "one");
564+
565+
// Act & Assert
566+
Assert.IsFalse(entry.Equals(null), "An entry should not equal null");
567+
Assert.IsFalse(entry.Equals("not an entry"), "An entry should not equal an unrelated type");
568+
}
569+
570+
[TestMethod]
571+
public void Entry_KeyAndValue_ExposeConstructorArguments()
572+
{
573+
// Arrange & Act
574+
ContiguousMap<int, string>.Entry entry = new(7, "seven");
575+
576+
// Assert
577+
Assert.AreEqual(7, entry.Key);
578+
Assert.AreEqual("seven", entry.Value);
579+
}
528580
}

0 commit comments

Comments
 (0)