📄 teststring.cs
字号:
{ String fubar = "Foo Bar"; try { fubar.IndexOf(null); Fail("fubar.IndexOf(null) should throw an ArgumentNullException"); fubar.IndexOf(null,0); Fail("fubar.IndexOf(null,0) should throw an ArgumentNullException"); fubar.IndexOf(null,0,1); Fail("fubar.IndexOf(null,0,1) should throw an ArgumentNullException"); } catch(ArgumentNullException err) { //all's well } try { fubar.IndexOf('f',fubar.Length+1); Fail("fubar.IndexOf('f',fubar.Length+1) should throw an ArgumentOutOfRangeException"); fubar.IndexOf('f',fubar.Length+1,1); Fail("fubar.IndexOf('f',fubar.Length+1,1) should throw an ArgumentOutOfRangeException"); fubar.IndexOf("foo",fubar.Length+1); Fail("fubar.IndexOf(\"foo\",fubar.Length+1) should throw an ArgumentOutOfRangeException"); fubar.IndexOf("foo",fubar.Length+1,1); Fail("fubar.IndexOf(\"foo\",fubar.Length+1,1) should throw an ArgumentOutOfRangeException"); } catch(ArgumentOutOfRangeException err) { //all's well } AssertEquals("fubar.IndexOf('o')", fubar.IndexOf('o'),1); AssertEquals("fubar.IndexOf('F')", fubar.IndexOf('F'),0); AssertEquals("fubar.IndexOf('z')", fubar.IndexOf('z'),-1); AssertEquals("fubar.IndexOf(\"oo\")", fubar.IndexOf("oo"),1); AssertEquals("fubar.IndexOf(\"Bar\")", fubar.IndexOf("Bar"),4); AssertEquals("fubar.IndexOf(\"qwaz\")", fubar.IndexOf("qwaz"),-1); AssertEquals("fubar.IndexOf('o',1)", fubar.IndexOf('o',1),1); AssertEquals("fubar.IndexOf('o',3)", fubar.IndexOf('o',3),-1); AssertEquals("fubar.IndexOf('z',4)", fubar.IndexOf('z',4),-1); AssertEquals("fubar.IndexOf(\"oo\",1)", fubar.IndexOf("oo",1),1); AssertEquals("fubar.IndexOf(\"oo\",2)", fubar.IndexOf("oo",2),-1); AssertEquals("fubar.IndexOf('o',1,2)", fubar.IndexOf('o',1,1),1); AssertEquals("fubar.IndexOf('o',0,1)", fubar.IndexOf('o',0,1),-1); AssertEquals("fubar.IndexOf(' ',1,5)", fubar.IndexOf(' ',1,5),3); try { fubar.IndexOf('h', fubar.Length); // shouldn't throw per ECMA } catch (ArgumentOutOfRangeException) { Fail("IndexOf(char, int) should not throw when passed Length as the int"); } /* TODO: I don't know any more test ideas , do you have one ? */ } public void TestStringIndexOfAny() { String fubar="mary had a little lamb ...."; try { fubar.IndexOfAny(null); Fail("fubar.IndexOfAny(null) should throw an ArgumentNullException"); fubar.IndexOfAny(null,0); Fail("fubar.IndexOfAny(null,0) should throw an ArgumentNullException"); fubar.IndexOfAny(null,0,1); Fail("fubar.IndexOfAny(null,0,1) should throw an ArgumentNullException"); } catch(ArgumentNullException err) { //all's A-OK ! } char[] c={'a','e','i','o','u'}; try { fubar.IndexOfAny(c,fubar.Length+1); Fail("fubar.IndexOfAny(c,fubar.Length+1) should throw an ArgumentOutOfRangeException"); fubar.IndexOfAny(c,fubar.Length+1,1); Fail("fubar.IndexOfAny(c,fubar.Length+1,1) should throw an ArgumentOutOfRangeException"); fubar.IndexOfAny(c,fubar.Length-3,5); Fail("fubar.IndexOfAny(c,fubar.Length-3,5) should throw an ArgumentOutOfRangeException"); } catch(ArgumentOutOfRangeException) { //all's well in code and data :) } AssertEquals("fubar.IndexOfAny(c)",fubar.IndexOfAny(c),1); //m`a' AssertEquals("fubar.IndexOfAny(c,2)",fubar.IndexOfAny(c,2),6);//h`a AssertEquals("fubar.IndexOfAny(c,21)",fubar.IndexOfAny(c,21),-1); AssertEquals("fubar.IndexOfAny(c,2,5)",fubar.IndexOfAny(c,2,5),6); AssertEquals("fubar.IndexOfAny(c,2,3)",fubar.IndexOfAny(c,2,3),-1); } public void TestStringInsert() { String fubar = "FooBar"; AssertEquals("fubar.Insert(3,\" \")","Foo Bar",fubar.Insert(3," ")); AssertEquals("fubar.Insert(0,\" \")"," FooBar",fubar.Insert(0," ")); AssertEquals("fubar.Insert(fubar.Length,\" \")","FooBar ",fubar.Insert(fubar.Length," ")); try { fubar.Insert(0,null); Fail("fubar.Insert(0,null) should throw an ArgumentNullException"); } catch(ArgumentNullException) { // checks out ok } try { fubar.Insert(fubar.Length+1," "); Fail("fubar.Insert(fubar.Length+1,\" \") should throw an ArgumentOutOfRangeException"); } catch(ArgumentOutOfRangeException) { // this works too } } public void TestStringIntern() { String foobar = "if the sun refused to shine, I don't mind, I don't mind... if the mountains fell in the sea, let it be, it aint me - hendrix"; String fubar = foobar.Substring(0,10); fubar += foobar.Substring(10,foobar.Length-10); fubar = String.Intern(fubar); AssertEquals("String.Intern(fubar)",(Object)foobar,(Object)fubar); try { String.Intern(null); Fail("String.Intern(null) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // this works } } public void TestStringIsInterned() { // I can't imagine anyone using a string like this in pnetlib or the unit tests, so this should work char[] fu = new char[13] { 'q','w','e','r','t','y','u','i','o','p','\t','\0','\n' }; String foobar = new String(fu); String fubar = String.IsInterned(foobar); Assert("String.IsInterned(foobar)",fubar == null); try { String.IsInterned(null); Fail("String.IsInterned(null) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // all is good } } public void TestStringJoin() { String fu = " fu "; String fu2 = ""; String[] foo = new String[6] { "foo","ofo","oof","FOO","OFO","OOF" }; String[] foo2 = new String[3] { "","","" }; String[] foo3 = new String[0] { }; AssertEquals("String.Join(fu,foo)","foo fu ofo fu oof fu FOO fu OFO fu OOF",String.Join(fu,foo)); AssertEquals("String.Join(fu,foo,0,foo.Length)","foo fu ofo fu oof fu FOO fu OFO fu OOF",String.Join(fu,foo,0,foo.Length)); AssertEquals("String.Join(fu,foo,0,1)","foo",String.Join(fu,foo,0,1)); AssertEquals("String.Join(fu,foo,1,1)","ofo",String.Join(fu,foo,1,1)); AssertEquals("String.Join(fu,foo,2,1)","oof",String.Join(fu,foo,2,1)); AssertEquals("String.Join(fu,foo,3,1)","FOO",String.Join(fu,foo,3,1)); AssertEquals("String.Join(fu,foo,4,1)","OFO",String.Join(fu,foo,4,1)); AssertEquals("String.Join(fu,foo,5,1)","OOF",String.Join(fu,foo,5,1)); AssertEquals("String.Join(fu,foo,0,2)","foo fu ofo",String.Join(fu,foo,0,2)); AssertEquals("String.Join(fu,foo,1,2)","ofo fu oof",String.Join(fu,foo,1,2)); AssertEquals("String.Join(fu,foo,2,2)","oof fu FOO",String.Join(fu,foo,2,2)); AssertEquals("String.Join(fu,foo,3,2)","FOO fu OFO",String.Join(fu,foo,3,2)); AssertEquals("String.Join(fu,foo,4,2)","OFO fu OOF",String.Join(fu,foo,4,2)); AssertEquals("String.Join(fu,foo,0,3)","foo fu ofo fu oof",String.Join(fu,foo,0,3)); AssertEquals("String.Join(fu,foo,1,3)","ofo fu oof fu FOO",String.Join(fu,foo,1,3)); AssertEquals("String.Join(fu,foo,2,3)","oof fu FOO fu OFO",String.Join(fu,foo,2,3)); AssertEquals("String.Join(fu,foo,3,3)","FOO fu OFO fu OOF",String.Join(fu,foo,3,3)); AssertEquals("String.Join(fu,foo,0,4)","foo fu ofo fu oof fu FOO",String.Join(fu,foo,0,4)); AssertEquals("String.Join(fu,foo,1,4)","ofo fu oof fu FOO fu OFO",String.Join(fu,foo,1,4)); AssertEquals("String.Join(fu,foo,2,4)","oof fu FOO fu OFO fu OOF",String.Join(fu,foo,2,4)); AssertEquals("String.Join(fu,foo,0,5)","foo fu ofo fu oof fu FOO fu OFO",String.Join(fu,foo,0,5)); AssertEquals("String.Join(fu,foo,1,5)","ofo fu oof fu FOO fu OFO fu OOF",String.Join(fu,foo,1,5)); AssertEquals("String.Join(fu,foo,0,0)","",String.Join(fu,foo,0,0)); AssertEquals("String.Join(fu2,foo2,0,foo2.Length)","",String.Join(fu2,foo2,0,foo2.Length)); AssertEquals("String.Join(fu,foo3,0,foo3.Length)","",String.Join(fu,foo3,0,foo3.Length)); try { String.Join(fu,null); Fail("String.Join(fu,null) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // works } try { String.Join(fu,foo,0,foo.Length+1); Fail("String.Join(fu,foo,0,foo.Length+1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // works } } public void TestStringLastIndexOf() { String foo = "Foo Bar foo bar fu bar Fu Bar"; /* String.LastIndexOf(char) */ AssertEquals("foo.LastIndexOf('r')",28,foo.LastIndexOf('r')); AssertEquals("foo.LastIndexOf('a')",27,foo.LastIndexOf('a')); AssertEquals("foo.LastIndexOf('B')",26,foo.LastIndexOf('B')); AssertEquals("foo.LastIndexOf(' ')",25,foo.LastIndexOf(' ')); AssertEquals("foo.LastIndexOf('u')",24,foo.LastIndexOf('u')); AssertEquals("foo.LastIndexOf('F')",23,foo.LastIndexOf('F')); AssertEquals("foo.LastIndexOf('b')",19,foo.LastIndexOf('b')); AssertEquals("foo.LastIndexOf('f')",16,foo.LastIndexOf('f')); AssertEquals("foo.LastIndexOf('o')",10,foo.LastIndexOf('o')); AssertEquals("foo.LastIndexOf('c')",-1,foo.LastIndexOf('c')); /* String.LastIndexOf(char,int) */ AssertEquals("foo.LastIndexOf('f',16)",16,foo.LastIndexOf('f',16)); AssertEquals("foo.LastIndexOf('f',15)",8,foo.LastIndexOf('f',15)); AssertEquals("foo.LastIndexOf('f',7)",-1,foo.LastIndexOf('f',7)); AssertEquals("foo.LastIndexOf('f',foo.Length-1)",16,foo.LastIndexOf('f',foo.Length-1)); try { AssertEquals("foo.LastIndexOf('f',foo.Length)",16,foo.LastIndexOf('f',foo.Length)); // don't ask me } catch (ArgumentOutOfRangeException) { // This looks like brain damage in the spec, but it's easy enough to implement. Fail("foo.LastIndexOf('f',foo.Length) should NOT throw an ArgumentOutOfRangeException"); } try { foo.LastIndexOf('f',-1); Fail("foo.LastIndexOf('f',-1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // nothing wrong here } try { foo.LastIndexOf('f',foo.Length+1); Fail("foo.LastIndexOf('f',foo.Length+1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // or here } /* String.LastIndexOf(char,int,int) */ AssertEquals("foo.LastIndexOf('f',16,17)",16,foo.LastIndexOf('f',16,17)); AssertEquals("foo.LastIndexOf('f',15,16)",8,foo.LastIndexOf('f',15,16)); AssertEquals("foo.LastIndexOf('f',15,7)",-1,foo.LastIndexOf('f',15,7)); AssertEquals("foo.LastIndexOf('f',foo.Length-1,1)",-1,foo.LastIndexOf('f',foo.Length-1,1)); AssertEquals("foo.LastIndexOf('r',foo.Length-1,1)",-1,foo.LastIndexOf('r',foo.Length-1,0)); AssertEquals("foo.LastIndexOf('r',foo.Length-1,1)",28,foo.LastIndexOf('r',foo.Length-1,1)); AssertEquals("foo.LastIndexOf('F',0,1)",0,foo.LastIndexOf('F',0,1)); AssertEquals("foo.LastIndexOf('F',1,1)",-1,foo.LastIndexOf('F',1,1)); try { AssertEquals("foo.LastIndexOf('r',foo.Length,0)",-1,foo.LastIndexOf('r',foo.Length,0)); // ask the ECMA } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf('r',foo.Length,0) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf('r',foo.Length,1)",-1,foo.LastIndexOf('r',foo.Length,1)); // b/c these are } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf('r',foo.Length,1) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf('r',foo.Length,2)",28,foo.LastIndexOf('r',foo.Length,2)); // all valid, } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf('r',foo.Length,2) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf('f',foo.Length+10,0)",-1,foo.LastIndexOf('f',foo.Length+10,0)); // believe } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf('f',foo.Length+10,0) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf('f',foo.Length+10,1)",-1,foo.LastIndexOf('f',foo.Length+10,1)); // it or not. } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf('f',foo.Length+10,1) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf('f',foo.Length+10,11)",-1,foo.LastIndexOf('f',foo.Length+10,11)); // amazing, } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf('f',foo.Length+10,11) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf('r',foo.Length+10,12)",28,foo.LastIndexOf('r',foo.Length+10,12)); // isn't it? } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf('r',foo.Length+10,12) should NOT throw an ArgumentOutOfRangeException"); } try { foo.LastIndexOf('f',-1,0); Fail("foo.LastIndexOf('f',-1,0) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // a-ok } try { foo.LastIndexOf('f',0,-1); Fail("foo.LastIndexOf('f',0,-1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // no problems here } try { foo.LastIndexOf('f',0,2); Fail("foo.LastIndexOf('f',0,2) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // blah } /* String.LastIndexOf(String) */ AssertEquals("foo.LastIndexOf(\"Bar\")",26,foo.LastIndexOf("Bar")); AssertEquals("foo.LastIndexOf(\"Fu\")",23,foo.LastIndexOf("Fu")); AssertEquals("foo.LastIndexOf(\"bar\")",19,foo.LastIndexOf("bar")); AssertEquals("foo.LastIndexOf(\"fu\")",16,foo.LastIndexOf("fu")); AssertEquals("foo.LastIndexOf(\"foo\")",8,foo.LastIndexOf("foo")); AssertEquals("foo.LastIndexOf(\"Foo\")",0,foo.LastIndexOf("Foo")); AssertEquals("foo.LastIndexOf(\"blah\")",-1,foo.LastIndexOf("blah")); AssertEquals("foo.LastIndexOf(\"\")",0,foo.LastIndexOf("")); try { foo.LastIndexOf((String)null); Fail("foo.LastIndexOf((String)null) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // looks good so far } /* String.LastIndexOf(String,int) */ AssertEquals("foo.LastIndexOf(\"Bar\",foo.Length-1)",26,foo.LastIndexOf("Bar",foo.Length-1)); AssertEquals("foo.LastIndexOf(\"Bar\",foo.Length-2)",4,foo.LastIndexOf("Bar",foo.Length-2)); AssertEquals("foo.LastIndexOf(\"Fu\",foo.Length-3)",23,foo.LastIndexOf("Fu",foo.Length-3)); AssertEquals("foo.LastIndexOf(\"Fu\",foo.Length-6)",-1,foo.LastIndexOf("Fu",foo.Length-6)); try { AssertEquals("foo.LastIndexOf(\"\",0)",0,foo.LastIndexOf("",0)); // this is absurd, } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf(\"\",0) should NOT throw an ArgumentOutOfRangeException"); } try { AssertEquals("foo.LastIndexOf(\"\",1)",0,foo.LastIndexOf("",1)); // as is this } catch (ArgumentOutOfRangeException) { Fail("foo.LastIndexOf(\"\",1) should NOT throw an ArgumentOutOfRangeException"); } try { foo.LastIndexOf((String)null,0); Fail("foo.LastIndexOf((String)null,0) should throw an ArgumentNullException"); } catch (ArgumentNullException) { // move along, nothing to see here } try { foo.LastIndexOf("foo",-1); Fail("foo.LastIndexOf(\"foo\",-1) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // this works } try { foo.LastIndexOf("foo",foo.Length); Fail("foo.LastIndexOf(\"foo\",foo.Length) should throw an ArgumentOutOfRangeException"); } catch (ArgumentOutOfRangeException) { // this checks out } try
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -