Skip to content

Commit

Permalink
feat: Added tests & CI
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrypt0nn committed Sep 26, 2023
1 parent 31e0849 commit 2f8203a
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 35 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: AoC CI (Lint & Test)

on: [ push, pull_request ]

permissions:
contents: read

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21.1
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
test:
name: Test
needs: lint
runs-on: ubuntu-latest
strategy:
matrix:
version: ["1.18.0", "1.19.0", "1.20.0", "1.21.0", "stable"]
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: {{matrix.version}}
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
1 change: 1 addition & 0 deletions aoc.go
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Package aoc provides some utilities to make the Advent of Code solutions easier, faster and shorter to solve
package aoc
15 changes: 15 additions & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package aoc

import "testing"

func TestBoolToInt(t *testing.T) {
intTrue := BoolToInt(true)
intFalse := BoolToInt(false)
expectedTrue := 1
expectedFalse := 0
if intTrue == expectedTrue && intFalse == expectedFalse {
t.Log("Successfully converted a bool value to its int representation")
return
}
t.Errorf("Failed converting a bool value to its int representation (got: true=%d/false=%d, expected: true=%d/false=%d)", intTrue, intFalse, expectedTrue, expectedFalse)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/kkrypt0nn/aoc

go 1.21.1
go 1.18
14 changes: 14 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package aoc

import "testing"

func TestMapSum(t *testing.T) {
theMap := map[int]int{1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10}
mapSum := MapSum(theMap)
expected := 54
if mapSum == expected {
t.Log("Successfully got the sum of the map")
return
}
t.Errorf("Failed getting the sum of the map (got: %d, expected: %d)", mapSum, expected)
}
55 changes: 37 additions & 18 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ package aoc

import "sort"

// Contains checks whether an element of type T is contained in the slice or not.
func Contains[T comparable](slice []T, element T) bool {
// SliceContainsAll checks whether all elements of slice B are in slice A.
func SliceContainsAll[T comparable](sliceA []T, sliceB []T) bool {
for _, item := range sliceB {
if !SliceContains(sliceA, item) {
return false
}
}
return true
}

// SliceContains checks whether an element of type T is contained in the slice.
func SliceContains[T comparable](slice []T, element T) bool {
for _, item := range slice {
if item == element {
return true
Expand All @@ -12,51 +22,60 @@ func Contains[T comparable](slice []T, element T) bool {
return false
}

// Max returns the maximum value in the slice as an integer.
func Max(slice []int) int {
// SliceMax returns the maximum value in the slice as an integer.
func SliceMax(slice []int) int {
sort.Slice(slice, func(i, j int) bool {
return slice[i] > slice[j]
})
return slice[0]
}

// MaxSlice returns an amount of maximum values in the slice as a new slice.
func MaxSlice(slice []int, amount int) []int {
// SliceMultipleMax returns an amount of maximum values in the slice as a new slice.
func SliceMultipleMax(slice []int, amount int) []int {
sort.Slice(slice, func(i, j int) bool {
return slice[i] > slice[j]
})
var max []int
var maxValues []int
for i := 0; i < amount; i++ {
max = append(max, slice[i])
maxValues = append(maxValues, slice[i])
}
return max
return maxValues
}

// Min returns the minimum value in the slice as an integer.
func Min(slice []int) int {
// SliceMin returns the minimum value in the slice as an integer.
func SliceMin(slice []int) int {
sort.Slice(slice, func(i, j int) bool {
return slice[i] < slice[j]
})
return slice[0]
}

// MinSlice returns an amount of minimum values in the slice as a new slice.
func MinSlice(slice []int, amount int) []int {
// SliceMultipleMin returns an amount of minimum values in the slice as a new slice.
func SliceMultipleMin(slice []int, amount int) []int {
sort.Slice(slice, func(i, j int) bool {
return slice[i] < slice[j]
})
var min []int
var minValues []int
for i := 0; i < amount; i++ {
min = append(min, slice[i])
minValues = append(minValues, slice[i])
}
return min
return minValues
}

// Sum returns the sum of the values in the slice.
func Sum(slice []int) int {
// SliceSum returns the sum of the values in the slice.
func SliceSum(slice []int) int {
sum := 0
for _, x := range slice {
sum += x
}
return sum
}

// SliceReverse reverses a slice of elements of type T.
func SliceReverse[T any](slice []T) []T {
var newSlice []T
for i := len(slice) - 1; i >= 0; i-- {
newSlice = append(newSlice, slice[i])
}
return newSlice
}
93 changes: 93 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package aoc

import "testing"

func TestSliceContainsAll(t *testing.T) {
sliceA := []int{7, 5, 3, 9, 2, 6, 10, 8, 4, 1, -1}
sliceB := []int{3, 7, 1}
containsAll := SliceContainsAll(sliceA, sliceB)
if containsAll {
t.Log("Successfully checked if all the elements of slice B are in slice A")
return
}
t.Errorf("Failed checking if all the elements of slice B are in slice A (got: %t, expected: true)", containsAll)
}

func TestContains(t *testing.T) {
theSlice := []int{7, 5, 3, 9, 2, 6, 10, 8, 4, 1, -1}
element := 8
contains := SliceContains(theSlice, element)
if contains {
t.Log("Successfully checked if the element is contained in the slice")
return
}
t.Errorf("Failed checking if the element is contained in the slice (got: %t, expected: true)", contains)
}

func TestSliceMax(t *testing.T) {
theSlice := []int{7, 5, 3, 9, 2, 6, 10, 8, 4, 1, -1}
sliceMax := SliceMax(theSlice)
expected := 10
if sliceMax == expected {
t.Log("Successfully got the biggest element in the slice")
return
}
t.Errorf("Failed getting the biggest element in the slice (got: %d, expected: %d)", sliceMax, expected)
}

func TestSliceMultipleMax(t *testing.T) {
theSlice := []int{7, 5, 3, 9, 2, 6, 10, 8, 4, 1, -1}
sliceMax := SliceMultipleMax(theSlice, 2)
expected := []int{9, 10}
if SliceContainsAll(sliceMax, expected) && len(sliceMax) == len(expected) {
t.Log("Successfully got the two biggest numbers in the slice")
return
}
t.Errorf("Failed getting the two biggest numbers in the slice (got: %#v, expected: %#v)", sliceMax, expected)
}

func TestSliceMin(t *testing.T) {
theSlice := []int{7, 5, 3, 9, 2, 6, 10, 8, 4, 1, -1}
sliceMin := SliceMin(theSlice)
expected := -1
if sliceMin == expected {
t.Log("Successfully got the smallest element in the slice")
return
}
t.Errorf("Failed getting the smallest element in the slice (got: %d, expected: %d)", sliceMin, expected)
}

func TestSliceMultipleMin(t *testing.T) {
theSlice := []int{7, 5, 3, 9, 2, 6, 10, 8, 4, 1, -1}
sliceMin := SliceMultipleMin(theSlice, 2)
expected := []int{-1, 1}
if SliceContainsAll(sliceMin, expected) && len(sliceMin) == len(expected) {
t.Log("Successfully got the two smallest numbers in the slice")
return
}
t.Errorf("Failed getting the two smallest numbers in the slice (got: %#v, expected: %#v)", sliceMin, expected)
}

func TestSliceSum(t *testing.T) {
theSlice := []int{7, 5, 3, 9, 2, 6, 10, 8, 4, 1, -1}
sliceSum := SliceSum(theSlice)
expected := 54
if sliceSum == expected {
t.Log("Successfully got the sum of the slice")
return
}
t.Errorf("Failed getting the sum of the slice (got: %d, expected: %d)", sliceSum, expected)
}

func TestSliceReverse(t *testing.T) {
theSlice := []int{7, 5, 3}
sliceReverse := SliceReverse(theSlice)
elementA := 3
elementB := 5
elementC := 7
if sliceReverse[0] == elementA && sliceReverse[1] == elementB && sliceReverse[2] == elementC {
t.Log("Successfully reversed the slice")
return
}
t.Errorf("Failed reversing the slice (got: %#v, expected: %#v)", sliceReverse, []int{elementA, elementB, elementC})
}
23 changes: 7 additions & 16 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import (
"strings"
)

// Reverse reverses a slice of elements of type T.
func Reverse[T any](slice []T) []T {
var newSlice []T
for i := len(slice) - 1; i >= 0; i-- {
newSlice = append(newSlice, slice[i])
}
return newSlice
}

// ReverseString reverses a string.
func ReverseString(str string) string {
var result string
Expand All @@ -29,22 +20,22 @@ func Atoi(str string) int {
return i
}

// ContainsSubstr returns true if the string contains the given substring.
func ContainsSubstr(str string, substr string) bool {
for _, i := range substr {
// ContainsCharacters returns true if the string contains the given characters anywhere in any order.
func ContainsCharacters(str string, characters string) bool {
for _, i := range characters {
if !strings.Contains(str, string(i)) {
return false
}
}
return true
}

// ContainsExactly returns true if the string contains the given substring and has the same length.
func ContainsExactly(str string, substr string) bool {
if len(str) != len(substr) {
// ContainsExactly returns true if the string contains the given characters and has the same length.
func ContainsExactly(str string, characters string) bool {
if len(str) != len(characters) {
return false
}
for _, i := range substr {
for _, i := range characters {
// Yes that is a bit wacky if the string contains the same character multiple times, though worked for the challenge in 2021.
if !strings.Contains(str, string(i)) {
return false
Expand Down
47 changes: 47 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package aoc

import "testing"

func TestReverseString(t *testing.T) {
name := "Krypton"
reversed := ReverseString(name)
expected := "notpyrK"
if reversed == expected {
t.Log("Successfully reversed the string")
return
}
t.Errorf("Failed reversing the string (got: %s, expected: %s)", reversed, expected)
}

func TestAtoi(t *testing.T) {
stringNumber := "1337"
converted := Atoi(stringNumber)
expected := 1337
if converted == expected {
t.Log("Successfully converted a string to an integer")
return
}
t.Errorf("Failed converting a string to an integer (got: %d, expected: %d)", converted, expected)
}

func TestContainsCharacters(t *testing.T) {
sentence := "the quick brown fox jumps over the lazy dog"
characters := "xyz"
contains := ContainsCharacters(sentence, characters)
if contains {
t.Log("Successfully checked if the characters of the substring is contained in the sentence")
return
}
t.Errorf("Failed checking if the characters of the substring is contained in the sentence (got: %t, expected: true)", contains)
}

func TestContainsExactly(t *testing.T) {
sentence := "thequickbrownfxjmpsvlazydg"
characters := "abcdefghijklmnopqrstuvwxyz"
contains := ContainsExactly(sentence, characters)
if contains {
t.Log("Successfully checked if the characters are in the sentence, and have the same length")
return
}
t.Errorf("Failed checking if the characters are in the sentence, and have the same length (got: %t, expected: true)", contains)
}

0 comments on commit 2f8203a

Please sign in to comment.