master
bicijinlian 6 years ago
parent 5ed8744464
commit ac154ff752

@ -9,10 +9,11 @@ using FluentAssertions;
namespace LinqStudy.Test.LinqToObject
{
/// <summary>
/// 生成操作符
/// 生成操作符
/// </summary>
public class CreateTest
{
#region Empty
/// <summary>
/// Empty 返回指定类型的空集,没有任何元素的集合。
/// Enumerable的静态方法常用来做初始种子集合。
@ -20,10 +21,10 @@ namespace LinqStudy.Test.LinqToObject
[Fact]
public void Empty_string_Test()
{
var ini= Enumerable.Empty<string>();
var ini = Enumerable.Empty<string>();
Assert.IsType<string[]>(ini);
Assert.Empty(ini);
Assert.IsType<string[]>(ini);
Assert.Empty(ini);
}
/// <summary>
@ -37,6 +38,9 @@ namespace LinqStudy.Test.LinqToObject
Assert.IsType<Person[]>(ini);
Assert.Empty(ini);
}
#endregion
#region DefaultIfEmpty
/// <summary>
/// DefaultIfEmpty
@ -45,11 +49,12 @@ namespace LinqStudy.Test.LinqToObject
[Fact]
public void DefaultIfEmpty_Test()
{
var defaultValue= Enumerable.DefaultIfEmpty(Enumerable.Empty<int>());
var defaultValue = Enumerable.DefaultIfEmpty(Enumerable.Empty<int>());
Assert.Single(defaultValue);
Assert.Equal(default(int), defaultValue.First());
}
/// <summary>
/// 数据源为Null时抛出异常。
/// </summary>
@ -61,6 +66,9 @@ namespace LinqStudy.Test.LinqToObject
Assert.Single(defaultValue);
Assert.Equal(default(Person), defaultValue.First());
}
#endregion
#region Range
/// <summary>
/// Range生成指定范围的整数序列
@ -86,7 +94,8 @@ namespace LinqStudy.Test.LinqToObject
//生成100项值均为"test@163.com"的字符串序列
var values = Enumerable.Repeat("test@163.com", 100);
Array.ForEach(values.ToArray(), (emai) => {
Array.ForEach(values.ToArray(), (emai) =>
{
Assert.Equal("test@163.com", emai);
});
}
@ -98,8 +107,8 @@ namespace LinqStudy.Test.LinqToObject
public void Repeat_TestData_Test()
{
//生成测试数据生成了有100项的一个序列这100项均是对同一个对象的引用而不是100个对象。
Random rdm = new Random(DateTime.Now.Ticks.GetHashCode());
//虽然生成时用了随机数但是因对象只生成了一次其它均是这个对象的浅拷贝所以100个对象完全相同100个对同一对象的引用。
var persons = Enumerable.Repeat(new Person()
{
@ -117,17 +126,20 @@ namespace LinqStudy.Test.LinqToObject
});
}
#endregion
#region SequenceEqual
/// <summary>
/// SequenceEqual: 比较源和目标序列返回一个bool值指示所含元素是否相同。
/// </summary>
[Fact]
public void SequenceEqual_Test()
{
Person p=new Person(){Id=1,Name="王小明",Age=20};
var s1=Enumerable.Repeat(p,100);
Person p = new Person() { Id = 1, Name = "王小明", Age = 20 };
var s2=Enumerable.Repeat(p,100);
var s1 = Enumerable.Repeat(p, 100);
var s2 = Enumerable.Repeat(p, 100);
Assert.True(s1.SequenceEqual(s2));
}
@ -135,13 +147,14 @@ namespace LinqStudy.Test.LinqToObject
[Fact]
public void SequenceEqual_No_Test()
{
Person p=new Person(){Id=1,Name="王小明",Age=20};
Person p = new Person() { Id = 1, Name = "王小明", Age = 20 };
var s1=Enumerable.Repeat(p,100);
var s1 = Enumerable.Repeat(p, 100);
var s2=Enumerable.Repeat(p,200);
var s2 = Enumerable.Repeat(p, 200);
Assert.False(s1.SequenceEqual(s2));
}
#endregion
}
}

@ -39,7 +39,7 @@ namespace LinqStudy.Test.LinqToObject
}
/// <summary>
/// 延迟执行
/// 延迟执行
/// </summary>
[Fact]
public void GroupBy_Delay_Test()
@ -128,8 +128,9 @@ namespace LinqStudy.Test.LinqToObject
Assert.Equal(expected,actual);
}
/// <summary>
/// 重载4keySelector elementSelector comparer
/// 重载4keySelector elementSelector comparer
/// </summary>
[Fact]
public void GroupBy_keySelector_elementSelector_comparer_Test()

@ -16,6 +16,8 @@ namespace LinqStudy.Test.LinqToObject
/// </summary>
public class ProjectiveTest
{
#region Select
/// <summary>
/// Select投影简单投影一对一
/// </summary>
@ -26,11 +28,12 @@ namespace LinqStudy.Test.LinqToObject
var persons = PersonManager.GetPersons();
// Act
var maps = persons.Select(p=>p.Age).ToList();
var maps = persons.Select(p => p.Age).ToList();
// Assert
Assert.IsType<List<int>>(maps);
}
/// <summary>
/// 投影为匿名类
/// </summary>
@ -41,7 +44,7 @@ namespace LinqStudy.Test.LinqToObject
var persons = PersonManager.GetPersons();
// Act
var maps = persons.Select(p=>new {Id=p.Id, Node=$"姓名{p.Name},年龄{p.Age}."}).ToList();
var maps = persons.Select(p => new { Id = p.Id, Node = $"姓名{p.Name},年龄{p.Age}." }).ToList();
// Assert
Assert.IsNotType<List<Person>>(maps);
@ -55,12 +58,15 @@ namespace LinqStudy.Test.LinqToObject
{
var persons = PersonManager.GetPersons();
var maps = persons.Select((query,index)=> new KeyValuePair<int, Person>(index,query)).ToList();
var maps = persons.Select((query, index) => new KeyValuePair<int, Person>(index, query)).ToList();
var indexs = persons.Select((query, index) => index).ToList();
Assert.IsType<List<KeyValuePair<int, Person>>>(maps);
Assert.Equal(0,indexs[0]);
Assert.Equal(0, indexs[0]);
}
#endregion
#region SelectMany
/// <summary>
/// 将序列的每个元素投影到 IEnumerable<TResult> 并将结果序列合并为一个序列。
@ -82,7 +88,7 @@ namespace LinqStudy.Test.LinqToObject
new Employee(){Id=5,Name="东升",Emails=new List<string>(){ "ebc@163.com", "ecd@163.com", "ede@163.com" }},
};
var maps = employees.SelectMany(q=>q.Emails).ToList();
var maps = employees.SelectMany(q => q.Emails).ToList();
Assert.IsType<List<string>>(maps);
Assert.Equal(15, maps.Count);
@ -134,7 +140,7 @@ namespace LinqStudy.Test.LinqToObject
new Employee(){ Id=5,Name="东升",Emails=new List<string>(){ "e1@163.com", "e2@163.com", "e3@163.com" } },
};
var maps = employees.SelectMany((employee)=> employee.Emails,(person,email)=> new { Name=person.Name,Email=email}).ToList();
var maps = employees.SelectMany((employee) => employee.Emails, (person, email) => new { Name = person.Name, Email = email }).ToList();
Assert.Equal(15, maps.Count);
}
@ -165,7 +171,7 @@ namespace LinqStudy.Test.LinqToObject
return new List<Employee>() { new Employee() { Id = employee.Id, Name = employee.Name + "_" + idx, Emails = employee.Emails } };
},
(person,email) => new
(person, email) => new
{
Name = email.Name,
Email = email.Emails
@ -175,26 +181,6 @@ namespace LinqStudy.Test.LinqToObject
Assert.Equal(5, maps.Count);
}
[Fact]
public void Test()
{
var cc= DocDemo("");
}
/// <summary>
/// Doc示例
/// </summary>
/// <param name="aa">参数</param>
/// <exception cref="ArgumentNullException">
/// ArgumentNullException
/// </exception>
/// <example>DecDemo("dsfsadf")</example>
/// <seealso cref="SortedDictionary{TKey, TValue}"/>
/// <returns>参数+“-”</returns>
private string DocDemo(string aa)
{
return aa + "-";
}
#endregion
}
}

@ -14,6 +14,7 @@ namespace LinqStudy.Test.LinqToObject
/// </summary>
public class SequenceEqualTest
{
#region SequenceEqual
/// <summary>
/// SequenceEqual
/// 序列相等比较:所含项数量相同、内容相同、位置相同、引用类型则引用相同的对象,则两个序列相等。
@ -60,7 +61,6 @@ namespace LinqStudy.Test.LinqToObject
[Fact]
public void SequenceEqual_ValueType_Test()
{
List<int> sequence1 = new List<int>() { 1, 2, 3 };
List<int> sequence2 = new List<int>() { 1, 2, 3 };
@ -194,5 +194,6 @@ namespace LinqStudy.Test.LinqToObject
Assert.True(sequence1.SequenceEqual(sequence2, new PersonEqualityComparer()));
}
#endregion
}
}

@ -7,8 +7,8 @@ using Xunit;
namespace LinqStudy.Test.LinqToObject
{
/// <summary>
/// Sets 集合操作符
/// 注意:集合操作为 “延迟执行”数据源为null时均引发异常。
/// Sets 集合操作符
/// 注意:集合操作为 “延迟执行”数据源为null时均引发异常。
/// </summary>
public class SetsTest
{

@ -13,6 +13,8 @@ namespace LinqStudy.Test.LinqToObject
/// </summary>
public class WhereTest
{
#region Where
/// <summary>
/// where过滤查询条件
/// </summary>
@ -39,9 +41,10 @@ namespace LinqStudy.Test.LinqToObject
{
List<Person> Persons = null;
Action act = ()=> { Persons.Where(p => p.Name.StartsWith("zhang")); };
Action act = () => { Persons.Where(p => p.Name.StartsWith("zhang")); };
Assert.Throws<ArgumentNullException>(act);
}
#endregion
}
}

@ -76,6 +76,5 @@ namespace LinqStudy.Test.Other
Employees[idx] = value;
}
}
}
}

Loading…
Cancel
Save