#[derive(Clone, Copy, Debug, Default)]
pub struct Braintax {
nesting: u32,
branches: u32,
}
impl Braintax {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn with_nesting(mut self, nesting: u32) -> Self {
self.nesting = nesting;
self
}
#[must_use]
pub const fn with_branches(mut self, branches: u32) -> Self {
self.branches = branches;
self
}
#[must_use]
pub const fn compute(&self) -> u64 {
(self.nesting as u64).pow(2) + self.branches as u64 + 1
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn baseline_returns_one() {
let tax = Braintax::new().compute();
assert_eq!(tax, 1);
}
#[test]
fn nesting_increases_score_quadratically() {
let tax = Braintax::new().with_nesting(3).compute();
assert_eq!(tax, 10); }
#[test]
fn branches_add_linearly() {
let tax = Braintax::new().with_branches(5).compute();
assert_eq!(tax, 6); }
#[test]
fn combined_nesting_and_branches() {
let tax = Braintax::new().with_nesting(2).with_branches(4).compute();
assert_eq!(tax, 9); }
#[test]
fn large_values_do_not_overflow() {
let tax = Braintax::new()
.with_nesting(1_000)
.with_branches(100_000)
.compute();
assert_eq!(tax, 1_100_001); }
#[test]
fn debug_format_does_not_panic() {
let tax = Braintax::new().with_nesting(1).with_branches(2);
let _ = format!("{tax:?}");
}
#[test]
fn clone_produces_equal_score() {
let original = Braintax::new().with_nesting(4).with_branches(3);
let cloned = original;
assert_eq!(original.compute(), cloned.compute());
}
}