Replies: 2 comments 2 replies
-
老蕭OLDShaw(程式) 我的答案namespace PG0008.Tests
{
public class MockRandom : IRandom
{
float _rand;
public MockRandom(float rand)
{
_rand = rand;
}
public float Next()
{
return _rand;
}
}
public class Question1Test
{
[Test]
public void TestInvalidCritRate()
{
MockRandom random = new(0);
DamageCalculator damageCalculator = new(random);
int atk = 0;
float critRate = -1;
Assert.Throws<ArgumentException>(() => { damageCalculator.Damage(atk, critRate); });
}
[TestCase(15, 100, 100, 10)]
[TestCase(10, 0, 0, 10)]
public void TestCrit(int result, int rand, float critRate, int atk)
{
MockRandom random = new(rand);
DamageCalculator damageCalculator = new(random);
Assert.AreEqual(result, damageCalculator.Damage(atk, critRate));
}
[Test]
public void TestNegativeAtk()
{
MockRandom random = new(0);
DamageCalculator damageCalculator = new(random);
int atk = -1;
float critRate = 0;
Assert.AreEqual(0, damageCalculator.Damage(atk, critRate));
}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
小4 ( 程式 ) 答案public class PG0008Test
{
[TestCase(0)]
[TestCase(1)]
[TestCase(null)]
[TestCase(int.MaxValue)]
public void TestDamage_Critical(int atk)
{
MoqRandom random = new MoqRandom(0);
DamageCalculator dc = new DamageCalculator(random);
int actual = dc.Damage(atk, 1);
int expected = (int)(atk * 1.5f);
Assert.AreEqual(expected, actual);
}
[TestCase(0)]
[TestCase(1)]
[TestCase(null)]
[TestCase(int.MaxValue)]
public void TestDamage_NoCritical(int atk)
{
MoqRandom random = new MoqRandom(1);
DamageCalculator dc = new DamageCalculator(random);
int actual = dc.Damage(atk, 0);
float expected = atk;
Assert.AreEqual((int)expected, actual);
}
[TestCase(0, 1)]
[TestCase(int.MaxValue, float.MaxValue)]
public void TestDamage_RandomNegative(int atk, float critRate)
{
MoqRandom random = new MoqRandom(-1);
DamageCalculator dc = new DamageCalculator(random);
Assert.AreEqual((int)(atk * 1.5f), dc.Damage(atk, critRate));
}
[TestCase(0)]
[TestCase(1)]
[TestCase(null)]
[TestCase(int.MaxValue)]
public void TestDamage_AttackNegative(float critRate)
{
MoqRandom random = new MoqRandom();
DamageCalculator dc = new DamageCalculator(random);
Assert.AreEqual(0, dc.Damage(-1, critRate));
}
[TestCase(0)]
[TestCase(1)]
[TestCase(null)]
[TestCase(int.MaxValue)]
[TestCase(int.MinValue)]
public void TestDamage_CritRateNegative(int atk)
{
Assert.Throws<ArgumentException>(() =>
{
MoqRandom random = new MoqRandom();
DamageCalculator dc = new DamageCalculator(random);
int actual = dc.Damage(atk, -1);
});
}
}
public class MoqRandom : IRandom
{
private bool test;
private float value;
public MoqRandom(float value)
{
test = true;
this.value = value;
}
public MoqRandom()
{
}
public float Next()
{
if (test)
{
return value;
}
Random random = new Random();
return random.Next();
}
} 反饋我好像搞錯重點了 我以為要驗證有沒有算錯數值 所以直接Damage裡面的code直接貼到test裡面去 理所當然的測試全對 然後我發現一個問題 如果Damage的atk給int.MaxValue就算沒有爆擊還是會溢位耶 int max = int.MaxValue;
float fMax = max;
Debug.Log((int)fMax); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Introduction
請各位根據我提供的程式碼,
撰寫自己的測試程式碼。
測試如何寫通常不會有硬性要求,
不過通常可以看看公司內(如果已就業)的測試程式碼怎麼寫,
跟著寫即可。
測試最好的情況我們會要求覆蓋率達到90%以上,
簡單來說只要你有if,那麼你就要測試滿足與不滿足if的情況。
Answer
解答
Beta Was this translation helpful? Give feedback.
All reactions