[Picross-commit] SF.net SVN: picross: [41] trunk
Status: Pre-Alpha
Brought to you by:
yvan_norsa
From: <yva...@us...> - 2007-06-17 18:55:45
|
Revision: 41 http://picross.svn.sourceforge.net/picross/?rev=41&view=rev Author: yvan_norsa Date: 2007-06-17 11:55:46 -0700 (Sun, 17 Jun 2007) Log Message: ----------- more tests Modified Paths: -------------- trunk/build.xml trunk/src/picross/PicrossException.java trunk/src/picross/game/simple/XBMModel.java trunk/src/picross/tests/AbstractPicrossGridTest.java Added Paths: ----------- trunk/src/picross/game/simple/XBMException.java trunk/src/picross/game/simple/XBMModelTest.java Modified: trunk/build.xml =================================================================== --- trunk/build.xml 2007-06-15 19:57:57 UTC (rev 40) +++ trunk/build.xml 2007-06-17 18:55:46 UTC (rev 41) @@ -148,8 +148,8 @@ <target name="doc" depends="compile"> - <!-- FIXME test classes which are *not* in a "tests" subpackage - are included in the Javadoc --> + <!-- FIXME test classes which are not in a "tests" subpackage + shouldn't be included in the generated Javadoc --> <javadoc destdir="${doc.dir}" link="http://java.sun.com/j2se/1.5.0/docs/api"> <packageset dir="${src.dir}" Modified: trunk/src/picross/PicrossException.java =================================================================== --- trunk/src/picross/PicrossException.java 2007-06-15 19:57:57 UTC (rev 40) +++ trunk/src/picross/PicrossException.java 2007-06-17 18:55:46 UTC (rev 41) @@ -38,13 +38,13 @@ * * @author Y. Norsa */ -public final class PicrossException extends Exception { +public class PicrossException extends Exception { /*** Constant ***/ /** Serialisation ID. */ private static final long serialVersionUID = 1716838910721477345L; - /*** Constructor ***/ + /*** Constructors ***/ /** * Constructor. @@ -54,5 +54,14 @@ public PicrossException(Throwable cause) { super(cause); } + + /** + * Constructor. + * + * @param msg message describing the exception + */ + public PicrossException(String msg) { + super(msg); + } } Added: trunk/src/picross/game/simple/XBMException.java =================================================================== --- trunk/src/picross/game/simple/XBMException.java (rev 0) +++ trunk/src/picross/game/simple/XBMException.java 2007-06-17 18:55:46 UTC (rev 41) @@ -0,0 +1,68 @@ +/* + * $Id$ + * + * Copyright (c) 2007 + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + + +package picross.game.simple; + +import picross.PicrossException; + +/** + * Exception thrown when reading an invalid XBM file. + * + * @author Y. Norsa + */ +final class XBMException extends PicrossException { + /*** Constant ***/ + + /** Serialisation ID. */ + private static final long serialVersionUID = 3928727649077087059L; + + /*** Constructors ***/ + + /** + * Constructor. + * + * @param msg message describing the exception + */ + XBMException(String msg) { + super(msg); + } + + /** + * Constructor. + * + * @param cause parent exception + */ + XBMException(Throwable cause) { + super(cause); + } +} Property changes on: trunk/src/picross/game/simple/XBMException.java ___________________________________________________________________ Name: svn:keywords + Id Modified: trunk/src/picross/game/simple/XBMModel.java =================================================================== --- trunk/src/picross/game/simple/XBMModel.java 2007-06-15 19:57:57 UTC (rev 40) +++ trunk/src/picross/game/simple/XBMModel.java 2007-06-17 18:55:46 UTC (rev 41) @@ -51,6 +51,44 @@ * @author Y. Norsa */ final class XBMModel extends AbstractPicrossModel { + /*** Constants ***/ + + /** Indicates a constant definition. */ + private static final String DEFINE_DIRECTIVE = "#define "; + + /** The width value. */ + private static final String WIDTH_KEYWORD = "width"; + + /** The height value. */ + private static final String HEIGHT_KEYWORD = "height"; + + /** Character before the int value. */ + private static final char VALUE_MARKER = ' '; + + /** The data values. */ + private static final String BITS_KEYWORD = "bits"; + + /** Value separator. */ + private static final String VALUE_SEPARATOR = ","; + + /** String indicating a hex value. */ + private static final String HEX_LEADING = "0x"; + + /** In case of error. */ + private static final int ERROR_VALUE = -1; + + /** Beginning of the actual value. */ + private static final int VALUE_BEGINNING_POS = 2; + + /** End of the actual value. */ + private static final int VALUE_END_POS = 4; + + /** Hexadecimal number radix. */ + private static final int HEX_RADIX = 16; + + /** Length of a byte. */ + private static final int BYTE_LENGTH = 8; + /*** Static field ***/ /** The class' logger. */ @@ -62,11 +100,19 @@ * Constructor. * * @param input XBM file input stream + * @throws IllegalArgumentException if <code>input</code> + * is <code>null</code> * @throws IOException if there is a problem while reading the file + * @throws XBMException if the file isn't a valid XBM file */ - XBMModel(InputStream input) throws IOException { + XBMModel(InputStream input) throws IOException, XBMException, + IllegalArgumentException { super(); + if (input == null) { + throw new IllegalArgumentException("input can't be null"); + } + List<String> byteValues = new ArrayList<String>(); BufferedReader in = new BufferedReader(new InputStreamReader(input)); @@ -75,37 +121,39 @@ try { while ((line = in.readLine()) != null) { - if (line.contains("width")) { - this.width = - Integer.parseInt(line.substring(line - .lastIndexOf(' ')) - .trim()); + if (line.startsWith(XBMModel.DEFINE_DIRECTIVE) + && line.contains(XBMModel.WIDTH_KEYWORD)) { + + this.width = XBMModel.readLineValue(line); + XBMModel.log.debug("width = " + this.width); continue; } - if (line.contains("height")) { - this.height = - Integer.parseInt(line.substring(line - .lastIndexOf(' ')) - .trim()); + if (line.startsWith(XBMModel.DEFINE_DIRECTIVE) + && line.contains(XBMModel.HEIGHT_KEYWORD)) { + + this.height = XBMModel.readLineValue(line); + XBMModel.log.debug("height = " + this.height); continue; } - if (line.contains("bits")) { + if (line.contains(XBMModel.BITS_KEYWORD)) { this.data = new boolean[this.width][this.height]; while ((line = in.readLine()) != null) { - String[] values = line.split(","); + String[] values = line.split(XBMModel.VALUE_SEPARATOR); for (int i = 0; i < values.length; i++) { //XBMModel.log.debug("values[" + i + "] = " //+ values[i]); - if (values[i].contains("0x")) { + if (values[i].contains(XBMModel.HEX_LEADING)) { String byteStr = - values[i].trim().substring(2, 4); + values[i].trim() + .substring(XBMModel.VALUE_BEGINNING_POS, + XBMModel.VALUE_END_POS); byteValues.add(byteStr); } } @@ -122,20 +170,19 @@ } } + if (width <= 0 || height <= 0 || this.data == null) { + throw new XBMException("Invalid XBM file"); + } + int xIndex = 0; int yIndex = 0; for (String byteStr : byteValues) { - int byteVal = Integer.parseInt(byteStr, 16); - String binaryStr = Integer.toBinaryString(byteVal); + String binaryStr = XBMModel.toBits(byteStr); - while (binaryStr.length() < 8) { - binaryStr = "0" + binaryStr; - } - //XBMModel.log.debug(binaryStr); - for (int j = 8 - 1; j >= 0; j--) { + for (int j = XBMModel.BYTE_LENGTH - 1; j >= 0; j--) { //XBMModel.log.debug("this.data[" + yIndex //+ "][" + xIndex + "] = " //+ (binaryStr.charAt(j) == '1' ? true @@ -155,5 +202,45 @@ } } } + + /*** Static methods ***/ + + /** + * Reads a constant value (C-style) in a line. + * + * @param line the line to read + * @return defined value or -1 + * @throws XBMException if the line format is incorrect + */ + private static int readLineValue(String line) throws XBMException { + int spaceIndex = line.lastIndexOf(XBMModel.VALUE_MARKER); + + if (spaceIndex == -1) { + return XBMModel.ERROR_VALUE; + } + + try { + return Integer.parseInt(line.substring(spaceIndex).trim()); + } catch (NumberFormatException numberEx) { + throw new XBMException(numberEx); + } + } + + /** + * Converts a hex value to its binary representation. + * + * @param byteStr the original String + * @return binary representation + */ + private static String toBits(String byteStr) { + int byteVal = Integer.parseInt(byteStr, XBMModel.HEX_RADIX); + String binaryStr = Integer.toBinaryString(byteVal); + + while (binaryStr.length() < XBMModel.BYTE_LENGTH) { + binaryStr = '0' + binaryStr; + } + + return binaryStr; + } } Added: trunk/src/picross/game/simple/XBMModelTest.java =================================================================== --- trunk/src/picross/game/simple/XBMModelTest.java (rev 0) +++ trunk/src/picross/game/simple/XBMModelTest.java 2007-06-17 18:55:46 UTC (rev 41) @@ -0,0 +1,99 @@ +/* + * $Id$ + * + * Copyright (c) 2007 + * + * This software is governed by the CeCILL license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL license and that you accept its terms. + */ + + +package picross.game.simple; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.apache.log4j.PropertyConfigurator; + +import org.junit.Assert; +import org.junit.Test; + +import picross.Picross; +import picross.PicrossGrid; + +import picross.tests.AbstractPicrossGridTest; + +/** + * Unit tests for XBMModel. + * + * @author Y. Norsa + */ +public final class XBMModelTest extends AbstractPicrossGridTest { + /*** Static field ***/ + + /** Valid XBM model. */ + private static PicrossGrid realModel; + + /*** Static initialisation block. Loads a valid XBM file. ***/ + static { + PropertyConfigurator.configure("log4j.properties"); + + try { + XBMModelTest.realModel = + new XBMModel(Picross.loadDataFile("blarg.xbm")); + } catch (IOException ioEx) { + throw new ExceptionInInitializerError(ioEx); + } catch (XBMException xbmEx) { + throw new ExceptionInInitializerError(xbmEx); + } + } + + /*** Method overloaded from the class AbstractPicrossGridTest ***/ + + /** {@inheritDoc} */ + protected PicrossGrid getPicrossGrid() { + return XBMModelTest.realModel; + } + + /** + * Tests the XBMModel constructor. + * + * @throws IOException if there is a problem with a stream loading + * @throws XBMException if a file isn't valid + */ + @Test + public void testXBMModel() throws IOException, XBMException { + try { + new XBMModel(null); + Assert.fail("Tried to load a null model"); + } catch (IllegalArgumentException argEx) { } + + try { + new XBMModel(new ByteArrayInputStream("".getBytes())); + Assert.fail("Empty file"); + } catch (XBMException xbmEx) { } + } +} Property changes on: trunk/src/picross/game/simple/XBMModelTest.java ___________________________________________________________________ Name: svn:keywords + Id Modified: trunk/src/picross/tests/AbstractPicrossGridTest.java =================================================================== --- trunk/src/picross/tests/AbstractPicrossGridTest.java 2007-06-15 19:57:57 UTC (rev 40) +++ trunk/src/picross/tests/AbstractPicrossGridTest.java 2007-06-17 18:55:46 UTC (rev 41) @@ -97,5 +97,18 @@ Assert.assertEquals("data[" + i + "].length = " + data[i].length, height, data[i].length); } + + boolean atLeastOneCheckedBox = false; + + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + if (data[i][j]) { + atLeastOneCheckedBox = true; + break; + } + } + } + + Assert.assertTrue("Empty grid", atLeastOneCheckedBox); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |