mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-08-22 23:15:25 +00:00
fix: update tests
This commit is contained in:
@ -139,7 +139,7 @@ public final class DicdataStore {
|
|||||||
} else {
|
} else {
|
||||||
self.requestOptions = value
|
self.requestOptions = value
|
||||||
}
|
}
|
||||||
let shouldReset = self.learningManager.setRequestOptions(options: value)
|
let shouldReset = self.learningManager.setRequestOptions(value)
|
||||||
if shouldReset {
|
if shouldReset {
|
||||||
self.reloadMemory()
|
self.reloadMemory()
|
||||||
}
|
}
|
||||||
|
@ -653,47 +653,49 @@ final class LearningManager {
|
|||||||
(!self.memoryCollapsed) && self.options.learningType.needUsingMemory
|
(!self.memoryCollapsed) && self.options.learningType.needUsingMemory
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {}
|
||||||
self.memoryCollapsed = LongTermLearningMemory.memoryCollapsed(directoryURL: self.options.memoryDirectoryURL)
|
|
||||||
if self.memoryCollapsed && options.learningType.needUsingMemory {
|
/// - Returns: Whether cache should be reseted or not.
|
||||||
|
func setRequestOptions(_ newOptions: ConvertRequestOptions) -> Bool {
|
||||||
|
// 更新の必要がなければ何もしない
|
||||||
|
if !newOptions.learningType.needUsingMemory {
|
||||||
|
self.options = newOptions
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// 学習の壊れ状態を確認
|
||||||
|
self.memoryCollapsed = LongTermLearningMemory.memoryCollapsed(directoryURL: newOptions.memoryDirectoryURL)
|
||||||
|
if self.memoryCollapsed && newOptions.learningType.needUsingMemory {
|
||||||
do {
|
do {
|
||||||
try LongTermLearningMemory.merge(
|
try LongTermLearningMemory.merge(
|
||||||
tempTrie: TemporalLearningMemoryTrie(),
|
tempTrie: TemporalLearningMemoryTrie(),
|
||||||
directoryURL: self.options.memoryDirectoryURL,
|
directoryURL: newOptions.memoryDirectoryURL,
|
||||||
maxMemoryCount: options.maxMemoryCount,
|
maxMemoryCount: newOptions.maxMemoryCount,
|
||||||
char2UInt8: char2UInt8
|
char2UInt8: char2UInt8
|
||||||
)
|
)
|
||||||
} catch {
|
} catch {
|
||||||
debug("LearningManager init: automatic merge failed", error)
|
debug(#file, #function, "automatic merge failed", error)
|
||||||
}
|
}
|
||||||
self.memoryCollapsed = LongTermLearningMemory.memoryCollapsed(directoryURL: self.options.memoryDirectoryURL)
|
self.memoryCollapsed = LongTermLearningMemory.memoryCollapsed(directoryURL: newOptions.memoryDirectoryURL)
|
||||||
}
|
}
|
||||||
if memoryCollapsed {
|
if self.memoryCollapsed {
|
||||||
// 学習データが壊れている状態であることを警告する
|
// 学習データが壊れている状態であることを警告する
|
||||||
debug("LearningManager init: Memory Collapsed")
|
debug(#file, #function, "LearningManager init: Memory Collapsed")
|
||||||
}
|
}
|
||||||
if !options.learningType.needUsingMemory {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Self.updateChar2Int8(bundleURL: options.dictionaryResourceURL, target: &char2UInt8)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// - Returns: Whether cache should be reseted or not.
|
|
||||||
func setRequestOptions(options: ConvertRequestOptions) -> Bool {
|
|
||||||
// 変更があったら`char2Int8`を読み込み直す
|
// 変更があったら`char2Int8`を読み込み直す
|
||||||
if options.dictionaryResourceURL != self.options.dictionaryResourceURL {
|
if newOptions.dictionaryResourceURL != self.options.dictionaryResourceURL {
|
||||||
Self.updateChar2Int8(bundleURL: options.dictionaryResourceURL, target: &char2UInt8)
|
Self.updateChar2Int8(bundleURL: newOptions.dictionaryResourceURL, target: &char2UInt8)
|
||||||
}
|
}
|
||||||
self.options = options
|
// ここで更新
|
||||||
|
self.options = newOptions
|
||||||
|
|
||||||
switch options.learningType {
|
switch self.options.learningType {
|
||||||
case .inputAndOutput, .onlyOutput: break
|
case .inputAndOutput, .onlyOutput: break
|
||||||
case .nothing:
|
case .nothing:
|
||||||
self.temporaryMemory = TemporalLearningMemoryTrie()
|
self.temporaryMemory = TemporalLearningMemoryTrie()
|
||||||
}
|
}
|
||||||
|
|
||||||
// リセットチェックも実施
|
// リセットチェックも実施
|
||||||
if options.shouldResetMemory {
|
if self.options.shouldResetMemory {
|
||||||
self.reset()
|
self.reset()
|
||||||
self.options.shouldResetMemory = false
|
self.options.shouldResetMemory = false
|
||||||
return true
|
return true
|
||||||
|
@ -11,15 +11,36 @@ import XCTest
|
|||||||
final class LearningMemoryTests: XCTestCase {
|
final class LearningMemoryTests: XCTestCase {
|
||||||
static let resourceURL = Bundle.module.resourceURL!.appendingPathComponent("DictionaryMock", isDirectory: true)
|
static let resourceURL = Bundle.module.resourceURL!.appendingPathComponent("DictionaryMock", isDirectory: true)
|
||||||
|
|
||||||
|
private func getOptionsForMemoryTest(memoryDirectoryURL: URL) -> ConvertRequestOptions {
|
||||||
|
var options = ConvertRequestOptions.default
|
||||||
|
options.memoryDirectoryURL = memoryDirectoryURL
|
||||||
|
options.dictionaryResourceURL = Self.resourceURL
|
||||||
|
options.learningType = .inputAndOutput
|
||||||
|
options.maxMemoryCount = 32
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
func testPauseFileIsClearedOnInit() throws {
|
func testPauseFileIsClearedOnInit() throws {
|
||||||
let dir = ConvertRequestOptions.default.memoryDirectoryURL
|
let dir = FileManager.default.temporaryDirectory.appendingPathComponent("LearningMemoryTest-\(UUID().uuidString)", isDirectory: true)
|
||||||
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||||||
|
defer { try? FileManager.default.removeItem(at: dir) }
|
||||||
|
|
||||||
|
let options = self.getOptionsForMemoryTest(memoryDirectoryURL: dir)
|
||||||
|
let manager = LearningManager()
|
||||||
|
_ = manager.setRequestOptions(options)
|
||||||
|
|
||||||
|
let element = DicdataElement(word: "テスト", ruby: "テスト", cid: CIDData.一般名詞.cid, mid: MIDData.一般.mid, value: -10)
|
||||||
|
manager.update(data: [element])
|
||||||
|
manager.save()
|
||||||
|
|
||||||
|
// ポーズファイルを設置
|
||||||
let pauseURL = dir.appendingPathComponent(".pause", isDirectory: false)
|
let pauseURL = dir.appendingPathComponent(".pause", isDirectory: false)
|
||||||
FileManager.default.createFile(atPath: pauseURL.path, contents: Data())
|
FileManager.default.createFile(atPath: pauseURL.path, contents: Data())
|
||||||
XCTAssertTrue(LongTermLearningMemory.memoryCollapsed(directoryURL: dir))
|
XCTAssertTrue(LongTermLearningMemory.memoryCollapsed(directoryURL: dir))
|
||||||
|
|
||||||
// `init`に副作用がある
|
// ここで副作用が発生
|
||||||
_ = LearningManager()
|
_ = manager.setRequestOptions(options)
|
||||||
|
|
||||||
// 学習の破壊状態が回復されていることを確認
|
// 学習の破壊状態が回復されていることを確認
|
||||||
XCTAssertFalse(LongTermLearningMemory.memoryCollapsed(directoryURL: dir))
|
XCTAssertFalse(LongTermLearningMemory.memoryCollapsed(directoryURL: dir))
|
||||||
try? FileManager.default.removeItem(at: pauseURL)
|
try? FileManager.default.removeItem(at: pauseURL)
|
||||||
@ -30,13 +51,9 @@ final class LearningMemoryTests: XCTestCase {
|
|||||||
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
||||||
defer { try? FileManager.default.removeItem(at: dir) }
|
defer { try? FileManager.default.removeItem(at: dir) }
|
||||||
|
|
||||||
|
let options = self.getOptionsForMemoryTest(memoryDirectoryURL: dir)
|
||||||
let manager = LearningManager()
|
let manager = LearningManager()
|
||||||
var options = ConvertRequestOptions.default
|
_ = manager.setRequestOptions(options)
|
||||||
options.dictionaryResourceURL = Self.resourceURL
|
|
||||||
options.memoryDirectoryURL = dir
|
|
||||||
options.learningType = .inputAndOutput
|
|
||||||
options.maxMemoryCount = 32
|
|
||||||
_ = manager.setRequestOptions(options: options)
|
|
||||||
|
|
||||||
let element = DicdataElement(word: "テスト", ruby: "テスト", cid: CIDData.一般名詞.cid, mid: MIDData.一般.mid, value: -10)
|
let element = DicdataElement(word: "テスト", ruby: "テスト", cid: CIDData.一般名詞.cid, mid: MIDData.一般.mid, value: -10)
|
||||||
manager.update(data: [element])
|
manager.update(data: [element])
|
||||||
|
Reference in New Issue
Block a user