📄 teststring.cs
字号:
/* * TestString.cs - Tests for the "System.String" class. * * Copyright (C) 2002 Free Software Foundation, Inc. * * Authors : Stephen Compall, Gopal.V, & Richard Baumann * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */using CSUnit;using System;public class TestString : TestCase{ // Constructor. public TestString(String name) : base(name) { // Nothing to do here. } // Set up for the tests. protected override void Setup() { // Nothing to do here. } // Clean up after the tests. protected override void Cleanup() { // Nothing to do here. } //Methods public void TestStringClone() { String fubar = "Foo Bar"; AssertEquals("fubar.Clone() as String",fubar,(String)fubar.Clone()); AssertEquals("fubar.Clone() as Object",(Object)fubar,fubar.Clone()); AssertEquals("((ICloneable) fubar).Clone() as String",fubar,(String)((ICloneable) fubar).Clone()); AssertEquals("((ICloneable) fubar).Clone() as Object",(Object)fubar,((ICloneable) fubar).Clone()); } public void TestStringCompare() {/* String lesser = "abc"; String medium = "abcd"; String greater = "xyz"; String caps = "ABC";*/ AssertEquals("String.Compare(null,null))",0,String.Compare(null,null)); AssertEquals("String.Compare(\"abc\",null)",1, String.Compare("abc", null)); Assert("String.Compare(null,\"abc\")", String.Compare(null,"abc") < 0); Assert("String.Compare(\"abc\",\"xyz\")", String.Compare("abc","xyz") < 0); Assert("String.Compare(\"abc\",\"abcd\")", String.Compare("abc","abcd") < 0); Assert("String.Compare(\"xyz\",\"abc\")", String.Compare("xyz","abc") > 0); Assert("String.Compare(\"abc\",\"ABC\",true)", String.Compare("abc","ABC",true) == 0); Assert("String.Compare(\"abc\",\"ABC\",true)", String.Compare("abc","ABC",true) == 0); Assert("String.Compare(\"abc\",\"ABC\",false)", String.Compare("abc","ABC",false) != 0); Assert("String.Compare(\"a\",\"A\")",String.Compare("a","A") > 0); Assert("String.Compare(\"A\",\"a\")",String.Compare("A","a") < 0); AssertEquals("String.Compare(\"\",\"\")",String.Compare("",""),0); AssertEquals("String.Compare(\"abc\",0,\"xyz\",0,0)", String.Compare("abc",0,"xyz",0,0),0); AssertEquals("String.Compare(\"abcdabcd\",0,\"ab\",0,2)", String.Compare("abcdabcd",0,"ab",0,2),0); AssertEquals("String.Compare(\"abcdabcd\",4,\"ab\",0,2)", String.Compare("abcdabcd",4,"ab",0,2),0); Assert("String.Compare(\"abcdabcd\",1,\"ab\",0,2)", String.Compare("abcdabcd",1,"ab",0,2) > 0 ); Assert("String.Compare(\"abcdabcd\",0,\"xyz\",0,2)", String.Compare("abcdabcd",0,"xyz",0,2) < 0 ); AssertEquals("String.Compare(\"abcdabcd\",0,\"abd\",0,2)", String.Compare("abcdabcd",0,"abd",0,2),0); try { String.Compare("ab",3,"abc",1,7); Fail(" String.Compare(\"ab\",3,\"abc\",1,7) did not throw ArgumentOutOfRangeException"); } catch(ArgumentOutOfRangeException err) { //Ok, that worked :) //move on folks } Assert("String.Compare(\"ABCDC\",1,\"bcd\",0,3,false)", String.Compare("ABCDC",1,"bcd",0,3,false) !=0); Assert("String.Compare(\"ABCDC\",1,\"bcd\",0,3,true)", String.Compare("ABCDC",1,"bcd",0,3,true) !=1); try { String.Compare("ab",3,"abc",1,7,true); Fail(" String.Compare(\"ab\",3,\"abc\",1,7,true) did not throw ArgumentOutOfRangeException"); } catch(ArgumentOutOfRangeException err) { //Ok, that worked :) //move on folks } /* TODO: put in a looped check for Compare () */ } public void TestStringCompareOrdinal() { /* TODO: doesn't this need checks for all the I18n infos ? what's the sense in doing it for English , which is what Compare() does. */ } public void TestStringCompareTo() { /* NOTE: doesn't this actually call the Compare(this,a) func ? */ String str="abc"; Assert("str.CompareTo(\"xyz\")", str.CompareTo("xyz") < 0); Assert("str.CompareTo(\"abc\")", str.CompareTo("abc") == 0); Assert("str.CompareTo(\"0\")", str.CompareTo("0") > 0); } public void TestStringConcat() { //String str1="Fu"; //String str2="Bar"; AssertEquals("String.Concat(\"Fu\",\"Bar\")", String.Concat("Fu","Bar"),"FuBar"); AssertEquals("String.Concat(\"Foo\",\" \",\"Bar\")", String.Concat("Foo"," ","Bar"),"Foo Bar"); // yup , F Up Beyond All Recognition } public void TestStringCopy() { String str1="Foo"; String str2= String.Copy(str1); AssertEquals("String.Copy(str1)==str1",str2,str1); Assert("String.Copy(str1) as Object != str1 as Object",(Object)str2 != (Object)str1); try { String s = String.Copy(null); Fail(" String.Copy(null) should throw an ArgumentNullException"); } catch(ArgumentNullException err) { //ummm... looks good } } public void TestStringCopyTo() { /*TODO*/ String str1 = "FuBar"; try { str1.CopyTo(0,(char[])null,0,0); Fail("str1.CopyTo(0,(char[])null,0,0) should have throws a ArgumentNullException"); } catch(ArgumentNullException err) { //worked ! } char[] c = new char[str1.Length]; for (int i = 0; i < str1.Length ; i++) str1.CopyTo(i, c, i, 1); // copy 1 char at a time String str2 = new String(c); AssertEquals("str1.CopyTo() char by char",str1,str2); // must find a better error message :) } public void TestStringEndsWith() { /*TODO*/ String str = "Foo Bar"; try { bool check = str.EndsWith(null); Fail("String.EndsWith(null) should throw an ArgumentNullException"); } catch (ArgumentNullException) { //OK move on } Assert("str.EndsWith(\"r\")", str.EndsWith("r")); Assert("str.EndsWith(\"Bar\")", str.EndsWith("Bar")); Assert("str.EndsWith(\"Foo Bar\")", str.EndsWith("Foo Bar")); Assert("!str.EndsWith(\"Foo\")", !str.EndsWith("Foo")); } public void TestStringEquals() { /* TODO: really need to see how to I18n here */ String foobar = "FooBar"; String fubar = "FooBar"; String good_copy = foobar; String bad_copy = "I'm bad"; String bad = null; try { bool q=bad.Equals("nothing"); Fail("bad.Equals(\"nothing\") should have thrown a NullReferenceException"); } catch(NullReferenceException err) { //great ! } Assert("!foobar.Equals(null)", !foobar.Equals(null)); Assert("foobar.Equals(fubar)", foobar.Equals(fubar)); Assert("foobar.Equals(good_copy)", foobar.Equals(good_copy)); Assert("!foobar.Equals(bad_copy)", !foobar.Equals(bad_copy)); Assert("String.Equals(null,null)", String.Equals(null, null)); Assert("String.Equals(foobar,fubar)", String.Equals(foobar, fubar)); Assert("!String.Equals(foobar,bad_copy)", !String.Equals(foobar, bad_copy)); } public void TestStringFormat() { /*TODO*/ AssertEquals ("String.Format(\"\",0)", "", String.Format ("", 0)); AssertEquals ("String.Format(\"{0}\",\"FooBar\")", "FooBar", String.Format ("{0}","FooBar")); AssertEquals ("String.Format(\"{0}\",111)", "111", String.Format ("{0}",111)); try { String.Format(null,12); Fail("String.Format(null,12) should throw an ArgumentNullException"); } catch(ArgumentNullException err) { // all's well } try { String.Format("Er..",null); // all's well } catch(ArgumentNullException err) { Fail("String.Format(\"Er...\",null) should not throw an ArgumentNullException"); } try { String.Format("{-1}",12); Fail("String.Format(\"{-1}\",12) should throw an FormatException"); } catch(FormatException err) { // all's well } try { String.Format("{3}",12); Fail("String.Format(\"{3}\",12) should throw an FormatException"); } catch(FormatException err) { // all's well } try { String.Format("{}",12); Fail("String.Format(\"{}\",12) should throw an FormatException"); } catch(FormatException err) { // all's well } AssertEquals("String.Format (\"<{0,5}>\", 12)", "< 12>",String.Format ("<{0,5}>", 12)); AssertEquals("String.Format (\"<{0,-5}>\", 12)", "<12 >",String.Format ("<{0,-5}>", 12)); AssertEquals("String.Format (\"<{0,10}>\", 42)", "< 42>",String.Format ("<{0,10}>", 42)); AssertEquals("String.Format (\"<{0,-10}>\", 42)", "<42 >",String.Format ("<{0,-10}>", 42)); AssertEquals ("String.Format(\"The {1} of {1}\",\"Church\",\"Emacs\")", "The Church of Emacs", String.Format ("The {0} of {1}", "Church", "Emacs")); AssertEquals( "String.Format(\"G{0} N{1} U{2}\",\"nu's\",\"ot\",\"nix\")", "Gnu's Not Unix", String.Format ("G{0} N{1} U{2}", "nu's", "ot", "nix")); AssertEquals ("String.Format (\"{0:x8}\", 0xcafebabe),\"cafebabe\")", "cafebabe", String.Format ("{0:x8}", 0xcafebabe)); AssertEquals ("String.Format (\"{0:X8}\", 0xcafebabe),\"CAFEBABE\")", "CAFEBABE", String.Format ("{0:X8}", 0xcafebabe)); AssertEquals ("String.Format (\"<{0,5:x3}>\", 0x55)", "< 055>", String.Format ("<{0,5:x3}>", 0x55)); AssertEquals ("String.Format (\"<{0,-5:x3}>\", 0x55)", "<055 >", String.Format ("<{0,-5:x3}>", 0x55)); AssertEquals ("String.Format (\"if({0}==0){{ .... }}\",\"i\")", "if(i==0){ .... }", String.Format ("if({0}==0){{ .... }}", "i")); /* Some tests inspired by the mailing list */ AssertEquals ("String.Format (\"0x{0:X2}\", (byte)255)", "0xFF", String.Format("0x{0:X2}", (byte)255)); AssertEquals ("String.Format (\"0x{0:X2}\", (byte)14)", "0x0E", String.Format("0x{0:X2}", (byte)14)); AssertEquals ("String.Format (\"{0:D2}\", 9)", "09", String.Format("{0:D2}", 9)); // Can't use these any more since cultures mess them up -- Rhys.#if false#if CONFIG_EXTENDED_NUMERICS AssertEquals ("String.Format (\"{0:F2}\", 1234567.89)", "1234567.89", String.Format("{0:F2}", 1234567.89)); AssertEquals ("String.Format (\"{0:C2}\", 1234567)", "\u00a4"+"1,234,567.00", String.Format("{0:C2}", 1234567)); AssertEquals ("String.Format (\"{0:E}\", 1234568)", "1.234568E+006", String.Format("{0:E}", 1234568)); AssertEquals ("String.Format (\"{0:e}\", 0.325)", "3.25e-001", String.Format("{0:e}", 0.325)); /* Custom Format tests... */ AssertEquals ("String.Format(\"{0:#,###,##0.00}\", 1234567.81)", "1,234,567.81", String.Format("{0:#,###,##0.00}", 1234567.81)); AssertEquals ("String.Format(\"{0:#,##0,}M\", 1234000)", "1,234M", String.Format("{0:#,###,}M", 1234000)); AssertEquals("String.Format(\"{0:####.##}\", 0)", ".", String.Format("{0:####.##}", 0)); AssertEquals("String.Format(\"{0:###0.0#}\", 0)", "0.0", String.Format("{0:###0.0#}", 0));#endif#endif } public void TestStringGetEnumerator() { String str = "foobar"; char[] c = new char[str.Length]; CharEnumerator en = str.GetEnumerator(); AssertNotNull("CharEnumerator en!=null", en); for (int i = 0; i < str.Length; i++) { en.MoveNext(); c[i] = en.Current; } String copy = new String(c); AssertEquals("Copy by enumerator for string", str, copy); } public void TestStringGetHashCode() { /* TODO: what do I do here ?. (determinicity check ?) s.GetHashCode() == s.GetHashCode() ?. */ String str1 = "foobar"; String str2 = String.Copy(str1); AssertEquals("str1.GetHashCode() == str1.GetHashCode()", str1.GetHashCode(),str1.GetHashCode()); AssertEquals("str1.GetHashCode() == str2.GetHashCode()", str1.GetHashCode(),str2.GetHashCode()); } public void TestStringIndexOf()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -