github.com/kaydxh/golang@v0.0.131/go/io/io_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package io_test 23 24 import ( 25 "bytes" 26 "fmt" 27 "path/filepath" 28 "strings" 29 "sync" 30 "testing" 31 32 md5_ "github.com/kaydxh/golang/go/crypto/md5" 33 io_ "github.com/kaydxh/golang/go/io" 34 os_ "github.com/kaydxh/golang/go/os" 35 "github.com/stretchr/testify/assert" 36 ) 37 38 func TestWriteFile(t *testing.T) { 39 testCases := []struct { 40 name string 41 words []byte 42 expected string 43 }{ 44 { 45 name: "./tmp/1.txt", 46 words: []byte("hello world"), 47 expected: "", 48 }, 49 } 50 51 for _, testCase := range testCases { 52 t.Run(testCase.name, func(t *testing.T) { 53 err := io_.WriteFile(testCase.name, testCase.words, false) 54 if err != nil { 55 t.Fatalf("failed to write file: %v, got : %s", testCase.name, err) 56 57 } 58 59 }) 60 } 61 } 62 63 func TestWriteReadLine(t *testing.T) { 64 testCases := []struct { 65 name string 66 words []string 67 expected string 68 }{ 69 { 70 name: "write no word", 71 words: []string{}, 72 expected: "", 73 }, 74 { 75 name: "write one word", 76 words: []string{"test1"}, 77 expected: "test1\n", 78 }, 79 80 { 81 name: "write multi words", 82 words: []string{"test1", "test2", "test3"}, 83 expected: "test1 test2 test3\n", 84 }, 85 } 86 87 testBuffer := bytes.NewBuffer(nil) 88 for _, testCase := range testCases { 89 t.Run(testCase.name, func(t *testing.T) { 90 testBuffer.Reset() 91 io_.WriteLine(testBuffer, testCase.words...) 92 if !strings.EqualFold(testBuffer.String(), testCase.expected) { 93 t.Fatalf( 94 "write word is %v\n expected: %s, got: %s", 95 testCase.words, 96 testCase.expected, 97 testBuffer.String(), 98 ) 99 100 } 101 }) 102 } 103 104 lines := io_.ReadLines(testBuffer.Bytes()) 105 assert.Equal(t, "test1 test2 test3", lines[0]) 106 107 } 108 109 func TestWriteReadFileLine(t *testing.T) { 110 testCases := []struct { 111 name string 112 words []string 113 expected string 114 }{ 115 { 116 name: "write no word", 117 words: []string{}, 118 expected: "", 119 }, 120 { 121 name: "write one word", 122 words: []string{"test1"}, 123 expected: "test1\n", 124 }, 125 126 { 127 name: "write multi words", 128 words: []string{"test1", "test2", "test3"}, 129 expected: "test1 test2 test3\n", 130 }, 131 } 132 133 workDir, _ := os_.Getwd() 134 testFileNew := filepath.Join(workDir, "test-file-new") 135 testFileAppend := filepath.Join(workDir, "test-file-append") 136 137 for _, testCase := range testCases { 138 t.Run(testCase.name, func(t *testing.T) { 139 err := io_.WriteFileLine(testFileNew, testCase.words, false) 140 assert.Nil(t, err) 141 err = io_.WriteFileLine(testFileAppend, testCase.words, true) 142 assert.Nil(t, err) 143 /* 144 if !strings.EqualFold(testBuffer.String(), testCase.expected) { 145 t.Fatalf( 146 "write word is %v\n expected: %s, got: %s", 147 testCase.words, 148 testCase.expected, 149 testBuffer.String(), 150 ) 151 152 } 153 */ 154 }) 155 } 156 157 lines, err := io_.ReadFileLines(testFileAppend) 158 if err != nil { 159 t.Errorf("expect nil, got %v", err) 160 } 161 162 fmt.Println("read lines:", lines) 163 } 164 165 func TestWriteFileLines(t *testing.T) { 166 testCases := []struct { 167 name string 168 words []string 169 expected string 170 }{ 171 { 172 name: "write no word", 173 words: []string{}, 174 expected: "", 175 }, 176 { 177 name: "write one word", 178 words: []string{"test1"}, 179 expected: "test1\n", 180 }, 181 182 { 183 name: "write multi words", 184 words: []string{"test1", "test2", "test3"}, 185 expected: "test1 test2 test3\n", 186 }, 187 } 188 189 workDir, _ := os_.Getwd() 190 testFileNew := filepath.Join(workDir, "test-file-new") 191 testFileAppend := filepath.Join(workDir, "test-file-append") 192 193 for _, testCase := range testCases { 194 t.Run(testCase.name, func(t *testing.T) { 195 err := io_.WriteFileLines(testFileNew, testCase.words, false) 196 assert.Nil(t, err) 197 err = io_.WriteFileLines(testFileAppend, testCase.words, true) 198 assert.Nil(t, err) 199 /* 200 if !strings.EqualFold(testBuffer.String(), testCase.expected) { 201 t.Fatalf( 202 "write word is %v\n expected: %s, got: %s", 203 testCase.words, 204 testCase.expected, 205 testBuffer.String(), 206 ) 207 208 } 209 */ 210 }) 211 } 212 } 213 214 func TestWriteAtOneThread(t *testing.T) { 215 testCases := []struct { 216 name string 217 words []byte 218 expected string 219 }{ 220 { 221 name: "write one word", 222 words: []byte("test1"), 223 expected: "test1", 224 }, 225 { 226 name: "write one word", 227 words: []byte("test2"), 228 expected: "test2", 229 }, 230 231 { 232 name: "write one word", 233 words: []byte("test3"), 234 expected: "test3", 235 }, 236 } 237 238 var offsets []int64 = []int64{10, 0, 5} 239 workDir, _ := os_.Getwd() 240 testFileOffset := filepath.Join(workDir, "test-file-offset") 241 for i, testCase := range testCases { 242 t.Run(testCase.name, func(t *testing.T) { 243 var offset int64 244 if i > 0 { 245 offset += int64(i * len(testCases[i-1].words)) 246 } 247 fmt.Println("offset: ", offsets[i]) 248 //err := io_.WriteBytesAt(testFileOffset, testCases[i].words, offset) 249 err := io_.WriteBytesAt(testFileOffset, testCases[i].words, offsets[i]) 250 assert.Nil(t, err) 251 }) 252 } 253 } 254 255 func TestWriteAtMutilThreads(t *testing.T) { 256 testCases := []struct { 257 name string 258 words []byte 259 expected string 260 }{ 261 { 262 name: "write one word", 263 words: []byte("test1"), 264 expected: "test1", 265 }, 266 { 267 name: "write one word", 268 words: []byte("test2"), 269 expected: "test2", 270 }, 271 272 { 273 name: "write one word", 274 words: []byte("test3"), 275 expected: "test3", 276 }, 277 } 278 279 workDir, _ := os_.Getwd() 280 testFileOffset := filepath.Join(workDir, "test-file-offset") 281 wg := sync.WaitGroup{} 282 for i, testCase := range testCases { 283 wg.Add(1) 284 go func(name string, index int) { 285 defer wg.Done() 286 t.Run(name, func(t *testing.T) { 287 var offset int64 288 for j := 0; j < index; j++ { 289 offset += int64(len(testCases[j].words)) 290 } 291 fmt.Printf("write: %s, offset: %d", testCases[index].words, offset) 292 err := io_.WriteBytesAt(testFileOffset, testCases[index].words, offset) 293 assert.Nil(t, err) 294 }) 295 }(testCase.name, i) 296 } 297 wg.Wait() 298 } 299 300 func TestWriteReaderAtOneThread(t *testing.T) { 301 testCases := []struct { 302 name string 303 words []string 304 expected string 305 }{ 306 { 307 name: "write one word", 308 words: []string{"test1"}, 309 expected: "test1", 310 }, 311 { 312 name: "write one word", 313 words: []string{"test2"}, 314 expected: "test2", 315 }, 316 317 { 318 name: "write one word", 319 words: []string{"test3"}, 320 expected: "test3", 321 }, 322 } 323 324 workDir, _ := os_.Getwd() 325 testFile := filepath.Join(workDir, "test-file") 326 for _, testCase := range testCases { 327 t.Run(testCase.name, func(t *testing.T) { 328 err := io_.WriteFileLine(testFile, testCase.words, true) 329 assert.Nil(t, err) 330 }) 331 } 332 333 file, err := os_.OpenFile(testFile, true) 334 if err != nil { 335 return 336 } 337 defer file.Close() 338 339 /* 340 fileInfo, err := file.Stat() 341 if err != nil { 342 return 343 } 344 */ 345 346 testFileCopy := filepath.Join(workDir, "test-file-copy") 347 var offset int64 348 349 for _, testCase := range testCases { 350 351 err = io_.WriteReaderAt(testFileCopy, file, offset, int64(len(testCase.words[0])+1)) 352 offset += int64(len(testCase.words[0]) + 1) 353 assert.Nil(t, err) 354 } 355 356 sumTestFile, _ := md5_.SumFile(testFile) 357 sumTestFileCopy, _ := md5_.SumFile(testFileCopy) 358 359 assert.Equal(t, sumTestFile, sumTestFileCopy) 360 361 }