feat: add Rust theory guides (terminology, algorithms, testing, debugging) and 2 new crates
This commit is contained in:
1 parent
2e002b6176
commit
015eae08c3
14 files changed
+357
-5
No files matched your search
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "core11-algorithms"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -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 (`src/lib.rs`)
|
||||
1. `binary_search(sorted: &[i32], target: i32) -> Option<usize>` — wyszukiwanie binarne w `O(log n)`.
|
||||
2. `two_sum(nums: &[i32], target: i32) -> Option<(usize, usize)>` — znajdź parę indeksów `(i, j)` (`i < j`) dającą `nums[i] + nums[j] == target` w `O(n)` używając `HashMap`.
|
||||
3. `is_palindrome(text: &str) -> bool` — metoda **dwóch wskaźników** (*two pointers*) na znakach alfanumerycznych (lowercase, obsługa UTF-8).
|
||||
4. `max_window_sum(nums: &[i32], k: usize) -> Option<i32>` — 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=core11-algorithms
|
||||
```
|
||||
@@ -0,0 +1,53 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn binary_search(sorted: &[i32], target: i32) -> Option<usize> {
|
||||
todo!("Wyszukiwanie binarne O(log n): zwróć Some(indeks) lub None")
|
||||
}
|
||||
|
||||
pub fn two_sum(nums: &[i32], target: i32) -> Option<(usize, usize)> {
|
||||
let _ = HashMap::<i32, usize>::new();
|
||||
todo!("Znajdź parę indeksów (i, j) w O(n) używając HashMap")
|
||||
}
|
||||
|
||||
pub fn is_palindrome(text: &str) -> bool {
|
||||
todo!("Dwa wskaźniki na znakach alfanumerycznych (to_lowercase, UTF-8)")
|
||||
}
|
||||
|
||||
pub fn max_window_sum(nums: &[i32], k: usize) -> Option<i32> {
|
||||
todo!("Okno przesuwne O(n); None gdy k == 0 lub k > nums.len()")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn finds_with_binary_search() {
|
||||
assert_eq!(binary_search(&[], 5), None);
|
||||
assert_eq!(binary_search(&[1, 3, 5, 7, 9], 7), Some(3));
|
||||
assert_eq!(binary_search(&[1, 3, 5, 7, 9], 4), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finds_two_sum_in_linear_time() {
|
||||
assert_eq!(two_sum(&[2, 7, 11, 15], 9), Some((0, 1)));
|
||||
assert_eq!(two_sum(&[3, 2, 4], 6), Some((1, 2)));
|
||||
assert_eq!(two_sum(&[1, 2, 3], 99), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checks_utf8_palindrome() {
|
||||
assert!(is_palindrome("Kobyła ma mały bok"));
|
||||
assert!(is_palindrome("A man, a plan, a canal: Panama"));
|
||||
assert!(!is_palindrome("rustacean"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn computes_max_window_sum() {
|
||||
assert_eq!(max_window_sum(&[2, 1, 5, 1, 3, 2], 3), Some(9));
|
||||
assert_eq!(max_window_sum(&[1, 2], 3), None);
|
||||
assert_eq!(max_window_sum(&[1, 2], 0), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "eng05-testing-types-and-mutation"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -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 (`src/lib.rs`)
|
||||
1. **Unit (granice zabijające mutanty):** zaimplementuj `is_within_rate_limit(current: i32, limit: i32) -> Result<bool, RateLimitError>` — dla `current < 0` zwróć `Err(RateLimitError::NegativeCount)`, dla `limit <= 0` zwróć `Ok(false)`, inaczej `Ok(current <= limit)`.
|
||||
2. **Integration (prawdziwy system plików):** zaimplementuj `save_and_load_kv(path: &Path, pairs: &[(&str, &str)]) -> io::Result<Vec<(String, String)>>` — zapisz pary `KEY=VALUE\n` na dysk (`std::fs::write`) i odczytaj z powrotem (`std::fs::read_to_string`).
|
||||
3. **Mutation Testing (eksperyment ręczny):** po przejściu testów zmień `<=` na `<` w `is_within_rate_limit` (mutant!), uruchom `make test PKG=eng05-testing-types-and-mutation` i sprawdź, czy test graniczny `current == limit` zabił mutanta.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test PKG=eng05-testing-types-and-mutation
|
||||
```
|
||||
@@ -0,0 +1,63 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum RateLimitError {
|
||||
NegativeCount,
|
||||
}
|
||||
|
||||
pub fn is_within_rate_limit(current: i32, limit: i32) -> Result<bool, RateLimitError> {
|
||||
todo!(
|
||||
"Err(NegativeCount) dla current < 0; Ok(false) dla limit <= 0; inaczej Ok(current <= limit)"
|
||||
)
|
||||
}
|
||||
|
||||
pub fn save_and_load_kv(path: &Path, pairs: &[(&str, &str)]) -> io::Result<Vec<(String, String)>> {
|
||||
todo!("Zapisz pary KEY=VALUE przez std::fs::write i odczytaj przez std::fs::read_to_string")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
// 1. Testy jednostkowe + warunki brzegowe zabijające mutanty (<= vs <, > 0 vs >= 0)
|
||||
#[test]
|
||||
fn rate_limit_boundaries_kill_mutants() {
|
||||
assert_eq!(is_within_rate_limit(9, 10), Ok(true));
|
||||
assert_eq!(is_within_rate_limit(10, 10), Ok(true)); // Zabija mutanta `<=` -> `<`
|
||||
assert_eq!(is_within_rate_limit(11, 10), Ok(false));
|
||||
assert_eq!(is_within_rate_limit(0, 0), Ok(false)); // Zabija mutanta `limit > 0` -> `>= 0`
|
||||
assert_eq!(is_within_rate_limit(0, -1), Ok(false));
|
||||
assert_eq!(
|
||||
is_within_rate_limit(-1, 10),
|
||||
Err(RateLimitError::NegativeCount)
|
||||
);
|
||||
}
|
||||
|
||||
// 2. Test integracyjny na prawdziwym pliku tymczasowym z deterministycznym sprzątaniem
|
||||
#[test]
|
||||
fn saves_and_loads_kv_file_integration() {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let file_path = std::env::temp_dir().join(format!("rust-learning-kv-{nanos}.conf"));
|
||||
|
||||
let res = save_and_load_kv(&file_path, &[("env", "prod"), ("region", "eu-central-1")]);
|
||||
let exists = file_path.exists();
|
||||
let _ = fs::remove_file(&file_path);
|
||||
|
||||
assert!(exists);
|
||||
assert_eq!(
|
||||
res.unwrap(),
|
||||
vec![
|
||||
(String::from("env"), String::from("prod")),
|
||||
(String::from("region"), String::from("eu-central-1")),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user