本文整理汇总了C#中NUnit.Framework.List.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# List.Sum方法的具体用法?C# List.Sum怎么用?C# List.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NUnit.Framework.List
的用法示例。
在下文中一共展示了List.Sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestFiniteReverseAggregate
public void TestFiniteReverseAggregate()
{
var seq = new List<int> { 1, 2, 3, 4, 5 };
Assert.That(seq.ReverseAggregate(0, (x, acc) => acc.Value + x), Is.EqualTo(seq.Sum()));
Assert.That(seq.ReverseAggregate((x1, x2) => x1 + x2.Value), Is.EqualTo(seq.Sum()));
}
示例2: Check_if_data_is_not_retrieved_from_queue
public void Check_if_data_is_not_retrieved_from_queue()
{
const int N = 200;
const int N_THREADS = 3;
var sut = new Parallelize(N_THREADS);
var are = new AutoResetEvent(false);
var results = new List<int>();
var threads = new Dictionary<int,bool>();
Action<IMessage> dequeue = _ => {
lock(results)
{
if (!threads.ContainsKey(Thread.CurrentThread.ManagedThreadId))
threads.Add(Thread.CurrentThread.ManagedThreadId, true);
var i = (int) _.Data;
results.Add(i);
if (results.Count == N) are.Set();
Thread.Sleep(i % 20);
}
};
sut.Start();
for(var i = 1; i<=N; i++)
sut.Process(new Message("x", i), dequeue);
Assert.IsTrue(are.WaitOne(4000));
Assert.AreEqual((N*(N+1)/2), results.Sum());
Assert.AreEqual(N_THREADS, threads.Count);
}
示例3: Download
public void Download(byte[] data, int bufferSize, DownloadSegmentPositions[] segmentPositionInfos)
{
BeforeDownload();
DownloadStream inputStream = new DownloadStream(data);
IList<ISegmentDownloadTask> segmentDownloadTasks = new List<ISegmentDownloadTask>(segmentPositionInfos.Length);
IList<DownloadStream> downloadStreams = new List<DownloadStream>(segmentPositionInfos.Length);
MemoryStream outputStream = new MemoryStream();
for (int i = 0; i < segmentPositionInfos.Length; i++)
{
DownloadSegmentPositions segmentPosition = segmentPositionInfos[i];
byte[] dataPart = data.Skip((int) segmentPosition.StartPosition).Take((int)(segmentPosition.EndPosition - segmentPosition.StartPosition + 1)).ToArray();
DownloadStream downloadStream = new DownloadStream(dataPart);
segmentDownloadTasks.Add(CreateSegmentDownloadTask(bufferSize, CreateSegmentDownloader(downloadStream, segmentPosition), CreateSegmentWriter(outputStream)));
downloadStreams.Add(downloadStream);
}
SegmentDownloadManager segmentDownloadManager = new SegmentDownloadManager(new SegmentDownloadTaskCollection(segmentDownloadTasks));
segmentDownloadManager.Start();
segmentDownloadManager.Finish(true);
AfterDownload();
long totalDownloads = downloadStreams.Sum(x => x.TotalDownloads);
Assert.AreEqual(data.Length, totalDownloads);
Assert.AreEqual(inputStream.ToArray().Take(data.Length).ToArray(), outputStream.ToArray().Take(data.Length).ToArray());
Assert.AreEqual(inputStream.ToArray(), outputStream.ToArray());
}
示例4: FindPandigitalProducts
public void FindPandigitalProducts()
{
var products = new List<int>();
// Highest pandigital standalone number = 987654321
// Square root of this is 31426
//var limit = (int) Math.Pow(10, 9);
// Most lopsided calc would be single digit * 4 digit => 4/5 digit product
for (int a = 1; a < 10000; ++a)
{
for (int b = (a + 1); b < 10000; ++b)
{
var product = a * b;
if (product < 1000) continue; // Not going to have enough digits between a, b and product
if (product > 99999) break; // Too many digits
if (products.Contains(product)) continue;
var isPandigital = IsPandigital(a, b, product);
if (isPandigital)
products.Add(product);
}
}
var sum = products.Sum();
Console.WriteLine("Sum: {0}", sum);
foreach (var product in products)
{
Console.WriteLine(" {0}", product);
}
sum.Should().Be(45228);
}
示例5: FindAmicableNumbersUnderTenThousand
public void FindAmicableNumbersUnderTenThousand()
{
const long limit = 10000;
var list = new List<long>();
for (long l = 1; l < limit; ++l)
{
if (list.Contains(l)) continue;
var amicable = GetAmicablePair(l);
if (amicable != 0 && amicable < limit)
{
list.Add(l);
if (!list.Contains(amicable)) list.Add(amicable);
}
}
foreach (var l in list)
{
Console.WriteLine(l);
}
var result = list.Sum();
Console.WriteLine("Sum of amicable numbers is: {0}", result);
result.Should().Be(31626);
}
示例6: Check_if_data_is_not_retrieved_from_queue
public void Check_if_data_is_not_retrieved_from_queue()
{
const int N = 200;
const int N_THREADS = 3;
var sut = new Parallelize<int>(N_THREADS);
var are = new AutoResetEvent(false);
var results = new List<int>();
var threads = new Dictionary<long,bool>();
Action<int> dequeue = _ => {
lock(results)
{
if (!threads.ContainsKey(Thread.CurrentThread.GetHashCode()))
threads.Add(Thread.CurrentThread.GetHashCode(), true);
results.Add(_);
if (results.Count == N) are.Set();
Thread.Sleep(_ % 10);
}
};
sut.Start();
for(var i = 1; i<=N; i++)
sut.Process(i, dequeue);
Assert.IsTrue(are.WaitOne(4000));
Assert.AreEqual((N*(N+1)/2), results.Sum());
Assert.AreEqual(N_THREADS, threads.Count);
}
示例7: Should38
public void Should38()
{
var times = new List<long>();
var lines = File.ReadAllLines("C:\\tmp\\badphonenumber-clean.log");
foreach (var line in lines)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
var isValidNumber = IsValidNumber(line.Trim());
stopwatch.Stop();
if (isValidNumber)
{
Console.WriteLine("{0}", line);
}
times.Add(stopwatch.ElapsedMilliseconds);
Thread.Sleep(TimeSpan.FromMilliseconds(500));
}
var average = times.Average();
var count = times.Count;
var sum = times.Sum();
var max = times.Max();
Console.WriteLine("{0} ms. {1} numbers. {2} sum. {3} max", average, count, sum, max);
}
示例8: Add_method_accepts_unknown_number_of_numbers
public void Add_method_accepts_unknown_number_of_numbers()
{
// Allow the Add method to handle an unknown amount of numbers
// Arrange
var randomNumber = new Random().Next(1, 100);
var numbersList = new List<int>(randomNumber);
var builder = new StringBuilder();
for (var index = 0; index < randomNumber; index++)
{
var thisNumber = new Random().Next(0, 100);
numbersList.Add(thisNumber);
if (builder.Length > 0)
builder.Append(",");
builder.Append(thisNumber);
}
var numbers = builder.ToString();
var expected = numbersList.Sum();
// Act
var actual = new Calculator().Add(numbers);
// Assert
Assert.That(actual, Is.EqualTo(expected));
}
示例9: FileSizeSumTestManyGBs
public void FileSizeSumTestManyGBs()
{
var list = new List<FileSize>();
Enumerable.Range(1, 1024 * 1024 * 10).ToList().ForEach(it => list.Add(new FileSize(1, Prefix.Mb)));
var result = new FileSize(10, Prefix.Tb);
list.Sum().Should().Be(result);
}
示例10: should_sum_values
public void should_sum_values()
{
var nums = new List<int>
{
5,
9,
1,
10
};
int sum = nums.Sum();
sum.ShouldBe(25);
}
示例11: Process_messages_on_different_threads
public void Process_messages_on_different_threads()
{
Console.WriteLine("test thread: {0}", Thread.CurrentThread.GetHashCode());
var frc = new FlowRuntimeConfiguration();
frc.AddStream(new Stream(".in", "doParallel**"));
frc.AddStream(new Stream("doParallel", ".out"));
var threads = new Dictionary<long, int>();
frc.AddFunc<int, int>("doParallel",
x =>
{
lock (threads)
{
if (
threads.ContainsKey(
Thread.CurrentThread.GetHashCode()))
threads[Thread.CurrentThread.GetHashCode()] += 1;
else
threads.Add(Thread.CurrentThread.GetHashCode(), 1);
}
Console.WriteLine("thread {0}: {1}.",
Thread.CurrentThread.GetHashCode(), x);
Thread.Sleep((DateTime.Now.Millisecond % 100 + 1) * 50);
return x;
});
using (var sut = new FlowRuntime(frc))
{
var are = new AutoResetEvent(false);
var results = new List<int>();
sut.Result += _ =>
{
Console.WriteLine("result: {0}.", _.Data);
lock (results)
{
results.Add((int) _.Data);
if (results.Count == 5) are.Set();
}
};
sut.Process(new Message(".in", 1));
sut.Process(new Message(".in", 2));
sut.Process(new Message(".in", 3));
sut.Process(new Message(".in", 4));
sut.Process(new Message(".in", 5));
Assert.IsTrue(are.WaitOne(10000), "Processing took too long; not enough numbers received");
Assert.AreEqual(15, results.Sum(), "Wrong sum; some number got processed twice");
}
}
示例12: CommandExecuted_event_contains_sequential_entry_ids
public void CommandExecuted_event_contains_sequential_entry_ids()
{
var config = new EngineConfiguration()
.ForImmutability()
.ForIsolatedTest();
var engine = Engine.Create<ImmutableModel>(config);
var sequence = new List<ulong>();
engine.CommandExecuted += (s, e) => sequence.Add(e.JournalEntryId);
for(int i = 1; i <=100; i++) engine.Execute(new AppendNumberCommand(i));
var sum = engine.Execute(m => m.Numbers().Sum());
Assert.AreEqual(sum, sequence.Sum(n => (decimal) n));
}
示例13: should_be_able_to_specify_property_to_sum
public void should_be_able_to_specify_property_to_sum()
{
var jedi = new List<Jedi>
{
new Jedi("Yoda").MidichlorianCountIs(1000),
new Jedi("Anakin Skywalker").MidichlorianCountIs(3000),
new Jedi("Luke Skywalker").MidichlorianCountIs(1500),
new Jedi("Obi-wan Kenobi").MidichlorianCountIs(500),
};
int sum = jedi.Sum(j => j.MidichlorianCount);
sum.ShouldBe(6000);
}
示例14: GetTestData
public void 配列をたくさん作って合成()
{
var data = GetTestData();
for (var i = 0; i < 1000; i++) {
var list = new List<double[]>();
var spectrums = new double[data.Length][];
Parallel.For(
0,
data.Length,
j => spectrums[j] = ArrayFactory.ByFuncDouble(GetSingleGoussianFunc(data[j].Pc, data[j].Pr), 4096));
var array = new double[4096];
Parallel.For(0, 4096, j => array[j] = list.Sum(a => a[j]));
}
}
示例15: TestSumAggregate
public void TestSumAggregate()
{
List<int?> l = new List<int?>
{
0,
3,
null,
4,
5
};
int? sum = l.Sum();
int? agg = l.Aggregate((int?)0, (s, x) => s + x);
Assert.IsFalse(sum == null);
Assert.IsTrue(agg == null);
}