Es gibt zahlreiche Informationen, die statische Überprüfung von Contract.ForAll
hat nur begrenzte oder keine Unterstützung.Code Contracts - ForAll - Was wird durch statische Verifizierung unterstützt
Ich habe viel experimentiert und fand kann es mit arbeiten:
Contract.ForAll(items, i => i != null)
Contract.ForAll(items, p)
wop
vom TypPredicate<T>
es kann nicht Arbeit mit:
- Feld Zugang
- Zugang zur Immobilie
- Methodengruppe (ich glaube, delegieren hier sowieso zugeordnet ist)
- Instanz Methodenaufruf
Meine Fragen sind:
- Was sind andere Arten von Code, mit denen
ForAll
arbeiten kann? - Entspricht der Code Contracts, dass nach
Contract.ForAll(items, i => i != null)
bewiesen ist, dass wenn ein Element aus der Liste später im Code (d. H. Durch Indizierung) genommen wird, das Element nicht null ist?
ist hier voll Testcode:
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
}
}
Vielen Dank für die Mühe, aber meine Frage bezieht sich auf Funktionen der statischen Analyse von Code Contract Engine durchgeführt. Es handelt sich überhaupt nicht um das Laufzeitmerkmal, wie es im Code der ForAll-Funktion implementiert ist. –