kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/platform/delimited/delimited_test.go (about) 1 /* 2 * Copyright 2014 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package delimited 18 19 import ( 20 "bytes" 21 "errors" 22 "io" 23 "reflect" 24 "strings" 25 "testing" 26 ) 27 28 const testData = "\x00\x01A\x02BC\x03DEF" 29 30 func TestGoodReader(t *testing.T) { 31 r := strings.NewReader(testData) 32 rd := NewReader(r) 33 34 for _, want := range []string{"", "A", "BC", "DEF"} { 35 got, err := rd.Next() 36 if err != nil { 37 t.Errorf("Unexpected read error: %v", err) 38 } else if s := string(got); s != want { 39 t.Errorf("Next record: got %q, want %q", s, want) 40 } 41 } 42 43 // The stream should have been fully consumed. 44 if got, err := rd.Next(); err != io.EOF { 45 t.Errorf("Next record: got %q [%v], want EOF", string(got), err) 46 } 47 } 48 49 func TestCorruptReader(t *testing.T) { 50 const corrupt = "\x05ABCD" // n = 5, only 4 bytes of data 51 52 r := strings.NewReader(corrupt) 53 rd := NewReader(r) 54 55 got, err := rd.Next() 56 if err != io.ErrUnexpectedEOF { 57 t.Fatalf("Next record: got %q [%v], want %v", string(got), err, io.ErrUnexpectedEOF) 58 } 59 t.Logf("Next record gave expected error: %v", err) 60 } 61 62 func TestGoodWriter(t *testing.T) { 63 var w bytes.Buffer 64 wr := NewWriter(&w) 65 66 for _, record := range []string{"", "A", "BC", "DEF"} { 67 if err := wr.Put([]byte(record)); err != nil { 68 t.Errorf("Put %q: unexpected error: %v", record, err) 69 } 70 } 71 if got := w.String(); got != testData { 72 t.Errorf("Writer result: got %q, want %q", got, testData) 73 } 74 } 75 76 type errWriter struct { 77 nc int 78 err error 79 } 80 81 func (w *errWriter) Write(data []byte) (int, error) { 82 if w.err != nil && w.nc == 0 { 83 return 0, w.err 84 } 85 w.nc-- 86 return len(data), nil 87 } 88 89 func TestCorruptWriter(t *testing.T) { 90 bad := errors.New("FAIL") 91 w := &errWriter{nc: 1, err: bad} 92 wr := NewWriter(w) 93 94 err := wr.Put([]byte("whatever")) 95 if err == nil { 96 t.Fatalf("Put: got error nil, want error %v", bad) 97 } 98 t.Logf("Put record gave expected error: %v", err) 99 } 100 101 func TestRoundTrip(t *testing.T) { 102 const input = "Some of what a fool thinks often remains." 103 104 // Write all the words in the input as records to a delimited writer. 105 words := strings.Fields(input) 106 var buf bytes.Buffer 107 108 wr := NewWriter(&buf) 109 for _, word := range words { 110 if err := wr.Put([]byte(word)); err != nil { 111 t.Errorf("Put %q: unexpected error: %v", word, err) 112 } 113 } 114 t.Logf("After writing, buf=%q len=%d", buf.Bytes(), buf.Len()) 115 116 // Read all the records back from the buffer with a delimited reader. 117 var got []string 118 rd := NewReader(&buf) 119 for { 120 rec, err := rd.Next() 121 if err != nil { 122 if err != io.EOF { 123 t.Errorf("Next: unexpected error: %v", err) 124 } 125 break 126 } 127 got = append(got, string(rec)) 128 } 129 130 // Verify that we got the original words back. 131 if !reflect.DeepEqual(got, words) { 132 t.Errorf("Round trip of %q: got %+q, want %+q", input, got, words) 133 } 134 }