threadsafe MathFont, MTFontV2, MTFontMathTableV2 with concurrent testScripts.

This commit is contained in:
Peter Tang
2023-09-18 13:30:31 +08:00
parent be802ae0c6
commit 5de5ea677e
6 changed files with 293 additions and 69 deletions

View File

@@ -1,6 +1,6 @@
//
// MTFontMathTableV2Tests.swift
//
//
//
// Created by Peter Tang on 15/9/2023.
//
@@ -25,4 +25,57 @@ final class MTFontMathTableV2Tests: XCTestCase {
print("\($0.rawValue).plist: \(values)")
}
}
private let executionQueue = DispatchQueue(label: "com.swiftmath.mathbundle", attributes: .concurrent)
private let executionGroup = DispatchGroup()
let totalCases = 1000
var testCount = 0
func testConcurrentThreadsafeScript() throws {
testCount = 0
var mathFont: MathFont { .allCases.randomElement()! }
var size: CGFloat { CGFloat.random(in: 20 ... 40) }
let mtfonts = Array( 0 ..< 10 ).map { _ in mathFont.mtfont(size: size) }
for caseNumber in 0 ..< totalCases {
helperConcurrentMTFontMathTableV2(caseNumber, mtfont: mtfonts.randomElement()!, in: executionGroup, on: executionQueue)
}
executionGroup.notify(queue: .main) { [weak self] in
guard let self = self else { return }
XCTAssertEqual(self.testCount, totalCases)
print("\(self.testCount) completed =================")
}
}
func helperConcurrentMTFontMathTableV2(_ count: Int, mtfont: MTFontV2, in group: DispatchGroup, on queue: DispatchQueue) {
let workitem = DispatchWorkItem {
let mTable = mtfont.mathTable
let values = [
mTable?.fractionNumeratorDisplayStyleShiftUp,
mTable?.fractionNumeratorShiftUp,
mTable?.fractionDenominatorDisplayStyleShiftDown,
mTable?.fractionDenominatorShiftDown,
mTable?.fractionNumeratorDisplayStyleGapMin,
mTable?.fractionNumeratorGapMin,
].compactMap{$0}
if count % 50 == 0 {
print(values) // accessed these values on global thread.
}
XCTAssertNotNil(mTable)
}
workitem.notify(queue: .main) { [weak self] in
// print("\(Thread.isMainThread ? "main" : "global") completed .....")
let mTable = mtfont.mathTable
if count % 70 == 0 {
let values = [
mTable?.fractionNumeratorDisplayStyleShiftUp,
mTable?.fractionNumeratorShiftUp,
mTable?.fractionDenominatorDisplayStyleShiftDown,
mTable?.fractionDenominatorShiftDown,
mTable?.fractionNumeratorDisplayStyleGapMin,
mTable?.fractionNumeratorGapMin,
].compactMap{$0}
print(values) // accessed these values on main thread.
}
self?.testCount += 1
}
queue.async(group: group, execute: workitem)
}
}

View File

@@ -1,6 +1,6 @@
//
// MTFontV2Tests.swift
//
//
//
// Created by Peter Tang on 15/9/2023.
//
@@ -18,4 +18,69 @@ final class MTFontV2Tests: XCTestCase {
XCTAssertNotNil(mTable)
}
}
private let executionQueue = DispatchQueue(label: "com.swiftmath.mathbundle", attributes: .concurrent)
private let executionGroup = DispatchGroup()
let totalCases = 1000
var testCount = 0
func testConcurrentThreadsafeScript() throws {
testCount = 0
var mathFont: MathFont { .allCases.randomElement()! }
for caseNumber in 0 ..< totalCases {
helperConcurrentMTFontV2(caseNumber, mathFont: mathFont, in: executionGroup, on: executionQueue)
}
executionGroup.notify(queue: .main) { [weak self] in
guard let self = self else { return }
XCTAssertEqual(self.testCount, totalCases)
print("\(self.testCount) completed =================")
}
}
func helperConcurrentMTFontV2(_ count: Int, mathFont: MathFont, in group: DispatchGroup, on queue: DispatchQueue) {
let size = CGFloat.random(in: 20 ... 40)
let workitem = DispatchWorkItem {
let fontV2 = mathFont.mtfont(size: size)
XCTAssertNotNil(fontV2)
let (cgfont, ctfont) = (fontV2.defaultCGFont, fontV2.ctFont)
XCTAssertNotNil(cgfont)
XCTAssertNotNil(ctfont)
}
workitem.notify(queue: .main) { [weak self] in
// print("\(Thread.isMainThread ? "main" : "global") completed .....")
let fontV2 = mathFont.mtfont(size: size)
XCTAssertNotNil(fontV2)
let (cgfont, ctfont) = (fontV2.defaultCGFont, fontV2.ctFont)
XCTAssertNotNil(cgfont)
XCTAssertNotNil(ctfont)
let mTable = mathFont.rawMathTable()
XCTAssertNotNil(mTable)
self?.testCount += 1
}
queue.async(group: group, execute: workitem)
}
func testConcurrentThreadsafeMathTableLockScript() throws {
testCount = 0
var mathFont: MathFont { .allCases.randomElement()! }
var size: CGFloat { CGFloat.random(in: 20 ... 40) }
let mtfonts = Array( 0 ..< 5 ).map { _ in mathFont.mtfont(size: size) }
for caseNumber in 0 ..< totalCases {
helperConcurrentMTFontV2MathTableLock(caseNumber, mtfont: mtfonts.randomElement()!, in: executionGroup, on: executionQueue)
}
executionGroup.notify(queue: .main) { [weak self] in
guard let self = self else { return }
XCTAssertEqual(self.testCount, totalCases)
print("\(self.testCount) completed =================")
}
}
func helperConcurrentMTFontV2MathTableLock(_ count: Int, mtfont: MTFontV2, in group: DispatchGroup, on queue: DispatchQueue) {
let workitem = DispatchWorkItem {
let mathTable = mtfont.mathTable as? MTFontMathTableV2
// each mathTable is initialized once per mtfont with a NSLock.
// this is even when mathTable is accessed via different threads.
XCTAssertNotNil(mathTable)
}
workitem.notify(queue: .main) { [weak self] in
// print("\(Thread.isMainThread ? "main" : "global") completed .....")
self?.testCount += 1
}
queue.async(group: group, execute: workitem)
}
}

View File

@@ -3,7 +3,7 @@ import XCTest
//
// MathFontTests.swift
//
//
//
// Created by Peter Tang on 12/9/2023.
//
@@ -16,7 +16,10 @@ final class MathFontTests: XCTestCase {
// print("\(#function) ctfont \($0.ctFont(withSize: CGFloat(size)))")
XCTAssertNotNil($0.cgFont())
XCTAssertNotNil($0.ctFont(withSize: CGFloat(size)))
XCTAssertEqual($0.ctFont(withSize: CGFloat(size)).fontSize, CGFloat(size), "ctFont fontSize test")
XCTAssertEqual($0.ctFont(withSize: CGFloat(size)).fontSize, CGFloat(size), "ctFont fontSize != size.")
XCTAssertEqual($0.cgFont().postScriptName as? String, $0.fontName, "postscript Name != UIFont fontName")
// XCTAssertEqual($0.uiFont(withSize: CGFloat(size))?.familyName, $0.fontFamilyName, "uifont familyName != familyName.")
XCTAssertEqual(CTFontCopyFamilyName($0.ctFont(withSize: CGFloat(size))) as String, $0.fontFamilyName, "ctfont.family != familyName")
}
#if os(iOS)
// for family in UIFont.familyNames.sorted() {
@@ -50,4 +53,95 @@ final class MathFontTests: XCTestCase {
var fontFamilyNames: [String] {
MathFont.allCases.map { $0.fontFamilyName }
}
private let executionQueue = DispatchQueue(label: "com.swiftmath.mathbundle", attributes: .concurrent)
private let executionGroup = DispatchGroup()
let totalCases = 5000
var testCount = 0
func testConcurrentThreadsafeScript() throws {
var mathFont: MathFont { .allCases.randomElement()! }
for caseNumber in 0 ..< totalCases {
switch caseNumber % 3 {
case 0:
helperConcurrentCGFont(caseNumber, mathFont: mathFont, in: executionGroup, on: executionQueue)
case 1:
helperConcurrentCTFont(caseNumber, mathFont: mathFont, in: executionGroup, on: executionQueue)
case 2:
helperConcurrentMathTable(caseNumber, mathFont: mathFont, in: executionGroup, on: executionQueue)
default:
continue
}
}
executionGroup.notify(queue: .main) { [weak self] in
guard let self = self else { return }
XCTAssertEqual(self.testCount, totalCases)
print("\(self.testCount) completed =================")
}
}
// func helperConcurrentOnDemandRegistration(_ count: Int, mathFont: MathFont, in group: DispatchGroup, on queue: DispatchQueue) {
// let workitem = DispatchWorkItem {
// BundleManager.manager.onDemandRegistration(mathFont: mathFont)
// }
// workitem.notify(queue: .main) { [weak self] in
// self?.testCount += 1
// }
// queue.async(group: group, execute: workitem)
// }
// func helperConcurrentBundleRegistration(mathFont: MathFont, in group: DispatchGroup, on queue: DispatchQueue) {
// let workitem = DispatchWorkItem {
// // BundleManager.manager.onDemandRegistration(mathFont: mathFont)
// try? BundleManager.manager.registerCGFont(mathFont: mathFont)
// try? BundleManager.manager.registerMathTable(mathFont: mathFont)
// let font = BundleManager.manager.cgFonts[mathFont]
// XCTAssertNotNil(font, "font != nil")
// }
// workitem.notify(queue: .main) { [weak self] in
// // print("\(Thread.isMainThread ? "main" : "global") completed .....")
// let font = mathFont.cgFont()
// XCTAssertNotNil(font, "font != nil")
// self?.testCount += 1
// }
// queue.async(group: group, execute: workitem)
// }
func helperConcurrentCGFont(_ count: Int, mathFont: MathFont, in group: DispatchGroup, on queue: DispatchQueue) {
let workitem = DispatchWorkItem {
let font = mathFont.cgFont()
XCTAssertNotNil(font, "font != nil")
}
workitem.notify(queue: .main) { [weak self] in
// print("\(Thread.isMainThread ? "main" : "global") completed .....")
let font = mathFont.cgFont()
XCTAssertNotNil(font, "font != nil")
self?.testCount += 1
}
queue.async(group: group, execute: workitem)
}
func helperConcurrentCTFont(_ count: Int, mathFont: MathFont, in group: DispatchGroup, on queue: DispatchQueue) {
let size = CGFloat.random(in: 20 ... 40)
let workitem = DispatchWorkItem {
let font = mathFont.ctFont(withSize: size)
XCTAssertNotNil(font, "font != nil")
}
workitem.notify(queue: .main) { [weak self] in
// print("\(Thread.isMainThread ? "main" : "global") completed .....")
let font = mathFont.ctFont(withSize: size)
XCTAssertNotNil(font, "font != nil")
self?.testCount += 1
}
queue.async(group: group, execute: workitem)
}
func helperConcurrentMathTable(_ count: Int, mathFont: MathFont, in group: DispatchGroup, on queue: DispatchQueue) {
let workitem = DispatchWorkItem {
let mathtable = mathFont.rawMathTable()
XCTAssertNotNil(mathtable, "mathTable != nil")
}
workitem.notify(queue: .main) { [weak self] in
// print("\(Thread.isMainThread ? "main" : "global") completed .....")
let mathtable = mathFont.rawMathTable()
XCTAssertNotNil(mathtable, "mathTable != nil")
self?.testCount += 1
}
queue.async(group: group, execute: workitem)
}
}