bounds checking stress tests

This commit is contained in:
Russ Ross 2011-07-03 10:51:07 -06:00
parent 123a149ec3
commit e35b4b66cc
4 changed files with 82 additions and 19 deletions

View File

@ -27,13 +27,32 @@ func runMarkdownBlock(input string, extensions int) string {
} }
func doTestsBlock(t *testing.T, tests []string, extensions int) { func doTestsBlock(t *testing.T, tests []string, extensions int) {
// catch and report panics
var candidate string
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v]\n", candidate)
}
}()
for i := 0; i+1 < len(tests); i += 2 { for i := 0; i+1 < len(tests); i += 2 {
input := tests[i] input := tests[i]
candidate = input
expected := tests[i+1] expected := tests[i+1]
actual := runMarkdownBlock(input, extensions) actual := runMarkdownBlock(candidate, extensions)
if actual != expected { if actual != expected {
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]", t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
input, expected, actual) candidate, expected, actual)
}
// now test every substring to stress test bounds checking
if !testing.Short() {
for start := 0; start < len(input); start++ {
for end := start + 1; end <= len(input); end++ {
candidate = input[start:end]
_ = runMarkdownBlock(candidate, extensions)
}
}
} }
} }
} }

View File

@ -31,13 +31,32 @@ func runMarkdownInline(input string) string {
} }
func doTestsInline(t *testing.T, tests []string) { func doTestsInline(t *testing.T, tests []string) {
// catch and report panics
var candidate string
defer func() {
if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v]\n", candidate)
}
}()
for i := 0; i+1 < len(tests); i += 2 { for i := 0; i+1 < len(tests); i += 2 {
input := tests[i] input := tests[i]
candidate = input
expected := tests[i+1] expected := tests[i+1]
actual := runMarkdownInline(input) actual := runMarkdownInline(candidate)
if actual != expected { if actual != expected {
t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]", t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
input, expected, actual) candidate, expected, actual)
}
// now test every substring to stress test bounds checking
if !testing.Short() {
for start := 0; start < len(input); start++ {
for end := start + 1; end <= len(input); end++ {
candidate = input[start:end]
_ = runMarkdownInline(candidate)
}
}
} }
} }
} }

View File

@ -270,6 +270,12 @@ func firstPass(parser *Parser, input []byte) []byte {
beg = end beg = end
} }
} }
// empty input?
if out.Len() == 0 {
out.WriteByte('\n')
}
return out.Bytes() return out.Bytes()
} }

View File

@ -25,27 +25,46 @@ func runMarkdownReference(input string) string {
} }
func doTestsReference(t *testing.T, files []string) { func doTestsReference(t *testing.T, files []string) {
for _, basename := range files { // catch and report panics
fn := filepath.Join("upskirtref", basename+".text") var candidate string
actualdata, err := ioutil.ReadFile(fn) defer func() {
if err != nil { if err := recover(); err != nil {
t.Errorf("Couldn't open '%s', error: %v\n", fn, err) t.Errorf("\npanic while processing [%#v]\n", candidate)
continue
}
fn = filepath.Join("upskirtref", basename+".html")
expecteddata, err := ioutil.ReadFile(fn)
if err != nil {
t.Errorf("Couldn't open '%s', error: %v\n", fn, err)
continue
} }
}()
actual := string(actualdata) for _, basename := range files {
actual = string(runMarkdownReference(actual)) filename := filepath.Join("upskirtref", basename+".text")
expected := string(expecteddata) inputBytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Couldn't open '%s', error: %v\n", filename, err)
continue
}
input := string(inputBytes)
filename = filepath.Join("upskirtref", basename+".html")
expectedBytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Errorf("Couldn't open '%s', error: %v\n", filename, err)
continue
}
expected := string(expectedBytes)
actual := string(runMarkdownReference(input))
if actual != expected { if actual != expected {
t.Errorf("\n [%#v]\nExpected[%#v]\nActual [%#v]", t.Errorf("\n [%#v]\nExpected[%#v]\nActual [%#v]",
basename+".text", expected, actual) basename+".text", expected, actual)
} }
// now test every substring of every input to check for
// bounds checking
if !testing.Short() {
start := 0
for end := start + 1; end <= len(input); end++ {
candidate = input[start:end]
_ = runMarkdownReference(candidate)
}
}
} }
} }