Skip to content

Commit

Permalink
Create test framework for Euler problems
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Sep 29, 2024
1 parent 5c134be commit 63f8172
Show file tree
Hide file tree
Showing 74 changed files with 91 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
charset = utf-8
trim_trailing_whitespace = true

end_of_line = lf
insert_final_newline = true

indent_style = space
indent_size = 4
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Custom
**/main/resources/*/Day*.txt
!**/main/resources/*/DayX.txt
**/main/resources/aoc/*/Day*.txt
!**/main/resources/aoc/*/DayX.txt
src/main/kotlin/com/fwdekker/euler


Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/com/fwdekker/aoc/Day.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ abstract class Day(val resource: String) {
*/
companion object {
/**
* Shorthand for returning the standard resource for the given [year] and [day].
* Shorthand for returning the resource for the given [year] and [day].
*
* Setting [sample] to `0` returns the final difficult input, whereas all other values return easier sample
* inputs.
*/
fun resource(year: Int, day: Int, sample: Int = 0) =
if (sample == 0) "/y$year/Day$day.txt"
else "/y${year}/Day${day}Sample${sample}.txt"
if (sample == 0) "/aoc/y$year/Day$day.txt"
else "/aoc/y${year}/Day${day}Sample${sample}.txt"
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/fwdekker/std/grid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ val <T> Grid<T>.lastRowIndex: Int get() = lastIndex
*/
val <T> Grid<T>.lastColIndex: Int get() = this[0].lastIndex

/**
* All pairs of coordinates in the grid.
*/
val <T> Grid<T>.allCoords: List<Coords> get() = rows.map { row -> cols.map { col -> Coords(col, row) } }.flatten()


/**
* Returns the first row.
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/fwdekker/std/maths.kt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,20 @@ fun IntRange.shift(by: Int): IntRange = this.first + by..this.last + by
fun LongRange.shift(by: Long): LongRange = this.first + by..this.last + by


/**
* Returns an infinite sequence of Fibonacci numbers, starting with 1, 1, 2.
*/
fun fibonacci(): Sequence<Long> =
sequence {
var a = 1L
var b = 1L

while (true) {
yield(a)
a = b.also { b += a }
}
}

/**
* Returns an infinite sequence of all natural numbers, starting at 0.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/aoc/y2023/DayX.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Put your input files in this directory with names 'Day1.txt' etc.

These files are ignored in the `.gitignore` because everyone will have different inputs.
1 change: 1 addition & 0 deletions src/main/resources/euler/Problem1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1000
1 change: 1 addition & 0 deletions src/main/resources/euler/Problem2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4000000
3 changes: 3 additions & 0 deletions src/main/resources/euler/ProblemX.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Put your input files in this directory with names 'Problem1.txt' etc.

These files are not ignored in the `.gitignore` because everyone has the same inputs anyway.
1 change: 0 additions & 1 deletion src/main/resources/y2023/DayX.txt

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/kotlin/com/fwdekker/aoc/DayTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.kotest.matchers.shouldBe
/**
* Tests that a given [Day] has the expected outputs for given file inputs.
*
* @param day creates a new [Day] from a path to a file
* @param day the constructor of the [Day] to test
* @param tests tests to run, expressed as an input file, a reference to the part to run, and the expected output
* @constructor
*/
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/com/fwdekker/euler/Problem1Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fwdekker.euler


/**
* Tests for [Problem1].
*/
class Problem1Test : ProblemTest<Int>(
::Problem1,
listOf(
Pair(Problem.resource(1, sample = 1), 0),
Pair(Problem.resource(1), 0),
)
)
10 changes: 10 additions & 0 deletions src/test/kotlin/com/fwdekker/euler/Problem2Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fwdekker.euler


/**
* Tests for [Problem2].
*/
class Problem2Test : ProblemTest<Long>(
::Problem2,
listOf(Pair(Problem.resource(2), 0L))
)
20 changes: 20 additions & 0 deletions src/test/kotlin/com/fwdekker/euler/ProblemTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fwdekker.euler

import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe


/**
* Tests that a given [Problem] has the expected outputs for given file inputs.
*
* @param problem the constructor of the [Problem] to test
* @param tests tests to run, expressed as an input file and the expected output
* @constructor
*/
abstract class ProblemTest<S>(problem: (String) -> Problem<S>, tests: Collection<Pair<String, S>>) : FunSpec({
withData(
nameFn = { (file, expected) -> "($file) = $expected" },
tests
) { (resource, expected) -> problem(resource).solve() shouldBe expected }
})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/test/resources/euler/Problem1Sample1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10

0 comments on commit 63f8172

Please sign in to comment.