Regular Expressions, Part 3

September 22, 2009

Here is our attempt. We use the assert macro from the Standard Prelude:

(define (test-rx)
  (define (test pat str result)
    (assert (rx-match? (make-rx pat) str) result))
  (test "" "" #t) (test "" "a" #t) (test "a" "" #f)
  (test "a" "a" #t) (test "a" "b" #f) (test "a" "ab" #t)
  (test "a" "ba" #t) (test "a" "aa" #t) (test "ab" "a" #f)
  (test "ab" "ab" #t) (test "ab" "ba" #f) (test "ab" "aab" #t)
  (test "ab" "bab" #t) (test "ab" "abab" #t) (test "ab" "ac" #f)
  (test "ab" "acb" #f) (test "\n" "a\n" #t) (test "\n" "abc" #f)
  (test "\t" "a\t" #t) (test "\t" "abc" #f) (test "\b" "abc" #t)
  (test "\b" "ac" #f) (test "a\b" "ab" #t) (test "a\b" "ba" #f)
  (test "^ab" "abc" #t) (test "^ab" "aab" #f) (test "ab$" "ab" #t)
  (test "ab$" "aab" #t) (test "ab$" "aba" #f) (test "^ab$" "ab" #t)
  (test "^ab$" "abc" #f) (test "." "" #f) (test "." "a" #t)
  (test "a." "a" #f) (test "a." "ab" #t) (test "a." "bb" #f)
  (test "[abc]" "a" #t) (test "[abc]" "b" #t) (test "[abc]" "c" #t)
  (test "[abc]" "d" #f) (test "[a-c]" "a" #t) (test "[a-c]" "b" #t)
  (test "[a-c]" "c" #t) (test "[a-c]" "d" #f) (test "[A-C]" "A" #t)
  (test "[A-C]" "B" #t) (test "[A-C]" "C" #t) (test "[A-C]" "D" #f)
  (test "[1-3]" "1" #t) (test "[1-3]" "2" #t) (test "[1-3]" "3" #t)
  (test "[1-3]" "4" #f) (test "[c-a]" "a" #t) (test "[c-a]" "b" #f)
  (test "[-]" "-" #t) (test "[-]" "a" #f) (test "[\n]" "\n" #t)
  (test "[\t]" "\t" #t) (test "[\n]" "a" #f) (test "[\t]" "a" #f)
  (test "[a-cd-f]" "e" #t) (test "[a-cd-f]" "g" #f)
  (test "[]]" "a" #f) (test "[]]" "[]" #t)
  (test "[^abc]" "a" #f) (test "[^abc]" "b" #f) (test "[^abc]" "c" #f)
  (test "[^abc]" "d" #t) (test "[^a-c]" "a" #f) (test "[^a-c]" "b" #f)
  (test "[^a-c]" "c" #f) (test "[^a-c]" "d" #t) (test "[^A-C]" "A" #f)
  (test "[^A-C]" "B" #f) (test "[^A-C]" "C" #f) (test "[^A-C]" "D" #t)
  (test "[^1-3]" "1" #f) (test "[^1-3]" "2" #f) (test "[^1-3]" "3" #f)
  (test "[^1-3]" "4" #t) (test "[^c-a]" "a" #f) (test "[^c-a]" "b" #t)
  (test "[^-]" "-" #f) (test "[^-]" "a" #t) (test "[^\n]" "\n" #f)
  (test "[^\t]" "\t" #f) (test "[^\n]" "a" #t) (test "[^\t]" "a" #t)
  (test "[^a-cd-f]" "e" #f) (test "[^a-cd-f]" "g" #t)
  (test "[^]]" "a" #t) (test "[^]]" "]" #f) (test "a*" "aaa" #t)
  (test "a*" "bbb" #t) (test "bb*" "abc" #t) (test "aa*" "bc" #f)
  (test "ab*c" "abbc" #t) (test "ab*c" "ac" #t) (test "ab*c" "abc" #t)
  (test "a.*c" "abbc" #t) (test "a.*c" "ac" #t) (test "a.*c" "abc" #t)
  (test "a[b-d]*e" "abcde" #t) (test "a[b-d]*e" "axe" #f)
  (test "a*a*a" "a" #t) (test "a*a*a" "aaa" #t) (test "a*a*a" "b" #f)
)

You can run the tests at http://programmingpraxis.codepad.org/sSob4fZM.

About these ads

Pages: 1 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 609 other followers

%d bloggers like this: