mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-28 05: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)]
|
#[cfg(test)]
|
||||||
mod BitMatrixTestCase;
|
mod BitMatrixTestCase;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod BitSourceTestCase;
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2010 ZXing authors
|
* Copyright (C) 2010 ZXing authors
|
||||||
*
|
*
|
||||||
@@ -1492,8 +1495,8 @@ pub trait ECIInput {
|
|||||||
*/
|
*/
|
||||||
pub struct BitSource {
|
pub struct BitSource {
|
||||||
bytes: Vec<u8>,
|
bytes: Vec<u8>,
|
||||||
byteOffset: usize,
|
byte_offset: usize,
|
||||||
bitOffset: usize,
|
bit_offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BitSource {
|
impl BitSource {
|
||||||
@@ -1504,8 +1507,8 @@ impl BitSource {
|
|||||||
pub fn new(bytes: Vec<u8>) -> Self {
|
pub fn new(bytes: Vec<u8>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
bytes,
|
bytes,
|
||||||
byteOffset: 0,
|
byte_offset: 0,
|
||||||
bitOffset: 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)}.
|
* @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 {
|
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)}.
|
* @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 {
|
pub fn getByteOffset(&self) -> usize {
|
||||||
return self.byteOffset;
|
return self.byte_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1536,38 +1539,38 @@ impl BitSource {
|
|||||||
|
|
||||||
let mut result = 0;
|
let mut result = 0;
|
||||||
|
|
||||||
let mut numBits = numBits;
|
let mut num_bits = numBits;
|
||||||
|
|
||||||
// First, read remainder from current byte
|
// First, read remainder from current byte
|
||||||
if self.bitOffset > 0 {
|
if self.bit_offset > 0 {
|
||||||
let bitsLeft = 8 - self.bitOffset;
|
let bitsLeft = 8 - self.bit_offset;
|
||||||
let toRead = cmp::min(numBits, bitsLeft);
|
let toRead = cmp::min(num_bits, bitsLeft);
|
||||||
let bitsToNotRead = bitsLeft - toRead;
|
let bitsToNotRead = bitsLeft - toRead;
|
||||||
let mask = (0xFF >> (8 - toRead)) << bitsToNotRead;
|
let mask = (0xFF >> (8 - toRead)) << bitsToNotRead;
|
||||||
result = (self.bytes[self.byteOffset] & mask) >> bitsToNotRead;
|
result = (self.bytes[self.byte_offset] & mask) >> bitsToNotRead;
|
||||||
numBits -= toRead;
|
num_bits -= toRead;
|
||||||
self.bitOffset += toRead;
|
self.bit_offset += toRead;
|
||||||
if self.bitOffset == 8 {
|
if self.bit_offset == 8 {
|
||||||
self.bitOffset = 0;
|
self.bit_offset = 0;
|
||||||
self.byteOffset += 1;
|
self.byte_offset += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next read whole bytes
|
// Next read whole bytes
|
||||||
if numBits > 0 {
|
if num_bits > 0 {
|
||||||
while numBits >= 8 {
|
while num_bits >= 8 {
|
||||||
result = (result << 8) | (self.bytes[self.byteOffset] & 0xFF);
|
result = ((result as u16) << 8) as u8 | (self.bytes[self.byte_offset] & 0xFF);
|
||||||
self.byteOffset += 1;
|
self.byte_offset += 1;
|
||||||
numBits -= 8;
|
num_bits -= 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally read a partial byte
|
// Finally read a partial byte
|
||||||
if numBits > 0 {
|
if num_bits > 0 {
|
||||||
let bitsToNotRead = 8 - numBits;
|
let bits_to_not_read = 8 - num_bits;
|
||||||
let mask = (0xFF >> bitsToNotRead) << bitsToNotRead;
|
let mask = (0xFF >> bits_to_not_read) << bits_to_not_read;
|
||||||
result =
|
result =
|
||||||
(result << numBits) | ((self.bytes[self.byteOffset] & mask) >> bitsToNotRead);
|
(result << num_bits) | ((self.bytes[self.byte_offset] & mask) >> bits_to_not_read);
|
||||||
self.bitOffset += numBits;
|
self.bit_offset += num_bits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1578,6 +1581,6 @@ impl BitSource {
|
|||||||
* @return number of bits that can be read successfully
|
* @return number of bits that can be read successfully
|
||||||
*/
|
*/
|
||||||
pub fn available(&self) -> usize {
|
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