Skip to content

Commit 09574b2

Browse files
committed
test: 🚨 add tests
1 parent f1a1f1d commit 09574b2

8 files changed

+299
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule InterpolationApp.Algo.Lagrange3InterpolationTest do
2+
@moduledoc """
3+
Testing InterpolationApp.Algo.Lagrange3Interpolation
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
use Quixir
8+
9+
alias InterpolationApp.Algo.Lagrange3Interpolation, as: Algo
10+
11+
describe "get_name/0" do
12+
test "returns correct name" do
13+
assert Algo.get_name() == "Интерполяционный многочлен Лагранжа для 3 точек"
14+
end
15+
end
16+
17+
describe "get_enough_points_count/0" do
18+
test "returns correct enough points count" do
19+
assert Algo.get_enough_points_count() == 3
20+
end
21+
end
22+
23+
describe "interpolate/2" do
24+
test "calculates correct interpolation for 3 points" do
25+
points = [{1, 1}, {2, 4}, {3, 9}]
26+
point = Algo.interpolate(points, 2.5)
27+
28+
assert point == {2.5, 6.25}
29+
end
30+
31+
test "interpolation returns a tuple {x, y} with x same as input" do
32+
ptest x: float(), p1: float(), p2: float(), p3: float() do
33+
points = [{1, p1}, {2, p2}, {3, p3}]
34+
{result_x, _} = Algo.interpolate(points, x)
35+
result_x == x
36+
end
37+
end
38+
end
39+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule InterpolationApp.Algo.Lagrange4InterpolationTest do
2+
@moduledoc """
3+
Testing InterpolationApp.Algo.Lagrange4Interpolation
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
use Quixir
8+
9+
alias InterpolationApp.Algo.Lagrange4Interpolation, as: Algo
10+
11+
describe "get_name/0" do
12+
test "returns correct name" do
13+
assert Algo.get_name() == "Интерполяционный многочлен Лагранжа для 4 точек"
14+
end
15+
end
16+
17+
describe "get_enough_points_count/0" do
18+
test "returns correct enough points count" do
19+
assert Algo.get_enough_points_count() == 4
20+
end
21+
end
22+
23+
describe "interpolate/2" do
24+
test "calculates correct interpolation for 4 points" do
25+
points = [{1, 1}, {2, 4}, {3, 9}, {4, 16}]
26+
point = Algo.interpolate(points, 2.5)
27+
28+
assert point == {2.5, 6.25}
29+
end
30+
31+
test "interpolation with 4 points returns a tuple {x, y} where x matches input" do
32+
ptest x: float(), p1: float(), p2: float(), p3: float(), p4: float() do
33+
points = [{1, p1}, {2, p2}, {3, p3}, {4, p4}]
34+
{result_x, _} = Algo.interpolate(points, x)
35+
result_x == x
36+
end
37+
end
38+
end
39+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule InterpolationApp.Algo.LinearInterpolationTest do
2+
@moduledoc """
3+
Testing InterpolationApp.Algo.LinearInterpolation
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
use Quixir
8+
9+
alias InterpolationApp.Algo.LinearInterpolation, as: Algo
10+
11+
describe "get_name/0" do
12+
test "returns correct name" do
13+
assert Algo.get_name() == "Линейная интерполяция"
14+
end
15+
end
16+
17+
describe "get_enough_points_count/0" do
18+
test "returns correct enough points count" do
19+
assert Algo.get_enough_points_count() == 2
20+
end
21+
end
22+
23+
describe "interpolate/2" do
24+
test "calculates correct linear interpolation for 2 points" do
25+
points = [{1, 2}, {2, 4}]
26+
point = Algo.interpolate(points, 1.5)
27+
28+
assert point == {1.5, 3.0}
29+
end
30+
31+
test "linear interpolation returns tuple {x, y} with x as input" do
32+
ptest x: float(), p1: float(), p2: float() do
33+
points = [{1, p1}, {2, p2}]
34+
{result_x, _} = Algo.interpolate(points, x)
35+
result_x == x
36+
end
37+
end
38+
end
39+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule InterpolationApp.Algo.NewtonInterpolationTest do
2+
@moduledoc """
3+
Testing InterpolationApp.Algo.NewtonInterpolation
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
use Quixir
8+
9+
alias InterpolationApp.Algo.NewtonInterpolation, as: Algo
10+
11+
describe "get_name/0" do
12+
test "returns correct name" do
13+
assert Algo.get_name() == "Интерполяция по Ньютону"
14+
end
15+
end
16+
17+
describe "get_enough_points_count/0" do
18+
test "returns correct enough points count" do
19+
assert Algo.get_enough_points_count() == 2
20+
end
21+
end
22+
23+
describe "interpolate/2" do
24+
test "calculates correct Newton interpolation for given points" do
25+
points = [{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}]
26+
{x, y} = Algo.interpolate(points, 4.5)
27+
28+
assert x == 4.5 and y > 17 and y < 22
29+
end
30+
31+
test "Newton interpolation returns {x, y} with x matching input" do
32+
ptest x: float(), p1: float(), p2: float(), p3: float() do
33+
points = [{1, p1}, {2, p2}, {3, p3}]
34+
{result_x, _} = Algo.interpolate(points, x)
35+
result_x == x
36+
end
37+
end
38+
end
39+
end

test/cli/config_parser_test.exs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
defmodule InterpolationApp.CLI.ConfigParserTest do
2+
@moduledoc """
3+
Testing InterpolationApp.CLI.ConfigParser
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
use Quixir
8+
9+
alias InterpolationApp.CLI.ConfigParser
10+
11+
describe "parse_args/1" do
12+
test "parses correct args" do
13+
args = ["methods=linear,lagrange3", "step=0.5", "accuracy=3"]
14+
15+
assert ConfigParser.parse_args(args) ==
16+
{:ok,
17+
%{
18+
methods: [
19+
InterpolationApp.Algo.LinearInterpolation,
20+
InterpolationApp.Algo.Lagrange3Interpolation
21+
],
22+
step: 0.5,
23+
accuracy: 3
24+
}}
25+
end
26+
27+
test "returns error for no args" do
28+
assert match?({:error, _}, ConfigParser.parse_args([""]))
29+
end
30+
31+
test "returns error for repetitive methods" do
32+
args = ["methods=linear,linear,linear", "step=0.5", "accuracy=3"]
33+
assert match?({:error, _}, ConfigParser.parse_args(args))
34+
end
35+
36+
test "returns error for invalid methods" do
37+
args = ["methods=invalid,method", "step=0.5", "accuracy=3"]
38+
assert match?({:error, _}, ConfigParser.parse_args(args))
39+
end
40+
41+
test "returns error for invalid step" do
42+
args = ["methods=invalid,method", "step=-0.5", "accuracy=3"]
43+
assert match?({:error, _}, ConfigParser.parse_args(args))
44+
end
45+
46+
test "returns error for invalid accuracy" do
47+
args = ["methods=invalid,method", "step=-0.5", "accuracy=3.2"]
48+
assert match?({:error, _}, ConfigParser.parse_args(args))
49+
end
50+
51+
test "accepts positive float step and integer accuracy" do
52+
ptest step: float(min: 0.1), accuracy: int(min: 1) do
53+
args = ["methods=linear", "step=#{step}", "accuracy=#{accuracy}"]
54+
{:ok, config} = ConfigParser.parse_args(args)
55+
config.step == step and config.accuracy == accuracy
56+
end
57+
end
58+
end
59+
end

test/cli/printer_test.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule InterpolationApp.CLI.PrinterTest do
2+
@moduledoc """
3+
Testing InterpolationApp.CLI.Printer
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
import ExUnit.CaptureIO
8+
9+
alias InterpolationApp.CLI.Printer
10+
11+
describe "print/2" do
12+
test "prints method and result in expected format" do
13+
{:ok, pid} = Printer.start_link(%{step: 0.1, accuracy: 2})
14+
15+
assert capture_io([pid: pid], fn ->
16+
GenServer.cast(
17+
pid,
18+
{:print, InterpolationApp.Algo.LinearInterpolation, [{0.0, 0.0}]}
19+
)
20+
21+
:sys.get_state(pid)
22+
end) =~ ""
23+
end
24+
end
25+
end

test/utils/input_validator_test.exs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule InterpolationApp.Utils.InputValidatorTest do
2+
@moduledoc """
3+
Testing InterpolationApp.Utils.InputValidator
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
use Quixir
8+
9+
alias InterpolationApp.Utils.InputValidator
10+
11+
describe "validate_input/1" do
12+
test "validates correct input format" do
13+
assert InputValidator.validate_input("1.0;2.0") == {:ok, {1.0, 2.0}}
14+
end
15+
16+
test "returns error for non-numeric input" do
17+
assert InputValidator.validate_input("a;b") == {:error, "Координаты должны быть числами"}
18+
end
19+
20+
test "returns error for incorrect format" do
21+
assert InputValidator.validate_input("1.0") == {:error, "У точки должны быть координаты"}
22+
end
23+
24+
test "validates that input coordinates are numeric" do
25+
ptest x: float(), y: float() do
26+
input = "#{x};#{y}"
27+
{:ok, {parsed_x, parsed_y}} = InputValidator.validate_input(input)
28+
parsed_x == x and parsed_y == y
29+
end
30+
end
31+
end
32+
end

test/utils/point_generator_test.exs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule InterpolationApp.Utils.PointGeneratorTest do
2+
@moduledoc """
3+
Testing InterpolationApp.Utils.PointGenerator
4+
"""
5+
6+
use ExUnit.Case, async: true
7+
use Quixir
8+
9+
alias InterpolationApp.Utils.PointGenerator
10+
11+
describe "generate/2" do
12+
test "generates points with correct step between given points" do
13+
points = [{1, 1}, {3, 3}]
14+
result = PointGenerator.generate(points, 0.5)
15+
expected = [1.0, 1.5, 2.0, 2.5, 3.0, 3.5]
16+
assert result == expected
17+
end
18+
19+
test "generates intermediate points within the start and end points" do
20+
ptest start: float(), finish: float(), step: float(min: 0.1, max: 1.0) do
21+
points = [{start, 0}, {finish, 0}]
22+
result = PointGenerator.generate(points, step)
23+
Enum.all?(result, fn x -> x >= start and x <= finish + step end)
24+
end
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)