Skip to content

Commit

Permalink
Add unit tests. Fix bug in decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
U-NDC\nmryan authored and U-NDC\nmryan committed May 7, 2019
1 parent d5e2eab commit 1bcb906
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hew"
version = "0.2.3"
version = "0.2.4"
authors = ["nsmryan <nsmryan@gmail.com>"]
edition = "2018"
keywords = ["encoding", "decoding", "hexadecimal", "binary", "cli"]
Expand All @@ -14,3 +14,5 @@ clap = "2.32.0"

[profile.release]
debug = true
lto = true
opt-level = 3
84 changes: 81 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
extern crate clap;

use std::io::{Write, Read, BufReader, BufWriter};
use std::io::{Write, Read, BufReader, BufWriter, Cursor};
use std::fs::OpenOptions;
use std::path::Path;

use clap::{App, Arg, ArgMatches};


const NIBBLE_TO_HEX_UPPER: [char; 16] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'E', 'D', 'F'];
const NIBBLE_TO_HEX_LOWER: [char; 16] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'e', 'd', 'f'];
const NIBBLE_TO_HEX_UPPER: [char; 16] =
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];

const NIBBLE_TO_HEX_LOWER: [char; 16] =
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];

#[derive(Copy, Clone, PartialEq)]
enum LineWidth {
Expand Down Expand Up @@ -190,6 +193,64 @@ fn encode<R: Read, W: Write>(mut input: R, mut output: W) {
}
}

#[test]
fn test_encode_upper_case() {
// upper case
let input = "0123456789ABCDEF";
let mut output = Vec::with_capacity(input.len());

encode(input.as_bytes(), &mut output);
assert_eq!(output, vec!(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF));
}

#[test]
fn test_encode_lower_case() {
// lower case
let input = "0123456789abcdef";
let mut output = Vec::with_capacity(input.len());

encode(input.as_bytes(), &mut output);
assert_eq!(output, vec!(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF));
}

#[test]
fn test_encode_with_whitespace() {
let input = "01 \n23456789\r\nabcd ef";
let mut output = Vec::with_capacity(input.len());

encode(input.as_bytes(), &mut output);
assert_eq!(output, vec!(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF));
}

#[test]
fn test_encode_with_prefix() {
let input = "0x010x23450x6789\r\nabcd 0xef";
let mut output = Vec::with_capacity(input.len());

encode(input.as_bytes(), &mut output);
assert_eq!(output, vec!(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF));
}

#[test]
// NOTE this does not test an odd number of hex digits, just an odd number of bytes
fn test_encode_odd_number() {
let input = "0123456789abcd";
let mut output = Vec::with_capacity(input.len());

encode(input.as_bytes(), &mut output);
assert_eq!(output, vec!(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD));
}

#[test]
fn test_encode_empty() {
let input = "";
let mut output = Vec::with_capacity(input.len());

encode(input.as_bytes(), &mut output);
assert_eq!(output, vec!());
}


fn decode<R: Read, W: Write>(mut input: R,
mut output: W,
line_width: LineWidth,
Expand Down Expand Up @@ -220,6 +281,7 @@ fn decode<R: Read, W: Write>(mut input: R,
hex_str.clear();
hex_str.push(hex_pair[0]);
hex_str.push(hex_pair[1]);

output.write_all(&hex_str.as_bytes()).unwrap();

chars_in_line += 1;
Expand All @@ -232,6 +294,22 @@ fn decode<R: Read, W: Write>(mut input: R,
}
}

#[test]
fn test_decode_simple() {
let input = vec!(0xCD, 0xEF);
let mut output = Vec::with_capacity(100);

let line_width = LineWidth::Unlimited;
let word_width = WordWidth::Unlimited;
let case = Case::Upper;
let prefix = Prefixed::NoPrefix;
let sep = "";

decode(Cursor::new(input), &mut output, line_width, word_width, case, prefix, &sep);

assert_eq!(output.into_iter().map(|byte| byte as char).collect::<String>(),
"CDEF");
}

fn run(matches: ArgMatches) {
let filename = matches.value_of("FILE").unwrap().to_string();
Expand Down

0 comments on commit 1bcb906

Please sign in to comment.