From f2b02462a35e46bbfb46df359494094722f95736 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 15 Mar 2015 04:28:11 -0400 Subject: tests --- src/bin/1.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'src/bin') diff --git a/src/bin/1.rs b/src/bin/1.rs index 3378e65..f8c1408 100644 --- a/src/bin/1.rs +++ b/src/bin/1.rs @@ -1,9 +1,19 @@ extern crate "rustc-serialize" as serialize; -use std::io::prelude::*; +#[cfg(not(test))] use std::io::prelude::*; + use serialize::base64::{ToBase64,STANDARD}; use serialize::hex::FromHex; +fn hex_to_base64 (hex: &str) -> String { + let bytes = match hex.from_hex() { + Ok(b) => b, + Err(e) => panic!("{}", e), + }; + return bytes.to_base64(STANDARD); +} + +#[cfg(not(test))] fn main () { loop { let mut buf = [0; 6]; @@ -14,12 +24,13 @@ fn main () { if len == 0 { break; } - let hex = std::str::from_utf8(&buf[..len]).unwrap(); - let bytes = match hex.from_hex() { - Ok(b) => b, - Err(e) => panic!("{}", e), - }; - let base64 = bytes.to_base64(STANDARD); - print!("{}", base64); + print!("{}", hex_to_base64(std::str::from_utf8(&buf[..len]).unwrap())); } } + +#[test] +fn test_base64 () { + let hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; + let base64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; + assert_eq!(hex_to_base64(hex), base64); +} -- cgit v1.2.3-54-g00ecf