6ff8f34666af0f9d72d40cf40cee5c98ad090dc0 / bench/tasks/fix_easy/swift.py · 3992 bytes · raw
"""Easy-tier Swift fixes, moved verbatim from coding-suite (2026-10-12): two from its tier 1, then two
from its tier 2. Runs on macOS only (xcrun); elsewhere the rows are skipped (infra)."""
ITEMS = [
{
"id": "swift-parseage", "lang": "swift", "cat": "bugfix",
"symptom": "parseAge should return nil for non-numeric input, but it crashes (force-unwrap of nil) on input like \"abc\".",
"buggy": "func parseAge(_ s: String) -> Int? {\n return Int(s)!\n}\n",
"fix": "func parseAge(_ s: String) -> Int? {\n return Int(s)\n}\n",
"test": 'import Foundation\n\nfunc check(_ cond: Bool, _ msg: String) {\n if !cond { FileHandle.standardError.write(("FAIL: " + msg + "\\n").data(using: .utf8)!); exit(1) }\n}\n\ncheck(parseAge("34") == 34, "parseAge(34)")\ncheck(parseAge("abc") == nil, "parseAge(abc) should be nil")\ncheck(parseAge("0") == 0, "parseAge(0)")\nprint("OK")\n',
},
{
"id": "swift-median", "lang": "swift", "cat": "bugfix",
"symptom": "median returns the wrong value because it indexes the array without sorting it first (e.g. median([3,1,2]) returns 1 instead of 2).",
"buggy": "func median(_ xs: [Double]) -> Double {\n let n = xs.count\n if n % 2 == 1 {\n return xs[n / 2]\n } else {\n return (xs[n / 2 - 1] + xs[n / 2]) / 2\n }\n}\n",
"fix": "func median(_ xs: [Double]) -> Double {\n let s = xs.sorted()\n let n = s.count\n if n % 2 == 1 {\n return s[n / 2]\n } else {\n return (s[n / 2 - 1] + s[n / 2]) / 2\n }\n}\n",
"test": 'import Foundation\n\nfunc check(_ cond: Bool, _ msg: String) {\n if !cond { FileHandle.standardError.write(("FAIL: " + msg + "\\n").data(using: .utf8)!); exit(1) }\n}\n\ncheck(median([3, 1, 2]) == 2, "median odd")\ncheck(median([4, 1, 3, 2]) == 2.5, "median even")\ncheck(median([10, 2, 8, 4]) == 6, "median even2")\nprint("OK")\n',
},
{
"id": "swift-generic-max", "lang": "swift", "cat": "bugfix",
"symptom": "maxElement does not compile because the generic type T is not constrained. It should return the largest element, or nil for an empty array.",
"buggy": "func maxElement<T>(_ xs: [T]) -> T? {\n guard !xs.isEmpty else { return nil }\n var m = xs[0]\n for x in xs {\n if x > m { m = x }\n }\n return m\n}\n",
"fix": "func maxElement<T: Comparable>(_ xs: [T]) -> T? {\n guard !xs.isEmpty else { return nil }\n var m = xs[0]\n for x in xs {\n if x > m { m = x }\n }\n return m\n}\n",
"test": 'import Foundation\n\nfunc check(_ cond: Bool, _ msg: String) {\n if !cond { FileHandle.standardError.write(("FAIL: " + msg + "\\n").data(using: .utf8)!); exit(1) }\n}\n\ncheck(maxElement([3, 1, 2]) == 3, "max ints")\ncheck(maxElement([Int]()) == nil, "max empty")\ncheck(maxElement(["b", "a", "c"]) == "c", "max strings")\nprint("OK")\n',
},
{
"id": "swift-mutating", "lang": "swift", "cat": "bugfix",
"symptom": "This code does not compile: a struct method modifies a stored property, and the calling code prevents mutation. After fixing, runCounter(5) must return 5.",
"buggy": "struct Counter {\n var count = 0\n func increment() {\n count += 1\n }\n}\n\nfunc runCounter(_ times: Int) -> Int {\n let c = Counter()\n for _ in 0..<times { c.increment() }\n return c.count\n}\n",
"fix": "struct Counter {\n var count = 0\n mutating func increment() {\n count += 1\n }\n}\n\nfunc runCounter(_ times: Int) -> Int {\n var c = Counter()\n for _ in 0..<times { c.increment() }\n return c.count\n}\n",
"test": 'import Foundation\n\nfunc check(_ cond: Bool, _ msg: String) {\n if !cond { FileHandle.standardError.write(("FAIL: " + msg + "\\n").data(using: .utf8)!); exit(1) }\n}\n\ncheck(runCounter(5) == 5, "runCounter(5)")\ncheck(runCounter(0) == 0, "runCounter(0)")\nprint("OK")\n',
},
]