1) testing method -  MskCommandFactory, applyMask,
reedSolomonComputeDivisor, getNumRawDataModules

2) Exception test with using 3 boundary value analysis

3) to use private methods and variables, make execute method and setter
pull/90/head
wslbal 5 years ago
parent 479357cc3b
commit 70439d47c5

@ -190,7 +190,7 @@ public final class QrCode {
BitBuffer bitBuffer = new BitBuffer(); BitBuffer bitBuffer = new BitBuffer();
for (QrSegment segment : segments) { for (QrSegment segment : segments) {
bitBuffer.appendBits(segment.mode.modeBits, 4); bitBuffer.appendBits(segment.mode.modeBits, 4);
bitBuffer.appendBits(segment.numChars, segment.mode.numCharCountBits(version)); bitBuffer.appendBits(segment.numberOfCharacters, segment.mode.numCharCountBits(version));
bitBuffer.appendData(segment.data); bitBuffer.appendData(segment.data);
} }
return bitBuffer; return bitBuffer;
@ -258,7 +258,13 @@ public final class QrCode {
// Indicates function modules that are not subjected to masking. Discarded when constructor finishes. // Indicates function modules that are not subjected to masking. Discarded when constructor finishes.
private boolean[][] isFunction; private boolean[][] isFunction;
public void setModules(int size) {
this.modules = new boolean [size][size];
}
public void setIsFunction(int size) {
this.isFunction = new boolean [size][size];
}
/*---- Constructor (low level) ----*/ /*---- Constructor (low level) ----*/
@ -581,9 +587,7 @@ public final class QrCode {
// the same mask value a second time will undo the mask. A final well-formed // the same mask value a second time will undo the mask. A final well-formed
// QR Code needs exactly one (not zero, two, etc.) mask applied. // QR Code needs exactly one (not zero, two, etc.) mask applied.
private void applyMask(int msk) { public void applyMask(int msk) {
if (msk < 0 || msk > 7) if (msk < 0 || msk > 7)
throw new IllegalArgumentException("Mask value out of range"); throw new IllegalArgumentException("Mask value out of range");
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {
@ -613,6 +617,9 @@ public final class QrCode {
return mask; // The caller shall assign this value to the final-declared field return mask; // The caller shall assign this value to the final-declared field
} }
public int executeHandleConstructorMasking(int mask) {
return handleConstructorMasking(mask);
}
// Automatically choose best mask // Automatically choose best mask
private int findBestMask() { private int findBestMask() {
@ -750,6 +757,10 @@ public final class QrCode {
return result; return result;
} }
public int excuteGetNumRawDataModules(int ver) {
return getNumRawDataModules(ver);
}
private static int calculateNumOfModules(int ver) { private static int calculateNumOfModules(int ver) {
int size = ver * 4 + 17; int size = ver * 4 + 17;
int result = size * size; // Number of modules in the whole QR Code square int result = size * size; // Number of modules in the whole QR Code square
@ -794,7 +805,9 @@ public final class QrCode {
return result; return result;
} }
public byte[] executeReedSolomonComputeDivisor(int degree) {
return reedSolomonComputeDivisor(degree);
}
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials. // Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
private static byte[] reedSolomonComputeRemainder(byte[] data, byte[] divisor) { private static byte[] reedSolomonComputeRemainder(byte[] data, byte[] divisor) {
Objects.requireNonNull(data); Objects.requireNonNull(data);

@ -0,0 +1,165 @@
package io.nayuki.qrcodegen;
import static org.junit.Assert.*;
import org.junit.*;
public class QrCodeTest {
@Test
public void testFactory() {
Command mskCommand0 = MskCommandFactory.getCommand(0);
Button button0 = new Button(mskCommand0);
assertEquals(true, button0.pressed(0, 2, 1));
Command mskCommand1 = MskCommandFactory.getCommand(1);
Button button1 = new Button(mskCommand1);
assertEquals(true, button1.pressed(0, 2, 1));
Command mskCommand2 = MskCommandFactory.getCommand(2);
Button button2 = new Button(mskCommand2);
assertEquals(true, button2.pressed(0, 3, 1));
Command mskCommand3 = MskCommandFactory.getCommand(3);
Button button3 = new Button(mskCommand3);
assertEquals(true, button3.pressed(1, 2, 1));
Command mskCommand4 = MskCommandFactory.getCommand(4);
Button button4 = new Button(mskCommand4);
assertEquals(true, button4.pressed(0, 2, 1));
Command mskCommand5 = MskCommandFactory.getCommand(5);
Button button5 = new Button(mskCommand5);
assertEquals(true, button5.pressed(0, 2, 1));
Command mskCommand6 = MskCommandFactory.getCommand(6);
Button button6 = new Button(mskCommand6);
assertEquals(true, button6.pressed(0, 2, 1));
Command mskCommand7 = MskCommandFactory.getCommand(7);
Button button7 = new Button(mskCommand7);
assertEquals(true, button7.pressed(0, 4, 1));
}
@Test(expected = AssertionError.class)
public void testAssertionError() {
Command mskCommand = MskCommandFactory.getCommand(9);
}
@Test
public void testApplyMask() {
Ecc errCorLvl = Ecc.LOW;
Ecc errCorLv2 = Ecc.MEDIUM;
Ecc errCorLv3 = Ecc.QUARTILE;
Ecc errCorLv4 = Ecc.HIGH;
int version1 = 1;
int version2 = 2;
int version3 = 39;
int version4 = 40;
int size1 = version1 * 4 + 17;
int size2 = version2 * 4 + 17;
int size3 = version3 * 4 + 17;
int size4 = version4 * 4 + 17;
String text = "Hello, world!";
QrCode qrcodeLOW = QrCode.encodeText(text, errCorLvl);
QrCode qrcodeMEDIUM = QrCode.encodeText(text, errCorLv2);
QrCode qrcodeQUARTILE = QrCode.encodeText(text, errCorLv3);
QrCode qrcodeHIGH = QrCode.encodeText(text, errCorLv4);
qrcodeLOW.excuteGetNumRawDataModules(version1);
qrcodeMEDIUM.excuteGetNumRawDataModules(version2);
qrcodeQUARTILE.excuteGetNumRawDataModules(version3);
qrcodeHIGH.excuteGetNumRawDataModules(version4);
qrcodeLOW.executeReedSolomonComputeDivisor(1);
qrcodeMEDIUM.executeReedSolomonComputeDivisor(2);
qrcodeQUARTILE.executeReedSolomonComputeDivisor(254);
qrcodeHIGH.executeReedSolomonComputeDivisor(255);
qrcodeLOW.setModules(size1);
qrcodeLOW.setIsFunction(size1);
qrcodeMEDIUM.setModules(size2);
qrcodeMEDIUM.setIsFunction(size2);
qrcodeQUARTILE.setModules(size3);
qrcodeQUARTILE.setIsFunction(size3);
qrcodeHIGH.setModules(size4);
qrcodeHIGH.setIsFunction(size4);
qrcodeLOW.applyMask(0);
qrcodeMEDIUM.applyMask(1);
qrcodeQUARTILE.applyMask(6);
qrcodeHIGH.applyMask(7);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalArgumentException_applyMask_MAXBound() {
Ecc errCorLvl = Ecc.LOW;
String text = "Hello, world!";
QrCode qrcodeLOW = QrCode.encodeText(text, errCorLvl);
qrcodeLOW.applyMask(8);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalArgumentException_applyMask_MINBouond() {
Ecc errCorLvl = Ecc.LOW;
String text = "Hello, world!";
QrCode qrcodeLOW = QrCode.encodeText(text, errCorLvl);
qrcodeLOW.applyMask(-1);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalArgumentException_getNumRawDataModules_MINBound() {
Ecc errCorLvl = Ecc.LOW;
String text = "Hello, world!";
QrCode qrcodeLOW = QrCode.encodeText(text, errCorLvl);
qrcodeLOW.excuteGetNumRawDataModules(0);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalArgumentException_getNumRawDataModules_MAXBound() {
Ecc errCorLvl = Ecc.LOW;
String text = "Hello, world!";
QrCode qrcodeLOW = QrCode.encodeText(text, errCorLvl);
qrcodeLOW.excuteGetNumRawDataModules(41);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalArgumentException_ReedSolomonComputeDivisor_MINBound() {
Ecc errCorLvl = Ecc.LOW;
String text = "Hello, world!";
QrCode qrcodeLOW = QrCode.encodeText(text, errCorLvl);
qrcodeLOW.executeReedSolomonComputeDivisor(0);
}
@Test(expected = IllegalArgumentException.class)
public void testIllegalArgumentException_ReedSolomonComputeDivisor_MAXBound() {
Ecc errCorLvl = Ecc.LOW;
String text = "Hello, world!";
QrCode qrcodeLOW = QrCode.encodeText(text, errCorLvl);
qrcodeLOW.executeReedSolomonComputeDivisor(256);
}
}

@ -29,7 +29,6 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import QrSegment.Mode;
/** /**
@ -145,7 +144,7 @@ public final class QrSegment {
/** The length of this segment's unencoded data. Measured in characters for /** The length of this segment's unencoded data. Measured in characters for
* numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode. * numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
* Always zero or positive. Not the same as the data's bit length. */ * Always zero or positive. Not the same as the data's bit length. */
public final int numChars; public final int numberOfCharacters;
// The data bits of this segment. Not null. Accessed through getData(). // The data bits of this segment. Not null. Accessed through getData().
final BitBuffer data; final BitBuffer data;

Loading…
Cancel
Save