diff options
| author | Richard Kistruck | 2007-08-13 15:55:32 +0100 |
|---|---|---|
| committer | Richard Kistruck | 2007-08-13 15:55:32 +0100 |
| commit | c6663dcbff1c7a33cf26d9729188f04b5941585b (patch) | |
| tree | c9edef641b630b71cc01fb15b6c811ba1f7b2f52 /mps/code | |
| parent | f27629ba7cbb630cad394635136d00b3e255d29a (diff) | |
| download | emacs-c6663dcbff1c7a33cf26d9729188f04b5941585b.tar.gz emacs-c6663dcbff1c7a33cf26d9729188f04b5941585b.zip | |
Mps br/diagtag: matchtag now uses patternoccurs, not stringequal, so
-DIAGTEST/*/*
will filter out all diags whose tags contain "DIAGTEST".
Unit test for PatternOccurs. (PatternOccurs used to be called
StringMatch).
Copied from Perforce
Change: 163111
ServerID: perforce.ravenbrook.com
Diffstat (limited to 'mps/code')
| -rw-r--r-- | mps/code/diag.c | 97 |
1 files changed, 82 insertions, 15 deletions
diff --git a/mps/code/diag.c b/mps/code/diag.c index 35270ae8956..ae028712366 100644 --- a/mps/code/diag.c +++ b/mps/code/diag.c | |||
| @@ -4,8 +4,6 @@ | |||
| 4 | * Copyright (c) 2007 Ravenbrook Limited. See end of file for license. | 4 | * Copyright (c) 2007 Ravenbrook Limited. See end of file for license. |
| 5 | * | 5 | * |
| 6 | * To Do: [RHSK 2007-08-11] | 6 | * To Do: [RHSK 2007-08-11] |
| 7 | * @@ unit test for StringMatch, and call it PatternOccurs | ||
| 8 | * @@ MatchTag should use StringMatch, not StringEqual | ||
| 9 | * @@ handle diag->buf overflow (currently asserts) | 7 | * @@ handle diag->buf overflow (currently asserts) |
| 10 | * @@ sigs and AVERTs for Diag and Rule | 8 | * @@ sigs and AVERTs for Diag and Rule |
| 11 | */ | 9 | */ |
| @@ -103,6 +101,7 @@ struct RuleStruct RulesGlobalX[] = { | |||
| 103 | 101 | ||
| 104 | struct RuleStruct RulesGlobal[] = { | 102 | struct RuleStruct RulesGlobal[] = { |
| 105 | { "+", "*", "*", "*" }, | 103 | { "+", "*", "*", "*" }, |
| 104 | { "-", "DIAGTEST-", "*", "*" }, | ||
| 106 | { "+", "ChainCondemnAuto", "gens [0..0]", "*" }, | 105 | { "+", "ChainCondemnAuto", "gens [0..0]", "*" }, |
| 107 | { "+", "TraceStart", "*", "*" }, | 106 | { "+", "TraceStart", "*", "*" }, |
| 108 | { "+", "TraceStart", "because code 1:", "*" }, | 107 | { "+", "TraceStart", "because code 1:", "*" }, |
| @@ -147,8 +146,13 @@ static Bool StringEqual(const char *s1, const char *s2) | |||
| 147 | return TRUE; | 146 | return TRUE; |
| 148 | } | 147 | } |
| 149 | 148 | ||
| 150 | static Bool StringMatch(const char *patt, Count pattLen, | 149 | /* PatternOccurs -- does patt occur in buf[i..j)? |
| 151 | const char *buf, Index i, Index j) | 150 | * |
| 151 | * Returns true iff patt[0..pattLen) literally occurs in buf[i..j). | ||
| 152 | */ | ||
| 153 | |||
| 154 | static Bool PatternOccurs(const char *patt, Count pattLen, | ||
| 155 | const char *buf, Index i, Index j) | ||
| 152 | { | 156 | { |
| 153 | Index im; /* start of tentative match */ | 157 | Index im; /* start of tentative match */ |
| 154 | Index ip; /* index into patt */ | 158 | Index ip; /* index into patt */ |
| @@ -174,8 +178,6 @@ static Bool StringMatch(const char *patt, Count pattLen, | |||
| 174 | 178 | ||
| 175 | static Bool MatchLine(Rule rule, Diag diag, Index i, Index j) | 179 | static Bool MatchLine(Rule rule, Diag diag, Index i, Index j) |
| 176 | { | 180 | { |
| 177 | Count pattLen; | ||
| 178 | |||
| 179 | AVER(rule); | 181 | AVER(rule); |
| 180 | AVER(diag); | 182 | AVER(diag); |
| 181 | AVER(i <= j); | 183 | AVER(i <= j); |
| @@ -183,25 +185,21 @@ static Bool MatchLine(Rule rule, Diag diag, Index i, Index j) | |||
| 183 | 185 | ||
| 184 | if(rule->line[0] == '*') | 186 | if(rule->line[0] == '*') |
| 185 | return TRUE; | 187 | return TRUE; |
| 186 | |||
| 187 | pattLen = StringLength(rule->line); | ||
| 188 | 188 | ||
| 189 | return StringMatch(rule->line, pattLen, diag->buf, i, j); | 189 | return PatternOccurs(rule->line, StringLength(rule->line), |
| 190 | diag->buf, i, j); | ||
| 190 | } | 191 | } |
| 191 | 192 | ||
| 192 | static Bool MatchPara(Rule rule, Diag diag) | 193 | static Bool MatchPara(Rule rule, Diag diag) |
| 193 | { | 194 | { |
| 194 | Count pattLen; | ||
| 195 | |||
| 196 | AVER(rule); | 195 | AVER(rule); |
| 197 | AVER(diag); | 196 | AVER(diag); |
| 198 | 197 | ||
| 199 | if(rule->para[0] == '*') | 198 | if(rule->para[0] == '*') |
| 200 | return TRUE; | 199 | return TRUE; |
| 201 | 200 | ||
| 202 | pattLen = StringLength(rule->para); | 201 | return PatternOccurs(rule->para, StringLength(rule->para), |
| 203 | 202 | diag->buf, 0, diag->n); | |
| 204 | return StringMatch(rule->para, pattLen, diag->buf, 0, diag->n); | ||
| 205 | } | 203 | } |
| 206 | 204 | ||
| 207 | static Bool MatchTag(Rule rule, const char *tag) | 205 | static Bool MatchTag(Rule rule, const char *tag) |
| @@ -213,7 +211,8 @@ static Bool MatchTag(Rule rule, const char *tag) | |||
| 213 | if(rule->tag[0] == '*') | 211 | if(rule->tag[0] == '*') |
| 214 | return TRUE; | 212 | return TRUE; |
| 215 | 213 | ||
| 216 | return StringEqual(rule->tag, tag); | 214 | return PatternOccurs(rule->tag, StringLength(rule->tag), |
| 215 | tag, 0, StringLength(tag)); | ||
| 217 | } | 216 | } |
| 218 | 217 | ||
| 219 | static void LineOutput(Diag diag, Index i, Index j) | 218 | static void LineOutput(Diag diag, Index i, Index j) |
| @@ -530,6 +529,47 @@ void DiagEnd(const char *tag) | |||
| 530 | DiagTagEnd(DiagStream(), tag); | 529 | DiagTagEnd(DiagStream(), tag); |
| 531 | } | 530 | } |
| 532 | 531 | ||
| 532 | |||
| 533 | /* Test Code -- unit tests for this source file | ||
| 534 | * | ||
| 535 | * These are for developers to run if they modify this source file. | ||
| 536 | * There's no point running them otherwise. RHSK. | ||
| 537 | */ | ||
| 538 | |||
| 539 | static void PatternOccurs_test(Bool expect, const char *patt, | ||
| 540 | const char *text) | ||
| 541 | { | ||
| 542 | Count pattLen = StringLength(patt); | ||
| 543 | Count textLen = StringLength(text); | ||
| 544 | enum {bufLen = 100}; | ||
| 545 | char buf[bufLen]; | ||
| 546 | Index start, i; | ||
| 547 | Count padLen; | ||
| 548 | Bool occurs; | ||
| 549 | |||
| 550 | /* Call PatternOccurs with this patt and text 3 times: each time */ | ||
| 551 | /* putting the text in the buffer at a different offset, to */ | ||
| 552 | /* verify that PatternOccurs is not accepting matches outside the */ | ||
| 553 | /* [i..j) portion of the buffer. */ | ||
| 554 | |||
| 555 | for(start = 0; start < 21; start += 7) { | ||
| 556 | AVER(bufLen > (start + textLen)); | ||
| 557 | /* put text into buf at start */ | ||
| 558 | for(i = 0; i < start; i++) { | ||
| 559 | buf[i] = 'X'; | ||
| 560 | } | ||
| 561 | for(i = 0; i < textLen; i++) { | ||
| 562 | (buf+start)[i] = text[i]; | ||
| 563 | } | ||
| 564 | padLen = bufLen - (start + textLen); | ||
| 565 | for(i = 0; i < padLen; i++) { | ||
| 566 | (buf+start+textLen)[i] = 'X'; | ||
| 567 | } | ||
| 568 | occurs = PatternOccurs(patt, pattLen, buf, start, start+textLen); | ||
| 569 | AVER(occurs == expect); | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 533 | static void diag_test(void) | 573 | static void diag_test(void) |
| 534 | { | 574 | { |
| 535 | DIAG_SINGLEF(( "DIAGTEST-Tag1", "text $U.\n", 42, NULL )); | 575 | DIAG_SINGLEF(( "DIAGTEST-Tag1", "text $U.\n", 42, NULL )); |
| @@ -550,6 +590,33 @@ static void diag_test(void) | |||
| 550 | DIAG_MOREF(("0 = 000: $U.\n", StringEqual("", "\0\0"), NULL)); | 590 | DIAG_MOREF(("0 = 000: $U.\n", StringEqual("", "\0\0"), NULL)); |
| 551 | DIAG_END("DIAGTEST-StringEqual"); | 591 | DIAG_END("DIAGTEST-StringEqual"); |
| 552 | 592 | ||
| 593 | DIAG_FIRSTF(( "DIAGTEST-PatternOccurs", NULL )); | ||
| 594 | PatternOccurs_test(TRUE, "Fred", "Fred"); | ||
| 595 | PatternOccurs_test(TRUE, "Fred", "XFredX"); | ||
| 596 | PatternOccurs_test(TRUE, "Fred", "FFred"); | ||
| 597 | PatternOccurs_test(TRUE, "Fred", "FrFred"); | ||
| 598 | PatternOccurs_test(TRUE, "Fred", "FreFred"); | ||
| 599 | PatternOccurs_test(TRUE, "Fred", "FreFreFFred"); | ||
| 600 | PatternOccurs_test(TRUE, "Fred", "FredFred"); | ||
| 601 | PatternOccurs_test(TRUE, "Fred", "FFredFre"); | ||
| 602 | PatternOccurs_test(TRUE, "Fred", "FrFredFr"); | ||
| 603 | PatternOccurs_test(TRUE, "Fred", "FreFredF"); | ||
| 604 | PatternOccurs_test(TRUE, "Fred", "FreFreFFredFre"); | ||
| 605 | PatternOccurs_test(TRUE, "Fred", "FredFredF"); | ||
| 606 | PatternOccurs_test(TRUE, "X", "X"); | ||
| 607 | PatternOccurs_test(TRUE, "", "X"); | ||
| 608 | PatternOccurs_test(TRUE, "", "Whatever"); | ||
| 609 | PatternOccurs_test(FALSE, "Fred", "Tom"); | ||
| 610 | PatternOccurs_test(FALSE, "X", "Tom"); | ||
| 611 | PatternOccurs_test(FALSE, "X", "x"); | ||
| 612 | PatternOccurs_test(FALSE, "X", ""); | ||
| 613 | PatternOccurs_test(FALSE, "Whatever", ""); | ||
| 614 | PatternOccurs_test(FALSE, "Fred", "Fre"); | ||
| 615 | PatternOccurs_test(FALSE, "Fred", "red"); | ||
| 616 | PatternOccurs_test(FALSE, "Fred", "Fxred"); | ||
| 617 | PatternOccurs_test(FALSE, "Fred", "Frexd"); | ||
| 618 | DIAG_END("DIAGTEST-PatternOccurs"); | ||
| 619 | |||
| 553 | #if 0 | 620 | #if 0 |
| 554 | DIAG_FIRSTF(( "TestTag2", "text $U.\n", 42, NULL )); | 621 | DIAG_FIRSTF(( "TestTag2", "text $U.\n", 42, NULL )); |
| 555 | DIAG_MOREF(( NULL )); | 622 | DIAG_MOREF(( NULL )); |