test: add BATS test suite (79 tests)

- tests/common.bats: PROJECT_ROOT detection, is_cmd, timestamp, data_dir,
  logging functions, color handling, require_root
- tests/detect.bats: GPU sysfs reads with mock sysfs tree, kernel param
  parsing (word boundary, dot escaping, edge positions), recommended
  GTT/pages computation (64GB, 128GB, tiny, zero), firmware bad detection,
  stack detection
- tests/format.bats: human_bytes (0, KiB, MiB, GiB boundaries, 64GiB),
  human_mib (sub-GiB, exact-GiB, recommended values, empty input)
- tests/benchmark_compare.bats: improvement/regression display, empty
  results, missing files, usage output, config change detection
- tests/log_metrics.bats: CSV header, data format, field count, input
  validation, unknown argument handling
- tests/test_helper.sh: mock sysfs tree builder, bats-assert/support setup

Makefile: add 'make test' target

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Felipe Cardoso
2026-03-25 22:15:34 +01:00
parent da2c4c6b8a
commit a403dd9ce0
7 changed files with 679 additions and 1 deletions

104
tests/format.bats Normal file
View File

@@ -0,0 +1,104 @@
#!/usr/bin/env bats
# Tests for lib/format.sh — pure formatting functions
load test_helper.sh
setup() {
source_lib common.sh
source_lib format.sh
}
# ── human_bytes ──────────────────────────────────────────
@test "human_bytes: 0 bytes" {
run human_bytes 0
assert_output "0 B"
}
@test "human_bytes: small value (500 bytes)" {
run human_bytes 500
assert_output "500 B"
}
@test "human_bytes: exactly 1 KiB" {
run human_bytes 1024
assert_output "1 KiB"
}
@test "human_bytes: exactly 1 MiB" {
run human_bytes 1048576
assert_output "1 MiB"
}
@test "human_bytes: exactly 1 GiB" {
run human_bytes 1073741824
assert_output "1.0 GiB"
}
@test "human_bytes: 32 GiB (typical VRAM)" {
run human_bytes 34359738368
assert_output "32.0 GiB"
}
@test "human_bytes: 512 MiB (optimal VRAM)" {
run human_bytes 536870912
assert_output "512 MiB"
}
@test "human_bytes: sub-GiB value formats with leading zero (0.5 GiB)" {
# 0.5 GiB = 536870912 bytes — should NOT be "0.5 GiB" since < 1 GiB uses MiB
run human_bytes 536870912
assert_output "512 MiB"
}
@test "human_bytes: 1.5 GiB has correct decimal" {
run human_bytes 1610612736
assert_output "1.5 GiB"
}
@test "human_bytes: empty/missing argument defaults to 0" {
run human_bytes ""
assert_output "0 B"
}
@test "human_bytes: 64 GiB" {
run human_bytes 68719476736
assert_output "64.0 GiB"
}
# ── human_mib ────────────────────────────────────────────
@test "human_mib: 0 MiB" {
run human_mib 0
assert_output "0 MiB"
}
@test "human_mib: 512 MiB (stays as MiB)" {
run human_mib 512
assert_output "512 MiB"
}
@test "human_mib: 1023 MiB (just under GiB threshold)" {
run human_mib 1023
assert_output "1023 MiB"
}
@test "human_mib: 1024 MiB = 1.0 GiB" {
run human_mib 1024
assert_output "1.0 GiB"
}
@test "human_mib: 60416 MiB (recommended GTT for 64GB system)" {
run human_mib 60416
assert_output "59.0 GiB"
}
@test "human_mib: 61440 MiB = 60.0 GiB" {
run human_mib 61440
assert_output "60.0 GiB"
}
@test "human_mib: empty argument defaults to 0" {
run human_mib ""
assert_output "0 MiB"
}