Eric Gunnerson ???????????????????????????????????????????????????????ó????????????
??????????β??????飬?????????????????????????γ????????????? C# ????????????鳤??????????? C++ ????????????鳤????Щ????????????????????????????????????????

?????????????е???????????????????????????????飬?乤???????п??????????????????????????в????飬????????????????????????????????????????????????????????

???????????????????????????????ò???????????????????????????????????????????????????????????β??????????????????в?????????????????е??????????????????????? - ????????????????????????в??????????????????????????????У?

??????????????????????????????????д???????в???????????Щ????????????????????????????????????????????????????????????????????У????????????????????????????????????С?

???????????????

????????????????????к??

????1??????α?д????????????????
????2??????????д???????????????
????3???????е?????????????????????????
????4???????????????????????
????5??????????????

?????????????????·??????????????????????????????????????????????????????????????????????????????????????????? 100 ?? 1000 ????????????????????????????????1?????????????????????????汾??????????

????????????????????д????????????е????????????ò?????????????????д?????д????????е?????????????????????????????????????

??????????????

??????ν??????????????????д???????д???????????в???????????У??????????????????????????1????????Щ??????????????????????????κβ????

??????????? 20 ???? 90 ??????????? Smalltalk ???磬Kent Beck ??????д?? SmalltalkUnit????????????У?????????????????????????????????????????????? .NET Framework ??????????? nUnit????????

???????

?????????????д??? IntegerList ???????????????????????????IntegerList ?? ArrayList ?????壬?????????洢??????????????????????????????

????????????????????????????????????????? IntegerList.cs ??????????? nUnit ??????????? nUnit ???????á?????????У?????λ?? d:program files Unit v2.0in??

????????????Щ??俼????ζ??????в???????????????t????Щ???????????????????????????????????? 1 ?????б????????????????????????????????????????б???????????????????????????????????б??

????1???????????????
????2?????????????????б??????????????????????
????3???????????????????????????
????4???????б???????????????
????5????? foreach ?????б??

??????????????????????????????????????????????е?????????????????????С???????????????????????????

????????????????????????????? IntegerListTest.cs ???? C# ????????????????в????????????????????????????

using System??
using System.Collections??
using NUnit.Framework??

namespace IntegerList
{
/// <summary>
/// IntegerClassTest ?????????
/// </summary>
[TestFixture]
public class IntegerClassTest
{
[Test]
public void ListCreation????
{
IntegerList list = new IntegerList??????
Assertion.AssertNotNull??list????
}
}
}
 
[TestFixture] ??????????????????[Test] ????? ListCreation???? ????????????????????????У????????????б???????? Assertion ???????? gets ?????????

????????? nUnit GUI ??????????????????????????Щ???????????????????

 

? 1????????????? nUnit GUI

????????????в???????????????????????Щ??????????????????????б?????????????????????????????

[Test]
public void TestSimpleAdd????
{
IntegerList list = new IntegerList??????
list.Add??5????
list.Add??10????
Assertion.AssertEquals??2?? list.Count????
Assertion.AssertEquals??5?? list[0]????
Assertion.AssertEquals??10?? list[1]????
}

???????????У??????????????????????

????1???б??????? Count ?????
????2???б????????????

?????Щ??????????????????????????????????????????????????????????????????????????????????Щ??????飬????????????????????????

??????????δ?????????? IntegerList ??????з??????????????????????????′?????б???

public int Count
{
get
{
return -1??
}
}

public void Add??int value??
{
}

public int this[int index]
{
get
{
return -1??
}
}
 

????????????????в???????????????????????????????????????????ζ??????????????????????????????????д????????????Щ???????????????????Ч????????

public int Count
{
get
{
return elements.Length??
}
}

public void Add??int value??
{
int newIndex??
if ??elements != null??
{
int[] newElements = new int[elements.Length + 1]??
for ??int index = 0?? index < elements.Length??
index++??
{
newElements[index] = elements[index]??
}
newIndex = elements.Length??
elements = newElements??
}
else
{
elements = new int[1]??
newIndex = 0??
}
elements[newIndex] = value??
}

public int this[int index]
{
get
{
return elements[index]??
}
}
 

????????????????????С????????????д???????????????????????????????????????к???????????????????????д????????? 1000 ?????????

[Test]
public void TestOneThousandItems????
{
list = new IntegerList??????

for ??int i = 0?? i < 1000?? i++??
{
list.Add??i????
}

Assertion.AssertEquals??1000?? list.Count????
for ??int i = 0?? i < 1000?? i++??
{
Assertion.AssertEquals??i?? list[i]????
}
}
 

??????????????????????????????κθ????

??????? ToString???? ????

????????????????????????????? ToString???? ??????????У?

[Test]
public void TestToString????
{
IntegerList list = new IntegerList??????
list.Add??5????
list.Add??10????
string t = list.ToString??????
Assertion.AssertEquals??"5?? 10"?? t.ToString????????
}

???????????????′??????????????

public override string ToString????
{
string[] items = new string[elements.Length]??
for ??int index = 0?? index < elements.Length?? index++??
{
items[index] = elements[index].ToString??????
}
return String.Join??"?? "?? items????
}
 

 

???? Foreach

??????????????????? foreach ????????б???????????????????? Ienumerable???????????????????????? Ienumerable ????????????????

[Test]
public void TestForeach????
{
IntegerList list = new IntegerList??????
list.Add??5????
list.Add??10????
list.Add??15????
list.Add??20????

ArrayList items = new ArrayList??????

foreach ??int value in list??
{
items.Add??value????
}

Assertion.AssertEquals??"Count"?? 4?? items.Count????
Assertion.AssertEquals??"index 0"?? 5?? items[0]????
Assertion.AssertEquals??"index 1"?? 10?? items[1]????
Assertion.AssertEquals??"index 2"?? 15?? items[2]????
Assertion.AssertEquals??"index 3"?? 20?? items[3]????
}

?????? IntegerList ??? IEnumerable??

public IEnumerator GetEnumerator????
{
return null??
}
 
???в???????????????????????????????????????????????????????????

class IntegerListEnumerator: IEnumerator
{
IntegerList list??
int index = -1??

public IntegerListEnumerator??IntegerList list??
{
this.list = list??
}
public bool MoveNext????
{
index++??
if ??index == list.Count??
return??false????
else
return??true????
}
public object Current
{
get
{
return??list[index]????
}
}
public void Reset????
{
index = -1??
}
}
 

????????????????? IntegerList ?????????????????е?????

?????????????????б???? foreach ??????????????? Current ????????????????????ζ????????????????????????????????????????????????????????????????????????????? GetEnumerator???? ??????????????????? IEnumerator??????????е? Current ????? int ?????

?????????д??????????????????????????????????????????????????????????????д??????????????? foreach ??????????

????foreach ??int value in ??IEnumerable?? list??

????????????????б??????????????????????С???????????????????????????????

???????????

????????μ???????±?д????????????????????С???????????д????????????????????????????Щ????????????????????????????????????????д??

????????????С????????????????????????????????????С??????????????????????????????ν???????????????????