2
當我在發佈模式下啓動以下測試時,它們都通過,但在調試模式下它們都失敗。爲什麼使用方法返回指針會導致測試在調試模式下失敗?
[TestFixture]
public unsafe class WrapperTests
{
[Test]
public void should_correctly_set_the_size()
{
var wrapper = new Wrapper();
wrapper.q->size = 1;
Assert.AreEqual(1, wrapper.rep()->size); // Expected 1 But was: 0
}
[Test]
public void should_correctly_set_the_refcount()
{
var wrapper = new Wrapper();
Assert.AreEqual(1, wrapper.rep()->refcount); // Expected 1 But was:508011008
}
}
public unsafe class Wrapper
{
private Rep* q;
public Wrapper()
{
var rep = new Rep();
q = &rep;
q->refcount = 1;
}
public Rep* rep()
{
return q;
}
}
public unsafe struct Rep
{
public int refcount;
public int size;
public double* data;
}
但是如果刪除代表()方法,使q指針公衆,測試在調試和釋放模式通過兩者。
[TestFixture]
public unsafe class WrapperTests
{
[Test]
public void should_correctly_set_the_size()
{
var wrapper = new Wrapper();
wrapper.q->size = 1;
Assert.AreEqual(1, wrapper.q->size);
}
[Test]
public void should_correctly_set_the_refcount()
{
var wrapper = new Wrapper();
Assert.AreEqual(1, wrapper.q->refcount);
}
}
public unsafe class Wrapper
{
public Rep* q;
public Wrapper()
{
var rep = new Rep();
q = &rep;
q->refcount = 1;
}
}
public unsafe struct Rep
{
public int refcount;
public int size;
public double* data;
}
我不明白什麼會導致這種行爲?
當我使用一種方法返回q的值時,爲什麼測試失敗?