Вообщем не пойму, почему не проходит задача..вроде все верно решил..
Задача:
Given two integers, which can be positive and negative, find the sum of all the numbers between including them too and return it. If both numbers are equal return a or b. Note! a and b are not ordered!
Example:
GetSum(1, 0) == 1 // 1 + 0 = 1
GetSum(1, 2) == 3 // 1 + 2 = 3
GetSum(0, 1) == 1 // 0 + 1 = 1
GetSum(1, 1) == 1 // 1 Since both are same
GetSum(-1, 0) == -1 // -1 + 0 = -1
GetSum(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2
Мое решение:
using System;
public class Sum
{
public int GetSum(int a, int b)
{
int sum = 0;
if (a >= 0 && b >= 0)
{
sum = a + b;
}
else
{
int min = Math.Min(a, b);
int max = Math.Max(a, b);
for (int i = min; i <= max; i++)
{
sum += i;
}
}
return sum;
}
}
Тест:
using NUnit.Framework;
using System;
[TestFixture]
public class SumTest1
{
Sum s = new Sum();
[Test]
public void Test1()
{
Assert.AreEqual(1, s.GetSum(0, 1));
Assert.AreEqual(-1, s.GetSum(0, -1));
}
}
Нажимаю RunTest, выдает:
SumTest1
Test1
Test Passed
1 Passed
0 Failed
0 Errors
Process took 556ms to complete
Нажимаю Submit, выдает:
SumTest
TestEqual
Expected: 17
But was: 34
at SumTest.TestEqual () <0x40e34090 + 0x00085> in :0
TestNegative
Test Passed
TestPositive
Expected: 127759
But was: 509
at SumTest.TestPositive () <0x40e3e1d0 + 0x000e4> in :0
TestSwitchMinMax
Expected: 15
But was: 6
at SumTest.TestSwitchMinMax () <0x40e3e300 + 0x00085> in :0
TestZero
Test Passed
2 Passed
3 Failed
0 Errors
Process took 598ms to complete
В чем проблема?