Skip to content

Commit f541d09

Browse files
committed
Fixed some small issues found by AI review.
1 parent 7891fbf commit f541d09

3 files changed

Lines changed: 103 additions & 3 deletions

File tree

UnicodeHelper.Tests/UStringTests.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,100 @@ public void DetermineDirection(string? testStr, TextDirection expectedDirection)
731731
}
732732
#endregion
733733

734+
#region CompareTo tests
735+
private static IEnumerable<object[]> CompareToTestData =>
736+
[
737+
["", "", 0],
738+
["a", "a", 0],
739+
["a", "b", -1],
740+
["b", "a", 1],
741+
["ab", "abc", -1],
742+
["abc", "ab", 1],
743+
["abc", "ABC", 1]
744+
];
745+
746+
[TestMethod]
747+
[DynamicData(nameof(CompareToTestData))]
748+
public void CompareTo(string string1, string string2, int expectedResult)
749+
{
750+
UString us1 = new(string1);
751+
UString us2 = new(string2);
752+
753+
switch (expectedResult)
754+
{
755+
case > 0:
756+
Assert.IsGreaterThan(0, us1.CompareTo(us2));
757+
Assert.IsGreaterThan(0, ((IComparable)us1).CompareTo(us2));
758+
break;
759+
case < 0:
760+
Assert.IsLessThan(0, us1.CompareTo(us2));
761+
Assert.IsLessThan(0, ((IComparable)us1).CompareTo(us2));
762+
break;
763+
default:
764+
Assert.AreEqual(0, us1.CompareTo(us2));
765+
Assert.AreEqual(0, ((IComparable)us1).CompareTo(us2));
766+
break;
767+
}
768+
}
769+
#endregion
770+
771+
#region Clone tests
772+
[TestMethod]
773+
public void Clone()
774+
{
775+
UString original = new("test");
776+
object cloned = original.Clone();
777+
778+
Assert.IsInstanceOfType(cloned, typeof(UString));
779+
Assert.AreEqual(original, cloned);
780+
Assert.AreNotSame(original, cloned); // Should be different objects
781+
}
782+
783+
[TestMethod]
784+
public void Clone_Substring()
785+
{
786+
UString original = CreateTestSubstring("test");
787+
object cloned = original.Clone();
788+
789+
Assert.IsInstanceOfType(cloned, typeof(UString));
790+
Assert.AreEqual(original, cloned);
791+
Assert.AreNotSame(original, cloned); // Should be different objects
792+
793+
UString expected = new("test");
794+
Assert.AreEqual(expected, cloned);
795+
}
796+
#endregion
797+
798+
#region GetHashCode tests
799+
private static IEnumerable<object[]> GetHashCodeTestData =>
800+
[
801+
[""],
802+
["a"],
803+
["ab"],
804+
["Hello"],
805+
["😁🤔😮"]
806+
];
807+
808+
[TestMethod]
809+
[DynamicData(nameof(GetHashCodeTestData))]
810+
public void GetHashCode(string testString)
811+
{
812+
UString us1 = new(testString);
813+
814+
// Test that GetHash code for same content is consistent
815+
int hash1 = us1.GetHashCode();
816+
int hash2 = us1.GetHashCode();
817+
Assert.AreEqual(hash1, hash2); // Same instance should always give same hash
818+
819+
UString us2 = new(testString);
820+
Assert.AreEqual(hash1, us2.GetHashCode()); // Same contents should always give same hash
821+
822+
// For different strings with same content (substring vs full), the hash should be same
823+
UString sub = CreateTestSubstring(testString);
824+
Assert.AreEqual(us1.GetHashCode(), sub.GetHashCode());
825+
}
826+
#endregion
827+
734828
#region Normalization tests
735829
[TestMethod]
736830
public void Normalization_FormC()

UnicodeHelper/UCodepoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static UnicodeBidiClass GetBidiClass(UCodepoint uc)
128128
return UnicodeData.GetBidiClass(uc);
129129
}
130130

131-
// TODO: Should this be here for convenience or stay only in UnicodeProperties?
131+
// REVIEW: Should this be here for convenience or stay only in UnicodeProperties?
132132
//public static UnicodeProperty GetProperties(UCodepoint uc)
133133
//{
134134
// return UnicodeProperties.GetProps(uc);

UnicodeHelper/UStringBuilder.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public UStringBuilder() : this(DefaultCapacity)
4343
/// </summary>
4444
public UStringBuilder(int startingCapacity)
4545
{
46+
if (startingCapacity < 0)
47+
throw new ArgumentException("capacity must be greater than or equal to zero", nameof(startingCapacity));
48+
4649
if (startingCapacity < DefaultCapacity)
4750
startingCapacity = DefaultCapacity;
4851

@@ -62,7 +65,7 @@ public UStringBuilder(UString ustr) : this(ustr, ustr?.Length ?? 0)
6265
/// <see cref="UString"/> and starting capacity.
6366
/// </summary>
6467
public UStringBuilder(UString ustr, int startingCapacity) :
65-
this(startingCapacity < ustr.Length ? ustr.Length : startingCapacity)
68+
this(ustr != null && startingCapacity < ustr.Length ? ustr.Length : startingCapacity)
6669
{
6770
Append(ustr);
6871
}
@@ -80,7 +83,7 @@ public UStringBuilder(string ustr) : this(ustr, ustr?.Length ?? 0)
8083
/// .Net string and starting capacity.
8184
/// </summary>
8285
public UStringBuilder(string str, int startingCapacity) :
83-
this(startingCapacity < str.Length ? str.Length : startingCapacity)
86+
this(str != null && startingCapacity < str.Length ? str.Length : startingCapacity)
8487
{
8588
Append(str);
8689
}
@@ -103,6 +106,9 @@ internal UStringBuilder(int startingCapacity, int startingLength) : this(startin
103106
/// </remarks>
104107
public void Dispose()
105108
{
109+
if (_codepoints == null)
110+
return; // Already disposed
111+
106112
codepointArrayPool.Return(_codepoints);
107113
_codepoints = null;
108114
_length = 0;

0 commit comments

Comments
 (0)