Sort the letters of string in alphabetical order: Difference between revisions
Content added Content deleted
Line 419: | Line 419: | ||
no_spaces: ∀ c:Result ¦ c /= ' ' -- All spaces removed. |
no_spaces: ∀ c:Result ¦ c /= ' ' -- All spaces removed. |
||
has_all: ∀ c:Result ¦ s.has (c) -- All non-space chars present. |
has_all: ∀ c:Result ¦ s.has (c) -- All non-space chars present. |
||
valid_count: Result.count = |
|||
(s.count - s.occurrences (' ')) -- Every character (even repeating). |
|||
end |
end |
||
end |
end |
||
</lang> |
</lang> |
||
Notice the use of Design-by-Contract in the "ensure" at the end of `sort_string'. At testing runtime, we want the routine itself to ensure that the resulting string has no space character and that every character that we passed in the `s' argument is represented in the result string. We even go so far as to ensure that repeating characters are all represented. We could go further, but we felt these contracts |
|||
were sufficient to get the point of Design-by-Contract across to you as the reader. |
|||
And the Test Code to operate it. |
And the Test Code to operate it. |