|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| - |
12 | 11 | //! Boolean logic
|
13 | 12 |
|
| 13 | +#[cfg(notest)] |
| 14 | +use cmp::{Eq, Ord, TotalOrd, Ordering}; |
14 | 15 | use option::{None, Option, Some};
|
15 | 16 | use from_str::FromStr;
|
16 | 17 |
|
17 |
| -#[cfg(notest)] use cmp; |
18 |
| - |
19 | 18 | /// Negation / inverse
|
20 | 19 | pub fn not(v: bool) -> bool { !v }
|
21 | 20 |
|
@@ -73,40 +72,86 @@ pub fn all_values(blk: &fn(v: bool)) {
|
73 | 72 | }
|
74 | 73 |
|
75 | 74 | /// converts truth value to an 8 bit byte
|
| 75 | +#[inline(always)] |
76 | 76 | pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
|
77 | 77 |
|
78 | 78 | #[cfg(notest)]
|
79 |
| -impl cmp::Eq for bool { |
| 79 | +impl Ord for bool { |
| 80 | + #[inline(always)] |
| 81 | + fn lt(&self, other: &bool) -> bool { to_bit(*self) < to_bit(*other) } |
| 82 | + #[inline(always)] |
| 83 | + fn le(&self, other: &bool) -> bool { to_bit(*self) <= to_bit(*other) } |
| 84 | + #[inline(always)] |
| 85 | + fn gt(&self, other: &bool) -> bool { to_bit(*self) > to_bit(*other) } |
| 86 | + #[inline(always)] |
| 87 | + fn ge(&self, other: &bool) -> bool { to_bit(*self) >= to_bit(*other) } |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(notest)] |
| 91 | +impl TotalOrd for bool { |
| 92 | + #[inline(always)] |
| 93 | + fn cmp(&self, other: &bool) -> Ordering { to_bit(*self).cmp(&to_bit(*other)) } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(notest)] |
| 97 | +impl Eq for bool { |
| 98 | + #[inline(always)] |
80 | 99 | fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
|
| 100 | + #[inline(always)] |
81 | 101 | fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
|
82 | 102 | }
|
83 | 103 |
|
84 |
| -#[test] |
85 |
| -pub fn test_bool_from_str() { |
86 |
| - use from_str::FromStr; |
| 104 | +#[cfg(test)] |
| 105 | +mod tests { |
| 106 | + use super::*; |
| 107 | + use prelude::*; |
87 | 108 |
|
88 |
| - do all_values |v| { |
89 |
| - assert!(Some(v) == FromStr::from_str(to_str(v))) |
| 109 | + #[test] |
| 110 | + fn test_bool_from_str() { |
| 111 | + use from_str::FromStr; |
| 112 | + |
| 113 | + do all_values |v| { |
| 114 | + assert!(Some(v) == FromStr::from_str(to_str(v))) |
| 115 | + } |
90 | 116 | }
|
91 |
| -} |
92 | 117 |
|
93 |
| -#[test] |
94 |
| -pub fn test_bool_to_str() { |
95 |
| - assert!(to_str(false) == ~"false"); |
96 |
| - assert!(to_str(true) == ~"true"); |
97 |
| -} |
| 118 | + #[test] |
| 119 | + fn test_bool_to_str() { |
| 120 | + assert!(to_str(false) == ~"false"); |
| 121 | + assert!(to_str(true) == ~"true"); |
| 122 | + } |
98 | 123 |
|
99 |
| -#[test] |
100 |
| -pub fn test_bool_to_bit() { |
101 |
| - do all_values |v| { |
102 |
| - assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 }); |
| 124 | + #[test] |
| 125 | + fn test_bool_to_bit() { |
| 126 | + do all_values |v| { |
| 127 | + assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 }); |
| 128 | + } |
103 | 129 | }
|
104 |
| -} |
105 | 130 |
|
106 |
| -// Local Variables: |
107 |
| -// mode: rust; |
108 |
| -// fill-column: 78; |
109 |
| -// indent-tabs-mode: nil |
110 |
| -// c-basic-offset: 4 |
111 |
| -// buffer-file-coding-system: utf-8-unix |
112 |
| -// End: |
| 131 | + #[test] |
| 132 | + fn test_bool_ord() { |
| 133 | + assert!(true > false); |
| 134 | + assert!(!(false > true)); |
| 135 | + |
| 136 | + assert!(false < true); |
| 137 | + assert!(!(true < false)); |
| 138 | + |
| 139 | + assert!(false <= false); |
| 140 | + assert!(false >= false); |
| 141 | + assert!(true <= true); |
| 142 | + assert!(true >= true); |
| 143 | + |
| 144 | + assert!(false <= true); |
| 145 | + assert!(!(false >= true)); |
| 146 | + assert!(true >= false); |
| 147 | + assert!(!(true <= false)); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_bool_totalord() { |
| 152 | + assert_eq!(true.cmp(&true), Equal); |
| 153 | + assert_eq!(false.cmp(&false), Equal); |
| 154 | + assert_eq!(true.cmp(&false), Greater); |
| 155 | + assert_eq!(false.cmp(&true), Less); |
| 156 | + } |
| 157 | +} |
0 commit comments