From 1c00338d2d8e95a53016273a9f4906c92c4ec985 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Tue, 11 Feb 2025 00:48:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=81=D1=82=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=D0=BA=D0=B8=D0=B9=20=D1=82=D0=B5=D1=81=D1=82=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=81=D0=B5=D0=BD=D1=81=D0=BE=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spbstu/telematics/java/SensorTests.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lab3/src/test/java/ru/spbstu/telematics/java/SensorTests.java diff --git a/lab3/src/test/java/ru/spbstu/telematics/java/SensorTests.java b/lab3/src/test/java/ru/spbstu/telematics/java/SensorTests.java new file mode 100644 index 0000000..ee65e45 --- /dev/null +++ b/lab3/src/test/java/ru/spbstu/telematics/java/SensorTests.java @@ -0,0 +1,54 @@ +package ru.spbstu.telematics.java; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + + +public class SensorTests { + /* + * Моковый класс комнаты для упрощения тестирования сенсоров. + */ + private class MockRoom extends Room { + double temperature; + double humidity; + + public MockRoom(double temperature, double humidity) { + this.temperature = temperature; + this.humidity = humidity; + } + + @Override + public double getTemperature() { + return temperature; + } + + @Override + public double getHumidity() { + return humidity; + } + } + + /* + * Проверяет, что сенсоры выдают реальную температуру и влажность комнаты в пределах + * некоторой погрешности. + */ + @Test + public void testSensor() throws InterruptedException { + double initialTemperature = 30.0; + double initialHumidity = 0.8; + Room room = new MockRoom(initialTemperature, initialHumidity); + + Sensor sensor = new Sensor(room); + Thread thread = new Thread(sensor); + thread.start(); + + for (int i = 0; i < 10; i++) { + Thread.sleep(1000); + assertTrue(Math.abs(initialTemperature - sensor.getTemperature()) <= 1); + assertTrue(Math.abs(initialHumidity - sensor.getHumidity()) <= 0.05); + } + + thread.interrupt(); + thread.join(); + } +}