CalibrationsTests.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Foundation
  2. import Swinject
  3. import Testing
  4. @testable import Trio
  5. @Suite("Calibration Service Tests", .serialized) 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. }