Skip to content
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

Add files for first version #1

Merged
merged 14 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add Clamp() to IntExtensions
  • Loading branch information
eduherminio committed Apr 30, 2020
commit 62953d3bdb29fa236341e4175fe16a3a5c2e69cf
16 changes: 16 additions & 0 deletions src/SheepTools/Extensions/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,21 @@ public static int Factorial(this int n)
? n * Factorial(n - 1)
: 1;
}

public static int Clamp(this int value, int min, int max)
{
if (value <= min)
{
return min;
}
else if (value >= max)
{
return max;
}
else
{
return value;
}
}
}
}
29 changes: 29 additions & 0 deletions tests/SheepTools.Test/Extensions/IntExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,34 @@ public void Factorial()
Assert.Equal(pair.Value, pair.Key.Factorial());
}
}

[Fact]
public void Clamp()
{
const int minValue = -3;
const int maxValue = +3;

var testCases = new Dictionary<int, int>()
{
[int.MinValue] = minValue,
[-99999999] = minValue,
[-4] = minValue,
[minValue] = minValue,
[-2] = -2,
[-1] = -1,
[0] = 0,
[+1] = +1,
[+2] = +2,
[maxValue] = maxValue,
[+4] = maxValue,
[+99999999] = maxValue,
[int.MaxValue] = maxValue
};

foreach (var pair in testCases)
{
Assert.Equal(pair.Value, pair.Key.Factorial());
}
}
}
}