




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
namespace ConsoleApplication1 class NUnitSample public int Add(int a, int b) return a + b; static void Main(string args) using NUnit.Framework;namespace ConsoleApplication1 TestFixture public class NUnitSampleTest Test public void AddTest() NUnitSample utl = new NUnitSample(); int result = utl.Add(4, 4); Assert.AreEqual(8, result); Test public void AddTestfailure() NUnitSample utl = new NUnitSample(); int result = utl.Add(4, 4); Assert.AreEqual(6, result); namespace ConsoleApplication1 public class Calculator public int Add(int a, int b) return a + b; public int Minus(int a, int b) return a - b; public int Multiply(int a, int b) return a * b; public int Divide(int a, int b) return a / b; static void Main(string args) Calculator cal = new Calculator(); int result = cal.Add(1,3); Console.WriteLine (result); Console.Read(); namespace ConsoleApplication1 TestFixture public class CalculatorTest TestFixtureSetUp public void openconnection() Console.WriteLine(打开数据连接); TestFixtureTearDown public void closeconnection() Console.WriteLine(关闭数据连接); private int a; private int b; private Calculator cal; SetUp public void Init() Console.WriteLine(共享资源,插入数据); cal = new Calculator(); a = 10; b = 5; TearDown public void destroy() Console.WriteLine(释放资源,删除数据); Test,Ignore (忽略测试) public void TestAdd() Calculator Cal = new Calculator(); int result = Cal.Add(1, 3); Assert.AreEqual(4, result); Console.WriteLine(hello); Test Explicit(显示执行) ExpectedException(typeof(DivideByZeroException) public void TestDivde() Calculator Cal = new Calculator(); int result = Cal.Divide(6, 0); /Assert.AreEqual(2, result); Test Category (number1) public void TsetMinus() int result = cal .Minus (a,b); Assert.AreEqual(5, result); namespace ConsoleApplication2 TestFixture public class TestCmp Test public void LargestOf3() Assert.AreEqual(9, Cmp.Largest(new int 8, 9, 7 ); public class Cmp public static int Largest(int list) int index, max = Int32.MaxValue; if (list.Length = 0) throw new ArgumentException(Empty list); for (index = 0; index max) max = listindex; return max; class class1 static void Main(string args) int list = new int 1, 2, 3, 4 ; int t = Cmp.Largest(list); Console.WriteLine(最大值是+t); Console.Read(); namespace ConsoleApplication3 public class Program public int Dubblesort(int array) if (array = null) Console.WriteLine(空数组); return new int ; for (int i = 0; i array.Length - 1; +i) bool swap=false ; for (int j = 0; j arrayj + 1) int t = arrayj; array j=array j+1; array j+1=t; swap=true ; if (!swap) return array ; return array ; namespace ConsoleApplication3 TestFixture public class testprogram Test public void TestDubbleSort() /int array=null; Program p = new Program(); int array = 1, 8, 4, 6, 5, 9 ; int result = p.Dubblesort(array); int expect = 1, 4, 5, 6, 8, 9 ; Assert.AreEqual(expect, result); Test public void TestDubbleSort2() int array =null ; Program p = new Program(); int result = p.Dubblesort(array); int expect = ; Assert.AreEqual(expect, result); namespace ConsoleApplication4 public class MyStack private int nextindex; private string element; /构造函数 public MyStack() element = new string100; nextindex = 0; public void push(string s) if (100=this.nextindex) throw new Exception (数组越界异常); elementnextindex+ = s; public string pop() if (this.nextindex = 0) throw new Exception(数组越界异常); return element-nextindex; public void delete(int n) if (this.nextindex - n 0) throw new Exception(删除异常); nextindex -= n; public string top() if (this.nextindex = 0) throw new Exception(数组越界异常); return elementnextindex - 1; public int Size() return this.nextindex; static void Main(string args) namespace ConsoleApplication4 TestFixture public class MyStackTest private MyStack mystack; SetUp public void init() mystack =new MyStack (); Test public void TestPush() mystack.push(hello); string result = mystack.pop(); Assert.AreEqual(result, hello); Test public void TestPush2() for (int i = 0; i 100; +i) mystack.push(i + ); for (int i = 0; i 100; +i) string result = mystack.pop(); Assert .AreEqual (99-i)+,result ); Test,ExpectedException public void TestPush3() for (int i = 0; i 101; +i) mystack.push(i + ); for (int i = 0; i 101; +i) string result = mystack.pop(); Assert.AreEqual(100 - i) + , result); Test,ExpectedException public void TestPop() mystack.pop(); Test public void Testpop1() mystack.push(hello); mystack.push(world); mystack.pop(); string result = mystack.pop(); Assert.AreEqual(hello, result); Test public void TestTop() mystack.push(hello); string result = mystack.top(); Assert.AreEqual(hello, result); Test,ExpectedException public void TestTop2() mystack.top(); Test public void TestDelete() for (int i = 0; i 10; +i) mystack.push(i + ); mystack.delete(10); Assert.AreEqual(0, mystack.Size(); Test,ExpectedException public void TestDelete2() for (int i = 0; i largest ) largest =element; return largest; static void Main(string args) using System.IO;namespace ConsoleApplication5 TestFixture public class MathComputeTest private MathCompute mc; TestFixtureSetUp public void init() mc = new MathCompute(); Test public void LargestTest() StreamReader sr = new StreamReader(././DataSet.txt); string line = null; while (null!=(line = sr.ReadLine() ) if (line.StartsWith (#) continue; string tokens = line.Split(,); int expected= Int32.Parse(tokens0); List list=new List(); for(int i=1;itokens .Length ;+i) list.Add (Int32.Parse(tokens i); int input=list .ToArray (); Assert.AreEqual(expected ,mc.Largest (input); sr.Close(); namespace ConsoleApplication6 public class Connection public static SqlConnection Getconnextion() string con = Data Source=localhost;Initial Catalog=test;Integrated Security=True; SqlConnection connection = new SqlConnection(con); try connection.Open(); catch return null; return connection; namespace ConsoleApplication6 TestFixture public class Connectiontest Test public void TestConnection() SqlConnection con = Connection.Getconnextion(); Assert.IsNotNull(con); public class person private int id; private string username; private string password; private int age; public int Id get return id; set id = value; public string Username get return username; set username = value; public string Password get return password; set password = value; public int Age get return age; set age = value; public class DDperson public void Insert(person person) string sql = insert into person(username,password,age) values(username,password,age); SqlConnection con = Connection.Getconnextion(); SqlCommand command = new SqlCommand(sql, con); command.Parameters.Add(new SqlParameter(username, SqlDbType.VarChar); command.Parameters.Add(new SqlParameter(password, SqlDbType.VarChar); command.Parameters.Add(new SqlParameter(age, SqlDbType.Int); command.Parametersusername.Value = person.Username; command.Parameterspassword.Value = person.Password; command.Parametersage.Value = person.Age; try command.ExecuteNonQuery(); catch (Exception ex) Console.WriteLine(ex.Message); finally con.Close(); public void Update(person person) string sql = update person set username=username,password=password,age=age where id=id; SqlConnection con = Connection.Getconnextion(); SqlCommand command = new SqlCommand(sql, con); command.Parameters.Add(new SqlParameter(username, SqlDbType.VarChar); command.Parameters.Add(new SqlParameter(password, SqlDbType.VarChar); command.Parameters.Add(new SqlParameter(age, SqlDbType.Int); command.Parameters.Add(new SqlParameter(id, SqlDbType.Int); command.Parametersusername.Value = person.Username; command.Parameterspassword.Value = person.Password; command.Parametersage.Value = person.Age; command.Parametersid.Value = person.Id; try command.ExecuteNonQuery(); catch (Exception ex) Console.WriteLine(ex.Message); finally con.Close(); public person Getbyid(int id) string sql = select * from person where id=id; SqlConnection con = Connection.Getconnextion(); SqlCommand command = new SqlCommand(sql, con); command.Parameters.Add(new SqlParameter(id, SqlDbType.Int); command.Parametersid.Value = id; SqlDataReader reader = command.ExecuteReader(); person person = null; if (reader.Read() person = new person(); person.Id = id; person.Username = readerusername.ToString(); person .Password =reader password.ToString(); person.Age = Convert.ToInt32(readerage); reader.Close(); con.Close(); return person; public void Removebyid(int id) string sql = delete from person where id=id; SqlConnection con = Connection.Getconnextion(); SqlCommand command = new SqlCommand(sql, con); command.Parameters.Add(new SqlParameter(id, SqlDbType.Int); command.Parametersid.Value = id; try command.ExecuteNonQuery(); catch (Exception ex) Console.WriteLine(ex.Message); finally con.Close(); static void Main(string args) person person = new person(); person.Username = lhj; person.Password = 123; person.Age = 20; DDperson ddperson = new DDperson(); / ddperson.Insert(person); int id = 19; person = ddperson.Getbyid(id); Console.WriteLine(person .Username + +person .Password + +person .Age ); person.Age = 50; Console.WriteLine(person.Username + + person.Password + + person.Age); ddperson.Update(person); int x = 20; ddperson.Removebyid(x); Console.Read(); namespace ConsoleApplication6 TestFixture public class DDPersonTest private DDperson ddperson; TestFixtureSetUp public void init() ddperson = new DDperson(); Test public void TestInsert() person person = new person(); person.Username = li; person.Password = hong; person.Age = 20; ddperson.Insert(person); int maxid = this.getmaxid(); person person2 = ddperson.Getbyid(maxid); person.Id = maxid; this.Compare(person, person2); /做?完测a试?后删?除y数y据Y库a中D的?记?录? ddperson.Removebyid(maxid); Test public void TestUpdate() person person = new person(); person.Username = fang; person.Password = 456; person.Age = 25; ddperson.Insert(person); int maxid = this.getmaxid(); person person2 = ddperson.Getbyid(maxid); person2.Username = xyz; person2.Password = 789; person2.Age = 30; ddperson.Update(person2); person person3 = ddperson.Getbyid(maxid ); this.Compare(person3, person2); ddperson .Removebyid (maxid ); Test public void TestGetbyid() person person = new person(); person
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工装模具库管理办法
- 水管道安装管理办法
- 商品房交付管理办法
- 原材料招标管理办法
- 旧放射诊疗管理办法
- 扫地车使用管理办法
- 无泄漏装置管理办法
- 批检验记录管理办法
- 合同履约率管理办法
- 新资本管理办法原文
- 供配电系统巡检记录表
- 食品安全与日常饮食智慧树知到期末考试答案章节答案2024年中国农业大学
- 食品安全培训之肉类加工业
- 人教版(新起点)小学六年级上册英语教案(全册)
- 中职英语课标词汇表
- (28)-1.1.1 国外生命教育发展
- 手术物品清点(获奖课件)
- RFJ05-2009-DQ人民防空工程电气大样图集
- GB/T 31586.1-2015防护涂料体系对钢结构的防腐蚀保护涂层附着力/内聚力(破坏强度)的评定和验收准则第1部分:拉开法试验
- 2023年冬雨季及夜间施工措施
- 康乐保造口产品课件
评论
0/150
提交评论