diff --git a/.github/workflows/my-app/.gitignore b/.github/workflows/my-app/.gitignore new file mode 100644 index 0000000..8d9590a Binary files /dev/null and b/.github/workflows/my-app/.gitignore differ diff --git a/.github/workflows/my-app/src/main/java/com/mycompany/app/App.java b/.github/workflows/my-app/src/main/java/com/mycompany/app/App.java new file mode 100644 index 0000000..1dab526 --- /dev/null +++ b/.github/workflows/my-app/src/main/java/com/mycompany/app/App.java @@ -0,0 +1,10 @@ +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); + } +} diff --git a/.github/workflows/my-app/src/main/java/com/mycompany/app/Sqrt.java b/.github/workflows/my-app/src/main/java/com/mycompany/app/Sqrt.java new file mode 100644 index 0000000..0ba567e --- /dev/null +++ b/.github/workflows/my-app/src/main/java/com/mycompany/app/Sqrt.java @@ -0,0 +1,33 @@ +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); + } +} diff --git a/.github/workflows/my-app/src/test/java/com/mycompany/app/AppTest.java b/.github/workflows/my-app/src/test/java/com/mycompany/app/AppTest.java new file mode 100644 index 0000000..c8b092b --- /dev/null +++ b/.github/workflows/my-app/src/test/java/com/mycompany/app/AppTest.java @@ -0,0 +1,116 @@ +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 AppTest { + + private static final double DELTA = 1e-6; + + // --- Тесты метода calc() --- + + @Test + public void testSqrtOf4() { + Sqrt sqrt = new Sqrt(4.0); + assertEquals(2.0, sqrt.calc(), DELTA); + } + + @Test + public void testSqrtOf9() { + Sqrt sqrt = new Sqrt(9.0); + assertEquals(3.0, sqrt.calc(), DELTA); + } + + @Test + public void testSqrtOf2() { + Sqrt sqrt = new Sqrt(2.0); + assertEquals(Math.sqrt(2.0), sqrt.calc(), DELTA); + } + + @Test + public void testSqrtOf1() { + Sqrt sqrt = new Sqrt(1.0); + assertEquals(1.0, sqrt.calc(), DELTA); + } + + @Test + public void testSqrtOf0() { + Sqrt sqrt = new Sqrt(0.0); + assertEquals(0.0, sqrt.calc(), DELTA); + } + + @Test + public void testSqrtOf100() { + Sqrt sqrt = new Sqrt(100.0); + assertEquals(10.0, sqrt.calc(), DELTA); + } + + @Test + public void testSqrtOf0Point25() { + Sqrt sqrt = new Sqrt(0.25); + assertEquals(0.5, sqrt.calc(), DELTA); + } + + // --- Тесты метода average() --- + + @Test + public void testAverageOfTwoNumbers() { + Sqrt sqrt = new Sqrt(1.0); + assertEquals(3.0, sqrt.average(2.0, 4.0), DELTA); + } + + @Test + public void testAverageOfSameNumbers() { + Sqrt sqrt = new Sqrt(1.0); + assertEquals(5.0, sqrt.average(5.0, 5.0), DELTA); + } + + @Test + public void testAverageOfZeroAndNumber() { + Sqrt sqrt = new Sqrt(1.0); + assertEquals(2.5, sqrt.average(0.0, 5.0), DELTA); + } + + // --- Тесты метода good() --- + + @Test + public void testGoodReturnsTrueForExactSqrt() { + Sqrt sqrt = new Sqrt(4.0); + assertTrue(sqrt.good(2.0, 4.0)); + } + + @Test + public void testGoodReturnsFalseForBadGuess() { + Sqrt sqrt = new Sqrt(4.0); + assertFalse(sqrt.good(1.0, 4.0)); + } + + // --- Тесты метода improve() --- + + @Test + public void testImproveConvergesCloser() { + Sqrt sqrt = new Sqrt(4.0); + double improved = sqrt.improve(1.0, 4.0); + double distBefore = Math.abs(1.0 * 1.0 - 4.0); + double distAfter = Math.abs(improved * improved - 4.0); + assertTrue(distAfter < distBefore); + } + + // --- Тесты метода iter() --- + + @Test + public void testIterConvergesFor9() { + Sqrt sqrt = new Sqrt(9.0); + assertEquals(3.0, sqrt.iter(1.0, 9.0), DELTA); + } + + // --- Точность результата --- + + @Test + public void testCalcAccuracy() { + Sqrt sqrt = new Sqrt(2.0); + double result = sqrt.calc(); + assertTrue(Math.abs(result * result - 2.0) < 1e-7); + } +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fa10819 --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + com.mycompany.app + my-app + 1.0-SNAPSHOT + jar + + + 11 + 11 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + +