|
| 1 | +! Some tests from https://github.com/urbanjost/M_match |
| 2 | +module regex_test_m_regex |
| 3 | + use regex_module |
| 4 | + public |
| 5 | + |
| 6 | + ! pattern, string, index>0 |
| 7 | + character(len=*,kind=RCK), parameter :: testMdata(3,91) = reshape([ character(len=30) :: & |
| 8 | + "Foo", "FooBar", "YES", & |
| 9 | + "Poo", "FooBar", "NO " , & |
| 10 | + "Bar", "FooBar", "YES", & |
| 11 | + "Par", "FooBar", "NO " , & |
| 12 | + "Foo", "Foo", "YES", & |
| 13 | + "Fo", "Foo", "YES", & |
| 14 | + "Foo", "Fo", "NO " , & |
| 15 | + "ooB", "FooBar", "YES", & |
| 16 | + "ooP", "FooBar", "NO " , & |
| 17 | + ".", "FooBar", "YES", & |
| 18 | + "P.", "FooBar", "NO " , & |
| 19 | + "^Foo", "FooBar", "YES", & |
| 20 | + "^Bar", "FooBar", "NO " , & |
| 21 | + "Foo$", "FooBar", "NO " , & |
| 22 | + "Bar$", "FooBar", "YES", & |
| 23 | + ".*o", "FooBar", "YES", & |
| 24 | + "o*o", "FooBar", "YES", & |
| 25 | + "P*o", "FooBar", "YES", & |
| 26 | + "Fo*o", "FooBar", "YES", & |
| 27 | + "Po*o", "FooBar", "NO " , & |
| 28 | + "F[po]o", "FooBar", "YES", & |
| 29 | + "F[op]o", "FooBar", "YES", & |
| 30 | + "F[qp]o", "FooBar", "NO " , & |
| 31 | + "F[^po]o", "FooBar", "NO " , & |
| 32 | + "F[^op]o", "FooBar", "NO " , & |
| 33 | + "F[^qp]o", "FooBar", "YES", & |
| 34 | + "F[po]*o", "FooBar", "YES", & |
| 35 | + "F[56]*o", "F5oBar", "YES", & |
| 36 | + "F[46]*o", "F5oBar", "NO " , & |
| 37 | + "F[46]*5", "F5oBar", "YES", & |
| 38 | + "F[46]*5o", "F5oBar", "YES", & |
| 39 | + "F[op]*o", "FooBar", "YES", & |
| 40 | + "F[qp]*o", "FooBar", "YES", & |
| 41 | + "P[qp]*o", "FooBar", "NO " , & |
| 42 | + "F[^po]*o", "FooBar", "YES", & |
| 43 | + "F[^op]*o", "FooBar", "YES", & |
| 44 | + "F[^qp]*o", "FooBar", "YES", & |
| 45 | + "P[^qp]*o", "FooBar", "NO " , & |
| 46 | + "[0-9][0-9]*$", "0123456789", "YES" , & |
| 47 | + "[0-9][0-9]*$", "A0123456789", "YES" , & |
| 48 | + "^[0-9][0-9]*$", "A0123456789", "NO ", & |
| 49 | + "^[0-9][0-9]*$", "", "NO ", & |
| 50 | + "^[0-9]$", "", "NO ", & |
| 51 | + "^[0-9]*$", "", "YES" , & |
| 52 | + "^$", "", "YES", & |
| 53 | + "^$", " ", "NO ", & |
| 54 | + "^[A-Z ][A-Z ]*$", "", "NO ", & |
| 55 | + "^[ ]*[A-Z][A-Z ]*$", " THIS IS ALL UPPERCASE", "YES", & |
| 56 | + "^[ ]*[a-z][a-z ]*$", " this is all lowercase", "YES", & |
| 57 | + "^[ ]*[A-Z][A-Z ]*$", " THIS IS not ALL UPPERCASE", "NO " , & |
| 58 | + "^[ ]*[a-z][a-z ]*$", " this is NOT all lowercase", "NO " , & |
| 59 | + "X[-+]Y", "X-Y", "YES", & |
| 60 | + "X[-+]Y", "X+Y", "YES", & |
| 61 | + "X[+-]Y", "X-Y", "YES", & |
| 62 | + "X[+-]Y", "X+Y", "YES", & |
| 63 | + "X[-+]Y", "Y-X", "NO ", & |
| 64 | + "X[-+]Y", "Y+X", "NO ", & |
| 65 | + "X[+-]Y", "Y-X", "NO ", & |
| 66 | + "X[+-]Y", "Y+X", "NO ", & |
| 67 | + "X"//TAB//"Y", "X"//TAB//"Y", "YES", & |
| 68 | + "X["//TAB//"ab]Y", "X"//TAB//"Y", "YES", & |
| 69 | + "X["//TAB//"ab]Y", "XtY", "NO ", & |
| 70 | + "X["//TAB//"ab]Y", "XaY", "YES", & |
| 71 | + "[0-9][0-9]*\.[0-9]*", "1.9", "YES", & |
| 72 | + "[0-9][0-9]*\.[0-9]*", "1.99", "YES", & |
| 73 | + "[0-9][0-9]*\.[0-9]*", "1.999", "YES", & |
| 74 | + "[0-9][0-9]*\.[0-9]*", "1.9999", "YES", & |
| 75 | + "[0-9][0-9]*\.[0-9]*", "1.99999", "YES", & |
| 76 | + "[0-9][0-9]*\.[0-9]*", "11.99999", "YES", & |
| 77 | + "[0-9][0-9]*\.[0-9]*", "111.99999", "YES", & |
| 78 | + "[0-9][0-9]*\.[0-9]*", "1111.99999", "YES", & |
| 79 | + "[0-9][0-9]*\.[0-9]*", "11111.99999", "YES", & |
| 80 | + "[0-9][0-9]*\.[0-9]*", "123456.99999", "YES", & |
| 81 | + "^[0-9][0-9]*\.[0-9]*", "1.9", "YES", & |
| 82 | + "^[0-9][0-9]*\.[0-9]*", "1.99", "YES", & |
| 83 | + "^[0-9][0-9]*\.[0-9]*", "1.999", "YES", & |
| 84 | + "^[0-9][0-9]*\.[0-9]*", "1.9999", "YES", & |
| 85 | + "^[0-9][0-9]*\.[0-9]*", "1.99999", "YES", & |
| 86 | + "^[0-9][0-9]*\.[0-9]*", "11.99999", "YES", & |
| 87 | + "^[0-9][0-9]*\.[0-9]*", "111.99999", "YES", & |
| 88 | + "^[0-9][0-9]*\.[0-9]*", "1111.99999", "YES", & |
| 89 | + "^[0-9][0-9]*\.[0-9]*", "11111.99999", "YES", & |
| 90 | + "^[0-9][0-9]*\.[0-9]*", "111111.99999", "YES", & |
| 91 | + "a[0-9][0-9]*\.[0-9]*", "a1.9", "YES", & |
| 92 | + "a[0-9][0-9]*\.", "a1.9", "YES", & |
| 93 | + "a[0-9][0-9]*", "a1.9", "YES", & |
| 94 | + "a", "a1.9", "YES", & |
| 95 | + "\\", "\", "YES", & |
| 96 | + "\.", "\", "NO " , & |
| 97 | + ".", "\", "YES", & |
| 98 | + "F[qpo", "FooBar", "NO "],[3,91]) |
| 99 | + |
| 100 | + ! These cases have C-specific characters and need be defined |
| 101 | + |
| 102 | + contains |
| 103 | + |
| 104 | + subroutine get_m_test(itest,valid,pattern,string) |
| 105 | + integer, intent(in) :: itest |
| 106 | + logical, intent(out) :: valid |
| 107 | + character(*), intent(out) :: pattern,string |
| 108 | + |
| 109 | + if (.not.(itest>0 .and. itest<=size(testMdata,2))) return |
| 110 | + |
| 111 | + valid = trim(testMdata(3,itest))=='YES' |
| 112 | + pattern = testMdata(1,itest) |
| 113 | + string = testMdata(2,itest) |
| 114 | + |
| 115 | + end subroutine get_m_test |
| 116 | + |
| 117 | + logical function run_m_test(valid,pattern,string) result(success) |
| 118 | + logical, intent(in) :: valid |
| 119 | + character(*), intent(in) :: pattern |
| 120 | + character(*), intent(in) :: string |
| 121 | + |
| 122 | + integer :: idx,length |
| 123 | + type(regex_op) :: re |
| 124 | + |
| 125 | + print "('regex test: pattern=',a,' string=',a,'....')",trim(pattern),trim(string) |
| 126 | + |
| 127 | + idx = regex(string, pattern, length) |
| 128 | + |
| 129 | + ! This test does not check the length of the match |
| 130 | + if (.not.valid) then |
| 131 | + success = idx<=0 |
| 132 | + else |
| 133 | + success = idx>0 .or. (idx==0 .and. len(string)==0) |
| 134 | + end if |
| 135 | + |
| 136 | + if (.not.success) then |
| 137 | + write(*,*) 'FAILED: regex result: idx=',idx,' length=',length,' expected valid = ',valid |
| 138 | + re = parse_pattern(pattern) |
| 139 | + print *, ' ...pattern breakdown: ' |
| 140 | + call re%write() |
| 141 | + endif |
| 142 | + |
| 143 | + end function run_m_test |
| 144 | + |
| 145 | + |
| 146 | +end module regex_test_m_regex |
0 commit comments