Skip to content

Commit

Permalink
Fix min/max also test (#1646)
Browse files Browse the repository at this point in the history
Co-authored-by: wixoa <[email protected]>
  • Loading branch information
amylizzle and wixoaGit authored Feb 17, 2024
1 parent 1627499 commit 003f7d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
22 changes: 22 additions & 0 deletions Content.Tests/DMProject/Tests/Math/minmax.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/proc/RunTest()
ASSERT(min(1,2,3) == 1)
ASSERT(min(null, 1) == null)
ASSERT(min(null, -1) == -1)
ASSERT(min(1, null) == null)
ASSERT(min(-1, null) == -1)
ASSERT(min(1, null) == null)
ASSERT(min(0, null) == null)
ASSERT(min(null, 0) == 0)
ASSERT(min("a","b","c")=="a")
ASSERT(min("b","a","c")=="a")

ASSERT(max(1,2,3) == 3)
ASSERT(max(null, 1) == 1)
ASSERT(max(null, -1) == null)
ASSERT(max(1,null) == 1)
ASSERT(max(-1,null) == null)
ASSERT(max(1, null) == 1)
ASSERT(max(0, null) == null)
ASSERT(max(null, 0) == 0)
ASSERT(max("a","b","c")=="c")
ASSERT(max("b","a","c")=="c")
15 changes: 10 additions & 5 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,14 +1473,19 @@ public static DreamValue NativeProc_md5(NativeProc.Bundle bundle, DreamObject? s
}

private static DreamValue MinComparison(DreamValue min, DreamValue value) {
if (value.TryGetValueAsFloat(out var lFloat) && min.TryGetValueAsFloat(out var rFloat)) {
if (lFloat < rFloat)
if (value.TryGetValueAsFloat(out var lFloat)) {
if (min.IsNull && lFloat <= 0)
min = value;
} else if (value.TryGetValueAsString(out var lString) && min.TryGetValueAsString(out var rString)) {
if (string.Compare(lString, rString, StringComparison.Ordinal) < 0)
else if (min.TryGetValueAsFloat(out var rFloat) && lFloat <= rFloat)
min = value;
} else if (value.IsNull) {
min = value;
if (min.TryGetValueAsFloat(out var minFloat) && minFloat >= 0)
min = value;
} else if (value.TryGetValueAsString(out var lString)) {
if (min.IsNull)
min = value;
else if (min.TryGetValueAsString(out var rString) && string.Compare(lString, rString, StringComparison.Ordinal) <= 0)
min = value;
} else {
throw new Exception($"Cannot compare {min} and {value}");
}
Expand Down

0 comments on commit 003f7d2

Please sign in to comment.