Created
April 5, 2013 21:22
-
-
Save clausecker/5322719 to your computer and use it in GitHub Desktop.
Testing file for the implementation of fmt.Fscanf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ppm | |
| import "fmt" | |
| import "io" | |
| import "testing" | |
| // io.Reader for TestFscanf. We use our own reader to be sure that pushing back | |
| // is not supported. | |
| type fscanfTestReader struct { | |
| content []byte | |
| count int | |
| } | |
| func (this *fscanfTestReader) Read(buf []byte) (n int, err error) { | |
| n = len(buf) | |
| m := len(this.content) | |
| if n > m { | |
| err = io.EOF | |
| n = m | |
| } | |
| copy(buf, this.content) | |
| this.content = this.content[n:] | |
| this.count += n | |
| return | |
| } | |
| // The code in ppm.go assumes that if the last formatting verb passed to Fscanf | |
| // is %c it is guaranteed that Fscanf does not consume more input than needed. | |
| // This assumption is needed to make parsing the header of a PPM-file with the | |
| // functions provided by fmt possible while keeping the ability to be certain | |
| // that we do not consume more of the input than we need. | |
| func TestFscanf(t *testing.T) { | |
| var d int | |
| var c byte | |
| r := fscanfTestReader{([]byte)("1234xy"), 0} | |
| items, err := fmt.Fscanf(&r, "%d%c", &d, &c) | |
| if r.count != 5 || items != 2 { | |
| t.Error("Fscanf behavior does not conform") | |
| } | |
| if err != nil { | |
| t.Error(err.Error()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment