diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b13f319 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +target/ +*.class +.idea/ +*.iml +.vscode/ +.DS_Store diff --git a/.mvn/jvm.config b/.mvn/jvm.config new file mode 100644 index 0000000..e69de29 diff --git a/.mvn/maven.config b/.mvn/maven.config new file mode 100644 index 0000000..e69de29 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..568add7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + com.mycompany.app + my-app + 1.0-SNAPSHOT + jar + + + 17 + 17 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter + 5.10.2 + test + + + + + + + maven-surefire-plugin + 3.3.0 + + 1 + true + + + + + org.jacoco + jacoco-maven-plugin + 0.8.11 + + + + prepare-agent + + + + report + prepare-package + + report + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/mycompany/app/App.java b/src/main/java/com/mycompany/app/App.java new file mode 100644 index 0000000..f67b506 --- /dev/null +++ b/src/main/java/com/mycompany/app/App.java @@ -0,0 +1,12 @@ +package com.mycompany.app; + +public class App +{ + public static void main(String[] args) + { + double val = Double.parseDouble("2.0"); + Sqrt sqrt = new Sqrt(val); + double result = sqrt.calc(); + System.out.println("Sqrt of " + val + " = " + result); + } +} \ No newline at end of file diff --git a/src/main/java/com/mycompany/app/Sqrt.java b/src/main/java/com/mycompany/app/Sqrt.java new file mode 100644 index 0000000..42c9eec --- /dev/null +++ b/src/main/java/com/mycompany/app/Sqrt.java @@ -0,0 +1,34 @@ +package com.mycompany.app; + +public class Sqrt +{ + double delta = 0.00000001; + double arg; + + public Sqrt(double arg) { + this.arg = arg; + } + + public double average(double x, double y) { + return (x + y) / 2.0; + } + + public boolean good(double guess, double x) { + return Math.abs(guess * guess - x) < delta; + } + + public double improve(double guess, double x) { + return average(guess, x / guess); + } + + public double iter(double guess, double x) { + if (good(guess, x)) + return guess; + else + return iter(improve(guess, x), x); + } + + public double calc() { + return iter(1.0, arg); + } +} \ No newline at end of file diff --git a/src/test/java/com/mycompany/app/SqrtTest.java b/src/test/java/com/mycompany/app/SqrtTest.java new file mode 100644 index 0000000..f1c7fb6 --- /dev/null +++ b/src/test/java/com/mycompany/app/SqrtTest.java @@ -0,0 +1,132 @@ +package com.mycompany.app; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; +import static org.junit.jupiter.api.Assertions.*; + +public class SqrtTest { + + private Sqrt sqrt4; + private Sqrt sqrt9; + private Sqrt sqrt2; + + @BeforeEach + public void setUp() { + sqrt4 = new Sqrt(4.0); + sqrt9 = new Sqrt(9.0); + sqrt2 = new Sqrt(2.0); + } + + /// --- Тесты метода calc() --- + + @Test + public void testCalcPerfectSquare4() { + assertEquals(2.0, sqrt4.calc(), 1e-6, + "Квадратный корень из 4 должен быть 2.0"); + } + + @Test + public void testCalcPerfectSquare9() { + assertEquals(3.0, sqrt9.calc(), 1e-6, + "Квадратный корень из 9 должен быть 3.0"); + } + + @Test + public void testCalcPerfectSquare1() { + Sqrt sqrt1 = new Sqrt(1.0); + assertEquals(1.0, sqrt1.calc(), 1e-6, + "Квадратный корень из 1 должен быть 1.0"); + } + + @Test + public void testCalcIrrational() { + assertEquals(Math.sqrt(2.0), sqrt2.calc(), 1e-6, + "Квадратный корень из 2 должен совпадать с Math.sqrt(2)"); + } + + @Test + public void testCalcLargeNumber() { + Sqrt sqrtLarge = new Sqrt(1000000.0); + assertEquals(1000.0, sqrtLarge.calc(), 1e-4, + "Квадратный корень из 1000000 должен быть 1000.0"); + } + + @Test + public void testCalcSmallNumber() { + Sqrt sqrtSmall = new Sqrt(0.25); + assertEquals(0.5, sqrtSmall.calc(), 1e-6, + "Квадратный корень из 0.25 должен быть 0.5"); + } + + /// --- Тесты метода average() --- + + @Test + public void testAverageOfTwoNumbers() { + assertEquals(3.0, sqrt4.average(2.0, 4.0), 1e-9, + "Среднее 2.0 и 4.0 должно быть 3.0"); + } + + @Test + public void testAverageSymmetry() { + assertEquals(sqrt4.average(1.0, 5.0), sqrt4.average(5.0, 1.0), 1e-9, + "average(a,b) должно быть равно average(b,a)"); + } + + @Test + public void testAverageEqualNumbers() { + assertEquals(7.0, sqrt4.average(7.0, 7.0), 1e-9, + "Среднее одинаковых чисел должно быть равно им самим"); + } + + /// --- Тесты метода good() --- + + @Test + public void testGoodWhenGuessIsExact() { + assertTrue(sqrt4.good(2.0, 4.0), + "good(2.0, 4.0) должен вернуть true"); + } + + @Test + public void testGoodWhenGuessFar() { + assertFalse(sqrt4.good(10.0, 4.0), + "good(10.0, 4.0) должен вернуть false"); + } + + @Test + public void testGoodBoundaryNearDelta() { + assertTrue(sqrt9.good(3.0, 9.0), + "good(3.0, 9.0) должен вернуть true"); + } + + /// --- Тесты метода improve() --- + + @Test + public void testImproveConvergesTowardsRoot() { + double improved = sqrt4.improve(1.0, 4.0); + assertEquals(2.5, improved, 1e-9, + "improve(1.0, 4.0) должен вернуть 2.5"); + } + + @Test + public void testImproveOnExactRoot() { + double improved = sqrt4.improve(2.0, 4.0); + assertEquals(2.0, improved, 1e-9, + "improve(2.0, 4.0) должен вернуть 2.0 (уже точный корень)"); + } + + /// --- Тесты метода iter() --- + + @Test + public void testIterFromGoodGuess() { + double result = sqrt4.iter(2.0, 4.0); + assertEquals(2.0, result, 1e-6, + "iter(2.0, 4.0) должен вернуть 2.0"); + } + + @Test + public void testIterFromBadGuess() { + double result = sqrt9.iter(1.0, 9.0); + assertEquals(3.0, result, 1e-6, + "iter(1.0, 9.0) должен сойтись к 3.0"); + } +} \ No newline at end of file