Có rất nhiều thông tin kiểm tra tĩnh của Contract.ForAll
chỉ giới hạn hoặc không có hỗ trợ.Hợp đồng mã - ForAll - Những gì được hỗ trợ bởi xác minh tĩnh
tôi đã làm rất nhiều thử nghiệm và thấy nó có thể làm việc với:
Contract.ForAll(items, i => i != null)
Contract.ForAll(items, p)
nơip
là loạiPredicate<T>
nó không thể làm việc với:
- truy cập Dòng
- truy cập tài sản nhóm
- Method (Tôi nghĩ đại biểu được phân bổ ở đây anyway)
- Instance gọi phương thức
Câu hỏi của tôi là:
- là gì các loại mã khác mà
ForAll
có thể hoạt động với? - Hợp đồng mã có đảm bảo rằng sau khi
Contract.ForAll(items, i => i != null)
được chứng minh, rằng khi lấy một mục từ danh sách sau này trong mã (tức là bằng cách lập chỉ mục), mục đó không phải là rỗng?
Ở đây có đầy đủ mã kiểm tra:
public sealed class Test
{
public bool Field;
public static Predicate<Test> Predicate;
[Pure]
public bool Property
{
get { return Field; }
}
[Pure]
public static bool Method(Test t)
{
return t.Field;
}
[Pure]
public bool InstanceMethod()
{
return Field;
}
public static void Test1()
{
var items = new List<Test>();
Contract.Assume(Contract.ForAll(items, i => i != null));
Contract.Assert(Contract.ForAll(items, i => i != null)); // OK
}
public static void Test2()
{
var items = new List<Test>();
Contract.Assume(Contract.ForAll(items, Predicate));
Contract.Assert(Contract.ForAll(items, Predicate)); // OK
}
public static void Test3()
{
var items = new List<Test>();
Contract.Assume(Contract.ForAll(items, i => i.Field));
Contract.Assert(Contract.ForAll(items, i => i.Field)); // assert unproven
}
public static void Test4()
{
var items = new List<Test>();
Contract.Assume(Contract.ForAll(items, i => i.Property));
Contract.Assert(Contract.ForAll(items, i => i.Property)); // assert unproven
}
public static void Test5()
{
var items = new List<Test>();
Contract.Assume(Contract.ForAll(items, Method));
Contract.Assert(Contract.ForAll(items, Method)); // assert unproven
}
public static void Test6()
{
var items = new List<Test>();
Contract.Assume(Contract.ForAll(items, i => i.InstanceMethod()));
Contract.Assert(Contract.ForAll(items, i => i.InstanceMethod()));// assert unproven
}
}
Cảm ơn bạn đã nỗ lực, nhưng câu hỏi của tôi là về khả năng phân tích tĩnh được thực hiện bởi Công cụ hợp đồng mã. Nó không phải là ở tất cả về đặc tính thời gian chạy như được thực hiện trong mã của chức năng ForAll. –