-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(STD-Dafny): HasSubString (#952)
* chore(STD-Dafny): HasSubString
- Loading branch information
Showing
4 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
include "UInt.dfy" | ||
|
||
module StandardLibrary.Sequence { | ||
// export provides SequenceEqual, StandardLibrary | ||
import opened UInt | ||
|
||
predicate method SequenceEqualNat<T(==)>(seq1: seq<T>, seq2: seq<T>, start1: nat, start2: nat, size: nat) : (ret : bool) | ||
requires start1 + size <= |seq1| | ||
requires start2 + size <= |seq2| | ||
ensures ret ==> seq1[start1..start1 + size] == seq2[start2..start2 + size] | ||
{ | ||
if |seq1| > UINT64_MAX_LIMIT || |seq2| > UINT64_MAX_LIMIT then | ||
// This line of code will never be executed, but is included for correctness | ||
seq1[start1..start1 + size] == seq2[start2..start2 + size] | ||
else | ||
SequenceEqual(seq1, seq2, start1 as uint64, start2 as uint64, size as uint64) | ||
} | ||
|
||
predicate SequenceEqual<T(==)>(seq1: seq64<T>, seq2: seq64<T>, start1: uint64, start2: uint64, size: uint64) : (ret : bool) | ||
requires start1 as nat + size as nat <= |seq1| | ||
requires start2 as nat + size as nat <= |seq2| | ||
ensures ret <==> seq1[start1..start1 + size] == seq2[start2..start2 + size] | ||
{ | ||
seq1[start1..start1 + size] == seq2[start2..start2 + size] | ||
} by method { | ||
var j := start2 as uint64; | ||
for i := start1 as uint64 to (start1 + size) as uint64 | ||
invariant j == i - start1 + start2 | ||
invariant forall k : uint64 | start1 <= k < i :: seq1[k] == seq2[k - start1 + start2] | ||
{ | ||
if seq1[i] != seq2[j] { | ||
return false; | ||
} | ||
j := j + 1; | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
include "../src/StandardLibrary.dfy" | ||
include "../src/String.dfy" | ||
include "../../libraries/src/Wrappers.dfy" | ||
|
||
module TestStrings { | ||
import StandardLibrary.String | ||
import opened Wrappers | ||
|
||
method {:test} TestHasSubStringPositive() | ||
{ | ||
var actual := String.HasSubString("Koda is a Dog.", "Koda"); | ||
expect actual == Some(0), "'Koda' is in 'Koda is a Dog.' at index 0, but HasSubString does not think so"; | ||
actual := String.HasSubString("Koda is a Dog.", "Koda is a Dog."); | ||
expect actual == Some(0), "'Koda is a Dog.' is in 'Koda is a Dog.' at index 0, but HasSubString does not think so"; | ||
actual := String.HasSubString("Koda is a Dog.", "Dog."); | ||
expect actual == Some(10), "'Dog.' is in 'Koda is a Dog.' at index 10, but HasSubString does not think so"; | ||
actual := String.HasSubString("Koda is a Dog.", "."); | ||
expect actual == Some(13), "'.' is in 'Koda is a Dog.' at index 13, but HasSubString does not think so"; | ||
actual := String.HasSubString("Koda is a Dog.", ""); | ||
expect actual == Some(0), "The empty string is in 'Koda is a Dog.' at index 0, but HasSubString does not think so"; | ||
} | ||
|
||
method {:test} TestHasSubStringNegative() | ||
{ | ||
var actual := String.HasSubString("Robbie is a Dog.", "Koda"); | ||
expect actual == None, "'Robbie is a Dog.' does not contain Koda"; | ||
actual := String.HasSubString("\t", " "); | ||
expect actual == None, "A tab is not a space"; | ||
actual := String.HasSubString("large", "larger"); | ||
expect actual == None, "Needle larger than haystack"; | ||
} | ||
} |