Skip to content

Commit

Permalink
Merge branch 'development' into dev/copyright-updater
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhazen authored Mar 7, 2024
2 parents 39933e8 + c47c114 commit 8b67686
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 1 deletion.
78 changes: 78 additions & 0 deletions Assets/Plugins/Source/Editor/Utility/SafeTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024 PlayEveryWare
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace PlayEveryWare.EpicOnlineServices.Editor.Utility
{
/// <summary>
/// Used to safely convert between signed and unsigned values.
/// </summary>
public static class SafeTranslator
{
/// <summary>
/// Try to convert an int value to a uint.
/// </summary>
/// <param name="value">The int value to convert.</param>
/// <param name="output">The uint equivalent to the int provided</param>
/// <returns>True if the conversion was successful, false otherwise.</returns>
public static bool TryConvert(int value, out uint output)
{
output = unchecked((uint)value);
return value >= 0;
}

/// <summary>
/// Try to convert uint value to an int.
/// </summary>
/// <param name="value">The uint value to convert.</param>
/// <param name="output">The int equivalent.</param>
/// <returns>True if the conversion was successful, false otherwise.</returns>
public static bool TryConvert(uint value, out int output)
{
output = unchecked((int)value);
return value <= int.MaxValue;
}

/// <summary>
/// Try to convert ulong to long.
/// </summary>
/// <param name="value">The ulong value to convert.</param>
/// <param name="output">The long equivalent.</param>
/// <returns>True if the conversion was successful, false otherwise.</returns>
public static bool TryConvert(ulong value, out long output)
{
output = unchecked((long)value);
return value <= long.MaxValue;
}

/// <summary>
/// Safely convert from a long value to a ulong value.
/// </summary>
/// <param name="value">The long value to convert.</param>
/// <param name="output">The ulong equivalent.</param>
/// <returns>True if the conversion was successful, false otherwise.</returns>
public static bool TryConvert(long value, out ulong output)
{
output = unchecked((ulong)value);
return value >= 0;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Plugins/Source/Editor/Utility/SafeTranslator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
}
],
"noEngineReferences": false
}
}
8 changes: 8 additions & 0 deletions Assets/Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Tests/EditMode.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Assets/Tests/EditMode/com.playeveryware.eos.tests.editmode.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "com.playeveryware.eos.tests.editmode",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Tests/PlayMode.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions Assets/Tests/PlayMode/SafeTranslatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2024 PlayEveryWare
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

using NUnit.Framework;

namespace PlayEveryWare.EpicOnlineServices.Editor.Utility
{
public class SafeTranslatorTests
{
private delegate bool TryConvertDelegate<TInput, TOutput>(TInput input, out TOutput output);

/// <summary>
/// Tests a given conversionMethod using provided input and expected output.
/// </summary>
/// <typeparam name="TInput">The input type.</typeparam>
/// <typeparam name="TOutput">The output type (opposite sign of the input type).</typeparam>
/// <param name="inputValue">The input value.</param>
/// <param name="expectedResult">Whether or not the conversion is successful.</param>
/// <param name="expectedOutput">The expected value of output after the conversion is attempted.</param>
/// <param name="conversionMethod">The method of conversion to use.</param>
private static void TestTryConvert<TInput, TOutput>(
TInput inputValue,
bool expectedResult,
TOutput expectedOutput,
TryConvertDelegate<TInput, TOutput> conversionMethod)
{
bool converted = conversionMethod(inputValue, out TOutput output);

Assert.AreEqual(expectedResult, converted, $"Expected conversion result to be {expectedResult}.");
Assert.AreEqual(expectedOutput, output, $"Expected output to be {expectedOutput}.");
}

/// <summary>
/// Tests that when you try to convert a negative int to a uint, the conversion
/// fails, and the output value is set to a straightforward cast.
/// </summary>
[Test]
public static void TryConvert_IntToUint_NegativeInt_ReturnsFalse()
{
const int negativeValue = -1;
const uint uncheckedCast = unchecked((uint)negativeValue);

TestTryConvert(negativeValue, false, uncheckedCast, SafeTranslator.TryConvert);
}

/// <summary>
/// Tests that an int can be converted safely to a uint when the int value
/// is set to int.MaxValue.
/// </summary>
[Test]
public static void TryConvert_IntToUint_PositiveInt_ReturnsTrue()
{
const int positiveValue = int.MaxValue;
const uint uncheckedCast = unchecked((uint)positiveValue);

TestTryConvert(positiveValue, true, uncheckedCast, SafeTranslator.TryConvert);
}

/// <summary>
/// Test that trying to convert a uint with a value that is higher than int.MaxValue
/// results in the TryConvert function returning false, and the output value being
/// set to what a straightforward.
/// </summary>
[Test]
public static void TryConvert_UintToInt_OverflowInt_ReturnsFalse()
{
const uint overflowValue = uint.MaxValue;
const int uncheckedCast = unchecked((int)overflowValue);

TestTryConvert(overflowValue, false, uncheckedCast, SafeTranslator.TryConvert);
}

/// <summary>
/// Test that trying to convert a uint to int within range works properly (returns
/// true, and has output set to unchecked cast.
/// </summary>
[Test]
public static void TryConvert_UintToInt_PositiveInt_ReturnsTrue()
{
const uint positiveValue = 55;
const int uncheckedCast = unchecked((int)positiveValue);

TestTryConvert(positiveValue, true, uncheckedCast, SafeTranslator.TryConvert);
}

[Test]
public static void TryConvert_LongToUlong_NegativeLong_ReturnsFalse()
{
const long negativeValue = -1;
const ulong uncheckedCast = unchecked((ulong)negativeValue);

TestTryConvert(negativeValue, false, uncheckedCast, SafeTranslator.TryConvert);
}

[Test]
public static void TryConvert_LongToULong_PositiveLong_ReturnsTrue()
{
const long positiveValue = 55;
const ulong uncheckedCast = unchecked((ulong)positiveValue);

TestTryConvert(positiveValue, true, uncheckedCast, SafeTranslator.TryConvert);
}

[Test]
public static void TryConvert_ULongToLong_OverflowLong_ReturnsFalse()
{
const ulong overflowValue = ulong.MaxValue;
const long uncheckedCast = unchecked((long)overflowValue);

TestTryConvert(overflowValue, false, uncheckedCast, SafeTranslator.TryConvert);
}

[Test]
public static void TryConvert_ULongToLong_PositiveLong_ReturnsTrue()
{
const long positiveValue = 55;
const ulong uncheckedCast = unchecked((ulong)positiveValue);

TestTryConvert(positiveValue, true, uncheckedCast, SafeTranslator.TryConvert);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/PlayMode/SafeTranslatorTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Assets/Tests/PlayMode/com.playeveryware.eos.tests.playmode.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "com.playeveryware.eos.tests.playmode",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"com.playeveryware.eos-Editor"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b67686

Please sign in to comment.