面试题4:替换空格之发散思维三(测试)
替换字符串代码
public interface IStringReplace { void CharsReplace(ref char[] value, char[] oldchars, char[] newChars); } public class StringReplace : IStringReplace { private static StringReplace instance; public static StringReplace CreateInstance() { if (instance == null) { instance = new StringReplace(); } return instance; } [ThreadStatic] static char[] mTempChars; protected char[] GetTempData() { if (mTempChars == null) mTempChars = new char[1024 * 64]; return mTempChars; } private int GetActualLength(char[] value) { int alen = 0; for (int i = 0; i < value.Length; i++) { if (value[i] == '\0') { break; } alen++; } return alen; } public void CharsReplace(ref char[] value, char[] oldchars, char[] newChars) { if (value == null || oldchars == null || newChars == null) return; int oAlen = GetActualLength(oldchars); if (oAlen < 1) return; int vAlen = GetActualLength(value); int nAlen = GetActualLength(newChars); char[] tmpchars = GetTempData(); int index = 0; int copyIndex = 0; bool eq = false; while (index + oAlen <= vAlen) { eq = true; for (int k = 0; k < oAlen; k++) { if (value[index + k] != oldchars[k]) { eq = false; break; } } if (eq) { for (int k = 0; k < nAlen; k++) { tmpchars[copyIndex] = newChars[k]; copyIndex++; } index += oAlen; } else { tmpchars[copyIndex] = value[index]; index++; copyIndex++; } } while (index < vAlen) { tmpchars[copyIndex] = value[index]; index++; copyIndex++; } //if (value.Length < copyIndex) //{ // value = new char[copyIndex]; //} value = new char[copyIndex]; for (int i = 0; i < copyIndex; i++) { value[i] = tmpchars[i]; } return; } }
上面是从C#中的char[]中替换字符串,不考虑内存消耗,可以为替换后的char[]数组开辟新的内存空间。
测试主要是黑盒测试,按照等价类划分设计测试用例。
value是母串,oldChars是原来的字符串,newChars是要替换的字符串。
表一 | ||||
value是母串,oldChars是原来的字符串,newChars是要替换的字符串 | 有效等价类 | 号码 | 无效等价类 | 号码 |
非空长度为零 | 1 | 存在空字符串 | 3 | |
长度全不为0 | 2(详见表2) | 存长度为0的字符串 | 4 |
表二 长度都不为零 | ||||
newChars大于oldChars的长度 | newChars小于oldChars的长度 | newChars等于oldChars的长度 | ||
value中包含oldChars | oldChars在value末尾 | 201 | 202 | 203 |
oldChars在value中央 | 204 | 205 | 206 | |
oldChars在value前端 | 207 | 208 | 209 | |
value中不包含oldChars | value的前端与oldChars相似 | 210 | 211 | 212 |
value的末端与oldChars相似 | 213 | 214 | 215 | |
value的中间与oldChars相似 | 216 | 217 | 218 | |
特殊情况 | value替换后长度为0 | 501 |
采用等价类测试和边界测试相结合的方法,表一中主要是等价类测试,表二是对有效等价类长度都不为0的一个详细划分,由于测试中与oldChars在value的位置有关,所以认为是边界测试法。
对于无效等价类中的,空字符串只测试value,oldChars和newChars分别为空,不再测试组合情况;对于长度为零只测试value为空和oldChars为空,newChars长度可以为0,不测试组合情况。
表二中主要是对长度都不为零的情况下:测试oldChars在Value的位置情况,oldChars与newChars的长度,oldChars是否是Value的子串,这三种情况的组合。
测试用例见下表:
测试用例编号 | value | oldChars | newChars | 覆盖编号 |
1 | null | {'q'} | {'q'} | 3 |
2 | {'q'} | null | {'q'} | 3 |
3 | {'q'} | {'q'} | null | 3 |
4 | {} | {'a','b'} | {} | 4 |
5 | {'a','b'} | {} | {} | 4 |
6 | {'a','b'} | {'a','b'} | {} | 501 |
7 | {'a','a','a','a','b','a','a'} | {'a','a'} | {'b','b'} | 203,206,209 |
8 | {'a','d','a','d','a','d','a'} | {'a','b'} | {'a','c'} | 212,215,218 |
9 | {'a','b','c','d','a','b','c','d','a','b','c'} | {'a','b','c'} | {'a','c'} | 202,205,208 |
10 | {'a','b','d','d','a','a','c','d','a','a','c'} | {'a','b','c'} | {'a','c'} | 211,214,217 |
11 | {'a','b','c','d','a','b','c','d','a','b','c'} | {'a','b','c'} | {'a','b','c','d'} | 201,204,207 |
12 | {'a','d','a','d','a','b','c','d','a','a','d'} | {'a','d','c'} | {'a','b','c','d'} | 210,213,216 |
下面是测试代码:
测试代码
public class TestStrReplace { private IStringReplace iface; char[] value; char[] oldChars; char[] newChars; public TestStrReplace() { iface = StringReplace.CreateInstance(); } private void PreTest(string testName) { Console.WriteLine(); Console.WriteLine(); Console.WriteLine("-------------------"); Console.WriteLine(testName); Console.WriteLine("-------------------"); Console.WriteLine("*****PreTest*****"); Console.WriteLine("*****Value*****"); Console.WriteLine(value); Console.WriteLine("*****oldChars*****"); Console.WriteLine(oldChars); Console.WriteLine("*****newChars*****"); Console.WriteLine(newChars); } private void PostTest() { Console.WriteLine("#####PostTest#####"); Console.WriteLine("#####Value#####"); Console.WriteLine(value); } #region 长度为0测试 /// <summary> /// 黑盒测试 /// newChars长度为0 /// </summary> public void BlacK_Test_01() { value = new char[] { 'a', 'b', 'c', 'a', 'b', 'c' }; oldChars = new char[] { 'a', 'b', 'c' }; newChars = new char[] { }; PreTest("BlacK_Test_01"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } /// <summary> /// 黑盒测试 /// oldChars长度为0 /// </summary> public void BlacK_Test_02() { value = new char[] { 'a', 'b', 'c', 'd', 'e', 'f' }; oldChars = new char[] { }; newChars = new char[] { 'd' }; PreTest("BlacK_Test_02"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } /// <summary> /// value长度为0 /// </summary> public void BlacK_Test_03() { value = new char[] { }; oldChars = new char[] { 'c' }; newChars = new char[] { 'd' }; PreTest("BlacK_Test_03"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } #endregion #region null 测试 /// <summary> /// value为空 /// </summary> public void BlacK_Test_04() { value = null; oldChars = new char[] { 'e', 'f', 'g' }; newChars = new char[] { 'e', 'f', 'g' }; PreTest("BlacK_Test_04"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } /// <summary> /// oldChars 为空 /// </summary> public void BlacK_Test_05() { value = new char[] { 'a', 'b', 'c', 'd', 'e', 'f' }; oldChars = null; newChars = new char[] { 'e', 'f', 'g' }; PreTest("BlacK_Test_05"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } /// <summary> /// /// </summary> public void BlacK_Test_06() { value = new char[] { 'a', 'b', 'c', 'd', 'e', 'f' }; oldChars = new char[] { 'a' }; newChars = null; PreTest("BlacK_Test_06"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } #endregion #region oldChars newChars 长度相等 /// <summary> /// value 有 oldChars /// </summary> public void BlacK_Test_07() { value = new char[] { 'a', 'a', 'a', 'a', 'b', 'a', 'a' }; oldChars = new char[] { 'a', 'a' }; newChars = new char[] { 'a', 'b' }; PreTest("BlacK_Test_07"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } /// <summary> /// value 没有 oldChars /// </summary> public void BlacK_Test_08() { value = new char[] { 'a', 'd', 'a', 'd', 'a', 'd', 'a' }; oldChars = new char[] { 'a', 'b' }; newChars = new char[] { 'a', 'c' }; PreTest("BlacK_Test_08"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } #endregion #region oldChars 长度> newChars长度 /// <summary> /// value 有 oldChars /// </summary> public void BlacK_Test_09() { value = new char[] { 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c' }; oldChars = new char[] { 'a', 'b', 'c' }; newChars = new char[] { 'a', 'c' }; PreTest("BlacK_Test_09"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } /// <summary> /// value 没有 oldChars /// </summary> public void BlacK_Test_10() { value = new char[] { 'a', 'b', 'd', 'd', 'a', 'a', 'c', 'd', 'a', 'a', 'c' }; oldChars = new char[] { 'a', 'b', 'c' }; newChars = new char[] { 'a', 'c' }; PreTest("BlacK_Test_10"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } #endregion #region oldChars 长度< newChars长度 /// <summary> /// value 有 oldChars /// </summary> public void BlacK_Test_11() { value = new char[] { 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c' }; oldChars = new char[] { 'a', 'b', 'c' }; newChars = new char[] { 'a', 'b', 'c', 'd' }; PreTest("BlacK_Test_11"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } /// <summary> /// value 没有 oldChars /// </summary> public void BlacK_Test_12() { value = new char[] { 'a', 'd', 'a', 'd', 'a', 'b', 'c', 'd', 'a', 'a', 'd' }; oldChars = new char[] { 'a', 'd', 'c' }; newChars = new char[] { 'a', 'b', 'c', 'd' }; PreTest("BlacK_Test_12"); iface.CharsReplace(ref value, oldChars, newChars); PostTest(); } #endregion } class Program { static void Main(string[] args) { TestStrReplace test = new TestStrReplace(); test.BlacK_Test_01(); test.BlacK_Test_02(); test.BlacK_Test_03(); test.BlacK_Test_04(); test.BlacK_Test_05(); test.BlacK_Test_06(); test.BlacK_Test_07(); test.BlacK_Test_08(); test.BlacK_Test_09(); test.BlacK_Test_10(); test.BlacK_Test_11(); test.BlacK_Test_12(); Console.ReadKey(); } }
2013年4月2日21:51:26
菜包子 于宿舍