mirror of
https://github.com/rjNemo/ai_advent_code_2024
synced 2026-06-06 02:26:44 +00:00
refactor: Move Day 1 tests to dedicated test file and simplify main test suite
fix bug
This commit is contained in:
parent
4c70af439e
commit
ebf68345ad
3 changed files with 55 additions and 32 deletions
|
|
@ -32,11 +32,14 @@ defmodule AdventCode2024.Solutions.Day1 do
|
|||
case File.read(input_file) do
|
||||
{:ok, content} ->
|
||||
case parse_input(content) do
|
||||
{:error, reason} -> {:error, reason}
|
||||
{left, right} ->
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
|
||||
{left, right} ->
|
||||
result = calculate_total_distance(left, right)
|
||||
{:ok, result}
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
|
|
@ -44,8 +47,14 @@ defmodule AdventCode2024.Solutions.Day1 do
|
|||
|
||||
defp valid_line?(line) do
|
||||
case String.split(line, ~r/\s+/, trim: true) do
|
||||
[left, right] -> match?({:ok, _}, Integer.parse(left)) && match?({:ok, _}, Integer.parse(right))
|
||||
_ -> false
|
||||
[left, right] ->
|
||||
case {Integer.parse(left), Integer.parse(right)} do
|
||||
{{n1, ""}, {n2, ""}} when is_integer(n1) and is_integer(n2) -> true
|
||||
_ -> false
|
||||
end
|
||||
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -66,11 +75,14 @@ defmodule AdventCode2024.Solutions.Day1 do
|
|||
case File.read(input_file) do
|
||||
{:ok, content} ->
|
||||
case parse_input(content) do
|
||||
{:error, reason} -> {:error, reason}
|
||||
{left, right} ->
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
|
||||
{left, right} ->
|
||||
result = calculate_similarity_score(left, right)
|
||||
{:ok, result}
|
||||
end
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
|
|
@ -79,7 +91,7 @@ defmodule AdventCode2024.Solutions.Day1 do
|
|||
@spec parse_input(String.t()) :: number_pair() | {:error, String.t()}
|
||||
defp parse_input(content) do
|
||||
lines = String.split(content, "\n", trim: true)
|
||||
|
||||
|
||||
if Enum.all?(lines, &valid_line?/1) do
|
||||
{left, right} =
|
||||
lines
|
||||
|
|
|
|||
33
test/advent_code2024/solutions/day1/day1_test.exs
Normal file
33
test/advent_code2024/solutions/day1/day1_test.exs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
defmodule AdventCode2024.Solutions.Day1Test do
|
||||
use ExUnit.Case
|
||||
alias AdventCode2024.Solutions.Day1
|
||||
|
||||
@test_input "test/fixtures/day1/input.txt"
|
||||
|
||||
setup do
|
||||
# Ensure the fixtures directory exists
|
||||
File.mkdir_p!("test/fixtures/day1")
|
||||
|
||||
# Create test input file
|
||||
test_content = """
|
||||
1 2
|
||||
3 4
|
||||
5 6
|
||||
"""
|
||||
File.write!(@test_input, test_content)
|
||||
|
||||
on_exit(fn ->
|
||||
File.rm!(@test_input)
|
||||
end)
|
||||
end
|
||||
|
||||
test "solves day 1 puzzle with test input" do
|
||||
assert {:ok, result} = Day1.solve(@test_input)
|
||||
assert is_integer(result)
|
||||
end
|
||||
|
||||
test "solves day 1 part 2 puzzle with test input" do
|
||||
assert {:ok, result} = Day1.solve_part2(@test_input)
|
||||
assert is_integer(result)
|
||||
end
|
||||
end
|
||||
|
|
@ -2,30 +2,8 @@ defmodule AdventCode2024Test do
|
|||
use ExUnit.Case
|
||||
doctest AdventCode2024
|
||||
|
||||
@test_input "test/fixtures/day1/input.txt"
|
||||
|
||||
setup do
|
||||
# Ensure the fixtures directory exists
|
||||
File.mkdir_p!("test/fixtures/day1")
|
||||
|
||||
# Create test input file
|
||||
test_content = """
|
||||
1 2
|
||||
3 4
|
||||
5 6
|
||||
"""
|
||||
File.write!(@test_input, test_content)
|
||||
|
||||
on_exit(fn ->
|
||||
File.rm!(@test_input)
|
||||
end)
|
||||
end
|
||||
|
||||
test "delegates day1 solutions to Day1 module" do
|
||||
assert {:ok, result} = AdventCode2024.solve_day1(@test_input)
|
||||
assert is_integer(result)
|
||||
|
||||
assert {:ok, result} = AdventCode2024.solve_day1_part2(@test_input)
|
||||
assert is_integer(result)
|
||||
test "delegates to solution modules" do
|
||||
assert function_exported?(AdventCode2024, :solve_day1, 1)
|
||||
assert function_exported?(AdventCode2024, :solve_day1_part2, 1)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue