From c167442e5b95186617c5074dc21993b7c6aaa0c6 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 15 Dec 2022 13:16:31 -0600 Subject: [PATCH] expanded decoders test ok --- .../decoders/AI013103DecoderTest.java | 58 -------- .../decoders/AI013X0X1XDecoderTest.java | 125 ------------------ .../expanded/decoders/AnyAIDecoderTest.java | 86 ------------ .../decoders/abstract_decoder_test_utils.rs | 52 ++++---- .../decoders/abstract_expanded_decoder.rs | 67 +++++----- .../expanded/decoders/ai_013103_decoder.rs | 50 ++++++- ...est.java => ai_0132023203_decoder_test.rs} | 31 ++--- .../expanded/decoders/ai_01320x_decoder.rs | 30 +++-- .../expanded/decoders/ai_013x0x1x_decoder.rs | 101 ++++++++++++++ .../expanded/decoders/ai_013x0x_decoder.rs | 17 ++- .../expanded/decoders/ai_01_and_other_ais.rs | 2 +- .../rss/expanded/decoders/any_ai_decoder.rs | 62 +++++++++ .../decoders/general_app_id_decoder.rs | 3 +- src/oned/rss/expanded/decoders/mod.rs | 4 + ...a => expanded_information_decoder_test.rs} | 22 +-- src/oned/rss/expanded/mod.rs | 3 + 16 files changed, 335 insertions(+), 378 deletions(-) delete mode 100644 src/oned/rss/expanded/decoders/AI013103DecoderTest.java delete mode 100644 src/oned/rss/expanded/decoders/AI013X0X1XDecoderTest.java delete mode 100644 src/oned/rss/expanded/decoders/AnyAIDecoderTest.java rename src/oned/rss/expanded/decoders/{AI0132023203DecoderTest.java => ai_0132023203_decoder_test.rs} (59%) rename src/oned/rss/expanded/{ExpandedInformationDecoderTest.java => expanded_information_decoder_test.rs} (64%) diff --git a/src/oned/rss/expanded/decoders/AI013103DecoderTest.java b/src/oned/rss/expanded/decoders/AI013103DecoderTest.java deleted file mode 100644 index 28e3c30..0000000 --- a/src/oned/rss/expanded/decoders/AI013103DecoderTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2010 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. - */ - -/* - * These authors would like to acknowledge the Spanish Ministry of Industry, - * Tourism and Trade, for the support in the project TSI020301-2008-2 - * "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled - * Mobile Dynamic Environments", led by Treelogic - * ( http://www.treelogic.com/ ): - * - * http://www.piramidepse.com/ - */ - -package com.google.zxing.oned.rss.expanded.decoders; - -import com.google.zxing.NotFoundException; -import org.junit.Test; - -/** - * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) - */ -public final class AI013103DecoderTest extends AbstractDecoderTest { - - private static final String header = "..X.."; - - @Test - public void test0131031() throws Exception { - CharSequence data = header + compressedGtin900123456798908 + compressed15bitWeight1750; - String expected = "(01)90012345678908(3103)001750"; - assertCorrectBinaryString(data, expected); - } - - @Test - public void test0131032() throws Exception { - CharSequence data = header + compressedGtin900000000000008 + compressed15bitWeight0; - String expected = "(01)90000000000003(3103)000000"; - assertCorrectBinaryString(data, expected); - } - - @Test(expected = NotFoundException.class) - public void test013103invalid() throws Exception { - CharSequence data = header + compressedGtin900123456798908 + compressed15bitWeight1750 + ".."; - assertCorrectBinaryString(data, ""); - } -} diff --git a/src/oned/rss/expanded/decoders/AI013X0X1XDecoderTest.java b/src/oned/rss/expanded/decoders/AI013X0X1XDecoderTest.java deleted file mode 100644 index 1781725..0000000 --- a/src/oned/rss/expanded/decoders/AI013X0X1XDecoderTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (C) 2010 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. - */ - -/* - * These authors would like to acknowledge the Spanish Ministry of Industry, - * Tourism and Trade, for the support in the project TSI020301-2008-2 - * "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled - * Mobile Dynamic Environments", led by Treelogic - * ( http://www.treelogic.com/ ): - * - * http://www.piramidepse.com/ - */ - -package com.google.zxing.oned.rss.expanded.decoders; - -import org.junit.Test; - -/** - * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) - */ -public final class AI013X0X1XDecoderTest extends AbstractDecoderTest { - - private static final String header310x11 = "..XXX..."; - private static final String header320x11 = "..XXX..X"; - private static final String header310x13 = "..XXX.X."; - private static final String header320x13 = "..XXX.XX"; - private static final String header310x15 = "..XXXX.."; - private static final String header320x15 = "..XXXX.X"; - private static final String header310x17 = "..XXXXX."; - private static final String header320x17 = "..XXXXXX"; - - @Test - public void test01310X1XendDate() throws Exception { - CharSequence data = header310x11 + compressedGtin900123456798908 + compressed20bitWeight1750 + compressedDateEnd; - String expected = "(01)90012345678908(3100)001750"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01310X111() throws Exception { - CharSequence data = header310x11 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3100)001750(11)100312"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01320X111() throws Exception { - CharSequence data = header320x11 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3200)001750(11)100312"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01310X131() throws Exception { - CharSequence data = header310x13 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3100)001750(13)100312"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01320X131() throws Exception { - CharSequence data = header320x13 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3200)001750(13)100312"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01310X151() throws Exception { - CharSequence data = header310x15 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3100)001750(15)100312"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01320X151() throws Exception { - CharSequence data = header320x15 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3200)001750(15)100312"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01310X171() throws Exception { - CharSequence data = header310x17 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3100)001750(17)100312"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void test01320X171() throws Exception { - CharSequence data = header320x17 + compressedGtin900123456798908 + compressed20bitWeight1750 + - compressedDateMarch12th2010; - String expected = "(01)90012345678908(3200)001750(17)100312"; - - assertCorrectBinaryString(data, expected); - } - -} diff --git a/src/oned/rss/expanded/decoders/AnyAIDecoderTest.java b/src/oned/rss/expanded/decoders/AnyAIDecoderTest.java deleted file mode 100644 index b79e636..0000000 --- a/src/oned/rss/expanded/decoders/AnyAIDecoderTest.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) 2010 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. - */ - -/* - * These authors would like to acknowledge the Spanish Ministry of Industry, - * Tourism and Trade, for the support in the project TSI020301-2008-2 - * "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled - * Mobile Dynamic Environments", led by Treelogic - * ( http://www.treelogic.com/ ): - * - * http://www.piramidepse.com/ - */ - -package com.google.zxing.oned.rss.expanded.decoders; - -import org.junit.Test; - -/** - * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) - */ -public final class AnyAIDecoderTest extends AbstractDecoderTest { - - private static final String header = "....."; - - @Test - public void testAnyAIDecoder1() throws Exception { - CharSequence data = header + numeric10 + numeric12 + numeric2alpha + alphaA + alpha2numeric + numeric12; - String expected = "(10)12A12"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void testAnyAIDecoder2() throws Exception { - CharSequence data = header + numeric10 + numeric12 + numeric2alpha + alphaA + alpha2isoiec646 + i646B; - String expected = "(10)12AB"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void testAnyAIDecoder3() throws Exception { - CharSequence data = header + numeric10 + numeric2alpha + alpha2isoiec646 + i646B + i646C + isoiec6462alpha + - alphaA + alpha2numeric + numeric10; - String expected = "(10)BCA10"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void testAnyAIDecodernumericFNC1secondDigit() throws Exception { - CharSequence data = header + numeric10 + numeric1FNC1; - String expected = "(10)1"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void testAnyAIDecoderalphaFNC1() throws Exception { - CharSequence data = header + numeric10 + numeric2alpha + alphaA + alphaFNC1; - String expected = "(10)A"; - - assertCorrectBinaryString(data, expected); - } - - @Test - public void testAnyAIDecoder646FNC1() throws Exception { - CharSequence data = header + numeric10 + numeric2alpha + alphaA + isoiec6462alpha + i646B + i646FNC1; - String expected = "(10)AB"; - - assertCorrectBinaryString(data, expected); - } -} diff --git a/src/oned/rss/expanded/decoders/abstract_decoder_test_utils.rs b/src/oned/rss/expanded/decoders/abstract_decoder_test_utils.rs index f78df3a..e00621c 100644 --- a/src/oned/rss/expanded/decoders/abstract_decoder_test_utils.rs +++ b/src/oned/rss/expanded/decoders/abstract_decoder_test_utils.rs @@ -24,45 +24,45 @@ * http://www.piramidepse.com/ */ -use crate::oned::rss::expanded::{binary_util, decoders::AbstractExpandedDecoder}; +use crate::oned::rss::expanded::{binary_util, decoders::{AbstractExpandedDecoder, abstract_expanded_decoder::createDecoder}}; /** * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) */ -const numeric10: &str = "..X..XX"; -const numeric12: &str = "..X.X.X"; -const numeric1FNC1: &str = "..XXX.X"; -// static final String numericFNC11 = "XXX.XXX"; +pub const numeric10: &str = "..X..XX"; +pub const numeric12: &str = "..X.X.X"; +pub const numeric1FNC1: &str = "..XXX.X"; + // static final String numericFNC11 = "XXX.XXX"; -const numeric2alpha: &str = "...."; +pub const numeric2alpha: &str = "...."; -const alphaA: &str = "X....."; -const alphaFNC1: &str = ".XXXX"; -const alpha2numeric: &str = "..."; -const alpha2isoiec646: &str = "..X.."; +pub const alphaA: &str = "X....."; +pub const alphaFNC1: &str = ".XXXX"; +pub const alpha2numeric: &str = "..."; +pub const alpha2isoiec646: &str = "..X.."; -const i646B: &str = "X.....X"; -const i646C: &str = "X....X."; -const i646FNC1: &str = ".XXXX"; -const isoiec6462alpha: &str = "..X.."; +pub const i646B: &str = "X.....X"; +pub const i646C: &str = "X....X."; +pub const i646FNC1: &str = ".XXXX"; +pub const isoiec6462alpha: &str = "..X.."; -const compressedGtin900123456798908: &str = ".........X..XXX.X.X.X...XX.XXXXX.XXXX.X."; -const compressedGtin900000000000008: &str = "........................................"; +pub const compressedGtin900123456798908: &str = ".........X..XXX.X.X.X...XX.XXXXX.XXXX.X."; +pub const compressedGtin900000000000008: &str = "........................................"; -const compressed15bitWeight1750: &str = "....XX.XX.X.XX."; -const compressed15bitWeight11750: &str = ".X.XX.XXXX..XX."; -const compressed15bitWeight0: &str = "..............."; +pub const compressed15bitWeight1750: &str = "....XX.XX.X.XX."; +pub const compressed15bitWeight11750: &str = ".X.XX.XXXX..XX."; +pub const compressed15bitWeight0: &str = "..............."; -const compressed20bitWeight1750: &str = ".........XX.XX.X.XX."; +pub const compressed20bitWeight1750: &str = ".........XX.XX.X.XX."; -const compressedDateMarch12th2010: &str = "....XXXX.X..XX.."; -const compressedDateEnd: &str = "X..X.XX........."; +pub const compressedDateMarch12th2010: &str = "....XXXX.X..XX.."; +pub const compressedDateEnd: &str = "X..X.XX........."; pub fn assertCorrectBinaryString(binaryString: &str, expectedNumber: &str) { let binary = binary_util::buildBitArrayFromStringWithoutSpaces(binaryString).expect("built"); - panic!("finish implementation for test"); - // let decoder = AbstractExpandedDecoder.createDecoder(binary); - // let result = decoder.parseInformation(); - // assert_eq!(expectedNumber, result); + + let mut decoder = createDecoder(&binary).expect("get decoder"); + let result = decoder.parseInformation().expect("information exists"); + assert_eq!(expectedNumber, result); } diff --git a/src/oned/rss/expanded/decoders/abstract_expanded_decoder.rs b/src/oned/rss/expanded/decoders/abstract_expanded_decoder.rs index 06f45ae..18307dd 100644 --- a/src/oned/rss/expanded/decoders/abstract_expanded_decoder.rs +++ b/src/oned/rss/expanded/decoders/abstract_expanded_decoder.rs @@ -26,7 +26,7 @@ use crate::{common::BitArray, Exceptions}; -use super::GeneralAppIdDecoder; +use super::{GeneralAppIdDecoder, AnyAIDecoder, AI01AndOtherAIs, AI013103decoder, AI01320xDecoder, AI01392xDecoder, AI01393xDecoder, AI013x0x1xDecoder}; /** * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) @@ -55,38 +55,43 @@ pub trait AbstractExpandedDecoder { // fn new(information:&BitArray) -> Self where Self:Sized; } -// pub fn createDecoder( information:&BitArray) -> Box{ -// if information.get(1) { -// return new AI01AndOtherAIs(information); -// } -// if !information.get(2) { -// return new AnyAIDecoder(information); -// } +pub fn createDecoder<'a>( information:&'a BitArray) -> Result,Exceptions>{ + if information.get(1) { + return Ok(Box::new(AI01AndOtherAIs::new(information))) + } + if !information.get(2) { + return Ok(Box::new( AnyAIDecoder::new(information))); + } -// let fourBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 4); +// let gen_decode = GeneralAppIdDecoder::new(information); -// switch (fourBitEncodationMethod) { -// case 4: return new AI013103decoder(information); -// case 5: return new AI01320xDecoder(information); -// } + let fourBitEncodationMethod = GeneralAppIdDecoder::extractNumericValueFromBitArrayWithInformation(information, 1, 4); -// int fiveBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 5); -// switch (fiveBitEncodationMethod) { -// case 12: return new AI01392xDecoder(information); -// case 13: return new AI01393xDecoder(information); -// } + match fourBitEncodationMethod { + 4=> return Ok(Box::new( AI013103decoder::new(information))), + 5=> return Ok(Box::new( AI01320xDecoder::new(information))), + _=>{}, + } -// int sevenBitEncodationMethod = GeneralAppIdDecoder.extractNumericValueFromBitArray(information, 1, 7); -// switch (sevenBitEncodationMethod) { -// case 56: return new AI013x0x1xDecoder(information, "310", "11"); -// case 57: return new AI013x0x1xDecoder(information, "320", "11"); -// case 58: return new AI013x0x1xDecoder(information, "310", "13"); -// case 59: return new AI013x0x1xDecoder(information, "320", "13"); -// case 60: return new AI013x0x1xDecoder(information, "310", "15"); -// case 61: return new AI013x0x1xDecoder(information, "320", "15"); -// case 62: return new AI013x0x1xDecoder(information, "310", "17"); -// case 63: return new AI013x0x1xDecoder(information, "320", "17"); -// } + let fiveBitEncodationMethod = GeneralAppIdDecoder::extractNumericValueFromBitArrayWithInformation(information, 1, 5); + match fiveBitEncodationMethod { + 12=> return Ok(Box::new( AI01392xDecoder::new(information))), + 13=> return Ok(Box::new( AI01393xDecoder::new(information))), + _=>{}, + } -// throw new IllegalStateException("unknown decoder: " + information); -// } + let sevenBitEncodationMethod = GeneralAppIdDecoder::extractNumericValueFromBitArrayWithInformation(information, 1, 7); + match sevenBitEncodationMethod { + 56=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "310".to_owned(), "11".to_owned()))), + 57=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "320".to_owned(), "11".to_owned()))), + 58=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "310".to_owned(), "13".to_owned()))), + 59=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "320".to_owned(), "13".to_owned()))), + 60=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "310".to_owned(), "15".to_owned()))), + 61=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "320".to_owned(), "15".to_owned()))), + 62=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "310".to_owned(), "17".to_owned()))), + 63=> return Ok(Box::new( AI013x0x1xDecoder::new(information, "320".to_owned(), "17".to_owned()))), + _=>{}, + } + + Err(Exceptions::IllegalStateException(format!("unknown decoder: {}" , information))) +} diff --git a/src/oned/rss/expanded/decoders/ai_013103_decoder.rs b/src/oned/rss/expanded/decoders/ai_013103_decoder.rs index 5624291..cd5a032 100644 --- a/src/oned/rss/expanded/decoders/ai_013103_decoder.rs +++ b/src/oned/rss/expanded/decoders/ai_013103_decoder.rs @@ -34,12 +34,12 @@ use super::{AI013x0xDecoder, AI01decoder, AI01weightDecoder, AbstractExpandedDec pub struct AI013103decoder<'a>(AI013x0xDecoder<'a>); impl AI01weightDecoder for AI013103decoder<'_> { - fn addWeightCode(&self, buf: &mut String, _weight: u32) { - buf.push_str("(3103)"); + fn addWeightCode(&self, buf: &mut String, weight: u32) { + self.0.addWeightCode(buf, weight) } fn checkWeight(&self, weight: u32) -> u32 { - weight + self.0.checkWeight(weight) } } impl AbstractExpandedDecoder for AI013103decoder<'_> { @@ -55,6 +55,48 @@ impl AI01decoder for AI013103decoder<'_> {} impl<'a> AI013103decoder<'_> { pub fn new(information: &'a BitArray) -> AI013103decoder<'a> { - AI013103decoder(AI013x0xDecoder::new(information)) + AI013103decoder(AI013x0xDecoder::new(information, addWeightCode, checkWeight)) } } + +fn addWeightCode( buf: &mut String, _weight: u32) { + buf.push_str("(3103)"); +} + +fn checkWeight( weight: u32) -> u32 { + weight +} + + +/** + * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) + */ +#[cfg(test)] +mod AI013103DecoderTest { + use crate::oned::rss::expanded::decoders::abstract_decoder_test_utils::*; + + + const header :&str = "..X.."; + + #[test] + fn test0131031() { + let data = format!("{}{}{}", header , compressedGtin900123456798908 , compressed15bitWeight1750); + let expected = "(01)90012345678908(3103)001750"; + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test0131032() { + let data = format!("{}{}{}",header , compressedGtin900000000000008 , compressed15bitWeight0); + let expected = "(01)90000000000003(3103)000000"; + assertCorrectBinaryString(&data, expected); + } + + #[test] + #[should_panic] + fn test013103invalid() { + let data = format!("{}{}{}..",header , compressedGtin900123456798908 , compressed15bitWeight1750 ); + assertCorrectBinaryString(&data, ""); + } + } + diff --git a/src/oned/rss/expanded/decoders/AI0132023203DecoderTest.java b/src/oned/rss/expanded/decoders/ai_0132023203_decoder_test.rs similarity index 59% rename from src/oned/rss/expanded/decoders/AI0132023203DecoderTest.java rename to src/oned/rss/expanded/decoders/ai_0132023203_decoder_test.rs index addf9a9..b4ae56e 100644 --- a/src/oned/rss/expanded/decoders/AI0132023203DecoderTest.java +++ b/src/oned/rss/expanded/decoders/ai_0132023203_decoder_test.rs @@ -24,30 +24,31 @@ * http://www.piramidepse.com/ */ -package com.google.zxing.oned.rss.expanded.decoders; +use super::abstract_decoder_test_utils::*; -import org.junit.Test; /** * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) */ -public final class AI0132023203DecoderTest extends AbstractDecoderTest { - private static final String header = "..X.X"; - @Test - public void test0132021() throws Exception { - CharSequence data = header + compressedGtin900123456798908 + compressed15bitWeight1750; - String expected = "(01)90012345678908(3202)001750"; + const header :&str = "..X.X"; - assertCorrectBinaryString(data, expected); + #[test] + fn test0132021() { + let data = format!("{}{}{}",header, compressedGtin900123456798908 , compressed15bitWeight1750); + let expected = "(01)90012345678908(3202)001750"; + + assertCorrectBinaryString(&data, expected); } - @Test - public void test0132031() throws Exception { - CharSequence data = header + compressedGtin900123456798908 + compressed15bitWeight11750; - String expected = "(01)90012345678908(3203)001750"; + #[test] + fn test0132031() { + let data = format!("{}{}{}",header , compressedGtin900123456798908 , compressed15bitWeight11750); + let expected = "(01)90012345678908(3203)001750"; - assertCorrectBinaryString(data, expected); + assertCorrectBinaryString(&data, expected); } -} + + + \ No newline at end of file diff --git a/src/oned/rss/expanded/decoders/ai_01320x_decoder.rs b/src/oned/rss/expanded/decoders/ai_01320x_decoder.rs index ad8fa65..b4b733a 100644 --- a/src/oned/rss/expanded/decoders/ai_01320x_decoder.rs +++ b/src/oned/rss/expanded/decoders/ai_01320x_decoder.rs @@ -35,19 +35,11 @@ pub struct AI01320xDecoder<'a>(AI013x0xDecoder<'a>); impl AI01weightDecoder for AI01320xDecoder<'_> { fn addWeightCode(&self, buf: &mut String, weight: u32) { - if weight < 10000 { - buf.push_str("(3202)"); - } else { - buf.push_str("(3203)"); - } + self.0.addWeightCode(buf, weight) } fn checkWeight(&self, weight: u32) -> u32 { - if weight < 10000 { - weight - } else { - weight - 10000 - } + self.0.checkWeight(weight) } } impl AbstractExpandedDecoder for AI01320xDecoder<'_> { @@ -62,6 +54,22 @@ impl AbstractExpandedDecoder for AI01320xDecoder<'_> { impl AI01decoder for AI01320xDecoder<'_> {} impl<'a> AI01320xDecoder<'_> { pub fn new(information: &'a BitArray) -> AI01320xDecoder<'a> { - AI01320xDecoder(AI013x0xDecoder::new(information)) + AI01320xDecoder(AI013x0xDecoder::new(information, addWeightCode, checkWeight)) } } + +fn addWeightCode(buf: &mut String, weight: u32) { + if weight < 10000 { + buf.push_str("(3202)"); + } else { + buf.push_str("(3203)"); + } +} + +fn checkWeight( weight: u32) -> u32 { + if weight < 10000 { + weight + } else { + weight - 10000 + } +} \ No newline at end of file diff --git a/src/oned/rss/expanded/decoders/ai_013x0x1x_decoder.rs b/src/oned/rss/expanded/decoders/ai_013x0x1x_decoder.rs index 5ceda53..01552bf 100644 --- a/src/oned/rss/expanded/decoders/ai_013x0x1x_decoder.rs +++ b/src/oned/rss/expanded/decoders/ai_013x0x1x_decoder.rs @@ -131,3 +131,104 @@ impl<'a> AI013x0x1xDecoder<'_> { buf.push_str(&day.to_string()); } } + + +/** + * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) + */ +#[cfg(test)] +mod AI013X0X1XDecoderTest { + use crate::oned::rss::expanded::decoders::abstract_decoder_test_utils::*; + + + const header310x11 : &str = "..XXX..."; + const header320x11 : &str = "..XXX..X"; + const header310x13 : &str = "..XXX.X."; + const header320x13 : &str = "..XXX.XX"; + const header310x15 : &str = "..XXXX.."; + const header320x15 : &str = "..XXXX.X"; + const header310x17 : &str = "..XXXXX."; + const header320x17 : &str = "..XXXXXX"; + + #[test] + fn test01310X1XendDate() { + let data = format!("{}{}{}{}", header310x11 , compressedGtin900123456798908 , compressed20bitWeight1750 , compressedDateEnd); + let expected = "(01)90012345678908(3100)001750"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01310X111() { + let data = format!("{}{}{}{}", header310x11 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3100)001750(11)100312"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01320X111() { + let data = format!("{}{}{}{}",header320x11 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3200)001750(11)100312"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01310X131() { + let data = format!("{}{}{}{}", header310x13 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3100)001750(13)100312"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01320X131() { + let data = format!("{}{}{}{}", header320x13 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3200)001750(13)100312"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01310X151() { + let data = format!("{}{}{}{}", header310x15 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3100)001750(15)100312"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01320X151() { + let data = format!("{}{}{}{}", header320x15 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3200)001750(15)100312"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01310X171() { + let data = format!("{}{}{}{}", header310x17 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3100)001750(17)100312"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn test01320X171() { + let data = format!("{}{}{}{}", header320x17 , compressedGtin900123456798908 , compressed20bitWeight1750 , + compressedDateMarch12th2010); + let expected = "(01)90012345678908(3200)001750(17)100312"; + + assertCorrectBinaryString(&data, expected); + } + + } + \ No newline at end of file diff --git a/src/oned/rss/expanded/decoders/ai_013x0x_decoder.rs b/src/oned/rss/expanded/decoders/ai_013x0x_decoder.rs index ca2a885..3a453c8 100644 --- a/src/oned/rss/expanded/decoders/ai_013x0x_decoder.rs +++ b/src/oned/rss/expanded/decoders/ai_013x0x_decoder.rs @@ -34,14 +34,18 @@ use super::{AI01decoder, AI01weightDecoder, AbstractExpandedDecoder, GeneralAppI pub struct AI013x0xDecoder<'a> { information: &'a BitArray, decoder: GeneralAppIdDecoder<'a>, + addWeightCodeFunction: fn(&mut String, u32), + checkWeightFunction: fn(u32) -> u32, } impl AI01weightDecoder for AI013x0xDecoder<'_> { - fn addWeightCode(&self, _buf: &mut String, _weight: u32) { - unimplemented!("no java implementation exists") + fn addWeightCode(&self, buf: &mut String, weight: u32) { + //unimplemented!("no java implementation exists") + (self.addWeightCodeFunction)(buf, weight) } - fn checkWeight(&self, _weight: u32) -> u32 { - unimplemented!("no java implementation exists") + fn checkWeight(&self, weight: u32) -> u32 { + (self.checkWeightFunction)(weight) + // unimplemented!("no java implementation exists") } } impl AbstractExpandedDecoder for AI013x0xDecoder<'_> { @@ -74,10 +78,13 @@ impl<'a> AI013x0xDecoder<'_> { const HEADER_SIZE: usize = 4 + 1; const WEIGHT_SIZE: usize = 15; - pub fn new(information: &'a BitArray) -> AI013x0xDecoder<'a> { + pub fn new(information: &'a BitArray, addWeightCodeFunction: fn(&mut String, u32), + checkWeightFunction: fn(u32) -> u32,) -> AI013x0xDecoder<'a> { AI013x0xDecoder { information, decoder: GeneralAppIdDecoder::new(information), + addWeightCodeFunction, + checkWeightFunction, } } } diff --git a/src/oned/rss/expanded/decoders/ai_01_and_other_ais.rs b/src/oned/rss/expanded/decoders/ai_01_and_other_ais.rs index 6e21aca..0a24d32 100644 --- a/src/oned/rss/expanded/decoders/ai_01_and_other_ais.rs +++ b/src/oned/rss/expanded/decoders/ai_01_and_other_ais.rs @@ -55,7 +55,7 @@ impl AbstractExpandedDecoder for AI01AndOtherAIs<'_> { } } impl<'a> AI01AndOtherAIs<'_> { - fn new(information: &'a BitArray) -> AI01AndOtherAIs<'a> { + pub fn new(information: &'a BitArray) -> AI01AndOtherAIs<'a> { AI01AndOtherAIs(information, GeneralAppIdDecoder::new(information)) } diff --git a/src/oned/rss/expanded/decoders/any_ai_decoder.rs b/src/oned/rss/expanded/decoders/any_ai_decoder.rs index 3a9b7db..a26d952 100644 --- a/src/oned/rss/expanded/decoders/any_ai_decoder.rs +++ b/src/oned/rss/expanded/decoders/any_ai_decoder.rs @@ -57,3 +57,65 @@ impl<'a> AnyAIDecoder<'_> { } } } + + +/** + * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) + */ +#[cfg(test)] +mod AnyAIDecoderTest { + use crate::oned::rss::expanded::decoders::abstract_decoder_test_utils::*; + + + const HEADER : &str = "....."; + + #[test] + fn testAnyAIDecoder1() { + let data =format!("{}{}{}{}{}{}{}", HEADER , numeric10 , numeric12 , numeric2alpha , alphaA , alpha2numeric , numeric12); + let expected = "(10)12A12"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn testAnyAIDecoder2() { + let data =format!("{}{}{}{}{}{}{}", HEADER , numeric10 , numeric12 , numeric2alpha , alphaA , alpha2isoiec646 , i646B); + let expected = "(10)12AB"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn testAnyAIDecoder3() { + let data = format!("{}{}{}{}{}{}{}{}{}{}",HEADER , numeric10 , numeric2alpha , alpha2isoiec646 , i646B , i646C , isoiec6462alpha , + alphaA , alpha2numeric , numeric10); + let expected = "(10)BCA10"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn testAnyAIDecodernumericFNC1secondDigit() { + let data = format!("{}{}{}",HEADER , numeric10 , numeric1FNC1); + let expected = "(10)1"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn testAnyAIDecoderalphaFNC1() { + let data = format!("{}{}{}{}{}", HEADER, numeric10 , numeric2alpha , alphaA , alphaFNC1); + let expected = "(10)A"; + + assertCorrectBinaryString(&data, expected); + } + + #[test] + fn testAnyAIDecoder646FNC1() { + let data = format!("{}{}{}{}{}{}{}",HEADER , numeric10 , numeric2alpha , alphaA , isoiec6462alpha , i646B , i646FNC1); + let expected = "(10)AB"; + + assertCorrectBinaryString(&data, expected); + } + } + diff --git a/src/oned/rss/expanded/decoders/general_app_id_decoder.rs b/src/oned/rss/expanded/decoders/general_app_id_decoder.rs index a0cfe03..25c64be 100644 --- a/src/oned/rss/expanded/decoders/general_app_id_decoder.rs +++ b/src/oned/rss/expanded/decoders/general_app_id_decoder.rs @@ -197,7 +197,8 @@ impl<'a> GeneralAppIdDecoder<'_> { } } //while (!isFinished); - Ok(result.getDecodedInformation().as_ref().unwrap().clone()) + if result.getDecodedInformation().is_some(){ Ok(result.getDecodedInformation().as_ref().unwrap().clone())} + else {Err(Exceptions::NotFoundException("".to_owned()))} } fn parseNumericBlock(&mut self) -> Result { diff --git a/src/oned/rss/expanded/decoders/mod.rs b/src/oned/rss/expanded/decoders/mod.rs index d5f85e9..060abb7 100644 --- a/src/oned/rss/expanded/decoders/mod.rs +++ b/src/oned/rss/expanded/decoders/mod.rs @@ -1,4 +1,5 @@ pub mod abstract_expanded_decoder; + pub use abstract_expanded_decoder::AbstractExpandedDecoder; mod ai_01_and_other_ais; pub use ai_01_and_other_ais::*; @@ -49,3 +50,6 @@ pub use ai_01393x_decoder::*; mod ai_013103_decoder; pub use ai_013103_decoder::*; + +#[cfg(test)] +mod ai_0132023203_decoder_test; \ No newline at end of file diff --git a/src/oned/rss/expanded/ExpandedInformationDecoderTest.java b/src/oned/rss/expanded/expanded_information_decoder_test.rs similarity index 64% rename from src/oned/rss/expanded/ExpandedInformationDecoderTest.java rename to src/oned/rss/expanded/expanded_information_decoder_test.rs index 4e408ae..c15faf6 100644 --- a/src/oned/rss/expanded/ExpandedInformationDecoderTest.java +++ b/src/oned/rss/expanded/expanded_information_decoder_test.rs @@ -24,27 +24,19 @@ * http://www.piramidepse.com/ */ -package com.google.zxing.oned.rss.expanded; +use crate::oned::rss::expanded::{binary_util, decoders::{ abstract_expanded_decoder}}; -import com.google.zxing.common.BitArray; -import com.google.zxing.oned.rss.expanded.decoders.AbstractExpandedDecoder; - -import org.junit.Assert; -import org.junit.Test; /** * @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es) * @author Eduardo Castillejo, University of Deusto (eduardo.castillejo@deusto.es) */ -public final class ExpandedInformationDecoderTest extends Assert { + #[test] + fn testNoAi() { + let information = binary_util::buildBitArrayFromString(" .......X ..XX..X. X.X....X .......X ....").expect("build"); - @Test - public void testNoAi() throws Exception { - BitArray information = BinaryUtil.buildBitArrayFromString(" .......X ..XX..X. X.X....X .......X ...."); - - AbstractExpandedDecoder decoder = AbstractExpandedDecoder.createDecoder(information); - String decoded = decoder.parseInformation(); - assertEquals("(10)12A", decoded); + let mut decoder = abstract_expanded_decoder::createDecoder(&information).expect("create"); + let decoded = decoder.parseInformation().expect("parsed"); + assert_eq!("(10)12A", decoded); } -} diff --git a/src/oned/rss/expanded/mod.rs b/src/oned/rss/expanded/mod.rs index 1951a66..0736927 100644 --- a/src/oned/rss/expanded/mod.rs +++ b/src/oned/rss/expanded/mod.rs @@ -5,3 +5,6 @@ pub mod bit_array_builder; mod expanded_pair; pub use expanded_pair::*; + +#[cfg(test)] +mod expanded_information_decoder_test; \ No newline at end of file