📄 teststring.cs
字号:
{ foo.LastIndexOf("foo",foo.Length+1); Fail("foo.LastIndexOf(\"foo\",foo.Length+1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // this checks out } /* String.LastIndexOf(String,int,int) */ AssertEquals("foo.LastIndexOf(\"Bar\",foo.Length-1,foo.Length)",26,foo.LastIndexOf("Bar",foo.Length-1,foo.Length)); AssertEquals("foo.LastIndexOf(\"Bar\",foo.Length-2,foo.Length-1)",4,foo.LastIndexOf("Bar",foo.Length-2,foo.Length-1)); AssertEquals("foo.LastIndexOf(\"Bar\",foo.Length-1,3)",26,foo.LastIndexOf("Bar",foo.Length-1,3)); AssertEquals("foo.LastIndexOf(\"Bar\",foo.Length-2,3)",-1,foo.LastIndexOf("Bar",foo.Length-2,3)); AssertEquals("foo.LastIndexOf(\"Bar\",foo.Length-1,2)",-1,foo.LastIndexOf("Bar",foo.Length-1,2)); AssertEquals("foo.LastIndexOf(\"Fu\",foo.Length-4,foo.Length-3)",23,foo.LastIndexOf("Fu",foo.Length-4,foo.Length-3)); AssertEquals("foo.LastIndexOf(\"Fu\",foo.Length-6,foo.Length-5)",-1,foo.LastIndexOf("Fu",foo.Length-6,foo.Length-5)); AssertEquals("foo.LastIndexOf(\"Fu\",foo.Length-4,3)",23,foo.LastIndexOf("Fu",foo.Length-4,3)); AssertEquals("foo.LastIndexOf(\"Fu\",foo.Length-6,3)",-1,foo.LastIndexOf("Fu",foo.Length-6,3)); try { AssertEquals("foo.LastIndexOf(\"\",0,0)",0,foo.LastIndexOf("",0,0)); // and the absurdity continues } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf(\"\",0,0) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf(\"\",0,1)",0,foo.LastIndexOf("",0,1)); // need I say more? } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf(\"\",0,1) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf(\"\",1,0)",0,foo.LastIndexOf("",1,0)); // ok, "more" } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf(\"\",1,0) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf(\"\",1,1)",0,foo.LastIndexOf("",1,1)); // and more } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf(\"\",1,1) should NOT throw an ArgumentOutOfRangeException"); } try { foo.LastIndexOf((String)null,0,0); Fail("foo.LastIndexOf((String)null,0,0) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // doing good } try { foo.LastIndexOf("foo",-1,0); Fail("foo.LastIndexOf(\"foo\",-1,0) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // even better } try { foo.LastIndexOf("foo",0,-1); Fail("foo.LastIndexOf(\"foo\",0,-1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // doing great, so far } try { foo.LastIndexOf("foo",0,2); Fail("foo.LastIndexOf(\"foo\",0,2) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // yay! we made it } } public void TestStringLastIndexOfAny() { // 00000000001111111111222222222 // 01234567890123456789012345678 String foo = "Foo Bar foo bar fu bar Fu Bar"; /* String.LastIndexOfAny(char[]) */ AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'})",28,foo.LastIndexOfAny(new char[] {'r','a','B'})); AssertEquals("foo.LastIndexOfAny(new char[] {'f','a','B'})",27,foo.LastIndexOfAny(new char[] {'f','a','B'})); AssertEquals("foo.LastIndexOfAny(new char[] {'r'})",28,foo.LastIndexOfAny(new char[] {'r'})); AssertEquals("foo.LastIndexOfAny(new char[] {'B'})",26,foo.LastIndexOfAny(new char[] {'B'})); AssertEquals("foo.LastIndexOfAny(new char[] {'B','B'})",26,foo.LastIndexOfAny(new char[] {'B','B'})); AssertEquals("foo.LastIndexOfAny(new char[] {'B','u'})",26,foo.LastIndexOfAny(new char[] {'B','u'})); AssertEquals("foo.LastIndexOfAny(new char[] {'u','B'})",26,foo.LastIndexOfAny(new char[] {'u','B'})); AssertEquals("foo.LastIndexOfAny(new char[] {'u','f'})",24,foo.LastIndexOfAny(new char[] {'u','f'})); AssertEquals("foo.LastIndexOfAny(new char[] {'F','f'})",23,foo.LastIndexOfAny(new char[] {'F','f'})); AssertEquals("foo.LastIndexOfAny(new char[] {'F','q'})",23,foo.LastIndexOfAny(new char[] {'F','q'})); AssertEquals("foo.LastIndexOfAny(new char[] {'p','q'})",-1,foo.LastIndexOfAny(new char[] {'p','q'})); try { foo.LastIndexOfAny((char[])null); Fail("foo.LastIndexOfAny((char[])null) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // this is good } /* String.LastIndexOfAny(char[],int) */ AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-1)",28,foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-1)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-2)",27,foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-2)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},4)",4,foo.LastIndexOfAny(new char[] {'r','a','B'},4)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},5)",5,foo.LastIndexOfAny(new char[] {'r','a','B'},5)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},6)",6,foo.LastIndexOfAny(new char[] {'r','a','B'},6)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},1)",-1,foo.LastIndexOfAny(new char[] {'r','a','B'},1)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},0)",-1,foo.LastIndexOfAny(new char[] {'r','a','B'},0)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},3)",-1,foo.LastIndexOfAny(new char[] {'r','a','B'},3)); try { foo.LastIndexOfAny((char[])null,0); Fail("foo.LastIndexOfAny((char[])null,0) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // caught it } try { foo.LastIndexOfAny(new char[] {'r','a','B'},-1); Fail("foo.LastIndexOfAny(new char[] {'r','a','B'},-1); should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // caught another one } try { foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length); Fail("foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length); should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // caught this one too } try { foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length+1); Fail("foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length+1); should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // and this one } /* String.LastIndexOfAny(char[],int,int) */ AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-1,1)",28,foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-1,1)); AssertEquals("foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-2,2)",27,foo.LastIndexOfAny(new char[] {'r','a','B'},foo.Length-2,2)); AssertEquals("foo.LastIndexOfAny(new char[] {'B'},foo.Length-1,foo.Length)",26,foo.LastIndexOfAny(new char[] {'B'},foo.Length-1,foo.Length)); try { AssertEquals("foo.LastIndexOfAny(new char[] {'F'},foo.Length,foo.Length)",23, foo.LastIndexOfAny(new char[] {'F'},foo.Length,foo.Length)); } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOfAny(new char[] {'F'},foo.Length,foo.Length) should NOT throw an ArgumentOutOfRangeException"); // flashing some leather } AssertEquals("foo.LastIndexOfAny(new char[] {'F'},foo.Length-11,foo.Length-10)",0,foo.LastIndexOfAny(new char[] {'F'},foo.Length-11,foo.Length-10)); AssertEquals("foo.LastIndexOfAny(new char[] {'F'},foo.Length-10,foo.Length-10)",-1,foo.LastIndexOfAny(new char[] {'F'},foo.Length-10,foo.Length-10)); AssertEquals("foo.LastIndexOfAny(new char[] {'F'},foo.Length+10,foo.Length)",23,foo.LastIndexOfAny(new char[] {'F'},foo.Length+10,foo.Length)); AssertEquals("foo.LastIndexOfAny(new char[] {'F','o','B','a','r','f','b','u'},foo.Length+10,10)",-1, foo.LastIndexOfAny(new char[] {'F','o','B','a','r','f','b','u'},foo.Length+10,10)); try { foo.LastIndexOfAny((char[])null,0,0); Fail("foo.LastIndexOfAny((char[])null,0,0) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // all good here } try { foo.LastIndexOfAny(new char[] {'r','a','B'},-1,0); Fail("foo.LastIndexOfAny(new char[] {'r','a','B'},-1,0); should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // and here } try { foo.LastIndexOfAny(new char[] {'r','a','B'},0,-1); Fail("foo.LastIndexOfAny(new char[] {'r','a','B'},0,-1); should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // and here too } try { foo.LastIndexOfAny(new char[] {'r','a','B'},0,2); Fail("foo.LastIndexOfAny(new char[] {'r','a','B'},0,2); should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // and here as well } } public void TestStringPadLeft() { String foo = "FooBar"; /* String.PadLeft(int) */ AssertEquals("foo.PadLeft(foo.Length)",foo,foo.PadLeft(foo.Length)); AssertEquals("foo.PadLeft(foo.Length+1)",(" FooBar"),foo.PadLeft(foo.Length+1)); AssertEquals("foo.PadLeft(foo.Length-1)",foo,foo.PadLeft(foo.Length-1)); AssertEquals("foo.PadLeft(0)",foo,foo.PadLeft(0)); try { foo.PadLeft(-1); Fail("foo.PadLeft(-1) should throw an ArgumentException"); } catch (ArgumentException) { // range check works } /* String.PadLeft(int,char) */ AssertEquals("foo.PadLeft(foo.Length,'_')",foo,foo.PadLeft(foo.Length,'_')); AssertEquals("foo.PadLeft(foo.Length+1,'_')",("_FooBar"),foo.PadLeft(foo.Length+1,'_')); AssertEquals("foo.PadLeft(foo.Length-1,'_')",foo,foo.PadLeft(foo.Length-1,'_')); AssertEquals("foo.PadLeft(0,'_')",foo,foo.PadLeft(0,'_')); try { foo.PadLeft(-1,'_'); Fail("foo.PadLeft(-1,'_') should throw an ArgumentException"); } catch (ArgumentException) { // range check works here too } } public void TestStringPadRight() { String foo = "FooBar"; /* String.PadRight(int) */ AssertEquals("foo.PadRight(foo.Length)",foo,foo.PadRight(foo.Length)); AssertEquals("foo.PadRight(foo.Length+1)",("FooBar "),foo.PadRight(foo.Length+1)); AssertEquals("foo.PadRight(foo.Length-1)",foo,foo.PadRight(foo.Length-1)); AssertEquals("foo.PadRight(0)",foo,foo.PadRight(0)); try { foo.PadRight(-1); Fail("foo.PadRight(-1) should throw an ArgumentException"); } catch (ArgumentException) { // range check works } /* String.PadRight(int,char) */ AssertEquals("foo.PadRight(foo.Length,'_')",foo,foo.PadRight(foo.Length,'_')); AssertEquals("foo.PadRight(foo.Length+1,'_')",("FooBar_"),foo.PadRight(foo.Length+1,'_')); AssertEquals("foo.PadRight(foo.Length-1,'_')",foo,foo.PadRight(foo.Length-1,'_')); AssertEquals("foo.PadRight(0,'_')",foo,foo.PadRight(0,'_')); try { foo.PadRight(-1,'_'); Fail("foo.PadRight(-1,'_') should throw an ArgumentException"); } catch (ArgumentException) { // range check works here too } } public void TestStringRemove() { String foo = "Foo Bar"; AssertEquals("foo.Remove(0,foo.Length)","",foo.Remove(0,foo.Length)); AssertEquals("foo.Remove(1,foo.Length-1)","F",foo.Remove(1,foo.Length-1)); AssertEquals("foo.Remove(0,1)","oo Bar",foo.Remove(0,1)); AssertEquals("foo.Remove(0,0)",foo,foo.Remove(0,0)); AssertEquals("foo.Remove(foo.Length,0)",foo,foo.Remove(foo.Length,0)); AssertEquals("foo.Remove(3,1)","FooBar",foo.Remove(3,1)); AssertEquals("foo.Remove(foo.Length-1,1)","Foo Ba",foo.Remove(foo.Length-1,1)); try { foo.Remove(-1,0); Fail("foo.Remove(-1,0) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // blah } try { foo.Remove(0,-1); Fail("foo.Remove(0,-1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // blah blah } try { foo.Remove(0,foo.Length+1); Fail("foo.Remove(0,foo.Length+1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // blah blah blah } } public void TestStringReplace() { String foo = "Foo Bar"; /* String.Replace(char,char) */ AssertEquals("foo.Replace('F','f')","foo Bar",foo.Replace('F','f')); AssertEquals("foo.Replace(' ','_')","Foo_Bar",foo.Replace(' ','_')); AssertEquals("foo.Replace('r','R')","Foo BaR",foo.Replace('r','R')); AssertEquals("foo.Replace('_',' ')",foo,foo.Replace('_',' ')); /* String.Replace(String,String) */ AssertEquals("foo.Replace(\"Foo\",\"Fu\")","Fu Bar",foo.Replace("Foo","Fu")); AssertEquals("foo.Replace(\"Fu\",\"Foo\")",foo,foo.Replace("Fu","Foo")); AssertEquals("foo.Replace(\"Foo Bar\",\"\")","",foo.Replace("Foo Bar","")); AssertEquals("foo.Replace(\"Foo Bar\",null)","",foo.Replace("Foo Bar",null)); AssertEquals("foo.Replace(null,\"Foo Bar\")",foo,foo.Replace(null,"Foo Bar")); // I'm beginning to hate the ECMA -- Rich } public void TestStringSubstring() { // if you pass Length, SHOULD NOT THROW! // per ECMA spec try { "x".Substring(1, 0); "x".Substring(1); } catch (ArgumentOutOfRangeException) { Fail("Substring should not throw when passed Length as the startIndex!"); } } public void TestStringop_Equality() { String foo = "Foo Bar"; String fu = "Fu Bar"; Assert("!(foo == fu)",!(foo == fu)); Assert("foo == foo",foo == foo); Assert("fu == fu",fu == fu); Assert("foo == String.Copy(foo)",foo == String.Copy(foo)); Assert("fu == String.Copy(fu)",fu == String.Copy(fu)); } public void TestStringop_Inequality() { String foo = "Foo Bar"; String fu = "Fu Bar"; Assert("foo != fu",foo != fu); Assert("!(foo != foo)",!(foo != foo)); Assert("!(fu != fu)",!(fu != fu)); Assert("!(foo != String.Copy(foo))",!(foo != String.Copy(foo))); Assert("!(fu != String.Copy(fu))",!(fu != String.Copy(fu))); } public void TestStringChars() { char[] fu = new char[] { 'F', 'o', 'o', ' ', 'B', 'a', 'r' }; String foo = new String(fu); for (int i = 0; i < foo.Length; i++) { Assert("foo["+i+"] == fu["+i+"]",foo[i] == fu[i]); } try { int i = foo[-1]; Fail("foo[-1] should throw an IndexOutOfRangeException"); } catch (IndexOutOfRangeException) { // works here } try { int i = foo[foo.Length]; Fail("foo[foo.Length] should throw an IndexOutOfRangeException"); } catch (IndexOutOfRangeException) { // and here } try { int i = foo[foo.Length+1]; Fail("foo[foo.Length+1] should throw an IndexOutOfRangeException"); } catch (IndexOutOfRangeException) { // and here } } public void TestStringLength() { AssertEquals("\"Foo Bar\".Length","Foo Bar".Length,7); AssertEquals("\"\".Length","".Length,0); }}; // class TestString
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -