feat: add Go theory guides (terminology, algorithms, testing, debugging) and 2 new chapters

This commit is contained in:
soda64 committed 2026-11-30 23:40:23 +01:00
1 parent 84be3e5d77
commit 011f23e637
13 files changed
+374 -3

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
# 11 — Algorytmy i Złożoność Obliczeniowa (`O(log n)`, `O(n)`)
Teoria: [notes/03-algorithms-and-complexity.md](../../../notes/03-algorithms-and-complexity.md)
## Zadania (`algorithms.go`)
1. `BinarySearch(sorted []int, target int) (int, bool)` — wyszukiwanie binarne w `O(log n)` (zwróć indeks i `true`, lub `-1, false`).
2. `TwoSum(nums []int, target int) (int, int, bool)` — znajdź parę indeksów `(i, j)` (`i < j`) dającą `nums[i] + nums[j] == target` w czasie `O(n)` przy pomocy `map[int]int`.
3. `IsPalindrome(text string) bool` — metoda **dwóch wskaźników** (*two pointers*) na `[]rune`, ignorując wielkość liter i znaki niealfanumeryczne (`unicode.IsLetter`, `unicode.IsDigit`, `unicode.ToLower`).
4. `MaxWindowSum(nums []int, k int) (int, bool)` — maksymalna suma spójnego podciągu długości `k` metodą **okna przesuwnego** (*sliding window*) w `O(n)`.
## Uruchomienie testów
```bash
make test PKG=./exercises/01-core/11-algorithms
```
@@ -0,0 +1,17 @@
package algorithms
func BinarySearch(sorted []int, target int) (int, bool) {
panic("TODO: wyszukiwanie binarne O(log n): zwróć (indeks, true) lub (-1, false)")
}
func TwoSum(nums []int, target int) (int, int, bool) {
panic("TODO: znajdź (i, j, true) w O(n) używając map[int]int")
}
func IsPalindrome(text string) bool {
panic("TODO: dwa wskaźniki na []rune, ignoruj wielkość liter i znaki niealfanumeryczne")
}
func MaxWindowSum(nums []int, k int) (int, bool) {
panic("TODO: okno przesuwne O(n); zwróć (0, false) gdy k <= 0 lub k > len(nums)")
}
@@ -0,0 +1,49 @@
package algorithms
import "testing"
func TestBinarySearch(t *testing.T) {
if _, ok := BinarySearch([]int{}, 5); ok {
t.Fatal("expected false for empty slice")
}
if idx, ok := BinarySearch([]int{1, 3, 5, 7, 9}, 7); !ok || idx != 3 {
t.Fatalf("got (%d, %v), want (3, true)", idx, ok)
}
if _, ok := BinarySearch([]int{1, 3, 5, 7, 9}, 4); ok {
t.Fatal("expected false for missing element")
}
}
func TestTwoSum(t *testing.T) {
i, j, ok := TwoSum([]int{2, 7, 11, 15}, 9)
if !ok || i != 0 || j != 1 {
t.Fatalf("got (%d, %d, %v), want (0, 1, true)", i, j, ok)
}
if _, _, ok := TwoSum([]int{1, 2, 3}, 99); ok {
t.Fatal("expected false when no pair exists")
}
}
func TestIsPalindrome(t *testing.T) {
if !IsPalindrome("Kobyła ma mały bok") {
t.Error("expected Polish UTF-8 palindrome to be true")
}
if !IsPalindrome("A man, a plan, a canal: Panama") {
t.Error("expected classic palindrome to be true")
}
if IsPalindrome("kubernetes") {
t.Error("expected false for kubernetes")
}
}
func TestMaxWindowSum(t *testing.T) {
if got, ok := MaxWindowSum([]int{2, 1, 5, 1, 3, 2}, 3); !ok || got != 9 {
t.Fatalf("got (%d, %v), want (9, true)", got, ok)
}
if _, ok := MaxWindowSum([]int{1, 2}, 3); ok {
t.Fatal("expected false when k > len")
}
if _, ok := MaxWindowSum([]int{1, 2}, 0); ok {
t.Fatal("expected false when k <= 0")
}
}
@@ -0,0 +1,13 @@
# 05 — Rodzaje Testów: Jednostkowe, Integracyjne i Mutacyjne
Teoria: [notes/04-testing-pyramid-and-types.md](../../../notes/04-testing-pyramid-and-types.md)
## Zadania (`testing_types.go` + `testing_types_test.go`)
1. **Unit (granice zabijające mutanty):** zaimplementuj `IsWithinRateLimit(current, limit int) (bool, error)` — dla `current < 0` zwróć `ErrNegativeCount`, dla `limit <= 0` zwróć `false, nil`, inaczej `current <= limit, nil`.
2. **Integration (prawdziwy system plików):** zaimplementuj `SaveAndLoadConfig(path string, data map[string]string) (map[string]string, error)` — zapis JSON na dysk (`os.WriteFile`) i odczyt z powrotem (`os.ReadFile` + `json.Unmarshal`).
3. **Mutation Testing (eksperyment ręczny):** po przejściu testów zmień `<=` na `<` w `IsWithinRateLimit` (mutant!), uruchom `make test PKG=./exercises/02-engineering-2026/05-testing-types-and-mutation` i sprawdź, czy test graniczny `current == limit` zabił mutanta.
## Uruchomienie testów
```bash
make test PKG=./exercises/02-engineering-2026/05-testing-types-and-mutation
```
@@ -0,0 +1,13 @@
package testingtypes
import "errors"
var ErrNegativeCount = errors.New("current request count cannot be negative")
func IsWithinRateLimit(current, limit int) (bool, error) {
panic("TODO: ErrNegativeCount dla current < 0; false dla limit <= 0; inaczej current <= limit")
}
func SaveAndLoadConfig(path string, data map[string]string) (map[string]string, error) {
panic("TODO: zserializuj data do JSON, zapisz pod path i odczytaj z powrotem")
}
@@ -0,0 +1,54 @@
package testingtypes
import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
)
// 1. Testy jednostkowe + warunki brzegowe zabijające mutanty (<= vs <, > 0 vs >= 0)
func TestRateLimitBoundariesKillMutants(t *testing.T) {
cases := []struct {
name string
current, limit int
want bool
}{
{"below_limit", 9, 10, true},
{"at_limit_kills_lt_mutant", 10, 10, true},
{"above_limit", 11, 10, false},
{"zero_limit_kills_gte_mutant", 0, 0, false},
{"negative_limit", 0, -1, false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := IsWithinRateLimit(tc.current, tc.limit)
if err != nil || got != tc.want {
t.Fatalf("got (%v, %v), want (%v, nil)", got, err, tc.want)
}
})
}
if _, err := IsWithinRateLimit(-1, 10); !errors.Is(err, ErrNegativeCount) {
t.Fatalf("expected ErrNegativeCount, got %v", err)
}
}
// 2. Test integracyjny (prawdziwy zapis i odczyt z systemu plików)
func TestSaveAndLoadConfigIntegration(t *testing.T) {
cfgPath := filepath.Join(t.TempDir(), "service-config.json")
payload := map[string]string{"env": "prod", "region": "eu-central-1"}
loaded, err := SaveAndLoadConfig(cfgPath, payload)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, statErr := os.Stat(cfgPath); statErr != nil {
t.Fatalf("expected file to exist on disk: %v", statErr)
}
if !reflect.DeepEqual(loaded, payload) {
t.Fatalf("got %v, want %v", loaded, payload)
}
}