-
-
Notifications
You must be signed in to change notification settings - Fork 24
Add SubtractSaturating extension method for TimeSpan #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
9dcbef7
10706ca
52f410f
3bbce51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,4 +65,43 @@ public void ApproximateAge() | |
Assert.Equal("2 days ago", DateTime.Now.AddHours(-48).ToApproximateAgeString()); | ||
Assert.Equal("1 week ago", DateTime.Now.AddDays(-7).ToApproximateAgeString()); | ||
} | ||
|
||
[Fact] | ||
public void SubtractSaturating() | ||
|
||
{ | ||
// Basic case: normal subtraction when result is positive | ||
var timeSpan1 = TimeSpan.FromHours(5); | ||
var timeSpan2 = TimeSpan.FromHours(2); | ||
Assert.Equal(TimeSpan.FromHours(3), timeSpan1.SubtractSaturating(timeSpan2)); | ||
|
||
// Saturating case: result would be negative, should return TimeSpan.Zero | ||
var timeSpan3 = TimeSpan.FromHours(2); | ||
var timeSpan4 = TimeSpan.FromHours(5); | ||
Assert.Equal(TimeSpan.Zero, timeSpan3.SubtractSaturating(timeSpan4)); | ||
|
||
// Edge case: equal values should return TimeSpan.Zero | ||
var timeSpan5 = TimeSpan.FromMinutes(30); | ||
var timeSpan6 = TimeSpan.FromMinutes(30); | ||
Assert.Equal(TimeSpan.Zero, timeSpan5.SubtractSaturating(timeSpan6)); | ||
|
||
// Zero cases | ||
Assert.Equal(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10).SubtractSaturating(TimeSpan.Zero)); | ||
Assert.Equal(TimeSpan.Zero, TimeSpan.Zero.SubtractSaturating(TimeSpan.FromSeconds(5))); | ||
Assert.Equal(TimeSpan.Zero, TimeSpan.Zero.SubtractSaturating(TimeSpan.Zero)); | ||
|
||
// Large values | ||
var largeTimeSpan1 = TimeSpan.FromDays(100); | ||
var largeTimeSpan2 = TimeSpan.FromDays(50); | ||
Assert.Equal(TimeSpan.FromDays(50), largeTimeSpan1.SubtractSaturating(largeTimeSpan2)); | ||
|
||
// Negative TimeSpan values - testing with negative self value | ||
var negativeTimeSpan = TimeSpan.FromHours(-2); | ||
var positiveTimeSpan = TimeSpan.FromHours(1); | ||
Assert.Equal(TimeSpan.Zero, negativeTimeSpan.SubtractSaturating(positiveTimeSpan)); | ||
|
||
// Negative other value (subtracting negative is like adding) | ||
var timeSpan7 = TimeSpan.FromHours(3); | ||
var negativeTimeSpan2 = TimeSpan.FromHours(-2); | ||
Assert.Equal(TimeSpan.FromHours(5), timeSpan7.SubtractSaturating(negativeTimeSpan2)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot add xml documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comprehensive XML documentation to the SubtractSaturating method following the project's documentation standards. The documentation includes a summary, parameter descriptions, and return value description. Commit: 91c1ff8