Skip to content

Commit

Permalink
Fix: have test generation return sequences with elements of right type (
Browse files Browse the repository at this point in the history
#5611)

### Description
Fixes #5608

<!-- Is this a user-visible change? Remember to update RELEASE_NOTES.md
-->
<!-- Is this a bug fix for an issue visible in the latest release?
Mention this in the PR details and ensure a patch release is considered
-->

### How has this been tested?
Passes the existing tests and fixes the issue:
```
import M
method {:test} Test0() {
var seqreal0 : seq<real> := [(1236 as real), (2437 as real)];
expect |seqreal0| >= 2, "If this check fails at runtime, the test does not meet the preconditions";
var r0 := M.example(seqreal0);
}
method {:test} Test1() {
var seqreal0 : seq<real> := [-1.0, 0.0];
expect |seqreal0| >= 2, "If this check fails at runtime, the test does not meet the preconditions";
var r0 := M.example(seqreal0);
}
method {:test} Test2() {
var seqreal0 : seq<real> := [0.0, 0.0];
expect |seqreal0| >= 2, "If this check fails at runtime, the test does not meet the preconditions";
var r0 := M.example(seqreal0);
}
}
```

<!-- Tests can be added to
`Source/IntegrationTests/TestFiles/LitTests/LitTest/` or to
`Source/*.Test/…` and run with `dotnet test` -->

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
  • Loading branch information
stefan-aws authored Jul 9, 2024
1 parent 7e81f44 commit bf592b8
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions Source/DafnyTestGeneration/TestMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private List<string> ExtractInputs(PartialState state, IReadOnlyList<string> pri
} else {
baseValue = printOutput[i];
}
result.Add(GetPrimitiveAsType(baseValue, type));
result.Add(GetPrimitiveAsType(baseValue, type, type));
continue;
}
foreach (var variable in vars) {
Expand Down Expand Up @@ -271,7 +271,7 @@ private string ExtractVariable(PartialValue variable, Type/*?*/ asType) {
case BoolType:
case CharType:
case BitvectorType:
return GetPrimitiveAsType(variable.PrimitiveLiteral, asType);
return GetPrimitiveAsType(variable.PrimitiveLiteral, variableType, asType);
case SeqType seqType:
var asBasicSeqType = GetBasicType(asType, type => type is SeqType) as SeqType;
if (variable?.Cardinality() == -1) {
Expand Down Expand Up @@ -478,16 +478,19 @@ private string GetFieldValue((string name, Type type, bool mutable, string/*?*/
return GetDefaultValue(field.type, field.type);
}

private static string GetPrimitiveAsType(string value, Type/*?*/ asType) {
if ((asType is null or IntType or RealType or BoolType or CharType
or BitvectorType) || value is "[]" or "{}" or "map[]") {
private static string GetPrimitiveAsType(string value, Type type, Type/*?*/ asType) {
if ((type is null) || (asType is null) || value is "[]" or "{}" or "map[]") {
return value;
}
var typeString = asType.ToString();
if (typeString.StartsWith("_System.")) {
typeString = typeString[8..];
var typeString = type.ToString();
var asTypeString = asType.ToString();
if (typeString == asTypeString) {
return value;
}
if (asTypeString.StartsWith("_System.")) {
asTypeString = asTypeString[8..];
}
return $"({value} as {typeString})";
return $"({value} as {asTypeString})";
}

/// <summary>
Expand All @@ -508,15 +511,15 @@ private string GetDefaultValue(Type type, Type/*?*/ asType = null) {
}
switch (type) {
case IntType:
return GetPrimitiveAsType("0", asType);
return GetPrimitiveAsType("0", type, asType);
case RealType:
return GetPrimitiveAsType("0.0", asType);
return GetPrimitiveAsType("0.0", type, asType);
case BoolType:
return GetPrimitiveAsType("false", asType);
return GetPrimitiveAsType("false", type, asType);
case CharType:
return GetPrimitiveAsType("\'a\'", asType);
return GetPrimitiveAsType("\'a\'", type, asType);
case BitvectorType bitvectorType:
return GetPrimitiveAsType($"(0 as bv{bitvectorType.Width})", asType);
return GetPrimitiveAsType($"(0 as bv{bitvectorType.Width})", type, asType);
case SeqType seqType:
return seqType.Arg is CharType ? "\"\"" : AddValue(asType ?? type, "[]");
case SetType:
Expand Down

0 comments on commit bf592b8

Please sign in to comment.