/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn_junit.util; import xbn.util.XBNTestCase; import xbn.AssertException; import xbn.util.UtilInt; import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; public class JUTUtilInt extends XBNTestCase { private UtilInt uInt = null; public JUTUtilInt(String s_name) { super(s_name); } public void test_areRangeBoundsValid() { //Do not check at all. xassertTrue(uInt.areRangeBoundsValid(2, false, 1, false, false)); //Do check, but nothing to enforce. xassertTrue(uInt.areRangeBoundsValid(2, false, 1, false)); xassertTrue(uInt.areRangeBoundsValid(2, false, 1, false, true)); //Do check, but only one bound to enforce xassertTrue(uInt.areRangeBoundsValid(-1, true, -1, false)); xassertTrue(uInt.areRangeBoundsValid(-1, false, -1, true)); xassertTrue(uInt.areRangeBoundsValid(-1, true, -1, false, true)); xassertTrue(uInt.areRangeBoundsValid(-1, false, -1, true, true)); //Enforce both xassertFalse(uInt.areRangeBoundsValid(1, 0)); xassertTrue(uInt.areRangeBoundsValid(1, 1)); xassertTrue(uInt.areRangeBoundsValid(1, 2)); xassertFalse(uInt.areRangeBoundsValid(1, true, 0, true)); xassertTrue(uInt.areRangeBoundsValid(1, true, 1, true)); xassertTrue(uInt.areRangeBoundsValid(1, true, 2, true)); xassertFalse(uInt.areRangeBoundsValid(1, true, 0, true, true)); xassertTrue(uInt.areRangeBoundsValid(1, true, 1, true, true)); xassertTrue(uInt.areRangeBoundsValid(1, true, 2, true, true)); } public void test_isInRange() { //Do not check range AND do not enforce bounds. xassertTrue(uInt.isInRange(1, 2, false, 1, false, false)); try { //Check range, range BAD uInt.isInRange(1, 2, true, 1, true); xfail("isInRange bad range (1), but no exception"); } catch(AssertException ax) { xassertTrue(); } try { //Check range, range BAD uInt.isInRange(1, 2, true, 1, true, true); xfail("isInRange bad range (2), but no exception"); } catch(AssertException ax) { xassertTrue(); } try { //Range BAD uInt.isBetween(1, 2, 1); xfail("isBetween bad range, but no exception"); } catch(AssertException ax) { xassertTrue(); } try { //Range BAD uInt.isBetweenInclusive(1, 2, 1); xfail("isBetweenInclusive bad range, but no exception"); } catch(AssertException ax) { xassertTrue(); } //Do enforce range, range good. xassertFalse(uInt.isBetween(1, 1, 3)); xassertFalse(uInt.isBetween(0, 1, 3)); xassertFalse(uInt.isBetween(4, 1, 3)); xassertTrue(uInt.isBetween(2, 1, 3)); xassertTrue(uInt.isBetweenInclusive(1, 1, 3)); xassertFalse(uInt.isBetweenInclusive(0, 1, 3)); xassertFalse(uInt.isBetweenInclusive(4, 1, 3)); xassertTrue(uInt.isBetweenInclusive(2, 1, 3)); xassertTrue(uInt.isInRange(1, 1, true, 3, true)); xassertFalse(uInt.isInRange(0, 1, true, 3, true)); xassertFalse(uInt.isInRange(4, 1, true, 3, true)); xassertTrue(uInt.isInRange(2, 1, true, 3, true)); xassertTrue(uInt.isInRange(1, 1, true, 3, true, true)); xassertFalse(uInt.isInRange(0, 1, true, 3, true, true)); xassertFalse(uInt.isInRange(4, 1, true, 3, true, true)); xassertTrue(uInt.isInRange(2, 1, true, 3, true, true)); } public void test_isGTOEMin() { //Do not enforce xassertTrue("isGTOEMin(-1, -1, false)", uInt.isGTOEMin(-1, -1, false)); //Do enforce xassertTrue("isGTOEMin(1, 1, true)", uInt.isGTOEMin(1, 1, true)); xassertTrue("isGTOEMin(2, 1, true)", uInt.isGTOEMin(2, 1, true)); xassertFalse("isGTOEMin(0, 1, true)", uInt.isGTOEMin(0, 1, true)); } public void test_isLTOEMax() { //Do not enforce xassertTrue(uInt.isLTOEMax(-1, -1, false)); //Do enforce xassertTrue(uInt.isLTOEMax(1, 1, true)); xassertFalse(uInt.isLTOEMax(2, 1, true)); xassertTrue(uInt.isLTOEMax(0, 1, true)); } public void test_getDigitLength() { xassertTrue(1 == uInt.getDigitLength(1)); xassertTrue(1 == uInt.getDigitLength(-1)); xassertTrue(2 == uInt.getDigitLength(10)); xassertTrue(2 == uInt.getDigitLength(-10)); xassertTrue(3 == uInt.getDigitLength(100)); xassertTrue(3 == uInt.getDigitLength(-100)); xassertTrue(4 == uInt.getDigitLength(8888)); xassertTrue(4 == uInt.getDigitLength(-8888)); xassertTrue(6 == uInt.getDigitLength(888888)); xassertTrue(6 == uInt.getDigitLength(-888888)); } public void test_getIdxNEToLmnt0() { //If is null (yes)... try { uInt.getIdxNEToLmnt0(null); xfail("getIdxNEToLmnt0(null) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } //If is 0 elements (yes)... try { uInt.getIdxNEToLmnt0(new int[] {}); xassertTrue(); } catch(AssertException ax) { xfail("getIdxNEToLmnt0(new int[] {}) should not throw an ax: " + ax.toString()); } //...else not null AND at least one element try { uInt.getIdxNEToLmnt0(new int[] {8}); xassertTrue(); } catch(AssertException ax) { xfail("getIdxNEToLmnt0(new int[] {8}) should not throw an ax: " + ax.toString()); } //Only one element. xassertTrue(-1 == uInt.getIdxNEToLmnt0(new int[] {8})); //More than one element xassertTrue(-1 == uInt.getIdxNEToLmnt0(new int[] {8, 8, 8, 8})); //Legal xassertTrue(2 == uInt.getIdxNEToLmnt0(new int[] {8, 8, 7, 8})); } public void setUp() throws Exception { super.setUp(); uInt = new UtilInt(); } public void tearDown() throws Exception { super.tearDown(); uInt = null; } public static Test suite() { TestSuite ts = new TestSuite(JUTUtilInt.class); return ts; } public static void main(String[] as_cmdLineParams) { TestRunner.run(suite()); printSummary(); } }