* Vec::reserve_additional -> Vec::reserve
* x[] -> &*x (as the former syntax is going away)
* XDeref -> Deref (as &T:Deref<T> is implemented)
* CloneableVector is gone? Move trait methods back into impl for now.
This commit is contained in:
kennytm 2014-11-11 08:36:43 +08:00
parent beace646d0
commit acddb36406
5 changed files with 61 additions and 76 deletions

View file

@ -77,9 +77,9 @@ impl Bits {
} }
/// Reserves `n` extra bits of space for pushing. /// Reserves `n` extra bits of space for pushing.
fn reserve_additional(&mut self, n: uint) { fn reserve(&mut self, n: uint) {
let extra_bytes = (n + (8 - self.bit_offset) % 8) / 8; let extra_bytes = (n + (8 - self.bit_offset) % 8) / 8;
self.data.reserve_additional(extra_bytes); self.data.reserve(extra_bytes);
} }
/// Convert the bits into a bytes vector. /// Convert the bits into a bytes vector.
@ -231,7 +231,7 @@ impl Bits {
/// If the designator is outside of the expected range, this method will /// If the designator is outside of the expected range, this method will
/// return `Err(InvalidECIDesignator)`. /// return `Err(InvalidECIDesignator)`.
pub fn push_eci_designator(&mut self, eci_designator: u32) -> QrResult<()> { pub fn push_eci_designator(&mut self, eci_designator: u32) -> QrResult<()> {
self.reserve_additional(12); // assume the common case that eci_designator <= 127. self.reserve(12); // assume the common case that eci_designator <= 127.
try!(self.push_mode_indicator(Eci)); try!(self.push_mode_indicator(Eci));
match eci_designator { match eci_designator {
0...127 => { 0...127 => {
@ -301,7 +301,7 @@ mod eci_tests {
impl Bits { impl Bits {
fn push_header(&mut self, mode: Mode, raw_data_len: uint) -> QrResult<()> { fn push_header(&mut self, mode: Mode, raw_data_len: uint) -> QrResult<()> {
let length_bits = mode.length_bits_count(self.version); let length_bits = mode.length_bits_count(self.version);
self.reserve_additional(length_bits + 4 + mode.data_bits_count(raw_data_len)); self.reserve(length_bits + 4 + mode.data_bits_count(raw_data_len));
try!(self.push_mode_indicator(Data(mode))); try!(self.push_mode_indicator(Data(mode)));
try!(self.push_number_checked(length_bits, raw_data_len)); try!(self.push_number_checked(length_bits, raw_data_len));
Ok(()) Ok(())
@ -314,7 +314,7 @@ impl Bits {
pub fn push_numeric_data(&mut self, data: &[u8]) -> QrResult<()> { pub fn push_numeric_data(&mut self, data: &[u8]) -> QrResult<()> {
try!(self.push_header(Numeric, data.len())); try!(self.push_header(Numeric, data.len()));
for chunk in data.chunks(3) { for chunk in data.chunks(3) {
let number = chunk.iter().map(|b| (b - b'0') as u16).fold(0, |a, b| a*10 + b); let number = chunk.iter().map(|b| (*b - b'0') as u16).fold(0, |a, b| a*10 + b);
let length = chunk.len() * 3 + 1; let length = chunk.len() * 3 + 1;
self.push_number(length, number); self.push_number(length, number);
} }
@ -666,7 +666,7 @@ impl Bits {
}; };
let cur_length = self.len(); let cur_length = self.len();
let data_length = try!(self.version.fetch(ec_level, DATA_LENGTHS[])); let data_length = try!(self.version.fetch(ec_level, &DATA_LENGTHS));
if cur_length > data_length { if cur_length > data_length {
return Err(DataTooLong); return Err(DataTooLong);
} }
@ -829,12 +829,12 @@ pub fn encode_auto(data: &[u8], ec_level: ErrorCorrectionLevel) -> QrResult<Bits
let segments = Parser::new(data).collect::<Vec<Segment>>(); let segments = Parser::new(data).collect::<Vec<Segment>>();
for version in [Version(9), Version(26), Version(40)].iter() { for version in [Version(9), Version(26), Version(40)].iter() {
let opt_segments = Optimizer::new(segments.iter().map(|s| *s), *version).collect::<Vec<Segment>>(); let opt_segments = Optimizer::new(segments.iter().map(|s| *s), *version).collect::<Vec<Segment>>();
let total_len = total_encoded_len(opt_segments[], *version); let total_len = total_encoded_len(&*opt_segments, *version);
let data_capacity = version.fetch(ec_level, DATA_LENGTHS[]).unwrap(); let data_capacity = version.fetch(ec_level, &DATA_LENGTHS).unwrap();
if total_len <= data_capacity { if total_len <= data_capacity {
let min_version = find_min_version(total_len, ec_level); let min_version = find_min_version(total_len, ec_level);
let mut bits = Bits::new(min_version); let mut bits = Bits::new(min_version);
bits.reserve_additional(total_len); bits.reserve(total_len);
try!(bits.push_segments(data, opt_segments.into_iter())); try!(bits.push_segments(data, opt_segments.into_iter()));
try!(bits.push_terminator(ec_level)); try!(bits.push_terminator(ec_level));
return Ok(bits); return Ok(bits);

View file

@ -190,7 +190,7 @@ mod basic_canvas_tests {
} }
} }
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
?????????????????????\n\ ?????????????????????\n\
?????????????????????\n\ ?????????????????????\n\
?????????????????????\n\ ?????????????????????\n\
@ -263,7 +263,7 @@ mod finder_pattern_tests {
fn test_qr() { fn test_qr() {
let mut c = Canvas::new(Version(1), L); let mut c = Canvas::new(Version(1), L);
c.draw_finder_patterns(); c.draw_finder_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.?????.#######\n\ #######.?????.#######\n\
#.....#.?????.#.....#\n\ #.....#.?????.#.....#\n\
#.###.#.?????.#.###.#\n\ #.###.#.?????.#.###.#\n\
@ -291,7 +291,7 @@ mod finder_pattern_tests {
fn test_micro_qr() { fn test_micro_qr() {
let mut c = Canvas::new(MicroVersion(1), L); let mut c = Canvas::new(MicroVersion(1), L);
c.draw_finder_patterns(); c.draw_finder_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.???\n\ #######.???\n\
#.....#.???\n\ #.....#.???\n\
#.###.#.???\n\ #.###.#.???\n\
@ -356,7 +356,7 @@ mod alignment_pattern_tests {
let mut c = Canvas::new(Version(1), L); let mut c = Canvas::new(Version(1), L);
c.draw_finder_patterns(); c.draw_finder_patterns();
c.draw_alignment_patterns(); c.draw_alignment_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.?????.#######\n\ #######.?????.#######\n\
#.....#.?????.#.....#\n\ #.....#.?????.#.....#\n\
#.###.#.?????.#.###.#\n\ #.###.#.?????.#.###.#\n\
@ -385,7 +385,7 @@ mod alignment_pattern_tests {
let mut c = Canvas::new(Version(3), L); let mut c = Canvas::new(Version(3), L);
c.draw_finder_patterns(); c.draw_finder_patterns();
c.draw_alignment_patterns(); c.draw_alignment_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.?????????????.#######\n\ #######.?????????????.#######\n\
#.....#.?????????????.#.....#\n\ #.....#.?????????????.#.....#\n\
#.###.#.?????????????.#.###.#\n\ #.###.#.?????????????.#.###.#\n\
@ -422,7 +422,7 @@ mod alignment_pattern_tests {
let mut c = Canvas::new(Version(7), L); let mut c = Canvas::new(Version(7), L);
c.draw_finder_patterns(); c.draw_finder_patterns();
c.draw_alignment_patterns(); c.draw_alignment_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.?????????????????????????????.#######\n\ #######.?????????????????????????????.#######\n\
#.....#.?????????????????????????????.#.....#\n\ #.....#.?????????????????????????????.#.....#\n\
#.###.#.?????????????????????????????.#.###.#\n\ #.###.#.?????????????????????????????.#.###.#\n\
@ -567,7 +567,7 @@ mod timing_pattern_tests {
fn test_draw_timing_patterns_qr() { fn test_draw_timing_patterns_qr() {
let mut c = Canvas::new(Version(1), L); let mut c = Canvas::new(Version(1), L);
c.draw_timing_patterns(); c.draw_timing_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
?????????????????????\n\ ?????????????????????\n\
?????????????????????\n\ ?????????????????????\n\
?????????????????????\n\ ?????????????????????\n\
@ -595,7 +595,7 @@ mod timing_pattern_tests {
fn test_draw_timing_patterns_micro_qr() { fn test_draw_timing_patterns_micro_qr() {
let mut c = Canvas::new(MicroVersion(1), L); let mut c = Canvas::new(MicroVersion(1), L);
c.draw_timing_patterns(); c.draw_timing_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
????????#.#\n\ ????????#.#\n\
???????????\n\ ???????????\n\
???????????\n\ ???????????\n\
@ -673,7 +673,7 @@ mod draw_version_info_tests {
fn test_draw_number() { fn test_draw_number() {
let mut c = Canvas::new(MicroVersion(1), L); let mut c = Canvas::new(MicroVersion(1), L);
c.draw_number(0b10101101u8, Dark, Light, [(0,0), (0,-1), (-2,-2), (-2,0)]); c.draw_number(0b10101101u8, Dark, Light, [(0,0), (0,-1), (-2,-2), (-2,0)]);
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#????????.?\n\ #????????.?\n\
???????????\n\ ???????????\n\
???????????\n\ ???????????\n\
@ -691,7 +691,7 @@ mod draw_version_info_tests {
fn test_draw_version_info_1() { fn test_draw_version_info_1() {
let mut c = Canvas::new(Version(1), L); let mut c = Canvas::new(Version(1), L);
c.draw_version_info_patterns(); c.draw_version_info_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
?????????????????????\n\ ?????????????????????\n\
?????????????????????\n\ ?????????????????????\n\
?????????????????????\n\ ?????????????????????\n\
@ -720,7 +720,7 @@ mod draw_version_info_tests {
let mut c = Canvas::new(Version(7), L); let mut c = Canvas::new(Version(7), L);
c.draw_version_info_patterns(); c.draw_version_info_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
??????????????????????????????????..#????????\n\ ??????????????????????????????????..#????????\n\
??????????????????????????????????.#.????????\n\ ??????????????????????????????????.#.????????\n\
??????????????????????????????????.#.????????\n\ ??????????????????????????????????.#.????????\n\
@ -772,7 +772,7 @@ mod draw_version_info_tests {
fn test_draw_reserved_format_info_patterns_qr() { fn test_draw_reserved_format_info_patterns_qr() {
let mut c = Canvas::new(Version(1), L); let mut c = Canvas::new(Version(1), L);
c.draw_reserved_format_info_patterns(); c.draw_reserved_format_info_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
????????.????????????\n\ ????????.????????????\n\
????????.????????????\n\ ????????.????????????\n\
????????.????????????\n\ ????????.????????????\n\
@ -800,7 +800,7 @@ mod draw_version_info_tests {
fn test_draw_reserved_format_info_patterns_micro_qr() { fn test_draw_reserved_format_info_patterns_micro_qr() {
let mut c = Canvas::new(MicroVersion(1), L); let mut c = Canvas::new(MicroVersion(1), L);
c.draw_reserved_format_info_patterns(); c.draw_reserved_format_info_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
???????????\n\ ???????????\n\
????????.??\n\ ????????.??\n\
????????.??\n\ ????????.??\n\
@ -886,7 +886,7 @@ mod all_functional_patterns_tests {
fn test_all_functional_patterns_qr() { fn test_all_functional_patterns_qr() {
let mut c = Canvas::new(Version(2), L); let mut c = Canvas::new(Version(2), L);
c.draw_all_functional_patterns(); c.draw_all_functional_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######..????????.#######\n\ #######..????????.#######\n\
#.....#..????????.#.....#\n\ #.....#..????????.#.....#\n\
#.###.#..????????.#.###.#\n\ #.###.#..????????.#.###.#\n\
@ -918,7 +918,7 @@ mod all_functional_patterns_tests {
fn test_all_functional_patterns_micro_qr() { fn test_all_functional_patterns_micro_qr() {
let mut c = Canvas::new(MicroVersion(1), L); let mut c = Canvas::new(MicroVersion(1), L);
c.draw_all_functional_patterns(); c.draw_all_functional_patterns();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.#.#\n\ #######.#.#\n\
#.....#..??\n\ #.....#..??\n\
#.###.#..??\n\ #.###.#..??\n\
@ -1212,7 +1212,7 @@ mod draw_codewords_test {
let mut c = Canvas::new(MicroVersion(1), L); let mut c = Canvas::new(MicroVersion(1), L);
c.draw_all_functional_patterns(); c.draw_all_functional_patterns();
c.draw_data(b"\x6e\x5d\xe2", b"\x2b\x63"); c.draw_data(b"\x6e\x5d\xe2", b"\x2b\x63");
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.#.#\n\ #######.#.#\n\
#.....#..-*\n\ #.....#..-*\n\
#.###.#..**\n\ #.###.#..**\n\
@ -1231,7 +1231,7 @@ mod draw_codewords_test {
let mut c = Canvas::new(Version(2), L); let mut c = Canvas::new(Version(2), L);
c.draw_all_functional_patterns(); c.draw_all_functional_patterns();
c.draw_data(b"\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$", b""); c.draw_data(b"\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$\x92I$", b"");
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######..--*---*-.#######\n\ #######..--*---*-.#######\n\
#.....#..-*-*-*-*.#.....#\n\ #.....#..-*-*-*-*.#.....#\n\
#.###.#..*---*---.#.###.#\n\ #.###.#..*---*---.#.###.#\n\
@ -1380,7 +1380,7 @@ mod mask_tests {
c.draw_all_functional_patterns(); c.draw_all_functional_patterns();
c.apply_mask(Checkerboard); c.apply_mask(Checkerboard);
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######...#.#.#######\n\ #######...#.#.#######\n\
#.....#..#.#..#.....#\n\ #.....#..#.#..#.....#\n\
#.###.#.#.#.#.#.###.#\n\ #.###.#.#.#.#.#.###.#\n\
@ -1408,7 +1408,7 @@ mod mask_tests {
fn test_draw_format_info_patterns_qr() { fn test_draw_format_info_patterns_qr() {
let mut c = Canvas::new(Version(1), L); let mut c = Canvas::new(Version(1), L);
c.draw_format_info_patterns(LargeCheckerboard); c.draw_format_info_patterns(LargeCheckerboard);
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
????????#????????????\n\ ????????#????????????\n\
????????#????????????\n\ ????????#????????????\n\
????????#????????????\n\ ????????#????????????\n\
@ -1436,7 +1436,7 @@ mod mask_tests {
fn test_draw_format_info_patterns_micro_qr() { fn test_draw_format_info_patterns_micro_qr() {
let mut c = Canvas::new(MicroVersion(2), L); let mut c = Canvas::new(MicroVersion(2), L);
c.draw_format_info_patterns(LargeCheckerboard); c.draw_format_info_patterns(LargeCheckerboard);
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
?????????????\n\ ?????????????\n\
????????#????\n\ ????????#????\n\
????????.????\n\ ????????.????\n\
@ -1632,7 +1632,7 @@ mod penalty_tests {
#[test] #[test]
fn check_penalty_canvas() { fn check_penalty_canvas() {
let c = create_test_canvas(); let c = create_test_canvas();
assert_eq!(c.to_debug_str()[], "\n\ assert_eq!(&*c.to_debug_str(), "\n\
#######.##....#######\n\ #######.##....#######\n\
#.....#.#..#..#.....#\n\ #.....#.#..#..#.....#\n\
#.###.#.#..##.#.###.#\n\ #.###.#.#..##.#.###.#\n\

View file

@ -43,19 +43,19 @@ mod ec_tests {
#[test] #[test]
fn test_poly_mod_1() { fn test_poly_mod_1() {
let res = create_error_correction_code(b" [\x0bx\xd1r\xdcMC@\xec\x11\xec\x11\xec\x11", 10); let res = create_error_correction_code(b" [\x0bx\xd1r\xdcMC@\xec\x11\xec\x11\xec\x11", 10);
assert_eq!(res[], b"\xc4#'w\xeb\xd7\xe7\xe2]\x17"); assert_eq!(&*res, b"\xc4#'w\xeb\xd7\xe7\xe2]\x17");
} }
#[test] #[test]
fn test_poly_mod_2() { fn test_poly_mod_2() {
let res = create_error_correction_code(b" [\x0bx\xd1r\xdcMC@\xec\x11\xec", 13); let res = create_error_correction_code(b" [\x0bx\xd1r\xdcMC@\xec\x11\xec", 13);
assert_eq!(res[], b"\xa8H\x16R\xd96\x9c\x00.\x0f\xb4z\x10"); assert_eq!(&*res, b"\xa8H\x16R\xd96\x9c\x00.\x0f\xb4z\x10");
} }
#[test] #[test]
fn test_poly_mod_3() { fn test_poly_mod_3() {
let res = create_error_correction_code(b"CUF\x86W&U\xc2w2\x06\x12\x06g&", 18); let res = create_error_correction_code(b"CUF\x86W&U\xc2w2\x06\x12\x06g&", 18);
assert_eq!(res[], b"\xd5\xc7\x0b-s\xf7\xf1\xdf\xe5\xf8\x9au\x9aoV\xa1o'"); assert_eq!(&*res, b"\xd5\xc7\x0b-s\xf7\xf1\xdf\xe5\xf8\x9au\x9aoV\xa1o'");
} }
} }
@ -63,23 +63,6 @@ mod ec_tests {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
//{{{ Interleave support //{{{ Interleave support
// TODO Make &[T] implement Deref<[T]>
trait XDeref<Sized? T> {
fn deref2(&self) -> &T;
}
impl<T> XDeref<[T]> for Vec<T> {
fn deref2(&self) -> &[T] {
self.deref()
}
}
impl<'a, Sized? T> XDeref<T> for &'a T {
fn deref2(&self) -> &T {
*self
}
}
/// This method interleaves a vector of slices into a single vector. /// This method interleaves a vector of slices into a single vector.
/// ///
/// It will first insert all the first elements of the slices in `blocks`, then /// It will first insert all the first elements of the slices in `blocks`, then
@ -87,16 +70,13 @@ impl<'a, Sized? T> XDeref<T> for &'a T {
/// ///
/// The longest slice must be at the last of `blocks`, and `blocks` must not be /// The longest slice must be at the last of `blocks`, and `blocks` must not be
/// empty. /// empty.
fn interleave<T: Copy, V>(blocks: &Vec<V>) -> Vec<T> fn interleave<T: Copy, V: Deref<[T]>>(blocks: &Vec<V>) -> Vec<T> {
where V: XDeref<[T]> let last_block_len = blocks.last().unwrap().len();
{
let last_block_len = blocks.last().unwrap().deref2().len();
let mut res = Vec::with_capacity(last_block_len * blocks.len()); let mut res = Vec::with_capacity(last_block_len * blocks.len());
for i in range(0, last_block_len) { for i in range(0, last_block_len) {
for t in blocks.iter() { for t in blocks.iter() {
let tref = t.deref2(); if i < t.len() {
if i < tref.len() { res.push(t[i]);
res.push(tref[i]);
} }
} }
} }
@ -106,7 +86,7 @@ fn interleave<T: Copy, V>(blocks: &Vec<V>) -> Vec<T>
#[test] #[test]
fn test_interleave() { fn test_interleave() {
let res = interleave(&vec![b"1234", b"5678", b"abcdef", b"ghijkl"]); let res = interleave(&vec![b"1234", b"5678", b"abcdef", b"ghijkl"]);
assert_eq!(res[], b"15ag26bh37ci48djekfl"); assert_eq!(&*res, b"15ag26bh37ci48djekfl");
} }
//}}} //}}}
@ -119,7 +99,7 @@ pub fn construct_codewords(rawbits: &[u8],
version: QrVersion, version: QrVersion,
ec_level: ErrorCorrectionLevel) -> QrResult<(Vec<u8>, Vec<u8>)> { ec_level: ErrorCorrectionLevel) -> QrResult<(Vec<u8>, Vec<u8>)> {
let (block_1_size, block_1_count, block_2_size, block_2_count) = let (block_1_size, block_1_count, block_2_size, block_2_count) =
try!(version.fetch(ec_level, DATA_BYTES_PER_BLOCK[])); try!(version.fetch(ec_level, &DATA_BYTES_PER_BLOCK));
let blocks_count = block_1_count + block_2_count; let blocks_count = block_1_count + block_2_count;
let block_1_end = block_1_size * block_1_count; let block_1_end = block_1_size * block_1_count;
@ -135,7 +115,7 @@ pub fn construct_codewords(rawbits: &[u8],
} }
// Generate EC codes. // Generate EC codes.
let ec_bytes = try!(version.fetch(ec_level, EC_BYTES_PER_BLOCK[])); let ec_bytes = try!(version.fetch(ec_level, &EC_BYTES_PER_BLOCK));
let ec_codes = blocks.iter() let ec_codes = blocks.iter()
.map(|block| create_error_correction_code(*block, ec_bytes)) .map(|block| create_error_correction_code(*block, ec_bytes))
.collect::<Vec<Vec<u8>>>(); .collect::<Vec<Vec<u8>>>();
@ -155,17 +135,17 @@ mod construct_codewords_test {
fn test_add_ec_simple() { fn test_add_ec_simple() {
let msg = b" [\x0bx\xd1r\xdcMC@\xec\x11\xec\x11\xec\x11"; let msg = b" [\x0bx\xd1r\xdcMC@\xec\x11\xec\x11\xec\x11";
let (blocks_vec, ec_vec) = construct_codewords(msg, Version(1), M).unwrap(); let (blocks_vec, ec_vec) = construct_codewords(msg, Version(1), M).unwrap();
assert_eq!(blocks_vec[], msg); assert_eq!(&*blocks_vec, msg);
assert_eq!(ec_vec[], b"\xc4#'w\xeb\xd7\xe7\xe2]\x17"); assert_eq!(&*ec_vec, b"\xc4#'w\xeb\xd7\xe7\xe2]\x17");
} }
#[test] #[test]
fn test_add_ec_complex() { fn test_add_ec_complex() {
let msg = b"CUF\x86W&U\xc2w2\x06\x12\x06g&\xf6\xf6B\x07v\x86\xf2\x07&V\x16\xc6\xc7\x92\x06\xb6\xe6\xf7w2\x07v\x86W&R\x06\x86\x972\x07F\xf7vV\xc2\x06\x972\x10\xec\x11\xec\x11\xec\x11\xec"; let msg = b"CUF\x86W&U\xc2w2\x06\x12\x06g&\xf6\xf6B\x07v\x86\xf2\x07&V\x16\xc6\xc7\x92\x06\xb6\xe6\xf7w2\x07v\x86W&R\x06\x86\x972\x07F\xf7vV\xc2\x06\x972\x10\xec\x11\xec\x11\xec\x11\xec";
let (blocks_vec, ec_vec) = construct_codewords(msg, Version(5), Q).unwrap(); let (blocks_vec, ec_vec) = construct_codewords(msg, Version(5), Q).unwrap();
assert_eq!(blocks_vec[], assert_eq!(&*blocks_vec,
b"C\xf6\xb6FU\xf6\xe6\xf7FB\xf7v\x86\x07wVWv2\xc2&\x86\x07\x06U\xf2v\x97\xc2\x07\x862w&W\x102V&\xec\x06\x16R\x11\x12\xc6\x06\xec\x06\xc7\x86\x11g\x92\x97\xec&\x062\x11\x07\xec"); b"C\xf6\xb6FU\xf6\xe6\xf7FB\xf7v\x86\x07wVWv2\xc2&\x86\x07\x06U\xf2v\x97\xc2\x07\x862w&W\x102V&\xec\x06\x16R\x11\x12\xc6\x06\xec\x06\xc7\x86\x11g\x92\x97\xec&\x062\x11\x07\xec");
assert_eq!(ec_vec[], assert_eq!(&*ec_vec,
b"\xd5W\x94\xeb\xc7\xcct\x9f\x0b`\xb1\x05-<\xd4\xads\xcaL\x18\xf7\xb6\x85\x93\xf1|K;\xdf\x9d\xf2!\xe5\xc8\xeej\xf8\x86L(\x9a\x1b\xc3\xffu\x81\xe6\xac\x9a\xd1\xbdRo\x11\n\x02V\xa3l\x83\xa1\xa3\xf0 ox\xc0\xb2'\x85\x8d\xec"); b"\xd5W\x94\xeb\xc7\xcct\x9f\x0b`\xb1\x05-<\xd4\xads\xcaL\x18\xf7\xb6\x85\x93\xf1|K;\xdf\x9d\xf2!\xe5\xc8\xeej\xf8\x86L(\x9a\x1b\xc3\xffu\x81\xe6\xac\x9a\xd1\xbdRo\x11\n\x02V\xa3l\x83\xa1\xa3\xf0 ox\xc0\xb2'\x85\x8d\xec");
} }
} }

View file

@ -24,7 +24,6 @@
extern crate test; extern crate test;
use std::slice::CloneableVector;
pub use types::{QrResult, ErrorCorrectionLevel, L, M, Q, H, pub use types::{QrResult, ErrorCorrectionLevel, L, M, Q, H,
QrVersion, Version, MicroVersion}; QrVersion, Version, MicroVersion};
@ -164,6 +163,18 @@ impl QrCode {
} }
res res
} }
/// Converts the QR code to a vector of booleans. Each entry represents the
/// color of the module, with "true" means dark and "false" means light.
pub fn to_vec(&self) -> Vec<bool> {
self.content.clone()
}
/// Converts the QR code to a vector of booleans. Each entry represents the
/// color of the module, with "true" means dark and "false" means light.
pub fn into_vec(self) -> Vec<bool> {
self.content.clone()
}
} }
impl Index<(uint, uint), bool> for QrCode { impl Index<(uint, uint), bool> for QrCode {
@ -173,12 +184,6 @@ impl Index<(uint, uint), bool> for QrCode {
} }
} }
impl CloneableVector<bool> for QrCode {
fn to_vec(&self) -> Vec<bool> {
self.content.clone()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use {QrCode, Version, MicroVersion, L, M}; use {QrCode, Version, MicroVersion, L, M};
@ -187,7 +192,7 @@ mod tests {
fn test_annex_i_qr() { fn test_annex_i_qr() {
// This uses the ISO Annex I as test vector. // This uses the ISO Annex I as test vector.
let code = QrCode::with_version(b"01234567", Version(1), M).unwrap(); let code = QrCode::with_version(b"01234567", Version(1), M).unwrap();
assert_eq!(code.to_debug_str('#', '.')[], "\n\ assert_eq!(&*code.to_debug_str('#', '.'), "\n\
#######..#.##.#######\n\ #######..#.##.#######\n\
#.....#..####.#.....#\n\ #.....#..####.#.....#\n\
#.###.#.#.....#.###.#\n\ #.###.#.#.....#.###.#\n\
@ -214,7 +219,7 @@ mod tests {
#[test] #[test]
fn test_annex_i_micro_qr() { fn test_annex_i_micro_qr() {
let code = QrCode::with_version(b"01234567", MicroVersion(2), L).unwrap(); let code = QrCode::with_version(b"01234567", MicroVersion(2), L).unwrap();
assert_eq!(code.to_debug_str('#', '.')[], "\n\ assert_eq!(&*code.to_debug_str('#', '.'), "\n\
#######.#.#.#\n\ #######.#.#.#\n\
#.....#.###.#\n\ #.....#.###.#\n\
#.###.#..##.#\n\ #.###.#..##.#\n\

View file

@ -314,15 +314,15 @@ mod optimize_tests {
use types::{Numeric, Alphanumeric, Byte, Kanji, QrVersion, Version, MicroVersion}; use types::{Numeric, Alphanumeric, Byte, Kanji, QrVersion, Version, MicroVersion};
fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: QrVersion) { fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: QrVersion) {
let prev_len = total_encoded_len(given[], version); let prev_len = total_encoded_len(&*given, version);
let opt_segs = Optimizer::new(given.iter().map(|seg| *seg), version).collect::<Vec<Segment>>(); let opt_segs = Optimizer::new(given.iter().map(|seg| *seg), version).collect::<Vec<Segment>>();
let new_len = total_encoded_len(opt_segs[], version); let new_len = total_encoded_len(&*opt_segs, version);
if given != opt_segs { if given != opt_segs {
assert!(prev_len > new_len, "{} > {}", prev_len, new_len); assert!(prev_len > new_len, "{} > {}", prev_len, new_len);
} }
assert!(opt_segs == expected, assert!(opt_segs == expected,
"Optimization gave something better: {} < {} ({})", "Optimization gave something better: {} < {} ({})",
new_len, total_encoded_len(expected[], version), opt_segs); new_len, total_encoded_len(&*expected, version), opt_segs);
} }
#[test] #[test]