| 1 |
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0. |
| 2 |
|
| 3 |
#![cfg(feature = "cli")] |
| 4 |
|
| 5 |
use std::fs; |
| 6 |
use std::process::Command; |
| 7 |
|
| 8 |
#[test] |
| 9 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 10 |
fn test_benchmark_flag_parsing() { |
| 11 |
let output = Command::new("cargo") |
| 12 |
.args([ |
| 13 |
"run", |
| 14 |
"--features", |
| 15 |
"cli", |
| 16 |
"--bin", |
| 17 |
"checksum", |
| 18 |
"--", |
| 19 |
"-a", |
| 20 |
"CRC-32/ISCSI", |
| 21 |
"-b", |
| 22 |
]) |
| 23 |
.output() |
| 24 |
.expect("Failed to execute command"); |
| 25 |
|
| 26 |
assert!( |
| 27 |
output.status.success(), |
| 28 |
"Command should succeed with -b flag" |
| 29 |
); |
| 30 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 31 |
assert!(stdout.contains("Algorithm: CRC-32/ISCSI")); |
| 32 |
assert!(stdout.contains("Throughput:")); |
| 33 |
assert!(stdout.contains("GiB/s")); |
| 34 |
} |
| 35 |
|
| 36 |
#[test] |
| 37 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 38 |
fn test_benchmark_with_size_parameter() { |
| 39 |
let output = Command::new("cargo") |
| 40 |
.args([ |
| 41 |
"run", |
| 42 |
"--features", |
| 43 |
"cli", |
| 44 |
"--bin", |
| 45 |
"checksum", |
| 46 |
"--", |
| 47 |
"-a", |
| 48 |
"CRC-32/ISCSI", |
| 49 |
"-b", |
| 50 |
"--size", |
| 51 |
"1024", |
| 52 |
]) |
| 53 |
.output() |
| 54 |
.expect("Failed to execute command"); |
| 55 |
|
| 56 |
assert!(output.status.success()); |
| 57 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 58 |
assert!(stdout.contains("Data Size: 1,024 bytes")); |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 63 |
fn test_benchmark_with_duration_parameter() { |
| 64 |
let output = Command::new("cargo") |
| 65 |
.args([ |
| 66 |
"run", |
| 67 |
"--features", |
| 68 |
"cli", |
| 69 |
"--bin", |
| 70 |
"checksum", |
| 71 |
"--", |
| 72 |
"-a", |
| 73 |
"CRC-32/ISCSI", |
| 74 |
"-b", |
| 75 |
"--duration", |
| 76 |
"1.0", |
| 77 |
]) |
| 78 |
.output() |
| 79 |
.expect("Failed to execute command"); |
| 80 |
|
| 81 |
assert!(output.status.success()); |
| 82 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 83 |
assert!(stdout.contains("Duration: 1.")); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 88 |
fn test_benchmark_invalid_size() { |
| 89 |
let output = Command::new("cargo") |
| 90 |
.args([ |
| 91 |
"run", |
| 92 |
"--features", |
| 93 |
"cli", |
| 94 |
"--bin", |
| 95 |
"checksum", |
| 96 |
"--", |
| 97 |
"-a", |
| 98 |
"CRC-32/ISCSI", |
| 99 |
"-b", |
| 100 |
"--size", |
| 101 |
"0", |
| 102 |
]) |
| 103 |
.output() |
| 104 |
.expect("Failed to execute command"); |
| 105 |
|
| 106 |
assert!(!output.status.success()); |
| 107 |
let stderr = String::from_utf8_lossy(&output.stderr); |
| 108 |
assert!(stderr.contains("Size must be greater than 0")); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 113 |
fn test_benchmark_invalid_duration() { |
| 114 |
let output = Command::new("cargo") |
| 115 |
.args([ |
| 116 |
"run", |
| 117 |
"--features", |
| 118 |
"cli", |
| 119 |
"--bin", |
| 120 |
"checksum", |
| 121 |
"--", |
| 122 |
"-a", |
| 123 |
"CRC-32/ISCSI", |
| 124 |
"-b", |
| 125 |
"--duration", |
| 126 |
"0", |
| 127 |
]) |
| 128 |
.output() |
| 129 |
.expect("Failed to execute command"); |
| 130 |
|
| 131 |
assert!(!output.status.success()); |
| 132 |
let stderr = String::from_utf8_lossy(&output.stderr); |
| 133 |
assert!(stderr.contains("Duration must be greater than 0")); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 138 |
fn test_benchmark_with_file_input() { |
| 139 |
// Create a temporary test file |
| 140 |
let test_file = "test_benchmark_file.txt"; |
| 141 |
fs::write(test_file, "Hello, benchmark world!").expect("Failed to create test file"); |
| 142 |
|
| 143 |
let output = Command::new("cargo") |
| 144 |
.args([ |
| 145 |
"run", |
| 146 |
"--features", |
| 147 |
"cli", |
| 148 |
"--bin", |
| 149 |
"checksum", |
| 150 |
"--", |
| 151 |
"-a", |
| 152 |
"CRC-32/ISCSI", |
| 153 |
"-b", |
| 154 |
"-f", |
| 155 |
test_file, |
| 156 |
"--duration", |
| 157 |
"0.5", |
| 158 |
]) |
| 159 |
.output() |
| 160 |
.expect("Failed to execute command"); |
| 161 |
|
| 162 |
// Clean up |
| 163 |
let _ = fs::remove_file(test_file); |
| 164 |
|
| 165 |
assert!(output.status.success()); |
| 166 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 167 |
assert!(stdout.contains("Data Size: 23 bytes")); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 172 |
fn test_benchmark_with_string_input() { |
| 173 |
let output = Command::new("cargo") |
| 174 |
.args([ |
| 175 |
"run", |
| 176 |
"--features", |
| 177 |
"cli", |
| 178 |
"--bin", |
| 179 |
"checksum", |
| 180 |
"--", |
| 181 |
"-a", |
| 182 |
"CRC-32/ISCSI", |
| 183 |
"-b", |
| 184 |
"-s", |
| 185 |
"test string", |
| 186 |
"--duration", |
| 187 |
"0.5", |
| 188 |
]) |
| 189 |
.output() |
| 190 |
.expect("Failed to execute command"); |
| 191 |
|
| 192 |
assert!(output.status.success()); |
| 193 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 194 |
assert!(stdout.contains("Data Size: 11 bytes")); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 199 |
fn test_benchmark_different_algorithms() { |
| 200 |
let algorithms = ["CRC-32/ISCSI", "CRC-64/NVME"]; |
| 201 |
|
| 202 |
for algorithm in &algorithms { |
| 203 |
let output = Command::new("cargo") |
| 204 |
.args([ |
| 205 |
"run", |
| 206 |
"--features", |
| 207 |
"cli", |
| 208 |
"--bin", |
| 209 |
"checksum", |
| 210 |
"--", |
| 211 |
"-a", |
| 212 |
algorithm, |
| 213 |
"-b", |
| 214 |
"--duration", |
| 215 |
"0.5", |
| 216 |
]) |
| 217 |
.output() |
| 218 |
.expect("Failed to execute command"); |
| 219 |
|
| 220 |
assert!( |
| 221 |
output.status.success(), |
| 222 |
"Algorithm {} should work", |
| 223 |
algorithm |
| 224 |
); |
| 225 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 226 |
assert!(stdout.contains(&format!("Algorithm: {}", algorithm))); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 232 |
fn test_benchmark_size_without_benchmark_flag() { |
| 233 |
let output = Command::new("cargo") |
| 234 |
.args([ |
| 235 |
"run", |
| 236 |
"--features", |
| 237 |
"cli", |
| 238 |
"--bin", |
| 239 |
"checksum", |
| 240 |
"--", |
| 241 |
"-a", |
| 242 |
"CRC-32/ISCSI", |
| 243 |
"--size", |
| 244 |
"1024", |
| 245 |
]) |
| 246 |
.output() |
| 247 |
.expect("Failed to execute command"); |
| 248 |
|
| 249 |
assert!(!output.status.success()); |
| 250 |
let stderr = String::from_utf8_lossy(&output.stderr); |
| 251 |
assert!(stderr.contains("--size and --duration can only be used with -b flag")); |
| 252 |
} |
| 253 |
|
| 254 |
#[test] |
| 255 |
#[cfg_attr(miri, ignore)] // Miri doesn't allow this due to isolation restrictions |
| 256 |
fn test_benchmark_nonexistent_file() { |
| 257 |
let output = Command::new("cargo") |
| 258 |
.args([ |
| 259 |
"run", |
| 260 |
"--features", |
| 261 |
"cli", |
| 262 |
"--bin", |
| 263 |
"checksum", |
| 264 |
"--", |
| 265 |
"-a", |
| 266 |
"CRC-32/ISCSI", |
| 267 |
"-b", |
| 268 |
"-f", |
| 269 |
"nonexistent_file.txt", |
| 270 |
]) |
| 271 |
.output() |
| 272 |
.expect("Failed to execute command"); |
| 273 |
|
| 274 |
assert!(!output.status.success()); |
| 275 |
let stderr = String::from_utf8_lossy(&output.stderr); |
| 276 |
assert!(stderr.contains("File not found")); |
| 277 |
} |