|
| 1 | +// Copyright 2020 Lyndon Brown |
| 2 | +// |
| 3 | +// This file is part of the PulseAudio Rust language binding. |
| 4 | +// |
| 5 | +// Licensed under the MIT license or the Apache license (version 2.0), at your option. You may not |
| 6 | +// copy, modify, or distribute this file except in compliance with said license. You can find copies |
| 7 | +// of these licenses either in the LICENSE-MIT and LICENSE-APACHE files, or alternatively at |
| 8 | +// <http://opensource.org/licenses/MIT> and <http://www.apache.org/licenses/LICENSE-2.0> |
| 9 | +// respectively. |
| 10 | +// |
| 11 | +// Portions of documentation are copied from the LGPL 2.1+ licensed PulseAudio C headers on a |
| 12 | +// fair-use basis, as discussed in the overall project readme (available in the git repository). |
| 13 | + |
| 14 | +//! Testing `MicroSeconds` operations |
| 15 | +//! |
| 16 | +//! (Covering stuff not already done in doc tests). |
| 17 | +
|
| 18 | +extern crate libpulse_binding as pulse; |
| 19 | + |
| 20 | +use std::time::Duration; |
| 21 | +use pulse::time::MicroSeconds; |
| 22 | + |
| 23 | +// Check basic addition / subtraction implementations |
| 24 | +#[test] |
| 25 | +fn math() { |
| 26 | + let mut a = MicroSeconds(30); |
| 27 | + let b = MicroSeconds(10); |
| 28 | + assert_eq!(a + b, MicroSeconds(40)); |
| 29 | + assert_eq!(a - b, MicroSeconds(20)); |
| 30 | + a += b; |
| 31 | + assert_eq!(a, MicroSeconds(40)); |
| 32 | + a -= b; |
| 33 | + assert_eq!(a, MicroSeconds(30)); |
| 34 | +} |
| 35 | + |
| 36 | +// Test that basic addition overflow panics |
| 37 | +#[test] |
| 38 | +#[should_panic] |
| 39 | +fn add_overflow() { |
| 40 | + let _ = MicroSeconds::MAX + MicroSeconds(10); |
| 41 | +} |
| 42 | + |
| 43 | +// Test that basic subtraction overflow panics |
| 44 | +#[test] |
| 45 | +#[should_panic] |
| 46 | +fn sub_overflow() { |
| 47 | + let _ = MicroSeconds(10) - MicroSeconds(20); |
| 48 | +} |
| 49 | + |
| 50 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 51 | +// Operations with `Duration` |
| 52 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 53 | + |
| 54 | +#[test] |
| 55 | +fn duration_math() { |
| 56 | + assert_eq!(MicroSeconds(300_000) + Duration::new(2, 0), MicroSeconds(2_300_000)); |
| 57 | + assert_eq!(MicroSeconds(3_500_000) - Duration::new(2, 0), MicroSeconds(1_500_000)); |
| 58 | + let mut x = MicroSeconds(300_000); |
| 59 | + x += Duration::new(2, 0); |
| 60 | + assert_eq!(x, MicroSeconds(2_300_000)); |
| 61 | + x -= Duration::new(2, 0); |
| 62 | + assert_eq!(x, MicroSeconds(300_000)); |
| 63 | + |
| 64 | + assert_eq!(Duration::new(2, 0) + MicroSeconds(300_000), Duration::new(2, 300_000_000)); |
| 65 | + assert_eq!(Duration::new(2, 0) - MicroSeconds(1_500_000), Duration::new(0, 500_000_000)); |
| 66 | + let mut x = Duration::new(2, 0); |
| 67 | + x += MicroSeconds(300_000); |
| 68 | + assert_eq!(x, Duration::new(2, 300_000_000)); |
| 69 | + x -= MicroSeconds(1_500_000); |
| 70 | + assert_eq!(x, Duration::new(0, 800_000_000)); |
| 71 | +} |
| 72 | + |
| 73 | +// Test that basic addition overflow panics |
| 74 | +#[test] |
| 75 | +#[should_panic] |
| 76 | +fn duration_add_overflow_to_micros() { |
| 77 | + let _ = MicroSeconds::MAX + Duration::new(2, 0); |
| 78 | +} |
| 79 | + |
| 80 | +// Test that basic addition overflow panics |
| 81 | +#[test] |
| 82 | +#[should_panic] |
| 83 | +fn duration_add_overflow_to_duration() { |
| 84 | + let _ = Duration::new(std::u64::MAX, 0) + MicroSeconds::MAX; |
| 85 | +} |
| 86 | + |
| 87 | +// Test that basic subtraction overflow panics |
| 88 | +#[test] |
| 89 | +#[should_panic] |
| 90 | +fn duration_sub_overflow_to_micros() { |
| 91 | + let _ = MicroSeconds(10) - Duration::new(1, 0); |
| 92 | +} |
| 93 | + |
| 94 | +// Test that basic subtraction overflow panics |
| 95 | +#[test] |
| 96 | +#[should_panic] |
| 97 | +fn duration_sub_overflow_to_duration() { |
| 98 | + let _ = Duration::new(1, 0) - MicroSeconds(2_000_000); |
| 99 | +} |
| 100 | + |
| 101 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 102 | +// Operations with primatives |
| 103 | +//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 104 | + |
| 105 | +#[test] |
| 106 | +fn primatives() { |
| 107 | + assert_eq!(MicroSeconds::SECOND * 2, MicroSeconds(2_000_000)); |
| 108 | + assert_eq!(2 * MicroSeconds::SECOND, MicroSeconds(2_000_000)); |
| 109 | + let mut x = MicroSeconds::SECOND; |
| 110 | + x *= 2; |
| 111 | + assert_eq!(x, MicroSeconds(2_000_000)); |
| 112 | + |
| 113 | + assert_eq!(MicroSeconds::SECOND / 2, MicroSeconds(500_000)); |
| 114 | + let mut x = MicroSeconds::SECOND; |
| 115 | + x /= 2; |
| 116 | + assert_eq!(x, MicroSeconds(500_000)); |
| 117 | + |
| 118 | + assert_eq!(MicroSeconds(200_000) % 7, MicroSeconds(3)); |
| 119 | + let mut x = MicroSeconds(200_000); |
| 120 | + x %= 7; |
| 121 | + assert_eq!(x, MicroSeconds(3)); |
| 122 | +} |
0 commit comments