-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix incorrect end of file checking and add some unit tests #41
Open
panikgaul
wants to merge
1
commit into
xitongsys:master
Choose a base branch
from
panikgaul:feature/unit-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package buffer | ||
|
||
import ( | ||
"io" | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
func TestNewBufferFile(t *testing.T) { | ||
ce := 512 | ||
bf := NewBufferFile() | ||
cg := cap(bf.Bytes()) | ||
|
||
if cg != ce { | ||
t.Errorf("expected capacity: %v but got: %v", ce, cg) | ||
} | ||
} | ||
|
||
func TestNewBufferFileFromBytes(t *testing.T) { | ||
l := 1024 | ||
b := make([]byte, l) | ||
bf := NewBufferFileFromBytes(b) | ||
|
||
lg := len(bf.Bytes()) | ||
|
||
if lg != l { | ||
t.Errorf("expected length: %v but got: %v", l, lg) | ||
} | ||
} | ||
|
||
func TestNewBufferFileCapacity(t *testing.T) { | ||
ce := 1024 | ||
bf := NewBufferFileCapacity(ce) | ||
cg := cap(bf.Bytes()) | ||
|
||
if cg != ce { | ||
t.Errorf("expected capacity: %v but got: %v", ce, cg) | ||
} | ||
} | ||
|
||
func TestCreate(t *testing.T) { | ||
bf := NewBufferFile() | ||
|
||
if _, err := bf.Create("foo"); err != nil { | ||
t.Errorf("unexpected error: %s", err) | ||
} | ||
} | ||
|
||
func TestOpen(t *testing.T) { | ||
bf := NewBufferFile() | ||
|
||
if _, err := bf.Open("foo"); err != nil { | ||
t.Errorf("unexpected error: %s", err) | ||
} | ||
} | ||
|
||
func TestRead(t *testing.T) { | ||
testCases := []struct { | ||
name string // name of test case | ||
capSrc int // capacity of source buffer | ||
capDst int // capacity of destination count | ||
cntExp int // expected copied bytes count | ||
errExp error // expected count | ||
}{ | ||
{ | ||
// regulary read without errors | ||
name: "case1", | ||
capSrc: 4, | ||
capDst: 5, | ||
cntExp: 4, | ||
errExp: nil, | ||
}, | ||
{ | ||
// read to buffer with not enaugh capacity | ||
name: "case2", | ||
capSrc: 4, | ||
capDst: 3, | ||
cntExp: 3, | ||
errExp: io.EOF, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
bufSrc := make([]byte, tc.capSrc) | ||
bufDst := make([]byte, tc.capDst) | ||
|
||
rand.Read(bufSrc) // fill source buffer with random | ||
bf := NewBufferFileFromBytes(bufSrc) | ||
|
||
cnt, err := bf.Read(bufDst) | ||
|
||
if tc.errExp != err { | ||
t.Errorf("expected error to be: %v but got: %v", tc.errExp, err) | ||
} | ||
|
||
if tc.cntExp != cnt { | ||
t.Errorf("expected copied bytes to be: %v but got: %v", tc.cntExp, cnt) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWrite(t *testing.T) { | ||
testCases := []struct { | ||
name string // name of test case | ||
capSrc int // capacity of source buffer | ||
capDst int // capacity of destination count | ||
errExp error // expected count | ||
}{ | ||
{ | ||
// regulary write with enaugh capacity | ||
name: "case1", | ||
capSrc: 4, | ||
capDst: 5, | ||
errExp: nil, | ||
}, | ||
{ | ||
// write to buffer with not enaugh capacity | ||
name: "case2", | ||
capSrc: 5, | ||
capDst: 3, | ||
errExp: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
bufSrc := make([]byte, tc.capSrc) | ||
|
||
rand.Read(bufSrc) // fill source buffer with random | ||
|
||
bf := NewBufferFileCapacity(tc.capDst) | ||
|
||
cnt, err := bf.Write(bufSrc) | ||
|
||
if err != tc.errExp { | ||
t.Errorf("expected error to be: %v but got: %v", tc.errExp, err) | ||
} | ||
|
||
if cnt != tc.capSrc { | ||
t.Errorf("expected copied bytes to be: %v but got: %v", tc.capSrc, cnt) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSeek(t *testing.T) { | ||
testCases := []struct { | ||
name string // name of test case | ||
offset int64 // seek to this position | ||
whence int // starting position | ||
offsetExp int64 // expected location after seeking | ||
errExp error // expected error | ||
}{ | ||
{ | ||
name: "case1", | ||
offset: 1, | ||
whence: io.SeekStart, | ||
offsetExp: 1, | ||
errExp: nil, | ||
}, | ||
{ | ||
name: "case2", | ||
offset: 1, | ||
whence: io.SeekCurrent, | ||
offsetExp: 2, | ||
errExp: nil, | ||
}, | ||
{ | ||
name: "case3", | ||
offset: 1, | ||
whence: io.SeekEnd, | ||
offsetExp: 42, | ||
errExp: nil, | ||
}, | ||
{ | ||
name: "case4", | ||
offset: -44, | ||
whence: io.SeekEnd, | ||
offsetExp: 42, | ||
errExp: errSeekToNegativeLocation, | ||
}, | ||
} | ||
|
||
// prepare buffer file with random bytes | ||
buf := make([]byte, 42) | ||
rand.Read(buf) | ||
bf := NewBufferFileFromBytes(buf) | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
offset, err := bf.Seek(tc.offset, tc.whence) | ||
|
||
if offset != tc.offsetExp { | ||
t.Errorf("expected offset to be %d but got %d", tc.offsetExp, offset) | ||
} | ||
if err != tc.errExp { | ||
t.Errorf("expected error to be %v but got %v", tc.offsetExp, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestClose(t *testing.T) { | ||
bf := NewBufferFile() | ||
|
||
if err := bf.Close(); err != nil { | ||
t.Errorf("unexpected error: %s\n", err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@panikgaul 100% unit test coverage is nice.
But, it seems that your change is wrong and the code before the change is correct.
Did you intended
if n < len(p) {
?Even so, I don't understand how io.EOF relates to the capacity of the destination buffer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello,
yes the
if n < len(p) {
was my intention.When I used this with
==
, I always got EOF, but that is not correct.If destination buffer is smaller then source buffer,
copy()
gives you the count of copied bytes -> size of destination bugger, and you should get EOF in this case, because the source was not copied completely. That was my understanding and why I switched to<
.Now it works as expected.
I hope it was plausible :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A partial copy due to insufficient capacity in the destination buffer (even if nothing is copied to a destination buffer with a capacity of 0 bytes) is not an error.
The io.EOF only indicates whether the end of the source has been reached.
It is absolutely not related at all with the destination buffer.
It is correct behavior for the Read of io.Reader to return io.EOF when the source is fully read.