Skip to content

Commit 839e37d

Browse files
committed
Rework photo bounds parsing, move bounds tests
1 parent f405201 commit 839e37d

4 files changed

Lines changed: 66 additions & 58 deletions

File tree

Refresh.Database/GameDatabaseContext.Photos.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public GamePhoto UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject
4747
PublishedAt = this._time.Now,
4848
};
4949

50-
float[] bounds = new float[PhotoHelper.SubjectBoundCount];
51-
5250
List<GamePhotoSubject> gameSubjects = new(subjects.Count());
5351
foreach (IPhotoUploadSubject subject in subjects)
5452
{
@@ -57,7 +55,7 @@ public GamePhoto UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject
5755
if (!string.IsNullOrEmpty(subject.Username))
5856
subjectUser = this.GetUserByUsername(subject.Username);
5957

60-
PhotoHelper.ParseBoundsList(subject.BoundsList, bounds);
58+
float[] bounds = PhotoHelper.ParseBoundsList(subject.BoundsList);
6159

6260
gameSubjects.Add(new GamePhotoSubject(subjectUser, subject.DisplayName, bounds));
6361

Refresh.Database/Helpers/PhotoHelper.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,29 @@ namespace Refresh.Database.Helpers;
44

55
public abstract class PhotoHelper
66
{
7-
public const byte SubjectBoundCount = 4;
7+
public const byte SubjectBoundaryCount = 4;
88

9-
public static void ParseBoundsList(ReadOnlySpan<char> input, float[] floats)
9+
public static float[] ParseBoundsList(string input)
1010
{
11-
byte start = 0;
12-
byte floatIndex = 0;
11+
if (string.IsNullOrWhiteSpace(input))
12+
throw new ArgumentException($"Bounds are empty");
1313

14-
for (byte i = 0; i < input.Length; i++)
14+
string[] boundsStr = input.Split(',');
15+
if (boundsStr.Length != SubjectBoundaryCount)
16+
throw new ArgumentException($"Number of bounds is invalid ({boundsStr.Length} instead of {SubjectBoundaryCount})");
17+
18+
float[] boundsParsed = new float[SubjectBoundaryCount];
19+
for (int i = 0; i < SubjectBoundaryCount; i++)
1520
{
16-
if(floatIndex == SubjectBoundCount - 1) break; // won't catch the last float - not worth reading
17-
if (input[i] != ',') continue;
18-
19-
if (!float.TryParse(input.Slice(start, i - start), NumberFormatInfo.InvariantInfo, out float f))
20-
throw new FormatException("Invalid format");
21-
22-
floats[floatIndex] = f;
23-
floatIndex++;
24-
start = (byte)(i + 1);
21+
// No OutOfRangeExceptions as both arrays are ensured to be as long as the constant
22+
string boundaryStr = boundsStr[i];
23+
24+
if (!float.TryParse(boundaryStr, NumberFormatInfo.InvariantInfo, out float f))
25+
throw new FormatException($"Boundary {boundaryStr} ({i+1}/{SubjectBoundaryCount}) is not a float");
26+
27+
boundsParsed[i] = f;
2528
}
26-
27-
// parse the last float value - bounds does not end with comma
28-
if (!float.TryParse(input[start..], NumberFormatInfo.InvariantInfo, out float lastFloat))
29-
throw new FormatException("Invalid format");
3029

31-
floats[SubjectBoundCount - 1] = lastFloat;
30+
return boundsParsed;
3231
}
3332
}

RefreshTests.GameServer/Tests/Parsing/PhotoBoundsTests.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Refresh.Database.Helpers;
2+
3+
namespace RefreshTests.GameServer.Tests.Photos;
4+
5+
public class PhotoBoundsTests
6+
{
7+
[Test]
8+
public void ParsesBounds()
9+
{
10+
string bounds = "0.652369,0.710704,0.838377,0.997791";
11+
float[] floats = PhotoHelper.ParseBoundsList(bounds);
12+
13+
#pragma warning disable NUnit2045
14+
Assert.That(floats.Length, Is.EqualTo(4));
15+
Assert.That(floats[0], Is.EqualTo(0.652369f));
16+
Assert.That(floats[1], Is.EqualTo(0.710704f));
17+
Assert.That(floats[2], Is.EqualTo(0.838377f));
18+
Assert.That(floats[3], Is.EqualTo(0.997791f));
19+
#pragma warning restore NUnit2045
20+
}
21+
22+
[Test]
23+
public void CatchesInvalidFloats()
24+
{
25+
const string bounds1 = "0.652369,0.71a0704,0.838377,0.997791";
26+
const string bounds2 = "0.652369,0.710704,0.838377,0.9977a91";
27+
28+
Assert.Multiple(() =>
29+
{
30+
Assert.That(() => PhotoHelper.ParseBoundsList(bounds1), Throws.TypeOf<FormatException>());
31+
Assert.That(() => PhotoHelper.ParseBoundsList(bounds2), Throws.TypeOf<FormatException>());
32+
});
33+
}
34+
35+
[Test]
36+
public void CatchesInvalidLength()
37+
{
38+
const string bounds1 = "0.652369,0.710704,0.838377,0.997791,1.0,2.0,67.0";
39+
const string bounds2 = "0.652369";
40+
41+
Assert.Multiple(() =>
42+
{
43+
Assert.That(() => PhotoHelper.ParseBoundsList(bounds1), Throws.TypeOf<ArgumentException>());
44+
Assert.That(() => PhotoHelper.ParseBoundsList(bounds2), Throws.TypeOf<ArgumentException>());
45+
});
46+
}
47+
}

0 commit comments

Comments
 (0)