MCPcopy
hub / github.com/SurgeDM/Surge / CreateTestFile

Function CreateTestFile

internal/testutil/test_files.go:27–61  ·  view source on GitHub ↗

CreateTestFile creates a test file with the specified size filled with either zeros or random data.

(dir, name string, size int64, random bool)

Source from the content-addressed store, hash-verified

25// CreateTestFile creates a test file with the specified size filled
26// with either zeros or random data.
27func CreateTestFile(dir, name string, size int64, random bool) (string, error) {
28 path := filepath.Join(dir, name)
29
30 f, err := os.Create(path)
31 if err != nil {
32 return "", err
33 }
34 defer func() { _ = f.Close() }()
35
36 if random {
37 // Write in chunks for large files
38 chunkSize := int64(64 * types.KB) // 64KB
39 chunk := make([]byte, chunkSize)
40 remaining := size
41
42 for remaining > 0 {
43 if remaining < chunkSize {
44 chunk = make([]byte, remaining)
45 }
46 _, _ = rand.Read(chunk)
47 n, err := f.Write(chunk)
48 if err != nil {
49 return "", err
50 }
51 remaining -= int64(n)
52 }
53 } else {
54 // Pre-allocate with zeros (sparse file)
55 if err := f.Truncate(size); err != nil {
56 return "", err
57 }
58 }
59
60 return path, nil
61}
62
63// CreateSurgeFile creates a .surge partial download file for resume testing.
64func CreateSurgeFile(dir, name string, totalSize, downloadedSize int64) (string, error) {

Callers 8

TestCopyFileFunction · 0.92
TestCopyFile_EmptyFileFunction · 0.92
TestCopyFile_LargeFileFunction · 0.92
TestCreateTestFileFunction · 0.85
TestVerifyFileSizeFunction · 0.85

Calls 2

CloseMethod · 0.45
ReadMethod · 0.45

Tested by 8

TestCopyFileFunction · 0.74
TestCopyFile_EmptyFileFunction · 0.74
TestCopyFile_LargeFileFunction · 0.74
TestCreateTestFileFunction · 0.68
TestVerifyFileSizeFunction · 0.68