piper-plus 0.2.0

High-quality neural text-to-speech engine with 8-language support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#![cfg(feature = "onnx")]
//! Integration tests for the device module.
//!
//! Validates DeviceSelection constructors, from_str parsing (valid & invalid),
//! DeviceKind / DeviceSelection / DeviceInfo Display formatting,
//! enumerate_devices, is_device_available, auto(), recommended_device(),
//! and trait implementations (Clone, PartialEq, Hash).

use std::str::FromStr;

use piper_plus::device::*;

// =========================================================================
// DeviceSelection constructors
// =========================================================================

#[test]
fn test_constructor_cpu() {
    let sel = DeviceSelection::cpu();
    assert_eq!(sel.kind, DeviceKind::Cpu);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_constructor_cuda() {
    let sel = DeviceSelection::cuda(3);
    assert_eq!(sel.kind, DeviceKind::Cuda);
    assert_eq!(sel.device_id, 3);
}

#[test]
fn test_constructor_cuda_zero() {
    let sel = DeviceSelection::cuda(0);
    assert_eq!(sel.kind, DeviceKind::Cuda);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_constructor_coreml() {
    let sel = DeviceSelection::coreml();
    assert_eq!(sel.kind, DeviceKind::CoreML);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_constructor_directml() {
    let sel = DeviceSelection::directml(1);
    assert_eq!(sel.kind, DeviceKind::DirectML);
    assert_eq!(sel.device_id, 1);
}

#[test]
fn test_constructor_directml_zero() {
    let sel = DeviceSelection::directml(0);
    assert_eq!(sel.kind, DeviceKind::DirectML);
    assert_eq!(sel.device_id, 0);
}

// =========================================================================
// DeviceSelection::from_str — valid inputs
// =========================================================================

#[test]
fn test_from_str_cpu() {
    let sel = DeviceSelection::from_str("cpu").unwrap();
    assert_eq!(sel.kind, DeviceKind::Cpu);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_from_str_cuda_bare() {
    let sel = DeviceSelection::from_str("cuda").unwrap();
    assert_eq!(sel.kind, DeviceKind::Cuda);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_from_str_cuda_with_id_0() {
    let sel = DeviceSelection::from_str("cuda:0").unwrap();
    assert_eq!(sel.kind, DeviceKind::Cuda);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_from_str_cuda_with_id_1() {
    let sel = DeviceSelection::from_str("cuda:1").unwrap();
    assert_eq!(sel.kind, DeviceKind::Cuda);
    assert_eq!(sel.device_id, 1);
}

#[test]
fn test_from_str_coreml() {
    let sel = DeviceSelection::from_str("coreml").unwrap();
    assert_eq!(sel.kind, DeviceKind::CoreML);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_from_str_directml_bare() {
    let sel = DeviceSelection::from_str("directml").unwrap();
    assert_eq!(sel.kind, DeviceKind::DirectML);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_from_str_directml_with_id() {
    let sel = DeviceSelection::from_str("directml:2").unwrap();
    assert_eq!(sel.kind, DeviceKind::DirectML);
    assert_eq!(sel.device_id, 2);
}

#[test]
fn test_from_str_tensorrt_bare() {
    let sel = DeviceSelection::from_str("tensorrt").unwrap();
    assert_eq!(sel.kind, DeviceKind::TensorRT);
    assert_eq!(sel.device_id, 0);
}

#[test]
fn test_from_str_tensorrt_with_id() {
    let sel = DeviceSelection::from_str("tensorrt:1").unwrap();
    assert_eq!(sel.kind, DeviceKind::TensorRT);
    assert_eq!(sel.device_id, 1);
}

#[test]
fn test_from_str_auto_returns_valid_device() {
    let sel = DeviceSelection::from_str("auto").unwrap();
    // auto always returns a valid device; on any platform CPU is the fallback
    assert!(
        sel.kind == DeviceKind::Cpu
            || sel.kind == DeviceKind::Cuda
            || sel.kind == DeviceKind::CoreML
            || sel.kind == DeviceKind::DirectML
            || sel.kind == DeviceKind::TensorRT
    );
    assert!(sel.device_id >= 0);
}

// =========================================================================
// DeviceSelection::from_str — case insensitivity
// =========================================================================

#[test]
fn test_from_str_case_insensitive_uppercase() {
    let sel = DeviceSelection::from_str("CPU").unwrap();
    assert_eq!(sel.kind, DeviceKind::Cpu);

    let sel2 = DeviceSelection::from_str("CUDA").unwrap();
    assert_eq!(sel2.kind, DeviceKind::Cuda);
}

#[test]
fn test_from_str_case_insensitive_mixed_case() {
    let sel = DeviceSelection::from_str("Cuda:1").unwrap();
    assert_eq!(sel.kind, DeviceKind::Cuda);
    assert_eq!(sel.device_id, 1);

    let sel2 = DeviceSelection::from_str("CoreML").unwrap();
    assert_eq!(sel2.kind, DeviceKind::CoreML);

    let sel3 = DeviceSelection::from_str("DirectML:0").unwrap();
    assert_eq!(sel3.kind, DeviceKind::DirectML);
    assert_eq!(sel3.device_id, 0);
}

#[test]
fn test_from_str_case_insensitive_auto() {
    let sel = DeviceSelection::from_str("AUTO").unwrap();
    assert!(sel.device_id >= 0);
}

// =========================================================================
// DeviceSelection::from_str — whitespace handling
// =========================================================================

#[test]
fn test_from_str_leading_trailing_whitespace() {
    let sel = DeviceSelection::from_str("  cpu  ").unwrap();
    assert_eq!(sel.kind, DeviceKind::Cpu);
    assert_eq!(sel.device_id, 0);
}

// =========================================================================
// DeviceSelection::from_str — error cases
// =========================================================================

#[test]
fn test_from_str_empty_string_errors() {
    let result = DeviceSelection::from_str("");
    assert!(result.is_err(), "empty string should return an error");
}

#[test]
fn test_from_str_whitespace_only_errors() {
    let result = DeviceSelection::from_str("   ");
    assert!(
        result.is_err(),
        "whitespace-only string should return an error"
    );
}

#[test]
fn test_from_str_unknown_device_errors() {
    let result = DeviceSelection::from_str("gpu");
    assert!(result.is_err(), "\"gpu\" is not a valid device kind");
}

#[test]
fn test_from_str_garbage_errors() {
    let result = DeviceSelection::from_str("!@#$%");
    assert!(result.is_err(), "garbage input should return an error");
}

#[test]
fn test_from_str_invalid_device_id_non_numeric() {
    let result = DeviceSelection::from_str("cuda:abc");
    assert!(
        result.is_err(),
        "non-numeric device id should return an error"
    );
}

#[test]
fn test_from_str_invalid_device_id_float() {
    let result = DeviceSelection::from_str("cuda:1.5");
    assert!(result.is_err(), "float device id should return an error");
}

#[test]
fn test_from_str_completely_unknown() {
    let result = DeviceSelection::from_str("vulkan");
    assert!(
        result.is_err(),
        "unknown device kind should return an error"
    );
}

// =========================================================================
// DeviceKind — Display formatting
// =========================================================================

#[test]
fn test_device_kind_display_all_variants() {
    assert_eq!(DeviceKind::Cpu.to_string(), "cpu");
    assert_eq!(DeviceKind::Cuda.to_string(), "cuda");
    assert_eq!(DeviceKind::CoreML.to_string(), "coreml");
    assert_eq!(DeviceKind::DirectML.to_string(), "directml");
    assert_eq!(DeviceKind::TensorRT.to_string(), "tensorrt");
}

// =========================================================================
// DeviceSelection — Display formatting
// =========================================================================

#[test]
fn test_device_selection_display_cpu_no_id() {
    // CPU display should not include ":0"
    let sel = DeviceSelection::cpu();
    assert_eq!(sel.to_string(), "cpu");
}

#[test]
fn test_device_selection_display_cuda_includes_id() {
    let sel = DeviceSelection::cuda(0);
    assert_eq!(sel.to_string(), "cuda:0");

    let sel1 = DeviceSelection::cuda(1);
    assert_eq!(sel1.to_string(), "cuda:1");
}

#[test]
fn test_device_selection_display_coreml() {
    // CoreML has no device_id, but DeviceSelection stores device_id = 0
    // Display should show "coreml:0" since kind != Cpu
    let sel = DeviceSelection::coreml();
    let display = sel.to_string();
    assert!(
        display.contains("coreml"),
        "CoreML display should contain 'coreml', got: {}",
        display
    );
}

#[test]
fn test_device_selection_display_directml() {
    let sel = DeviceSelection::directml(2);
    assert_eq!(sel.to_string(), "directml:2");
}

// =========================================================================
// DeviceInfo — Display formatting
// =========================================================================

#[test]
fn test_device_info_display_cpu() {
    let info = DeviceInfo {
        kind: DeviceKind::Cpu,
        device_id: 0,
        name: "CPU".to_string(),
        available: true,
        memory_bytes: None,
    };
    assert_eq!(info.to_string(), "cpu (CPU) [available]");
}

#[test]
fn test_device_info_display_cuda_with_memory() {
    let info = DeviceInfo {
        kind: DeviceKind::Cuda,
        device_id: 0,
        name: "NVIDIA GeForce RTX 3090".to_string(),
        available: true,
        memory_bytes: Some(24 * 1024 * 1024 * 1024), // 24 GB
    };
    assert_eq!(
        info.to_string(),
        "cuda:0 (NVIDIA GeForce RTX 3090, 24GB) [available]"
    );
}

#[test]
fn test_device_info_display_unavailable() {
    let info = DeviceInfo {
        kind: DeviceKind::Cuda,
        device_id: 1,
        name: "CUDA Device 1".to_string(),
        available: false,
        memory_bytes: None,
    };
    assert_eq!(info.to_string(), "cuda:1 (CUDA Device 1) [unavailable]");
}

#[test]
fn test_device_info_display_no_memory() {
    let info = DeviceInfo {
        kind: DeviceKind::DirectML,
        device_id: 0,
        name: "DirectML Device 0".to_string(),
        available: true,
        memory_bytes: None,
    };
    let s = info.to_string();
    assert!(
        !s.contains("GB"),
        "no memory_bytes should omit the GB suffix, got: {}",
        s
    );
    assert!(s.contains("[available]"));
}

// =========================================================================
// enumerate_devices
// =========================================================================

#[test]
fn test_enumerate_devices_non_empty() {
    let devices = enumerate_devices();
    assert!(
        !devices.is_empty(),
        "enumerate_devices() should always return at least one device"
    );
}

#[test]
fn test_enumerate_devices_includes_cpu() {
    let devices = enumerate_devices();
    let has_cpu = devices.iter().any(|d| d.kind == DeviceKind::Cpu);
    assert!(
        has_cpu,
        "enumerate_devices() must always include a CPU entry"
    );
}

#[test]
fn test_enumerate_devices_cpu_is_available() {
    let devices = enumerate_devices();
    let cpu = devices
        .iter()
        .find(|d| d.kind == DeviceKind::Cpu)
        .expect("CPU device must be present");
    assert!(
        cpu.available,
        "CPU device must always be marked as available"
    );
    assert_eq!(cpu.device_id, 0, "CPU device_id should be 0");
}

#[test]
fn test_enumerate_devices_all_have_names() {
    let devices = enumerate_devices();
    for d in devices {
        assert!(
            !d.name.is_empty(),
            "device {:?} should have a non-empty name",
            d.kind
        );
    }
}

#[test]
fn test_enumerate_devices_cpu_first() {
    let devices = enumerate_devices();
    assert_eq!(
        devices[0].kind,
        DeviceKind::Cpu,
        "CPU should be the first device in the enumeration"
    );
}

// =========================================================================
// is_device_available
// =========================================================================

#[test]
fn test_cpu_always_available() {
    assert!(
        is_device_available(&DeviceKind::Cpu),
        "CPU must always be available"
    );
}

#[test]
fn test_is_device_available_consistency_with_enumerate() {
    // Every device reported by enumerate_devices as available should also
    // return true from is_device_available.
    let devices = enumerate_devices();
    for d in devices {
        if d.available {
            assert!(
                is_device_available(&d.kind),
                "enumerate_devices says {:?} is available, but is_device_available disagrees",
                d.kind
            );
        }
    }
}

// =========================================================================
// auto() and recommended_device() — must not panic
// =========================================================================

#[test]
fn test_auto_returns_valid_device() {
    let sel = DeviceSelection::auto();
    assert!(
        sel.kind == DeviceKind::Cpu
            || sel.kind == DeviceKind::Cuda
            || sel.kind == DeviceKind::CoreML
            || sel.kind == DeviceKind::DirectML
            || sel.kind == DeviceKind::TensorRT
    );
    assert!(sel.device_id >= 0);
}

#[test]
fn test_auto_selects_available_device() {
    let sel = DeviceSelection::auto();
    assert!(
        is_device_available(&sel.kind),
        "auto() should only select a device that is available"
    );
}

#[test]
fn test_recommended_device_returns_valid() {
    let sel = recommended_device();
    assert!(
        sel.kind == DeviceKind::Cpu
            || sel.kind == DeviceKind::Cuda
            || sel.kind == DeviceKind::CoreML
            || sel.kind == DeviceKind::DirectML
            || sel.kind == DeviceKind::TensorRT
    );
    assert!(sel.device_id >= 0);
}

#[test]
fn test_recommended_device_matches_auto() {
    let auto_sel = DeviceSelection::auto();
    let rec_sel = recommended_device();
    assert_eq!(
        auto_sel.kind, rec_sel.kind,
        "recommended_device() and auto() should select the same device kind"
    );
    assert_eq!(
        auto_sel.device_id, rec_sel.device_id,
        "recommended_device() and auto() should select the same device id"
    );
}

// =========================================================================
// DeviceKind — PartialEq, Clone, Hash
// =========================================================================

#[test]
fn test_device_kind_eq() {
    assert_eq!(DeviceKind::Cpu, DeviceKind::Cpu);
    assert_eq!(DeviceKind::Cuda, DeviceKind::Cuda);
    assert_eq!(DeviceKind::CoreML, DeviceKind::CoreML);
    assert_eq!(DeviceKind::DirectML, DeviceKind::DirectML);
    assert_eq!(DeviceKind::TensorRT, DeviceKind::TensorRT);
}

#[test]
fn test_device_kind_ne() {
    assert_ne!(DeviceKind::Cpu, DeviceKind::Cuda);
    assert_ne!(DeviceKind::Cuda, DeviceKind::CoreML);
    assert_ne!(DeviceKind::CoreML, DeviceKind::DirectML);
    assert_ne!(DeviceKind::DirectML, DeviceKind::TensorRT);
}

#[test]
fn test_device_kind_clone() {
    let original = DeviceKind::Cuda;
    let cloned = original.clone();
    assert_eq!(original, cloned);
}

#[test]
fn test_device_kind_hash_dedup() {
    use std::collections::HashSet;
    let mut set = HashSet::new();
    set.insert(DeviceKind::Cpu);
    set.insert(DeviceKind::Cuda);
    set.insert(DeviceKind::Cpu); // duplicate
    set.insert(DeviceKind::CoreML);
    assert_eq!(set.len(), 3);
    assert!(set.contains(&DeviceKind::Cpu));
    assert!(set.contains(&DeviceKind::Cuda));
    assert!(set.contains(&DeviceKind::CoreML));
    assert!(!set.contains(&DeviceKind::DirectML));
}

// =========================================================================
// Round-trip: from_str -> Display -> from_str
// =========================================================================

#[test]
fn test_roundtrip_cpu() {
    let original = DeviceSelection::from_str("cpu").unwrap();
    let display = original.to_string();
    let reparsed = DeviceSelection::from_str(&display).unwrap();
    assert_eq!(reparsed.kind, original.kind);
    assert_eq!(reparsed.device_id, original.device_id);
}

#[test]
fn test_roundtrip_cuda_0() {
    let original = DeviceSelection::from_str("cuda:0").unwrap();
    let display = original.to_string();
    let reparsed = DeviceSelection::from_str(&display).unwrap();
    assert_eq!(reparsed.kind, DeviceKind::Cuda);
    assert_eq!(reparsed.device_id, 0);
}

#[test]
fn test_roundtrip_cuda_1() {
    let original = DeviceSelection::from_str("cuda:1").unwrap();
    let display = original.to_string();
    let reparsed = DeviceSelection::from_str(&display).unwrap();
    assert_eq!(reparsed.kind, DeviceKind::Cuda);
    assert_eq!(reparsed.device_id, 1);
}

#[test]
fn test_roundtrip_directml_2() {
    let original = DeviceSelection::from_str("directml:2").unwrap();
    let display = original.to_string();
    let reparsed = DeviceSelection::from_str(&display).unwrap();
    assert_eq!(reparsed.kind, DeviceKind::DirectML);
    assert_eq!(reparsed.device_id, 2);
}

#[test]
fn test_roundtrip_tensorrt() {
    let original = DeviceSelection::from_str("tensorrt:0").unwrap();
    let display = original.to_string();
    let reparsed = DeviceSelection::from_str(&display).unwrap();
    assert_eq!(reparsed.kind, DeviceKind::TensorRT);
    assert_eq!(reparsed.device_id, 0);
}