/* 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.string; import xbn.util.XBNTestCase; import xbn.AssertException; import xbn.string.SOBString; import xbn.string.SOBStringBuffer; import xbn.string.StringOrBuffer; import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; public class JUTStringOrBuffer extends XBNTestCase { //ENTIRE (and obvious to understand from the name) strings used four or more times private static final String sHTMF = "Hello there, my friend."; private static final String sX = "x"; private static final String sY = "y"; private static final String sES = ""; private static final String s12345 = "12345"; private static final char cL = 'l'; private SOBString ssES = null; private SOBStringBuffer ssbES = null; private SOBString ssHTMF = null; private SOBStringBuffer ssbHTMF = null; public JUTStringOrBuffer(String s_name) { super(s_name); } public void test_constructors() { //ss...START //The passed in value should equal the stored value xassertTrue(sES.equals(ssES.toString())); xassertTrue(sX.equals((new SOBString(sX)).toString())); try { new SOBString(null); xfail("new SOBString(null) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } //ssb...END //ssb...START //The passed in value should equal the stored value xassertTrue(sES.equals(ssbES.toString())); //The passed in sb should be directly stored (not cloned //then stored) StringBuffer sb = new StringBuffer(sES); xassertTrue(sb == (new SOBStringBuffer(sb)).getStringBuffer()); xassertTrue(sX.equals((new SOBStringBuffer(sX)).toString())); xassertTrue(sX.equals((new SOBStringBuffer(new StringBuffer(sX))).toString())); try { new SOBStringBuffer((String)null); xfail("new SOBString((String)null) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } try { new SOBStringBuffer((StringBuffer)null); xfail("new SOBString((StringBuffer)null) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } //ssb...END } public void test_isStringBuffer() { xassertTrue(ssES.isString()); xassertFalse(ssES.isStringBuffer()); xassertTrue(ssbES.isStringBuffer()); xassertFalse(ssbES.isString()); } public void test_length() { SOBString ss = new SOBString(s12345); SOBStringBuffer ssb = new SOBStringBuffer(s12345); xassertTrue(5 == ss.length()); xassertTrue(5 == ssb.length()); } public void test_isEmpty() { xassertTrue(ssES.isEmpty()); xassertTrue(ssbES.isEmpty()); SOBString ss = new SOBString(s12345); SOBStringBuffer ssb = new SOBStringBuffer(s12345); xassertFalse(ss.isEmpty()); xassertFalse(ssb.isEmpty()); } public void test_startsWith() { xassertTrue(ssHTMF.startsWith("Hel")); xassertTrue(ssbHTMF.startsWith("Hel")); xassertFalse(ssHTMF.startsWith(sX)); xassertFalse(ssbHTMF.startsWith(sX)); xassertFalse(ssHTMF.startsWith(sHTMF + sX)); xassertFalse(ssbHTMF.startsWith(sHTMF + sX)); } public void test_endsWith() { xassertTrue(ssHTMF.endsWith("end.")); xassertTrue(ssbHTMF.endsWith("end.")); xassertFalse(ssHTMF.endsWith(sX)); xassertFalse(ssbHTMF.endsWith(sX)); xassertFalse(ssHTMF.endsWith(sX + sHTMF)); xassertFalse(ssbHTMF.endsWith(sX + sHTMF)); } public void test_getTrimmed() { SOBString ss = new SOBString(" x "); SOBStringBuffer ssb = new SOBStringBuffer(" y "); xassertTrue(sX.equals(ss.getTrimmed())); xassertTrue(sY.equals(ssb.getTrimmed())); //getTrimmed() should not affect the internally-held value. xassertTrue(" x ".equals(ss.toString())); xassertTrue(" y ".equals(ssb.toString())); } public void test_trim() { String sXWithSpaces = " x "; String sYWithSpaces = " y "; SOBString ss = new SOBString(sXWithSpaces); SOBStringBuffer ssb = new SOBStringBuffer(sYWithSpaces); //trim() should affect the value. ss.trim(); xassertTrue(sX.equals(ss.toString())); ssb.trim(); xassertTrue(sY.equals(ssb.toString())); } public void test_charAt() { //JACTAF xassertTrue('e' == ssHTMF.charAt(1)); xassertTrue('e' == ssbHTMF.charAt(1)); xassertTrue(cL == ssHTMF.charAt(2)); xassertTrue(cL == ssbHTMF.charAt(2)); } public void test_append() { ssHTMF.append(sX); xassertTrue((sHTMF + sX).equals(ssHTMF.toString())); ssHTMF.append(sES); xassertTrue((sHTMF + sX).equals(ssHTMF.toString())); ssHTMF.append('1'); xassertTrue((sHTMF + "x1").equals(ssHTMF.toString())); //JACTAF ssbHTMF.append(sY); xassertTrue((sHTMF + sY).equals(ssbHTMF.toString())); ssbHTMF.append('2'); xassertTrue((sHTMF + "y2").equals(ssbHTMF.toString())); } public void test_appendToLeft() { ssHTMF.appendToLeft(sX); xassertTrue((sX + sHTMF).equals(ssHTMF.toString())); ssHTMF.appendToLeft(sES); xassertTrue((sX + sHTMF).equals(ssHTMF.toString())); ssHTMF.appendToLeft('1'); xassertTrue(("1x" + sHTMF).equals(ssHTMF.toString())); //JACTAF ssbHTMF.appendToLeft(sY); xassertTrue((sY + sHTMF).equals(ssbHTMF.toString())); ssbHTMF.appendToLeft('2'); xassertTrue(("2y" + sHTMF).equals(ssbHTMF.toString())); } public void test_insert() { ssHTMF.insert(1, sX); xassertTrue("Hxello there, my friend.".equals(ssHTMF.toString())); ssHTMF.insert(1, sES); xassertTrue("Hxello there, my friend.".equals(ssHTMF.toString())); ssHTMF.insert(1, '1'); xassertTrue("H1xello there, my friend.".equals(ssHTMF.toString())); //JACTAF ssbHTMF.insert(2, sY); xassertTrue("Heyllo there, my friend.".equals(ssbHTMF.toString())); ssbHTMF.insert(2, '2'); xassertTrue("He2yllo there, my friend.".equals(ssbHTMF.toString())); } public void test_deleteCharAt() { ssHTMF.deleteCharAt(1); xassertTrue("Hllo there, my friend.".equals(ssHTMF.toString())); //JACTAF ssbHTMF.deleteCharAt(2); xassertTrue("Helo there, my friend.".equals(ssbHTMF.toString())); } public void test_delete() { ssHTMF.delete(1, 3); xassertTrue("Hlo there, my friend.".equals(ssHTMF.toString())); //JACTAF ssbHTMF.delete(2, 4); xassertTrue("Heo there, my friend.".equals(ssbHTMF.toString())); } public void test_setLength() { ssHTMF.setLength(13); xassertTrue("Hello there, ".equals(ssHTMF.toString())); //JACTAF ssbHTMF.setLength(14); xassertTrue("Hello there, m".equals(ssbHTMF.toString())); } public void test_substring() { //JACTAF xassertTrue("ello there, ".equals(ssHTMF.substring(1, 13))); xassertTrue("llo there, m".equals(ssbHTMF.substring(2, 14))); xassertTrue("ello there, my friend.".equals(ssHTMF.substring(1))); xassertTrue("llo there, my friend.".equals(ssbHTMF.substring(2))); } public void test_deleteAll() { ssHTMF.deleteAll(); ssbHTMF.deleteAll(); xassertTrue(sES.equals(ssHTMF.toString())); xassertTrue(sES.equals(ssbHTMF.toString())); } public void test_indexOf_lastIndexOf() { //Twisted bounds io_lio_assertAX(sHTMF, 1, 0); //Bounds must be at least one apart, can't be less //than zero or greater than length() io_lio_assertAX(sHTMF, -1, 0); io_lio_assertAX(sHTMF, 0, -1); io_lio_assertAX(sX, 1, 100); io_lio_assertAX(sX, 100, 101); //Null/empty to-search-for. io_lio_assertAX(null, 1, 2); io_lio_assertAX(sES, 1, 2); //Search for the whole string. I can't believe I ate the //*whole* string. io_lio_assertTrue(0, 0, sHTMF, 0, sHTMF.length()); io_lio_assertTrue(-1, 0, sHTMF, 0, (sHTMF.length() - 1)); io_lio_assertTrue(-1, -1, sHTMF, 1, sHTMF.length()); //Left side String sHT = "Hello there"; io_lio_assertTrue(0, sHT); io_lio_assertTrue(0, sHT, 0); io_lio_assertTrue(-1, sHT, 1); io_lio_assertTrue(-1, sHT, (sHTMF.length() - 1)); io_lio_assertTrue(0, 0, sHT, 0, sHTMF.length()); io_lio_assertTrue(0, 0, sHT, 0, sHT.length()); io_lio_assertTrue(-1, -1, sHT, 1, sHT.length()); io_lio_assertTrue(-1, 0, sHT, 0, (sHT.length() - 1)); //Right side String sMF = "my friend."; io_lio_assertTrue(13, sMF); io_lio_assertTrue(13, 13, sMF, 0, sHTMF.length()); io_lio_assertTrue(13, 13, sMF, 0, sHTMF.length()); io_lio_assertTrue(13, 13, sMF, 1, sHTMF.length()); io_lio_assertTrue(-1, 13, sMF, 0, (sHTMF.length() - 3)); io_lio_assertTrue(13, 13, sMF, 13, (13 + sMF.length())); io_lio_assertTrue(-1, 13, sMF, 0, (sHTMF.length() - sMF.length())); io_lio_assertTrue(-1, -1, sMF, 0, (sHTMF.length() - (sMF.length() + 1))); //Middle String sTM = "there, my"; io_lio_assertTrue(6, sTM); io_lio_assertTrue(6, 6, sTM, 0, sHTMF.length()); io_lio_assertTrue(6, 6, sTM, 0, sHTMF.length()); io_lio_assertTrue(6, 6, sTM, 1, sHTMF.length()); io_lio_assertTrue(-1, 6, sTM, 0, (6 + sTM.length() - 1)); io_lio_assertTrue(-1, 6, sTM, 6, (6 + sTM.length() - 1)); io_lio_assertTrue(-1, 6, sTM, 6, 7); io_lio_assertTrue(-1, 6, sTM, 0, 6); io_lio_assertTrue(-1, -1, sTM, 0, 5); //Single character, single occurance String sm = "m"; io_lio_assertTrue(13, sm); io_lio_assertTrue(13, 13, sm, 0, sHTMF.length()); io_lio_assertTrue(13, 13, sm, 0, sHTMF.length()); io_lio_assertTrue(13, 13, sm, 1, sHTMF.length()); io_lio_assertTrue(-1, -1, sm, 0, 12); io_lio_assertTrue(-1, -1, sm, 0, 0); io_lio_assertTrue(-1, 13, sm, 13, 13); io_lio_assertTrue(13, 13, sm, 13, 14); io_lio_assertTrue(-1, 13, sm, 0, 13); io_lio_assertTrue(-1, -1, sm, 0, 12); //Single character, multiple occurance xassertTrue(2 == ssHTMF.indexOf(cL)); xassertTrue(2 == ssHTMF.indexOf(cL, 1)); xassertTrue(2 == ssHTMF.indexOf(cL, 2)); xassertTrue(3 == ssHTMF.indexOf(cL, 3)); xassertTrue(-1 == ssHTMF.indexOf(cL, 4)); xassertTrue(-1 == ssHTMF.indexOf(cL, 0, 1)); xassertTrue(-1 == ssHTMF.indexOf(cL, 0, 2)); xassertTrue(2 == ssHTMF.indexOf(cL, 0, 3)); xassertTrue(2 == ssHTMF.indexOf(cL, 0, 4)); xassertTrue(-1 == ssHTMF.indexOf(cL, 1, 1)); xassertTrue(-1 == ssHTMF.indexOf(cL, 1, 2)); xassertTrue(2 == ssHTMF.indexOf(cL, 1, 3)); xassertTrue(2 == ssHTMF.indexOf(cL, 1, 4)); xassertTrue(-1 == ssHTMF.indexOf(cL, 2, 2)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 3)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 4)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 5)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 4)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 5)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 6)); xassertTrue(3 == ssHTMF.lastIndexOf(cL)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 2)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 3)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 4)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 0, 1)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 0, 2)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 0, 3)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 0, 4)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1, 1)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1, 2)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 1, 3)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 1, 4)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 2, 2)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 2, 3)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 2, 4)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 2, 5)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 3, 3)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 4)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 5)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 6)); //Same, explicitely bounded. xassertTrue(-1 == ssHTMF.indexOf(cL, 0, 1, true)); xassertTrue(-1 == ssHTMF.indexOf(cL, 0, 2, true)); xassertTrue(2 == ssHTMF.indexOf(cL, 0, 3, true)); xassertTrue(2 == ssHTMF.indexOf(cL, 0, 4, true)); xassertTrue(-1 == ssHTMF.indexOf(cL, 1, 1, true)); xassertTrue(-1 == ssHTMF.indexOf(cL, 1, 2, true)); xassertTrue(2 == ssHTMF.indexOf(cL, 1, 3, true)); xassertTrue(2 == ssHTMF.indexOf(cL, 1, 4, true)); xassertTrue(-1 == ssHTMF.indexOf(cL, 2, 2, true)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 3, true)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 4, true)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 5, true)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 4, true)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 5, true)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 6, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 2, true)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 3, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 4, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 0, 1, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 0, 2, true)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 0, 3, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 0, 4, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1, 1, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1, 2, true)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 1, 3, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 1, 4, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 2, 2, true)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 2, 3, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 2, 4, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 2, 5, true)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 3, 3, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 4, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 5, true)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 6, true)); //Same, unbounded. xassertTrue(-1 == ssHTMF.indexOf(cL, 0, 1, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 0, 2, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 0, 3, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 0, 4, false)); xassertTrue(-1 == ssHTMF.indexOf(cL, 1, 1, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 1, 2, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 1, 3, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 1, 4, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 2, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 3, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 4, false)); xassertTrue(2 == ssHTMF.indexOf(cL, 2, 5, false)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 4, false)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 5, false)); xassertTrue(3 == ssHTMF.indexOf(cL, 3, 6, false)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1, false)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 2, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 4, false)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 0, 1, false)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 0, 2, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 0, 3, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 0, 4, false)); xassertTrue(-1 == ssHTMF.lastIndexOf(cL, 1, 1, false)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 1, 2, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 1, 3, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 1, 4, false)); xassertTrue(2 == ssHTMF.lastIndexOf(cL, 2, 2, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 2, 3, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 2, 4, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 2, 5, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 3, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 4, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 5, false)); xassertTrue(3 == ssHTMF.lastIndexOf(cL, 3, 6, false)); //Documentation examples SOBStringBuffer ssb = new SOBStringBuffer("abcdef"); final String sBCDE = "bcde"; xassertTrue(1 == ssb.indexOf(sBCDE, 0, ssb.length(), true)); xassertTrue(-1 == ssb.indexOf(sBCDE, 0, 1, true)); xassertTrue(1 == ssb.indexOf(sBCDE, 0, 1, false)); xassertTrue(-1 == ssb.lastIndexOf(sBCDE, 1, 1, true)); xassertTrue(1 == ssb.lastIndexOf(sBCDE, 1, 1, false)); xassertTrue(-1 == ssb.indexOf(sBCDE, 0, 0, true)); xassertTrue(-1 == ssb.indexOf(sBCDE, 0, 3, true)); xassertTrue(1 == ssb.indexOf(sBCDE, 0, 3, false)); xassertTrue(-1 == ssb.indexOf(sBCDE, 2, ssb.length(), true)); xassertTrue(-1 == ssb.indexOf(sBCDE, 2, ssb.length(), false)); xassertTrue(1 == ssb.lastIndexOf(sBCDE, 0, ssb.length(), true)); xassertTrue(-1 == ssb.lastIndexOf(sBCDE, 0, 1, true)); xassertTrue(1 == ssb.lastIndexOf(sBCDE, 0, 1, false)); xassertTrue(-1 == ssb.lastIndexOf(sBCDE, 1, 1, true)); xassertTrue(1 == ssb.lastIndexOf(sBCDE, 1, 1, false)); xassertTrue(-1 == ssb.lastIndexOf(sBCDE, 0, 0, true)); xassertTrue(-1 == ssb.lastIndexOf(sBCDE, 0, 3, true)); xassertTrue(1 == ssb.lastIndexOf(sBCDE, 0, 3, false)); xassertTrue(-1 == ssb.lastIndexOf(sBCDE, 2, ssb.length(), true)); xassertTrue(-1 == ssb.lastIndexOf(sBCDE, 2, ssb.length(), false)); } private void io_lio_assertTrue(int i_idxExpected, String s_toSearchFor) { xassertTrue(i_idxExpected == ssHTMF.indexOf(s_toSearchFor)); xassertTrue(i_idxExpected == ssbHTMF.indexOf(s_toSearchFor)); xassertTrue(i_idxExpected == ssHTMF.lastIndexOf(s_toSearchFor)); xassertTrue(i_idxExpected == ssbHTMF.lastIndexOf(s_toSearchFor)); if(s_toSearchFor.length() == 1) { char c = s_toSearchFor.charAt(0); xassertTrue(i_idxExpected == ssHTMF.indexOf(c)); xassertTrue(i_idxExpected == ssbHTMF.indexOf(c)); xassertTrue(i_idxExpected == ssHTMF.lastIndexOf(c)); xassertTrue(i_idxExpected == ssbHTMF.lastIndexOf(c)); } } private void io_lio_assertTrue(int i_idxExpected, String s_toSearchFor, int i_idxLeft) { xassertTrue(i_idxExpected == ssHTMF.indexOf(s_toSearchFor, i_idxLeft)); xassertTrue(i_idxExpected == ssbHTMF.indexOf(s_toSearchFor, i_idxLeft)); xassertTrue(i_idxExpected == ssHTMF.indexOf(s_toSearchFor, i_idxLeft, ssHTMF.length())); xassertTrue(i_idxExpected == ssbHTMF.indexOf(s_toSearchFor, i_idxLeft, ssbHTMF.length())); if(i_idxLeft == 0) { //The one-param version defaults to "0, length()" xassertTrue(i_idxExpected == ssHTMF.indexOf(s_toSearchFor)); xassertTrue(i_idxExpected == ssbHTMF.indexOf(s_toSearchFor)); //The two-param version of lio defaults i_idxLeft to zero. xassertTrue(i_idxExpected == ssHTMF.lastIndexOf(s_toSearchFor, ssHTMF.length())); xassertTrue(i_idxExpected == ssbHTMF.lastIndexOf(s_toSearchFor, ssbHTMF.length())); } if(s_toSearchFor.length() == 1) { char c = s_toSearchFor.charAt(0); xassertTrue(i_idxExpected == ssHTMF.indexOf(c, i_idxLeft)); xassertTrue(i_idxExpected == ssbHTMF.indexOf(c, i_idxLeft)); if(i_idxLeft == 0) { //The one-param version defaults to "0, length()" xassertTrue(i_idxExpected == ssHTMF.indexOf(c)); xassertTrue(i_idxExpected == ssbHTMF.indexOf(c)); xassertTrue(i_idxExpected == ssHTMF.lastIndexOf(c)); xassertTrue(i_idxExpected == ssbHTMF.lastIndexOf(c)); } } } private void io_lio_assertTrue(int i_idxXpctdBndd, int i_idxXpctdUnbndd, String s_toSearchFor, int i_idxLeft, int i_idxAfterRight) { if(i_idxAfterRight == ssHTMF.length()) { //We can also call the two-param version of indexOf, //which defaults i_idxAfterRight to length(). xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(s_toSearchFor, i_idxLeft)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(s_toSearchFor, i_idxLeft)); if(i_idxLeft == 0) { //The one-param version defaults to "0, length()" xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(s_toSearchFor)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(s_toSearchFor)); xassertTrue(i_idxXpctdBndd == ssHTMF.lastIndexOf(s_toSearchFor)); xassertTrue(i_idxXpctdBndd == ssbHTMF.lastIndexOf(s_toSearchFor)); } } xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight)); xassertTrue(i_idxXpctdBndd == ssHTMF.lastIndexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight)); xassertTrue(i_idxXpctdBndd == ssbHTMF.lastIndexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight)); //Same, but explicitely bounded xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, true)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, true)); xassertTrue(i_idxXpctdBndd == ssHTMF.lastIndexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, true)); xassertTrue(i_idxXpctdBndd == ssbHTMF.lastIndexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, true)); //Unbounded xassertTrue(i_idxXpctdUnbndd == ssHTMF.indexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, false)); xassertTrue(i_idxXpctdUnbndd == ssbHTMF.indexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, false)); xassertTrue(i_idxXpctdUnbndd == ssHTMF.lastIndexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, false)); xassertTrue(i_idxXpctdUnbndd == ssbHTMF.lastIndexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight, false)); if(s_toSearchFor.length() == 1) { char c = s_toSearchFor.charAt(0); if(i_idxAfterRight == ssHTMF.length()) { //We can also call the two-param version, which defaults to length() xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(c, i_idxLeft)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(c, i_idxLeft)); if(i_idxLeft == 0) { //The one-param version defaults to "0, length()" xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(c)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(c)); xassertTrue(i_idxXpctdBndd == ssHTMF.lastIndexOf(c)); xassertTrue(i_idxXpctdBndd == ssbHTMF.lastIndexOf(c)); } } else if(i_idxLeft == 0) { xassertTrue(i_idxXpctdBndd == ssHTMF.lastIndexOf(c, i_idxAfterRight)); xassertTrue(i_idxXpctdBndd == ssbHTMF.lastIndexOf(c, i_idxAfterRight)); } xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(c, i_idxLeft, i_idxAfterRight)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(c, i_idxLeft, i_idxAfterRight)); xassertTrue(i_idxXpctdBndd == ssHTMF.lastIndexOf(c, i_idxLeft, i_idxAfterRight)); xassertTrue(i_idxXpctdBndd == ssbHTMF.lastIndexOf(c, i_idxLeft, i_idxAfterRight)); //Same, but explicitely bounded xassertTrue(i_idxXpctdBndd == ssHTMF.indexOf(c, i_idxLeft, i_idxAfterRight, true)); xassertTrue(i_idxXpctdBndd == ssbHTMF.indexOf(c, i_idxLeft, i_idxAfterRight, true)); xassertTrue(i_idxXpctdBndd == ssHTMF.lastIndexOf(c, i_idxLeft, i_idxAfterRight, true)); xassertTrue(i_idxXpctdBndd == ssbHTMF.lastIndexOf(c, i_idxLeft, i_idxAfterRight, true)); //Unbounded xassertTrue(i_idxXpctdUnbndd == ssHTMF.indexOf(c, i_idxLeft, i_idxAfterRight, false)); xassertTrue(i_idxXpctdUnbndd == ssbHTMF.indexOf(c, i_idxLeft, i_idxAfterRight, false)); xassertTrue(i_idxXpctdUnbndd == ssHTMF.lastIndexOf(c, i_idxLeft, i_idxAfterRight, false)); xassertTrue(i_idxXpctdUnbndd == ssbHTMF.lastIndexOf(c, i_idxLeft, i_idxAfterRight, false)); } } private void io_lio_assertAX(String s_toSearchFor, int i_idxLeft, int i_idxAfterRight) { //The very first line checks the bounds, so no need to //test any other param combinations. //Right after params are checked, then the string is //verified to not be null. b_bounded parameter is ignored //to this point. try { ssHTMF.indexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight); xfail("indexOf('" + s_toSearchFor + "', " + i_idxLeft + ", " + i_idxAfterRight + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } try { ssbHTMF.lastIndexOf(s_toSearchFor, i_idxLeft, i_idxAfterRight); xfail("lastIndexOf('" + s_toSearchFor + "', " + i_idxLeft + ", " + i_idxAfterRight + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } if(s_toSearchFor != null && s_toSearchFor.length() > 0) { //This is bad for a reason concerning the bounds, not //the search string. try { ssHTMF.indexOf(' ', i_idxLeft, i_idxAfterRight); xfail("indexOf(' ', " + i_idxLeft + ", " + i_idxAfterRight + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } try { ssbHTMF.lastIndexOf(' ', i_idxLeft, i_idxAfterRight); xfail("lastIndexOf(' ', " + i_idxLeft + ", " + i_idxAfterRight + ") should throw an ax"); } catch(AssertException ax) { xassertTrue(); } } } public void test_isStringAt() { //If to-search-for is null. try { ssHTMF.isStringAt(null, 6); xfail("isStringAt(null, 6) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } //If to-search-for is empty. try { ssHTMF.isStringAt(sES, 6); xfail("isStringAt('', 6) should throw an ax"); } catch(AssertException ax) { xassertTrue(); } //Found isaTrue(ssHTMF, "there", 6); isaTrue(ssbHTMF, "here, ", 7); //Not found at all isaFalse(ssHTMF, sX, 6); isaFalse(ssbHTMF, sY, 7); //Found, but not at given idx isaFalse(ssHTMF, "Hello", 6); isaFalse(ssbHTMF, "friend", 7); } private void isaTrue(StringOrBuffer str_orBfr, String s_tr, int i_idx) { xassertTrue("[type=" + str_orBfr.getClass().getName() + "] '" + str_orBfr + "'.isStringAt('" + s_tr + "', " + i_idx + ")", str_orBfr.isStringAt(s_tr, i_idx)); } private void isaFalse(StringOrBuffer str_orBfr, String s_tr, int i_idx) { xassertFalse("[type=" + str_orBfr.getClass().getName() + "] '" + str_orBfr + "'.isStringAt('" + s_tr + "', " + i_idx + ")", str_orBfr.isStringAt(s_tr, i_idx)); } public void setUp() throws Exception { ssES = new SOBString(sES); ssbES = new SOBStringBuffer(sES); ssHTMF = new SOBString(sHTMF); ssbHTMF = new SOBStringBuffer(sHTMF); super.setUp(); } public void tearDown() throws Exception { ssES = null; ssbES = null; ssHTMF = null; ssbHTMF = null; super.tearDown(); } public static Test suite() { TestSuite ts = new TestSuite(JUTStringOrBuffer.class); return ts; } public static void main(String[] as_cmdLineParams) { TestRunner.run(suite()); printSummary(); } }