-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[stdlib] Expanded string slicing test #2367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,107 @@ | ||
| // RUN: %target-run-simple-swift | FileCheck %s | ||
| // RUN: rm -rf %t | ||
| // RUN: mkdir -p %t | ||
| // RUN: %target-build-swift %s -o %t/a.out | ||
| // RUN: %target-run %t/a.out | ||
| // REQUIRES: executable_test | ||
|
|
||
| func test(_ s: String) { | ||
| print(s) | ||
| var s2 = s[s.index(s.startIndex, offsetBy: 2)..<s.index(s.startIndex, offsetBy: 4)] | ||
| print(s2) | ||
| var s3 = s2[s2.startIndex..<s2.startIndex] | ||
| var s4 = s3[s2.startIndex..<s2.startIndex] | ||
| import StdlibUnittest | ||
|
|
||
| var SubstringTests = TestSuite("SubstringTests") | ||
|
|
||
| func checkMatch<S: Collection, T: Collection | ||
| where S.Index == T.Index, S.Iterator.Element == T.Iterator.Element, | ||
| S.Iterator.Element: Equatable>( | ||
| _ x: S, _ y: T, _ i: S.Index) { | ||
|
|
||
| expectEqual(x[i], y[i]) | ||
| } | ||
|
|
||
| SubstringTests.test("String") { | ||
| let s = "abcdefg" | ||
| let s1 = s[s.index(s.startIndex, offsetBy: 2) ..< | ||
| s.index(s.startIndex, offsetBy: 4)] | ||
| let s2 = s1[s1.startIndex..<s1.endIndex] | ||
| let s3 = s2[s1.startIndex..<s1.endIndex] | ||
|
|
||
| expectEqual(s1, "cd") | ||
| expectEqual(s2, "cd") | ||
| expectEqual(s3, "cd") | ||
| } | ||
|
|
||
| SubstringTests.test("CharacterView") | ||
| .xfail(.always("CharacterView slices don't share indices")) | ||
| .code { | ||
| let s = "abcdefg" | ||
| var t = s.characters.dropFirst(2) | ||
| var u = t.dropFirst(2) | ||
|
|
||
| checkMatch(s.characters, t, t.startIndex) | ||
| checkMatch(s.characters, t, t.index(after: t.startIndex)) | ||
| checkMatch(s.characters, t, t.index(before: t.endIndex)) | ||
|
|
||
| checkMatch(s.characters, t, u.startIndex) | ||
| checkMatch(t, u, u.startIndex) | ||
| checkMatch(t, u, u.index(after: u.startIndex)) | ||
| checkMatch(t, u, u.index(before: u.endIndex)) | ||
|
|
||
| t.replaceSubrange(t.startIndex...t.startIndex, with: ["C"]) | ||
| u.replaceSubrange(u.startIndex...u.startIndex, with: ["E"]) | ||
| expectEqual(String(u), "Efg") | ||
| expectEqual(String(t), "Cdefg") | ||
| expectEqual(s, "abcdefg") | ||
| } | ||
|
|
||
| test("some text") | ||
| SubstringTests.test("UnicodeScalars") | ||
| .xfail(.always("UnicodeScalarsView slices don't share indices")) | ||
| .code { | ||
| let s = "abcdefg" | ||
| var t = s.unicodeScalars.dropFirst(2) | ||
| var u = t.dropFirst(2) | ||
|
|
||
| checkMatch(s.unicodeScalars, t, t.startIndex) | ||
| checkMatch(s.unicodeScalars, t, t.index(after: t.startIndex)) | ||
| checkMatch(s.unicodeScalars, t, t.index(before: t.endIndex)) | ||
|
|
||
| checkMatch(s.unicodeScalars, t, u.startIndex) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both groups should be fixed now. |
||
| checkMatch(t, u, u.startIndex) | ||
| checkMatch(t, u, u.index(after: u.startIndex)) | ||
| checkMatch(t, u, u.index(before: u.endIndex)) | ||
|
|
||
| t.replaceSubrange(t.startIndex...t.startIndex, with: ["C"]) | ||
| u.replaceSubrange(u.startIndex...u.startIndex, with: ["E"]) | ||
| expectEqual(String(u), "Efg") | ||
| expectEqual(String(t), "Cdefg") | ||
| expectEqual(s, "abcdefg") | ||
| } | ||
|
|
||
| SubstringTests.test("UTF16View") | ||
| .xfail(.always("UTF16View slices don't share indices")) | ||
| .code { | ||
| let s = "abcdefg" | ||
| let t = s.utf16.dropFirst(2) | ||
| let u = t.dropFirst(2) | ||
|
|
||
| checkMatch(s.utf16, t, t.startIndex) | ||
| checkMatch(s.utf16, t, t.index(after: t.startIndex)) | ||
| checkMatch(s.utf16, t, t.index(before: t.endIndex)) | ||
|
|
||
| checkMatch(s.utf16, t, u.startIndex) | ||
| checkMatch(t, u, u.startIndex) | ||
| checkMatch(t, u, u.index(after: u.startIndex)) | ||
| checkMatch(t, u, u.index(before: u.endIndex)) | ||
| } | ||
|
|
||
| SubstringTests.test("UTF8View") { | ||
| let s = "abcdefg" | ||
| let t = s.utf8.dropFirst(2) | ||
| let u = t.dropFirst(2) | ||
|
|
||
| checkMatch(s.utf8, t, t.startIndex) | ||
| checkMatch(s.utf8, t, t.index(after: t.startIndex)) | ||
|
|
||
| checkMatch(s.utf8, t, u.startIndex) | ||
| checkMatch(t, u, u.startIndex) | ||
| checkMatch(t, u, u.index(after: u.startIndex)) | ||
| } | ||
|
|
||
| // CHECK: some text | ||
| // CHECK: me | ||
| runAllTests() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a valid assertion.
thas been mutated before the position referenced byu.startIndexThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point—I'll change it so it only tests string equality after a mutation. The mutations are there to ensure value semantics post-fix; those all pass today.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking that indices are interchangeable after mutation is a valid thing to do, we just need to mutate the tail part of the string that occurs after the index position.