CalibrationsTests.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import Foundation
  2. import Swinject
  3. import Testing
  4. @testable import Trio
  5. @Suite("Calibration Service Tests") struct CalibrationTests: Injectable {
  6. let fileStorage = BaseFileStorage()
  7. @Injected() var calibrationService: CalibrationService!
  8. let resolver = TrioApp().resolver
  9. init() {
  10. injectServices(resolver)
  11. }
  12. @Test("Can create simple calibration") func testCreateSimpleCalibration() {
  13. // Given
  14. calibrationService.removeAllCalibrations()
  15. let calibration = Calibration(x: 100.0, y: 102.0)
  16. // When
  17. calibrationService.addCalibration(calibration)
  18. // Then
  19. #expect(calibrationService.calibrations.isNotEmpty)
  20. #expect(calibrationService.slope == 1)
  21. #expect(calibrationService.intercept == 2)
  22. #expect(calibrationService.calibrate(value: 104) == 106)
  23. }
  24. @Test("Can handle multiple calibrations") func testCreateMultipleCalibration() {
  25. // Given
  26. calibrationService.removeAllCalibrations()
  27. let calibration = Calibration(x: 100.0, y: 120)
  28. let calibration2 = Calibration(x: 120.0, y: 130.0)
  29. // When
  30. calibrationService.addCalibration(calibration)
  31. calibrationService.addCalibration(calibration2)
  32. // Then
  33. #expect(abs(calibrationService.slope - 0.8) < 0.0001)
  34. #expect(abs(calibrationService.intercept - 37) < 0.0001)
  35. #expect(abs(calibrationService.calibrate(value: 80) - 101) < 0.0001)
  36. // When removing last
  37. calibrationService.removeLast()
  38. #expect(calibrationService.calibrations.count == 1)
  39. // When removing all
  40. calibrationService.removeAllCalibrations()
  41. #expect(calibrationService.calibrations.isEmpty)
  42. }
  43. @Test("Handles calibration bounds correctly") func testCalibrationBounds() {
  44. // Given
  45. calibrationService.removeAllCalibrations()
  46. // When no calibrations exist
  47. #expect(calibrationService.slope == 1, "Default slope should be 1")
  48. #expect(calibrationService.intercept == 0, "Default intercept should be 0")
  49. // When adding extreme values
  50. let extremeCalibration1 = Calibration(x: 0.0, y: 1000.0) // Should be clamped
  51. let extremeCalibration2 = Calibration(x: 1000.0, y: 0.0) // Should be clamped
  52. calibrationService.addCalibration(extremeCalibration1)
  53. calibrationService.addCalibration(extremeCalibration2)
  54. // Then check bounds
  55. #expect(calibrationService.slope >= 0.8, "Slope should not be less than minimum")
  56. #expect(calibrationService.slope <= 1.25, "Slope should not be more than maximum")
  57. #expect(calibrationService.intercept >= -100, "Intercept should not be less than minimum")
  58. #expect(calibrationService.intercept <= 100, "Intercept should not be more than maximum")
  59. }
  60. }
  61. // import Swinject
  62. // @testable import Trio
  63. // import XCTest
  64. //
  65. // class CalibrationsTests: XCTestCase, Injectable {
  66. // let fileStorage = BaseFileStorage()
  67. // @Injected() var calibrationService: CalibrationService!
  68. // let resolver = TrioApp().resolver
  69. //
  70. // override func setUp() {
  71. // injectServices(resolver)
  72. // }
  73. //
  74. // func testCreateSimpleCalibration() {
  75. // // restore state so each test is independent
  76. // calibrationService.removeAllCalibrations()
  77. //
  78. // let calibration = Calibration(x: 100.0, y: 102.0)
  79. // calibrationService.addCalibration(calibration)
  80. //
  81. // XCTAssertTrue(calibrationService.calibrations.isNotEmpty)
  82. //
  83. // XCTAssertTrue(calibrationService.slope == 1)
  84. //
  85. // XCTAssertTrue(calibrationService.intercept == 2)
  86. //
  87. // XCTAssertTrue(calibrationService.calibrate(value: 104) == 106)
  88. // }
  89. //
  90. // func testCreateMultipleCalibration() {
  91. // // restore state so each test is independent
  92. // calibrationService.removeAllCalibrations()
  93. //
  94. // let calibration = Calibration(x: 100.0, y: 120)
  95. // calibrationService.addCalibration(calibration)
  96. //
  97. // let calibration2 = Calibration(x: 120.0, y: 130.0)
  98. // calibrationService.addCalibration(calibration2)
  99. //
  100. // XCTAssertEqual(calibrationService.slope, 0.8, accuracy: 0.0001)
  101. // XCTAssertEqual(calibrationService.intercept, 37, accuracy: 0.0001)
  102. // XCTAssertEqual(calibrationService.calibrate(value: 80), 101, accuracy: 0.0001)
  103. //
  104. // calibrationService.removeLast()
  105. //
  106. // XCTAssertTrue(calibrationService.calibrations.count == 1)
  107. //
  108. // calibrationService.removeAllCalibrations()
  109. // XCTAssertTrue(calibrationService.calibrations.isEmpty)
  110. // }
  111. //
  112. // override func setUpWithError() throws {
  113. // // Put setup code here. This method is called before the invocation of each test method in the class.
  114. // }
  115. //
  116. // override func tearDownWithError() throws {
  117. // // Put teardown code here. This method is called after the invocation of each test method in the class.
  118. // }
  119. // }