File: file_test.go

package info (click to toggle)
golang-github-dsnet-golib 0.0~git20171103.1ea1667-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 240 kB
  • sloc: makefile: 2
file content (201 lines) | stat: -rw-r--r-- 5,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2017, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package memfile

import (
	"io"
	"io/ioutil"
	"os"
	"strings"
	"testing"
)

func TestFile(t *testing.T) {
	type (
		testRead struct {
			cnt      int
			wantData string
			wantErr  error
		}
		testReadAt struct {
			cnt      int
			pos      int64
			wantData string
			wantErr  error
		}
		testWrite struct {
			data    string
			wantCnt int
			wantErr error
		}
		testWriteAt struct {
			data    string
			pos     int64
			wantCnt int
			wantErr error
		}
		testSeek struct {
			offset  int64
			whence  int
			wantPos int64
			wantErr error
		}
		testTruncate struct {
			size    int64
			wantErr error
		}
		testBytes struct {
			wantData string
		}
	)

	sz := func(n int) string { return strings.Repeat("\x00", n) }
	tests := []interface{}{ // []T where T is one of the testX types above
		testRead{10, "", io.EOF},
		testSeek{5, io.SeekEnd, 5, nil},
		testSeek{5, io.SeekEnd, 5, nil},
		testWrite{"abcdefghijklmnopqrstuvwxyz", 26, nil},
		testTruncate{25, nil},
		testBytes{sz(5) + "abcdefghijklmnopqrst"},
		testTruncate{-1, errInvalid},
		testTruncate{10, nil},
		testWriteAt{"ABCDE", 15, 5, nil},
		testBytes{sz(5) + "abcde" + sz(5) + "ABCDE"},
		testReadAt{10, 15, "ABCDE", io.EOF},
		testReadAt{10, -15, "", errInvalid},
		testWriteAt{"ABCDE", -15, 0, errInvalid},
		testReadAt{8, 3, sz(2) + "abcde" + sz(1), nil},
		testSeek{0, io.SeekCurrent, 31, nil},
		testSeek{-32, io.SeekCurrent, 0, errInvalid},
		testSeek{-11, io.SeekCurrent, 20, nil},
		testWrite{"#", 1, nil},
		testBytes{sz(5) + "abcde" + sz(5) + "ABCDE#"},
		testTruncate{0, nil},
		testWrite{"#", 1, nil},
		testBytes{sz(21) + "#"},
		testSeek{5, io.SeekStart, 5, nil},
		testWrite{"12345", 5, nil},
		testSeek{5, io.SeekStart, 5, nil},
		testRead{5, "12345", nil},
		testSeek{-23, io.SeekEnd, 0, errInvalid},
		testSeek{0, io.SeekEnd, 22, nil},
		testSeek{-22, io.SeekEnd, 0, nil},
		testRead{10, sz(5) + "12345", nil},
		testSeek{0, io.SeekCurrent, 10, nil},
		testTruncate{0, nil},
		testBytes{""},
	}

	// Create a new File that emulates a file's operations.
	fb := new(File)

	// Open a temporary file to match behavior with.
	ft, err := ioutil.TempFile("", "")
	if err != nil {
		t.Fatal(err)
	}
	defer ft.Close()
	defer os.Remove(ft.Name())

	for i, tt := range tests {
		switch tt := tt.(type) {
		case testRead:
			b := make([]byte, tt.cnt)

			n, gotErr := readFull(fb, b)
			gotData := string(b[:n])
			if gotData != tt.wantData || gotErr != tt.wantErr {
				t.Fatalf("test %d, Read():\ngot  (%q, %v)\nwant (%q, %v)", i, gotData, gotErr, tt.wantData, tt.wantErr)
			}

			n, wantErr := readFull(ft, b)
			wantData := string(b[:n])
			if gotData != wantData || (gotErr == nil) != (wantErr == nil) {
				t.Fatalf("test %d, Read():\ngot  (%q, %v)\nwant (%q, %v)", i, gotData, gotErr, wantData, wantErr)
			}
		case testReadAt:
			b := make([]byte, tt.cnt)

			n, gotErr := fb.ReadAt(b, tt.pos)
			gotData := string(b[:n])
			if gotData != tt.wantData || gotErr != tt.wantErr {
				t.Fatalf("test %d, ReadAt():\ngot  (%q, %v)\nwant (%q, %v)", i, gotData, gotErr, tt.wantData, tt.wantErr)
			}

			n, wantErr := ft.ReadAt(b, tt.pos)
			wantData := string(b[:n])
			if gotData != wantData || (gotErr == nil) != (wantErr == nil) {
				t.Fatalf("test %d, ReadAt():\ngot  (%q, %v)\nwant (%q, %v)", i, gotData, gotErr, wantData, wantErr)
			}
		case testWrite:
			gotCnt, gotErr := fb.Write([]byte(tt.data))
			if gotCnt != tt.wantCnt || gotErr != tt.wantErr {
				t.Fatalf("test %d, Write():\ngot  (%d, %v)\nwant (%d, %v)", i, gotCnt, gotErr, tt.wantCnt, tt.wantErr)
			}

			wantCnt, wantErr := ft.Write([]byte(tt.data))
			if gotCnt != wantCnt || (gotErr == nil) != (wantErr == nil) {
				t.Fatalf("test %d, Write():\ngot  (%d, %v)\nwant (%d, %v)", i, gotCnt, gotErr, wantCnt, wantErr)
			}
		case testWriteAt:
			gotCnt, gotErr := fb.WriteAt([]byte(tt.data), tt.pos)
			if gotCnt != tt.wantCnt || gotErr != tt.wantErr {
				t.Fatalf("test %d, WriteAt():\ngot  (%d, %v)\nwant (%d, %v)", i, gotCnt, gotErr, tt.wantCnt, tt.wantErr)
			}

			wantCnt, wantErr := ft.WriteAt([]byte(tt.data), tt.pos)
			if gotCnt != wantCnt || (gotErr == nil) != (wantErr == nil) {
				t.Fatalf("test %d, WriteAt():\ngot  (%d, %v)\nwant (%d, %v)", i, gotCnt, gotErr, wantCnt, wantErr)
			}
		case testSeek:
			gotPos, gotErr := fb.Seek(tt.offset, tt.whence)
			if gotPos != tt.wantPos || gotErr != tt.wantErr {
				t.Fatalf("test %d, Seek():\ngot  (%d, %v)\nwant (%d, %v)", i, gotPos, gotErr, tt.wantPos, tt.wantErr)
			}

			wantPos, wantErr := ft.Seek(tt.offset, tt.whence)
			if gotPos != wantPos || (gotErr == nil) != (wantErr == nil) {
				t.Fatalf("test %d, Seek():\ngot  (%d, %v)\nwant (%d, %v)", i, gotPos, gotErr, wantPos, wantErr)
			}
		case testTruncate:
			gotErr := fb.Truncate(tt.size)
			if gotErr != tt.wantErr {
				t.Fatalf("test %d, Truncate() = %v, want %v", i, gotErr, tt.wantErr)
			}

			wantErr := ft.Truncate(tt.size)
			if (gotErr == nil) != (wantErr == nil) {
				t.Fatalf("test %d, Truncate() = %v, want %v", i, gotErr, wantErr)
			}
		case testBytes:
			gotData := string(fb.Bytes())
			if gotData != tt.wantData {
				t.Fatalf("test %d, Bytes():\ngot  %q\nwant %q", i, gotData, tt.wantData)
			}

			wantData, err := ioutil.ReadFile(ft.Name())
			if err != nil {
				t.Fatalf("test %d, unexpected ReadFile error: %v", i, err)
			}
			if gotData != string(wantData) {
				t.Fatalf("test %d, Bytes():\ngot  %q\nwant %q", i, gotData, wantData)
			}
		default:
			t.Fatalf("test %d, unknown test operation: %T", i, tt)
		}
	}
}

func readFull(r io.Reader, b []byte) (n int, err error) {
	b0 := b
	for len(b) > 0 && err == nil {
		n, err = r.Read(b)
		b = b[n:]
	}
	if len(b) == 0 && err == io.EOF {
		err = nil
	}
	return len(b0) - len(b), err
}