You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
9.3 KiB
C#

6 years ago
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using Xunit;
namespace LinqStudy.Test.LinqToObject
{
/// <summary>
/// 分区操作符
/// </summary>
public class ShardingTest
{
#region Take
/// <summary>
/// Take返回序列中,从0开始的连续指定项数据子序列延迟执行。
/// </summary>
[Fact]
public void Take_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody4"}
};
var queryItem = customers.Take(2);
var queryItemCount = queryItem.Count();
Assert.Equal(2,queryItemCount);
}
/// <summary>
/// 延迟执行
/// </summary>
[Fact]
public void Take_Daley_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody4"}
};
//使用操作符(相当于定义)
var queryItem = customers.Take(1);
//使用后,添加新项到集合头部
var addItem = new Person { Id = 0, Name = "woody0" };
customers.Insert(0, addItem);
//使用操作符操作结果(相当于:此时执行操作)
var firstItemId = queryItem.First().Id;
//操作结果,包含数据源的变化
Assert.Equal(0, firstItemId);
}
/// <summary>
/// 超过数据源项数:返回全部项,不抛出异常。
/// </summary>
[Fact]
public void Take_OverCount_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody4"}
};
var person = customers.Take(100);
var sourceCount = customers.Count;
var queryItecCount = person.Count();
//超过数据源项数,返回全部项。
Assert.Equal(sourceCount, queryItecCount);
}
/// <summary>
/// ArgumentNullException 异常
/// </summary>
[Fact]
public void Take_ArgumentNullException_Test()
{
List<Person> customers = null;
Action takeQuery = () => customers.Take(100);
Assert.Throws<ArgumentNullException>(takeQuery);
}
#endregion
#region TakeWhile
/// <summary>
/// TakeWhile从序列开头开始选序列项遇到第一个不满足选择条件的项时返回延迟执行。
/// </summary>
[Fact]
public void TakeWhile_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody1"}
};
var person = customers.TakeWhile(c => c.Name.StartsWith("woody1"));
//因为匹配第二项时结果是False程序返回不再进行下次迭代所以返回集合中只有第一项。
Assert.Equal(customers.FindAll(q => q.Id == 1), person);
}
[Fact]
public void TakeWhile_Test2()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody1"}
};
var person = customers.TakeWhile(c => c.Name.StartsWith("woody2"));
//因为匹配第一项时结果是False程序直接返回不再进行下次迭代所以返回空集合。
Assert.Empty(person);
}
/// <summary>
/// 延迟执行
/// </summary>
[Fact]
public void TakeWhile_Delay_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody1"}
};
//TakeWhile
var person = customers.TakeWhile(c => c.Name.StartsWith("woody"));
//TakeWhile后改变数据源
var addItem = new Person { Id = 6, Name = "sunday" };
customers.Insert(2,addItem);
//选择结果
var queryCount = person.Count();
//结果受数据源变化的影响
Assert.Equal(2, queryCount);
}
#endregion
#region Skip
/// <summary>
/// Skip跳过指定数据的元素返回其余元素。
/// </summary>
[Fact]
public void Skip_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody4"}
};
var queryItem = customers.Skip(1);
var queryItemCount = queryItem.Count();
Assert.Equal(3, queryItemCount);
}
/// <summary>
/// 延迟执行
/// </summary>
[Fact]
public void Skip_Daley_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody4"}
};
//使用操作符(相当于定义)
var queryItem = customers.Skip(4);
//使用后,添加新项到集合头部
var addItem = new Person { Id = 5, Name = "woody0" };
customers.Add(addItem);
//使用操作符操作结果(相当于:此时执行操作)
var firstItemId = queryItem.First().Id;
//操作结果,包含数据源的变化
Assert.Equal(5, firstItemId);
}
/// <summary>
/// 超过数据源项数返回0个项的序列不抛出异常。
/// </summary>
[Fact]
public void Skip_OverCount_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody4"}
};
var person = customers.Skip(100);
var queryItecCount = person.Count();
//超过数据源项数,返回0个元素的序列。
Assert.Equal(0, queryItecCount);
}
/// <summary>
/// ArgumentNullException 异常
/// </summary>
[Fact]
public void S_ArgumentNullException_Test()
{
List<Person> customers = null;
Action takeQuery = () => customers.Skip(100);
Assert.Throws<ArgumentNullException>(takeQuery);
}
#endregion
#region SkipWhile
/// <summary>
/// SkipWhile基于特定条件跳过元素直到第一次条件为False时停跳过选择其余项(包括条件为false的这一项),返回。
/// </summary>
[Fact]
public void SkipWhile_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="sunday3"},
new Person { Id=4,Name="woody1"}
};
//因为匹配第3项时结果是False停止跳过并选择后面所有包括第3所有项。
var person = customers.SkipWhile(c => c.Name.StartsWith("wood"));
var queryItemCount = person.Count();
Assert.Equal(2, queryItemCount);
}
/// <summary>
/// 延迟执行
/// </summary>
[Fact]
public void SkipWhile_Delay_Test()
{
List<Person> customers = new List<Person> {
new Person { Id=1,Name="woody1"},
new Person { Id=2,Name="woody2"},
new Person { Id=3,Name="woody3"},
new Person { Id=4,Name="woody1"}
};
//TakeWhile
var person = customers.SkipWhile(c => c.Name.StartsWith("woody"));
//TakeWhile后改变数据源
var addItem = new Person { Id = 6, Name = "sunday" };
customers.Insert(1, addItem);
//选择结果
var queryCount = person.Count();
//结果受数据源变化的影响
Assert.Equal(4, queryCount);
}
#endregion
}
}