Skip to content

Commit 190ca0f

Browse files
committed
NPOT sectors.
1 parent 1a223fa commit 190ca0f

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

internal/util/alloc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build unix
1+
//go:build unix && !sqlite3_nosys
22

33
package util
44

vfs/adiantum/hbsh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (h *hbshFile) Truncate(size int64) error {
186186
}
187187

188188
func (h *hbshFile) SectorSize() int {
189-
return max(h.File.SectorSize(), blockSize)
189+
return lcm(h.File.SectorSize(), blockSize)
190190
}
191191

192192
func (h *hbshFile) DeviceCharacteristics() vfs.DeviceCharacteristic {

vfs/adiantum/math.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package adiantum
2+
3+
func abs(n int) int {
4+
if n < 0 {
5+
return -n
6+
}
7+
return n
8+
}
9+
10+
func gcd(m, n int) int {
11+
for n != 0 {
12+
m, n = n, m%n
13+
}
14+
return abs(m)
15+
}
16+
17+
func lcm(m, n int) int {
18+
if n == 0 {
19+
return 0
20+
}
21+
return abs(n) * (abs(m) / gcd(m, n))
22+
}

vfs/adiantum/math_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package adiantum
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func Test_abs(t *testing.T) {
9+
tests := []struct {
10+
arg int
11+
want int
12+
}{
13+
{0, 0},
14+
{1, 1},
15+
{-1, 1},
16+
{math.MaxInt, math.MaxInt},
17+
{math.MinInt, math.MinInt},
18+
}
19+
for _, tt := range tests {
20+
t.Run("", func(t *testing.T) {
21+
if got := abs(tt.arg); got != tt.want {
22+
t.Errorf("abs(%d) = %d, want %d", tt.arg, got, tt.want)
23+
}
24+
})
25+
}
26+
}
27+
28+
func Test_gcd(t *testing.T) {
29+
tests := []struct {
30+
arg1 int
31+
arg2 int
32+
want int
33+
}{
34+
{0, 0, 0},
35+
{0, 1, 1},
36+
{1, 0, 1},
37+
{1, 1, 1},
38+
{2, 3, 1},
39+
{42, 56, 14},
40+
{48, -18, 6},
41+
{1e9, 1e9, 1e9},
42+
{1e9, -1e9, 1e9},
43+
{-1e9, -1e9, 1e9},
44+
{math.MaxInt, math.MaxInt, math.MaxInt},
45+
{math.MinInt, math.MinInt, math.MinInt},
46+
}
47+
for _, tt := range tests {
48+
t.Run("", func(t *testing.T) {
49+
if got := gcd(tt.arg1, tt.arg2); got != tt.want {
50+
t.Errorf("gcd(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
51+
}
52+
})
53+
}
54+
}
55+
56+
func Test_lcm(t *testing.T) {
57+
tests := []struct {
58+
arg1 int
59+
arg2 int
60+
want int
61+
}{
62+
{0, 0, 0},
63+
{0, 1, 0},
64+
{1, 0, 0},
65+
{1, 1, 1},
66+
{2, 3, 6},
67+
{42, 56, 168},
68+
{48, -18, 144},
69+
{1e9, 1e9, 1e9},
70+
{1e9, -1e9, 1e9},
71+
{-1e9, -1e9, 1e9},
72+
{math.MaxInt, math.MaxInt, math.MaxInt},
73+
{math.MinInt, math.MinInt, math.MinInt},
74+
}
75+
for _, tt := range tests {
76+
t.Run("", func(t *testing.T) {
77+
if got := lcm(tt.arg1, tt.arg2); got != tt.want {
78+
t.Errorf("lcm(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
79+
}
80+
})
81+
}
82+
}

0 commit comments

Comments
 (0)