-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
134 lines (113 loc) · 2.7 KB
/
main_test.go
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
package main
import (
"fmt"
"math"
"math/rand"
"os"
"testing"
"time"
"unsafe"
"github.com/stretchr/testify/assert"
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var src = rand.NewSource(time.Now().UnixNano())
func RandStringBytesMaskImprSrcUnsafe(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return *(*string)(unsafe.Pointer(&b))
}
func ByteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}
func VerifyFile(path string, data string) bool {
d1, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return data == string(d1)
}
func TestRewrite(t *testing.T) {
// Get temporary directory
dir := t.TempDir()
// Prepare sizes
sizes := []int{
1,
2,
3,
512,
1024,
2048,
4096,
8192,
BLOCKSIZE - 3,
BLOCKSIZE - 2,
BLOCKSIZE - 1,
BLOCKSIZE,
BLOCKSIZE + 1,
BLOCKSIZE + 2,
BLOCKSIZE + 3,
int(math.Pow(2, 24)),
}
// Loop through sizes
for i, size := range sizes {
// Prepare path
path := fmt.Sprintf("%s/%d", dir, i)
fmt.Printf("%s", path)
// Generate random sequence of bytes 16 megabytes
randomString := RandStringBytesMaskImprSrcUnsafe(size)
randomBytes := []byte(randomString)
// Log attempt
t.Logf("starting test with %d - %s", size, ByteCountIEC(int64(size)))
// Create file
f, err := os.Create(path)
if err != nil {
panic(err)
}
defer f.Close()
// Write bytes in file
_, err = f.Write(randomBytes)
if err != nil {
panic(err)
}
// Verify file
writtenBytes, err := os.ReadFile(path)
if err != nil {
panic(err)
}
// Ensure equal
assert.Equal(t, randomBytes, writtenBytes, "[step 1] written bytes != random bytes")
// Rewrite file
err = Rewrite(path, nil, err)
assert.NoError(t, err)
// Ensure equal
assert.Equal(t, randomBytes, writtenBytes, "[step 2] rewritten bytes != written bytes")
// Log success
t.Logf("successfully tested with %d - %s", size, ByteCountIEC(int64(size)))
}
}