Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Commit 4ae6201

Browse files
committed
Adds tests regarding negative integers in numerics.
1 parent e73af5e commit 4ae6201

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

Common.Tests/NumericUtilsTests.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace CarteDiem.Common.Tests {
55
public unsafe class NumericUtilTests {
66

77
[Test]
8-
public void CharEqualsDigit() {
8+
public void CharEqualsPositiveDigit() {
99
int value = 1234567890;
1010

1111
char* actual = stackalloc char[10];
@@ -15,13 +15,12 @@ public void CharEqualsDigit() {
1515
string expected = value.ToString();
1616

1717
for (int i = 0; i < count; i++) {
18-
UnityEngine.Debug.Log(actual[i]);
1918
Assert.AreEqual(expected[i], actual[i], "Mismatched digits");
2019
}
2120
}
2221

2322
[Test]
24-
public void CharEqualsDigitWithExcess() {
23+
public void CharEqualsPositiveDigitWithExcess() {
2524
int value = 12345;
2625

2726
char* actual = stackalloc char[15];
@@ -33,5 +32,31 @@ public void CharEqualsDigitWithExcess() {
3332
Assert.AreEqual(exepected[i], actual[i], "Mismatched digits");
3433
}
3534
}
35+
36+
[Test]
37+
public void CharEqualsNegativeDigits() {
38+
int value = -12345;
39+
40+
char* actual = stackalloc char[6];
41+
value.ToCharArray(actual, 6, out int count);
42+
string expected = value.ToString();
43+
44+
for (int i = 0; i < count; i++) {
45+
Assert.AreEqual(expected[i], actual[i], "Mismatch digits");
46+
}
47+
}
48+
49+
[Test]
50+
public void CharEqualsNegativeDigitsWIthExcess() {
51+
int value = -1234569;
52+
53+
char* actual = stackalloc char[15];
54+
value.ToCharArray(actual, 15, out int count);
55+
string expected = value.ToString();
56+
57+
for (int i = 0; i < count; i++) {
58+
Assert.AreEqual(expected[i], actual[i], "Mismatch digits");
59+
}
60+
}
3661
}
3762
}

Common/NumericUtils.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Unity.Collections;
2+
using Unity.Mathematics;
23

34
namespace CarteDiem.Common {
45

@@ -11,6 +12,9 @@ public unsafe static class NumericUtils {
1112
/// <param name="length">The size of the buffer</param>
1213
/// <param name="count">The total number of counted digits</param>
1314
public static void ToCharArray(this int value, char* ptr, in int length, out int count) {
15+
var offset = value < 0 ? 0 : -1;
16+
17+
value = math.abs(value);
1418
var stack = new NativeList<char>(Allocator.Temp);
1519
do {
1620
var mod = value % 10;
@@ -21,14 +25,20 @@ public static void ToCharArray(this int value, char* ptr, in int length, out int
2125
} while (value != 0);
2226

2327
for (int i = 0; i < stack.Length; i++) {
24-
var flipped = stack.Length - 1 - i;
28+
var flipped = stack.Length + offset - i;
2529

2630
if (flipped < length) {
2731
ptr[flipped] = stack[i];
2832
}
2933
}
3034

3135
count = stack.Length;
36+
37+
if (offset >= 0) {
38+
ptr[0] = '-';
39+
count++;
40+
}
41+
3242
stack.Dispose();
3343
}
3444

Common/UGUIDots.Common.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "UGUIDots.Common",
33
"references": [
4-
"GUID:e0cd26848372d4e5c891c569017e11f1"
4+
"GUID:e0cd26848372d4e5c891c569017e11f1",
5+
"GUID:d8b63aba1907145bea998dd612889d6b"
56
],
67
"includePlatforms": [],
78
"excludePlatforms": [],

0 commit comments

Comments
 (0)