Skip to content

Commit

Permalink
max2AgesOver and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phisakel committed Oct 18, 2023
1 parent 34edd26 commit bc99b3e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Sources/MdocDataModel18013/MdocKnownDocTypes/AgeAttest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ extension AgeAttest {
{ return overFalse}
return nil
}

public func max2AgesOver(ages: [Int]) -> [Int:Bool] {
guard ages.count > 2 else { return Dictionary(grouping: ages, by: { $0 }).mapValues { _ in true } }
let sortedAges = ages.sorted()
var res = Dictionary(grouping: sortedAges, by: { $0 }).mapValues { _ in false }
var numAges = 0
for age in sortedAges {
if isOver(age: age) != nil, numAges < 2 {
numAges += 1
res[age] = true
}
}
return res
}

public func max2AgesOverFiltered(ages: [Int]) -> [Int] { Array(max2AgesOver(ages: ages).filter { $1 }.keys).sorted() }

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension MdocDecodable {
for (ns,items) in nameSpaces {
for item in items {
let name = item.elementIdentifier
if name.hasPrefix("age_over_") { continue }
if !bDebugDisplay && name.hasPrefix("age_over_") { continue }
var value = bDebugDisplay ? item.debugDescription : item.description
if name == "sex", let isex = Int(value), isex <= 2 {
value = NSLocalizedString(isex == 1 ? "male" : "female", comment: "")
Expand Down
30 changes: 30 additions & 0 deletions Tests/MdocDataModel18013Tests/MdocDataModel18013Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,34 @@ final class MdocDataModel18013Tests: XCTestCase {
let str = df.string(from: Date())
print(str)
}

struct SimpleAgeAttest : AgeAttest {
var ageOverXX = [Int : Bool]()
init(ageOver1: Int, isOver1: Bool, ageOver2: Int, isOver2: Bool) {
ageOverXX[ageOver1] = isOver1
ageOverXX[ageOver2] = isOver2
}
} // end struct

let ageAttestIs19 = SimpleAgeAttest(ageOver1: 21, isOver1: false, ageOver2: 60, isOver2: false)
let ageAttestIs21 = SimpleAgeAttest(ageOver1: 21, isOver1: true, ageOver2: 60, isOver2: false)
let ageAttestIs30 = SimpleAgeAttest(ageOver1: 21, isOver1: true, ageOver2: 60, isOver2: false)
let ageAttestIs60 = SimpleAgeAttest(ageOver1: 21, isOver1: true, ageOver2: 60, isOver2: true)
let ageAttestIs64 = SimpleAgeAttest(ageOver1: 21, isOver1: true, ageOver2: 60, isOver2: true)


// test for FDIS ISO 18013-5 Table D.1 — Situations for answers to age_over_nn requests mDL holder actual age 19, 21, 30, 60, 64
func testAgeAttest() throws {
let testAges = [18, 19, 20, 21, 25, 30, 50, 60, 63, 64, 65]
XCTAssertEqual(testAges.map { ageAttestIs19.isOver(age: $0)?.value }, [nil, nil, nil, false, false, false, false, false, false, false, false], "ageAttestIs19")
XCTAssertEqual(testAges.map { ageAttestIs21.isOver(age: $0)?.value }, [true, true, true, true, nil, nil, nil, false, false, false, false], "ageAttestIs21")
XCTAssertEqual(testAges.map { ageAttestIs30.isOver(age: $0)?.value }, [true, true, true, true, nil, nil, nil, false, false, false, false], "ageAttestIs30")
XCTAssertEqual(testAges.map { ageAttestIs60.isOver(age: $0)?.value }, [true, true, true, true, true, true, true, true, nil, nil, nil], "ageAttestIs60")
XCTAssertEqual(testAges.map { ageAttestIs64.isOver(age: $0)?.value }, [true, true, true, true, true, true, true, true, nil, nil, nil], "ageAttestIs64")
}

func testMax2AgesOver() throws {
let testAges = [18, 19, 20, 21, 25, 30, 50, 60, 63, 64, 65]
XCTAssertEqual(ageAttestIs19.max2AgesOverFiltered(ages: testAges), [21,25])
}
}

0 comments on commit bc99b3e

Please sign in to comment.