Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bool: implement Ord and TotalOrd #5845

Merged
merged 1 commit into from
Apr 12, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 72 additions & 27 deletions src/libcore/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.


//! Boolean logic

#[cfg(notest)]
use cmp::{Eq, Ord, TotalOrd, Ordering};
use option::{None, Option, Some};
use from_str::FromStr;

#[cfg(notest)] use cmp;

/// Negation / inverse
pub fn not(v: bool) -> bool { !v }

Expand Down Expand Up @@ -73,40 +72,86 @@ pub fn all_values(blk: &fn(v: bool)) {
}

/// converts truth value to an 8 bit byte
#[inline(always)]
pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }

#[cfg(notest)]
impl cmp::Eq for bool {
impl Ord for bool {
#[inline(always)]
fn lt(&self, other: &bool) -> bool { to_bit(*self) < to_bit(*other) }
#[inline(always)]
fn le(&self, other: &bool) -> bool { to_bit(*self) <= to_bit(*other) }
#[inline(always)]
fn gt(&self, other: &bool) -> bool { to_bit(*self) > to_bit(*other) }
#[inline(always)]
fn ge(&self, other: &bool) -> bool { to_bit(*self) >= to_bit(*other) }
}

#[cfg(notest)]
impl TotalOrd for bool {
#[inline(always)]
fn cmp(&self, other: &bool) -> Ordering { to_bit(*self).cmp(&to_bit(*other)) }
}

#[cfg(notest)]
impl Eq for bool {
#[inline(always)]
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
#[inline(always)]
fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
}

#[test]
pub fn test_bool_from_str() {
use from_str::FromStr;
#[cfg(test)]
mod tests {
use super::*;
use prelude::*;

do all_values |v| {
assert!(Some(v) == FromStr::from_str(to_str(v)))
#[test]
fn test_bool_from_str() {
use from_str::FromStr;

do all_values |v| {
assert!(Some(v) == FromStr::from_str(to_str(v)))
}
}
}

#[test]
pub fn test_bool_to_str() {
assert!(to_str(false) == ~"false");
assert!(to_str(true) == ~"true");
}
#[test]
fn test_bool_to_str() {
assert!(to_str(false) == ~"false");
assert!(to_str(true) == ~"true");
}

#[test]
pub fn test_bool_to_bit() {
do all_values |v| {
assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
#[test]
fn test_bool_to_bit() {
do all_values |v| {
assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
}
}
}

// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
#[test]
fn test_bool_ord() {
assert!(true > false);
assert!(!(false > true));

assert!(false < true);
assert!(!(true < false));

assert!(false <= false);
assert!(false >= false);
assert!(true <= true);
assert!(true >= true);

assert!(false <= true);
assert!(!(false >= true));
assert!(true >= false);
assert!(!(true <= false));
}

#[test]
fn test_bool_totalord() {
assert_eq!(true.cmp(&true), Equal);
assert_eq!(false.cmp(&false), Equal);
assert_eq!(true.cmp(&false), Greater);
assert_eq!(false.cmp(&true), Less);
}
}