diff --git a/vfs/xts/math.go b/internal/util/math.go similarity index 58% rename from vfs/xts/math.go rename to internal/util/math.go index b6cf113c..ffbd0c2f 100644 --- a/vfs/xts/math.go +++ b/internal/util/math.go @@ -1,4 +1,4 @@ -package xts +package util func abs(n int) int { if n < 0 { @@ -7,16 +7,16 @@ func abs(n int) int { return n } -func gcd(m, n int) int { +func GCD(m, n int) int { for n != 0 { m, n = n, m%n } return abs(m) } -func lcm(m, n int) int { +func LCM(m, n int) int { if n == 0 { return 0 } - return abs(n) * (abs(m) / gcd(m, n)) + return abs(n) * (abs(m) / GCD(m, n)) } diff --git a/vfs/xts/math_test.go b/internal/util/math_test.go similarity index 91% rename from vfs/xts/math_test.go rename to internal/util/math_test.go index 5502c2c3..f403490c 100644 --- a/vfs/xts/math_test.go +++ b/internal/util/math_test.go @@ -1,4 +1,4 @@ -package xts +package util import ( "math" @@ -46,7 +46,7 @@ func Test_gcd(t *testing.T) { } for _, tt := range tests { t.Run("", func(t *testing.T) { - if got := gcd(tt.arg1, tt.arg2); got != tt.want { + if got := GCD(tt.arg1, tt.arg2); got != tt.want { t.Errorf("gcd(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want) } }) @@ -74,7 +74,7 @@ func Test_lcm(t *testing.T) { } for _, tt := range tests { t.Run("", func(t *testing.T) { - if got := lcm(tt.arg1, tt.arg2); got != tt.want { + if got := LCM(tt.arg1, tt.arg2); got != tt.want { t.Errorf("lcm(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want) } }) diff --git a/vfs/adiantum/hbsh.go b/vfs/adiantum/hbsh.go index b84de1b2..8fce7ee9 100644 --- a/vfs/adiantum/hbsh.go +++ b/vfs/adiantum/hbsh.go @@ -187,7 +187,7 @@ func (h *hbshFile) Truncate(size int64) error { } func (h *hbshFile) SectorSize() int { - return lcm(h.File.SectorSize(), blockSize) + return util.LCM(h.File.SectorSize(), blockSize) } func (h *hbshFile) DeviceCharacteristics() vfs.DeviceCharacteristic { diff --git a/vfs/adiantum/math.go b/vfs/adiantum/math.go deleted file mode 100644 index 17f7a291..00000000 --- a/vfs/adiantum/math.go +++ /dev/null @@ -1,22 +0,0 @@ -package adiantum - -func abs(n int) int { - if n < 0 { - return -n - } - return n -} - -func gcd(m, n int) int { - for n != 0 { - m, n = n, m%n - } - return abs(m) -} - -func lcm(m, n int) int { - if n == 0 { - return 0 - } - return abs(n) * (abs(m) / gcd(m, n)) -} diff --git a/vfs/adiantum/math_test.go b/vfs/adiantum/math_test.go deleted file mode 100644 index 2e2398a7..00000000 --- a/vfs/adiantum/math_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package adiantum - -import ( - "math" - "testing" -) - -func Test_abs(t *testing.T) { - tests := []struct { - arg int - want int - }{ - {0, 0}, - {1, 1}, - {-1, 1}, - {math.MaxInt, math.MaxInt}, - {math.MinInt, math.MinInt}, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - if got := abs(tt.arg); got != tt.want { - t.Errorf("abs(%d) = %d, want %d", tt.arg, got, tt.want) - } - }) - } -} - -func Test_gcd(t *testing.T) { - tests := []struct { - arg1 int - arg2 int - want int - }{ - {0, 0, 0}, - {0, 1, 1}, - {1, 0, 1}, - {1, 1, 1}, - {2, 3, 1}, - {42, 56, 14}, - {48, -18, 6}, - {1e9, 1e9, 1e9}, - {1e9, -1e9, 1e9}, - {-1e9, -1e9, 1e9}, - {math.MaxInt, math.MaxInt, math.MaxInt}, - {math.MinInt, math.MinInt, math.MinInt}, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - if got := gcd(tt.arg1, tt.arg2); got != tt.want { - t.Errorf("gcd(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want) - } - }) - } -} - -func Test_lcm(t *testing.T) { - tests := []struct { - arg1 int - arg2 int - want int - }{ - {0, 0, 0}, - {0, 1, 0}, - {1, 0, 0}, - {1, 1, 1}, - {2, 3, 6}, - {42, 56, 168}, - {48, -18, 144}, - {1e9, 1e9, 1e9}, - {1e9, -1e9, 1e9}, - {-1e9, -1e9, 1e9}, - {math.MaxInt, math.MaxInt, math.MaxInt}, - {math.MinInt, math.MinInt, math.MinInt}, - } - for _, tt := range tests { - t.Run("", func(t *testing.T) { - if got := lcm(tt.arg1, tt.arg2); got != tt.want { - t.Errorf("lcm(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want) - } - }) - } -} diff --git a/vfs/xts/xts.go b/vfs/xts/xts.go index fcd1321a..1c86d898 100644 --- a/vfs/xts/xts.go +++ b/vfs/xts/xts.go @@ -183,7 +183,7 @@ func (x *xtsFile) Truncate(size int64) error { } func (x *xtsFile) SectorSize() int { - return lcm(x.File.SectorSize(), sectorSize) + return util.LCM(x.File.SectorSize(), sectorSize) } func (x *xtsFile) DeviceCharacteristics() vfs.DeviceCharacteristic {