mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
bitsource tests pass
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.common;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Sean Owen
|
||||
*/
|
||||
public final class BitSourceTestCase extends Assert {
|
||||
|
||||
@Test
|
||||
public void testSource() {
|
||||
byte[] bytes = {(byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5};
|
||||
BitSource source = new BitSource(bytes);
|
||||
assertEquals(40, source.available());
|
||||
assertEquals(0, source.readBits(1));
|
||||
assertEquals(39, source.available());
|
||||
assertEquals(0, source.readBits(6));
|
||||
assertEquals(33, source.available());
|
||||
assertEquals(1, source.readBits(1));
|
||||
assertEquals(32, source.available());
|
||||
assertEquals(2, source.readBits(8));
|
||||
assertEquals(24, source.available());
|
||||
assertEquals(12, source.readBits(10));
|
||||
assertEquals(14, source.available());
|
||||
assertEquals(16, source.readBits(8));
|
||||
assertEquals(6, source.available());
|
||||
assertEquals(5, source.readBits(6));
|
||||
assertEquals(0, source.available());
|
||||
}
|
||||
|
||||
}
|
||||
50
src/common/BitSourceTestCase.rs
Normal file
50
src/common/BitSourceTestCase.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2007 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// package com.google.zxing.common;
|
||||
|
||||
// import org.junit.Assert;
|
||||
// import org.junit.Test;
|
||||
|
||||
// /**
|
||||
// * @author Sean Owen
|
||||
// */
|
||||
// public final class BitSourceTestCase extends Assert {
|
||||
|
||||
use super::BitSource;
|
||||
|
||||
#[test]
|
||||
fn test_source() {
|
||||
let bytes:Vec<u8> = vec![ 1, 2, 3, 4, 5];
|
||||
let mut source = BitSource::new(bytes);
|
||||
assert_eq!(40, source.available());
|
||||
assert_eq!(0, source.readBits(1).unwrap());
|
||||
assert_eq!(39, source.available());
|
||||
assert_eq!(0, source.readBits(6).unwrap());
|
||||
assert_eq!(33, source.available());
|
||||
assert_eq!(1, source.readBits(1).unwrap());
|
||||
assert_eq!(32, source.available());
|
||||
assert_eq!(2, source.readBits(8).unwrap());
|
||||
assert_eq!(24, source.available());
|
||||
assert_eq!(12, source.readBits(10).unwrap());
|
||||
assert_eq!(14, source.available());
|
||||
assert_eq!(16, source.readBits(8).unwrap());
|
||||
assert_eq!(6, source.available());
|
||||
assert_eq!(5, source.readBits(6).unwrap());
|
||||
assert_eq!(0, source.available());
|
||||
}
|
||||
|
||||
// }
|
||||
@@ -19,6 +19,9 @@ mod BitArrayTestCase;
|
||||
|
||||
#[cfg(test)]
|
||||
mod BitMatrixTestCase;
|
||||
|
||||
#[cfg(test)]
|
||||
mod BitSourceTestCase;
|
||||
/*
|
||||
* Copyright (C) 2010 ZXing authors
|
||||
*
|
||||
@@ -1492,8 +1495,8 @@ pub trait ECIInput {
|
||||
*/
|
||||
pub struct BitSource {
|
||||
bytes: Vec<u8>,
|
||||
byteOffset: usize,
|
||||
bitOffset: usize,
|
||||
byte_offset: usize,
|
||||
bit_offset: usize,
|
||||
}
|
||||
|
||||
impl BitSource {
|
||||
@@ -1504,8 +1507,8 @@ impl BitSource {
|
||||
pub fn new(bytes: Vec<u8>) -> Self {
|
||||
Self {
|
||||
bytes,
|
||||
byteOffset: 0,
|
||||
bitOffset: 0,
|
||||
byte_offset: 0,
|
||||
bit_offset: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1513,14 +1516,14 @@ impl BitSource {
|
||||
* @return index of next bit in current byte which would be read by the next call to {@link #readBits(int)}.
|
||||
*/
|
||||
pub fn getBitOffset(&self) -> usize {
|
||||
return self.bitOffset;
|
||||
return self.bit_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return index of next byte in input byte array which would be read by the next call to {@link #readBits(int)}.
|
||||
*/
|
||||
pub fn getByteOffset(&self) -> usize {
|
||||
return self.byteOffset;
|
||||
return self.byte_offset;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1536,38 +1539,38 @@ impl BitSource {
|
||||
|
||||
let mut result = 0;
|
||||
|
||||
let mut numBits = numBits;
|
||||
let mut num_bits = numBits;
|
||||
|
||||
// First, read remainder from current byte
|
||||
if self.bitOffset > 0 {
|
||||
let bitsLeft = 8 - self.bitOffset;
|
||||
let toRead = cmp::min(numBits, bitsLeft);
|
||||
if self.bit_offset > 0 {
|
||||
let bitsLeft = 8 - self.bit_offset;
|
||||
let toRead = cmp::min(num_bits, bitsLeft);
|
||||
let bitsToNotRead = bitsLeft - toRead;
|
||||
let mask = (0xFF >> (8 - toRead)) << bitsToNotRead;
|
||||
result = (self.bytes[self.byteOffset] & mask) >> bitsToNotRead;
|
||||
numBits -= toRead;
|
||||
self.bitOffset += toRead;
|
||||
if self.bitOffset == 8 {
|
||||
self.bitOffset = 0;
|
||||
self.byteOffset += 1;
|
||||
result = (self.bytes[self.byte_offset] & mask) >> bitsToNotRead;
|
||||
num_bits -= toRead;
|
||||
self.bit_offset += toRead;
|
||||
if self.bit_offset == 8 {
|
||||
self.bit_offset = 0;
|
||||
self.byte_offset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Next read whole bytes
|
||||
if numBits > 0 {
|
||||
while numBits >= 8 {
|
||||
result = (result << 8) | (self.bytes[self.byteOffset] & 0xFF);
|
||||
self.byteOffset += 1;
|
||||
numBits -= 8;
|
||||
if num_bits > 0 {
|
||||
while num_bits >= 8 {
|
||||
result = ((result as u16) << 8) as u8 | (self.bytes[self.byte_offset] & 0xFF);
|
||||
self.byte_offset += 1;
|
||||
num_bits -= 8;
|
||||
}
|
||||
|
||||
// Finally read a partial byte
|
||||
if numBits > 0 {
|
||||
let bitsToNotRead = 8 - numBits;
|
||||
let mask = (0xFF >> bitsToNotRead) << bitsToNotRead;
|
||||
if num_bits > 0 {
|
||||
let bits_to_not_read = 8 - num_bits;
|
||||
let mask = (0xFF >> bits_to_not_read) << bits_to_not_read;
|
||||
result =
|
||||
(result << numBits) | ((self.bytes[self.byteOffset] & mask) >> bitsToNotRead);
|
||||
self.bitOffset += numBits;
|
||||
(result << num_bits) | ((self.bytes[self.byte_offset] & mask) >> bits_to_not_read);
|
||||
self.bit_offset += num_bits;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1578,6 +1581,6 @@ impl BitSource {
|
||||
* @return number of bits that can be read successfully
|
||||
*/
|
||||
pub fn available(&self) -> usize {
|
||||
return 8 * (self.bytes.len() - self.byteOffset) - self.bitOffset;
|
||||
return 8 * (self.bytes.len() - self.byte_offset) - self.bit_offset;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user