OSDN Git Service

swiftlintを元に戻した
[kcd/KCD.git] / KCDTests / SakutekiTest.swift
1 //
2 //  SakutekiTest.swift
3 //  KCDTests
4 //
5 //  Created by Hori,Masaki on 2017/12/24.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import XCTest
10
11 @testable import KCD
12
13 class SakutekiTest: XCTestCase {
14     
15     let store = ServerDataStore.oneTimeEditor()
16     
17     override func setUp() {
18         super.setUp()
19         
20         // 千歳航改二
21         // 天山一二型(村田隊), 流星(六○一空),彩雲(東カロリン空),零式艦戦53型(岩本隊)
22         // Bofors 40mm四連装機関砲
23         setupShip(id: 228, slotItems: [36_082, 28_114, 67_268, 17_930], exSlot: 51_387, saku0: 108)
24         
25         // 熊野改二
26         // 紫雲, 零式水上偵察機11型乙, 強風改, 二式水戦改
27         // なし
28         setupShip(id: 4_184, slotItems: [65_505, 72_413, 70_952, 69_442], exSlot: 0, saku0: 110)
29         
30         // 筑摩改二
31         // 二式水戦改, 32号対水上電探+8, FuMO25レーダー, 零式水上観測機+6
32         // なし
33         setupShip(id: 220, slotItems: [47_272, 26_065, 61_168, 13_621], exSlot: 0, saku0: 130)
34         
35         
36         let tempStore = TemporaryDataStore.oneTimeEditor()
37         tempStore.guardEscaped().forEach(tempStore.delete)
38
39     }
40     
41     override func tearDown() {
42         
43         let store = TemporaryDataStore.oneTimeEditor()
44         store.guardEscaped().forEach(store.delete)
45         super.tearDown()
46     }
47     
48     func setupShip(id shipId: Int, slotItems: [Int], exSlot: Int, saku0: Int) {
49         
50         guard let ship = store.ship(by: shipId) else {
51             XCTFail("Can not get ship, \(shipId)")
52             fatalError()
53         }
54         ship.onslot_0 = ship.master_ship.maxeq_0
55         ship.onslot_1 = ship.master_ship.maxeq_1
56         ship.onslot_2 = ship.master_ship.maxeq_2
57         ship.onslot_3 = ship.master_ship.maxeq_3
58         ship.onslot_4 = ship.master_ship.maxeq_4
59         
60         setSlot(slotItems: slotItems, to: ship)
61         
62         ship.slot_ex = exSlot
63         ship.extraItem = store.slotItem(by: ship.slot_ex)
64         
65         ship.sakuteki_0 = saku0
66         
67     }
68     
69     func setSlot(slotItems: [Int], to ship: Ship) {
70     
71         let newItems: [SlotItem] = slotItems
72             .filter { $0 != 0 && $0 != -1 }
73             .flatMap { store.slotItem(by: $0) }
74         ship.equippedItem = NSOrderedSet(array: newItems)
75         
76         slotItems.enumerated().forEach { offset, element in
77             ship.setItem(element, to: offset)
78         }
79     }
80     
81     // 小数点第2位以下切り捨て
82     func fitSakuteki(_ sakuteki: Double) -> Double {
83         
84         return Double(Int(sakuteki * 10)) / 10
85     }
86
87     func testOneShip() {
88         
89         // 千歳航改二
90         guard let ship = store.ship(by: 228) else {
91             XCTFail("Can not get ship")
92             fatalError()
93         }
94         
95         XCTAssertEqual(SimpleCalculator().calculate([ship]), 108)
96         
97         XCTAssertEqual(fitSakuteki(Formula33(1).calculate([ship])), -11.2)
98         XCTAssertEqual(fitSakuteki(Formula33(3).calculate([ship])), 127.9)
99         XCTAssertEqual(fitSakuteki(Formula33(4).calculate([ship])), 249.7)
100     }
101     
102     func testOne2Ship() {
103         
104         // 熊野改二
105         guard let ship = store.ship(by: 4_184) else {
106             XCTFail("Can not get ship")
107             fatalError()
108         }
109         
110         XCTAssertEqual(SimpleCalculator().calculate([ship]), 110)
111         
112         XCTAssertEqual(fitSakuteki(Formula33(1).calculate([ship])), -10.3)
113         XCTAssertEqual(fitSakuteki(Formula33(3).calculate([ship])), 133.6)
114         XCTAssertEqual(fitSakuteki(Formula33(4).calculate([ship])), 259.6)
115     }
116     
117     func testTwoShips() {
118         
119         // 千歳航改二
120         guard let ship1 = store.ship(by: 228) else {
121             XCTFail("Can not get ship")
122             fatalError()
123         }
124         // 熊野改二
125         guard let ship2 = store.ship(by: 4_184) else {
126             XCTFail("Can not get ship")
127             fatalError()
128         }
129         
130         XCTAssertEqual(SimpleCalculator().calculate([ship1, ship2]), 218)
131         
132         XCTAssertEqual(fitSakuteki(Formula33(1).calculate([ship1, ship2])), 14.4)
133         XCTAssertEqual(fitSakuteki(Formula33(3).calculate([ship1, ship2])), 297.6)
134         XCTAssertEqual(fitSakuteki(Formula33(4).calculate([ship1, ship2])), 545.4)
135     }
136     
137     func testTreeShips() {
138         
139         // 千歳航改二
140         guard let ship1 = store.ship(by: 228) else {
141             XCTFail("Can not get ship")
142             fatalError()
143         }
144         // 熊野改二
145         guard let ship2 = store.ship(by: 4_184) else {
146             XCTFail("Can not get ship")
147             fatalError()
148         }
149         // 筑摩改二
150         guard let ship3 = store.ship(by: 220) else {
151             XCTFail("Can not get ship")
152             fatalError()
153         }
154         
155         XCTAssertEqual(SimpleCalculator().calculate([ship1, ship2, ship3]), 348)
156         
157         XCTAssertEqual(fitSakuteki(Formula33(1).calculate([ship1, ship2, ship3])), 47.7)
158         XCTAssertEqual(fitSakuteki(Formula33(3).calculate([ship1, ship2, ship3])), 531.8)
159         XCTAssertEqual(fitSakuteki(Formula33(4).calculate([ship1, ship2, ship3])), 955.3)
160     }
161     
162     func testEscapedShips() {
163         
164         // 千歳航改二
165         guard let ship1 = store.ship(by: 228) else {
166             XCTFail("Can not get ship")
167             fatalError()
168         }
169         // 熊野改二
170         guard let ship2 = store.ship(by: 4_184) else {
171             XCTFail("Can not get ship")
172             fatalError()
173         }
174         // 筑摩改二
175         guard let ship3 = store.ship(by: 220) else {
176             XCTFail("Can not get ship")
177             fatalError()
178         }
179         
180         // 熊野改二を退避
181         do {
182             let tempStore = TemporaryDataStore.oneTimeEditor()
183             let guardEscape = tempStore.createGuardEscaped()
184             guardEscape?.shipID = 4_184
185             guardEscape?.ensured = true
186         }
187         
188         XCTAssertEqual(SimpleCalculator().calculate([ship1, ship2, ship3]), 238)
189         
190         XCTAssertEqual(fitSakuteki(Formula33(1).calculate([ship1, ship2, ship3])), 22.0)
191         XCTAssertEqual(fitSakuteki(Formula33(3).calculate([ship1, ship2, ship3])), 362.1)
192         XCTAssertEqual(fitSakuteki(Formula33(4).calculate([ship1, ship2, ship3])), 659.6)
193     }
194 }