github.com/haraldrudell/parl@v0.4.176/pio/line-reader_test.go (about) 1 /* 2 © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package pio 7 8 import ( 9 "bytes" 10 "testing" 11 12 "github.com/haraldrudell/parl/perrors" 13 "golang.org/x/exp/slices" 14 ) 15 16 func TestLineReader(t *testing.T) { 17 line1 := "line1\n" 18 line2 := "line2\n" 19 20 var readWriteCloserSlice *ReadWriteCloserSlice 21 var byts = []byte(line1 + line2) 22 var n int 23 var err error 24 var lineReader *LineReader 25 var isEOF bool 26 27 readWriteCloserSlice = NewReadWriteCloserSlice() 28 if n, err = readWriteCloserSlice.Write(byts); err != nil { 29 t.Errorf("readWriteCloserSlice.Write err: %s", perrors.Short(err)) 30 t.FailNow() 31 } else if n != len(byts) { 32 t.Errorf("n %d exp %d", n, len(byts)) 33 t.FailNow() 34 } 35 if err = readWriteCloserSlice.Close(); err != nil { 36 t.Errorf("readWriteCloserSlice.Close err: %s", perrors.Short(err)) 37 t.FailNow() 38 } 39 40 lineReader = NewLineReader(readWriteCloserSlice) 41 42 if byts, isEOF, err = lineReader.ReadLine(byts); err != nil { 43 t.Errorf("ReadLine1 err: %s", perrors.Short(err)) 44 } 45 if isEOF { 46 t.Error("isEOF1 true") 47 } 48 if string(byts) != line1 { 49 t.Errorf("byts1: %q exp %q", string(byts), line1) 50 } 51 52 if byts, isEOF, err = lineReader.ReadLine(byts); err != nil { 53 t.Errorf("ReadLine2 err: %s", perrors.Short(err)) 54 } 55 if isEOF { 56 t.Error("isEOF2 true") 57 } 58 if string(byts) != line2 { 59 t.Errorf("byts2: %q exp %q", string(byts), line2) 60 } 61 62 if byts, isEOF, err = lineReader.ReadLine(byts); err != nil { 63 t.Errorf("ReadLine3 err: %s", perrors.Short(err)) 64 } 65 if !isEOF { 66 t.Error("isEOF3 false") 67 } 68 if string(byts) != "" { 69 t.Errorf("byts3: %q exp %q", string(byts), "") 70 } 71 72 } 73 74 func TestLineReader4k(t *testing.T) { 75 line1 := bytes.Repeat([]byte("a"), 4096) 76 77 var readWriteCloserSlice *ReadWriteCloserSlice 78 var n int 79 var err error 80 var lineReader *LineReader 81 var byts []byte 82 var isEOF bool 83 84 readWriteCloserSlice = NewReadWriteCloserSlice() 85 if n, err = readWriteCloserSlice.Write(line1); err != nil { 86 t.Errorf("readWriteCloserSlice.Write err: %s", perrors.Short(err)) 87 t.FailNow() 88 } else if n != len(line1) { 89 t.Errorf("n %d exp %d", n, len(line1)) 90 t.FailNow() 91 } 92 if err = readWriteCloserSlice.Close(); err != nil { 93 t.Errorf("readWriteCloserSlice.Close err: %s", perrors.Short(err)) 94 } 95 96 lineReader = NewLineReader(readWriteCloserSlice) 97 98 if byts, isEOF, err = lineReader.ReadLine(byts); err != nil { 99 t.Errorf("ReadLine1 err: %s", perrors.Short(err)) 100 } 101 if !isEOF { 102 t.Error("isEOF1 false") 103 } 104 if slices.Compare(byts, line1) != 0 { 105 t.Errorf("byts1: %d exp %d", len(byts), len(line1)) 106 } 107 }